This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Play text file

Requests for complete scripts or modifications/fixes for scripts you didn't write. Response not guaranteed, and no thread bumping!
Post Reply
User avatar
Madalin
Master
Posts: 310
Joined: Fri Jun 24, 2005 11:36 am
Location: Constanta, Romania
Contact:

Play text file

Post by Madalin »

Im trying to make a text file to play on a channel with commands like !start !stop !resume and !pause

!start and !stop no problem ... but !pause and !resume i cant really figure it out :) i messed the code so far that i didnt know what ive done good or wrong..

This is the code i managed to save upon now..

Code: Select all

bind PUB - !start pub:prestart
bind PUB - !pause pub:pause
bind PUB - !resume pub:resume
bind PUB - !stop pub:stop

proc pub:prestart {nick uhost hand chan arg} {
	global class

	set class(end) 0
	set class(numara) -1
	set class(linie) -1

	class:start $chan $nick
}

proc class:start {chan nick} {
	global class

	set of [open class]

	incr class(numara)
	
	while {[gets $of line] ne -1} {
		incr class(linie)
		if {$class(end)} { close $of; putlog "Ne oprim"; return }
		
		if {$class(numara) eq $class(linie)} {

			putserv "PRIVMSG $chan :$line"
			close $of
		}
	}
	class:start $chan $nick
}

proc pub:stop {nick uhost hand chan arg} {
	global class

	if {![info exists class(start)]} { putquick "PRIVMSG $chan :\002::\002 Nu ruleaza nimic nu am ce opri"; return }

	set class(end) 1

	putquick "PRIVMSG $chan :\002::\002 Am oprit cu succes"
}
If someone can give me some HINTS ill apreaciate
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

What you are looking for is called offset and as far as I know this can be accomplished only with seek, but since this is binary you basically need to make an index of where the each line starts and ends.

Long story told short, have a look at a good example from here and you will easily figure out based on that example how to read any given line.

If stuck drop a reply. :)
Once the game is over, the king and the pawn go back in the same box.
User avatar
Madalin
Master
Posts: 310
Joined: Fri Jun 24, 2005 11:36 am
Location: Constanta, Romania
Contact:

Post by Madalin »

Yeah that would be nice .. i tryed to test the code ... yet i have for three lines

Code: Select all

proc lineindex filename {
    set res 0
    set fp [open $filename]
    while {[gets $fp line]>=0} {lappend res [tell $fp]}
    close $fp
    set res
 }

[14:26:55] 0 10 20 28

And why set rest at the end of this proc ?

But i dont know how to use the second proc from the example

Code: Select all

proc lineread {fp index line} {
    seek $fp [lindex $index $line]
    gets $fp
 }
I mean .tcl lineread class 0 1, where class is the file it doesnt work.


PS: Starting from this example i can create my own array index and get the numbers from there var(1) "text" and get everything from there.. yet it would increase the memory of the eggdrop for a while
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Like i said this is binary, so from 0 to 10 is the first line, from 10 to 20 the second and from 20 to 28 the third, or that's how I imagine things are. I had a line with 6 lines and the output of lineindex was 0 8 16 24 32 40 41 and thus my silly explanation makes some sense after all. :)

You need to alter the second piece of code to "point" it to the opened file:

Code: Select all

proc lineRead {filename index line} {
	set fp [open $filename]
	seek $fp [lindex $index $line]
	set data [gets $fp]
	close $fp
	set data
}
And then build the index and call the above code to grab whatever line you want.

Code: Select all

set filename "class"

# build lines index
set index [lineindex $filename]

# read 3rd line from file
set result [lineRead $filename $index 3]
The set res will just return the output, just like a return $res.

How many lines and how long are them in text file you want to read anyway? If this is running on linux I'm sure there are better ways to use the system tools to get the same results at a lower CPU/RAM usage if that's your concern.
Once the game is over, the king and the pawn go back in the same box.
User avatar
Madalin
Master
Posts: 310
Joined: Fri Jun 24, 2005 11:36 am
Location: Constanta, Romania
Contact:

Post by Madalin »

Its a script im making for a friend .. he wants to play maximum 100 lines. He just wants stop/start/pause/resume commands, so nothing major.
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

When you pause you can save the offset into a channel string, for instance in your code you got:

Code: Select all

setudef str playOffset
and when pausing:

Code: Select all

channel set $chan playOffset $x
where $x is the last index you just pulled. As for when resuming just grab the number from there.

Code: Select all

set last [channel get $chan playOffset]
# and play from this
set result [lineRead $filename $index [expr $last + 1] ]
I imagine you use some sort of timed messaging to prevent the bot to flood the server or itself, so this should be easy to implement.
Once the game is over, the king and the pawn go back in the same box.
User avatar
Madalin
Master
Posts: 310
Joined: Fri Jun 24, 2005 11:36 am
Location: Constanta, Romania
Contact:

Post by Madalin »

Thats what i tryed to do. I mean i saved everything in array and started from there :) but i dont know where im getting it wrong.

If you take a closer look at the code i posted is something similar :) yet not working as it should.
User avatar
Madalin
Master
Posts: 310
Joined: Fri Jun 24, 2005 11:36 am
Location: Constanta, Romania
Contact:

Post by Madalin »

Its done .. Thanks
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Hey, sorry for the late reply.. Been busy with work and stuff and didn't get the chance to have a look on the code to adapt it to fit the requirements.

If it's done then why not share it? :)
Once the game is over, the king and the pawn go back in the same box.
User avatar
Madalin
Master
Posts: 310
Joined: Fri Jun 24, 2005 11:36 am
Location: Constanta, Romania
Contact:

Post by Madalin »

If someone will want to use it in the future i will make it public. He/She just needs to ask it. Don`t want to post it because maybe im gonna improve it so no need for older version posted here.
Post Reply