egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem with textfiles
Goto page 1, 2  Next
 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
darton
Op


Joined: 21 Jan 2006
Posts: 155

PostPosted: Fri May 05, 2006 6:27 pm    Post subject: Problem with textfiles Reply with quote

Hello!
When a bot in the channel writes one of the following sentences, my bot should save the playername with the number of MVPs and MostKills in a textfile. In the txt-file there should be displayed at first the playername, then the number of MVPs and then the number of MostKills.
Quote:
Host: [BR] genaraln7 has been recommended by BlazeRegulator: MVP on C&C_Islands with 1000 points
Host: [BR] genaraln7 has been recommended by BlazeRegulator: Most Kills on C&C_Canyon with 17 frags

I began with making this script:
Code:
bind pubm - * medals

proc medals {nick uhost hand chan arg} {
 set file scripts/medals.txt
 if {![file exists $file]} { close [open $file w] }
 set l [split [read [set f [open $file]]][close $f] \n]
 set n [lindex [split $arg] 2]
 set mvp 0
 set kills 0
 if {[string match -nocase {*BlazeRegulator: Most Kills*} $arg]} {
 set kills [expr $kills + 1]
  lappend l "$n $mvp $kills"
  set f [open $file w]
  foreach le $l {
   if {$le != ""} { puts $f $le }
  } 
  close $f
  }
  if {[string match -nocase {*BlazeRegulator: MVP*} $arg]} {
  set mvp [expr $mvp + 1]
   lappend l "$n $mvp $kills"
   set f [open $file w]
   foreach le $l {
   if {$le != ""} { puts $f $le }
  } 
  close $f
  }
}

I hope it is right till now. At least it works, but there is something I have to add and I don't know how to do this:
1. The design of the txt-file is like this: "genaraln7 0 1 genaraln7 1 0" Is it possbile that there is a break before a new name begins?
2. If a person gets for example MVP and this person is already listed in the textfile, my bot should not begin a new line, but it should add it to the nick. How is this possible?
Back to top
View user's profile Send private message
De Kus
Revered One


Joined: 15 Dec 2002
Posts: 1361
Location: Germany

PostPosted: Sat May 06, 2006 6:22 am    Post subject: Reply with quote

1. there should be a \n at the end of each entry. Try to open file with something else than notepad, add \r at the end of each line or fconfigure the file to use translation crlf (as output).
2. open file in r+, while !eof gets the file and lappend each line into a list. then lsearch -glob $list "$nickname *" and use the returned id to replace the entry with something else. then seek to 0 in the file and foreach puts it again.
_________________
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
Back to top
View user's profile Send private message MSN Messenger
darton
Op


Joined: 21 Jan 2006
Posts: 155

PostPosted: Sat May 06, 2006 6:34 am    Post subject: Reply with quote

OK, with UltraEdit it is displayed correctly. But now there is still the other problem.
Here is an example:
Quote:
Host: [BR] genaraln7 has been recommended by BlazeRegulator: MVP on C&C_Islands with 1000 points
Host: [BR] genaraln7 has been recommended by BlazeRegulator: Most Kills on C&C_Canyon with 17 frags

If this appears in the channel, my bot stores it like this:
genaraln7 0 1
genaraln7 1 0
But it should be stored like this: genaraln7 1 1
So if the nick is already in the textfile, my bot should add the number to this nick.

EDIT: I see, you edited your post, but can you please make a script? That would be nice.


Last edited by darton on Sat May 06, 2006 6:40 am; edited 1 time in total
Back to top
View user's profile Send private message
De Kus
Revered One


Joined: 15 Dec 2002
Posts: 1361
Location: Germany

PostPosted: Sat May 06, 2006 6:40 am    Post subject: Reply with quote

are you not familar enough with TCL to follow my description?

PS: I see, you edited as well Razz
_________________
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...


Last edited by De Kus on Sat May 06, 2006 6:44 am; edited 1 time in total
Back to top
View user's profile Send private message MSN Messenger
darton
Op


Joined: 21 Jan 2006
Posts: 155

PostPosted: Sat May 06, 2006 6:41 am    Post subject: Reply with quote

Not really Embarassed
Back to top
View user's profile Send private message
De Kus
Revered One


Joined: 15 Dec 2002
Posts: 1361
Location: Germany

PostPosted: Sat May 06, 2006 7:00 am    Post subject: Reply with quote

try following (untested):

Code:
set medalsfile "scripts/medals.txt"
if {![file exists $medalsfile]} {
   catch {close [open $medalsfile w]}
}

bind pubm - * medals

proc medals {nick uhost hand chan arg} {
   set mvp [string match -nocase {*BlazeRegulator: MVP*} $arg]
   set kills [string match -nocase {*BlazeRegulator: Most Kills*} $arg]
   if {$mvp || $kills} {
      set name [lindex [split $arg] 2]
      set fd [open $::medalsfile r+]
      while {![eof $fd]} {
         lappend list [gets $fd]
      }
      if {[lindex $list end] == ""} {
         set list [lreplace $list end end]
      }
      if {[set le [lsearch -glob $list "$name *"]] != -1} {
         set line [lindex $list $le]
         incr mvp [lindex $line 1]
         incr kills [lindex $line 2]
         set list [lreplace $list $le $le [list $n $mvp $kills]]
      } else {
         lappend list [list $name $mvp $kills]
      }
      seek $fd 0
      puts -nonewline $fd [join $list \n]
      close $fd
   }
   return 0
}

I tried to alter your existing code in something that I would call a "proper syntax" Smile. Note especially that your original version read the whole file regradless, if the line was even worth opening it.

If you are worried about memory usage within the lreplace (since the data will temporarily be doubled), you can use the "k" function mentioned somewhere on the forums to empty $l, before filled with result of lreplace.

Edit: 13:12 typo
_________________
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...


Last edited by De Kus on Sun May 07, 2006 6:23 am; edited 7 times in total
Back to top
View user's profile Send private message MSN Messenger
darton
Op


Joined: 21 Jan 2006
Posts: 155

PostPosted: Sat May 06, 2006 7:43 am    Post subject: Reply with quote

OK, first thanks for your work, but there is a mistake anywhere in your script.
If the textfile medals.txt does not exist, then there appears the following error:
Tcl error [medals]: couldn't open "scripts/medals.txt": no such file or directory
And if it exists:
Tcl error [medals]: can not find channel named "file9"
Back to top
View user's profile Send private message
De Kus
Revered One


Joined: 15 Dec 2002
Posts: 1361
Location: Germany

PostPosted: Sat May 06, 2006 7:45 am    Post subject: Reply with quote

obviously "catch {close [open $file w]}" was supposed to be "catch {close [open $medalsfile w]}" and the first "close $f" was of course a copy&paste mistake and should not be there Very Happy. I edited my post by that 2 changes.

PS: Intresting that you have a global var called "file" Very Happy. You now have a file called "filex" or whatever is/was the content of $file in your bot dir Wink.
_________________
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
Back to top
View user's profile Send private message MSN Messenger
darton
Op


Joined: 21 Jan 2006
Posts: 155

PostPosted: Sat May 06, 2006 9:08 am    Post subject: Reply with quote

OK, if the file doesnt exist, an error appears, so first I have to create a new file so that it works.
Then if my bot should write something in the textfile, it makes 3 lines, the first and the last are empty and in the second line it writes the playername with mvp and kills.
And if in the textfile there is "genaraln7 1 1" and someone in the channel writes "Host: [BR] genaraln7 has been recommended by BlazeRegulator: MVP on C&C_Islands with 1000 points" this entry should be changed to "genaraln7 2 1", but that doesn't happen.
Back to top
View user's profile Send private message
darton
Op


Joined: 21 Jan 2006
Posts: 155

PostPosted: Sat May 06, 2006 3:52 pm    Post subject: Reply with quote

Not possible??
Back to top
View user's profile Send private message
De Kus
Revered One


Joined: 15 Dec 2002
Posts: 1361
Location: Germany

PostPosted: Sun May 07, 2006 2:47 am    Post subject: Reply with quote

sorry, didnt consider empty file and that the last line would always expent by an empty line. I changed code to discard the empty last line and dont put no more \n before EOF. However you will have to remove the first empty line by hand Very Happy.

For the counting, you exressed yourself a bit misunderstanding, I thought you meant a boolan, not a counter Very Happy. These simply incr statements should work... if my sleepy brain didnt make a total disaster Very Happy.
_________________
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
Back to top
View user's profile Send private message MSN Messenger
darton
Op


Joined: 21 Jan 2006
Posts: 155

PostPosted: Sun May 07, 2006 4:12 am    Post subject: Reply with quote

Quote:
Tcl error [medals]: can't read "line": no such variable

Hm...what's that?
Back to top
View user's profile Send private message
De Kus
Revered One


Joined: 15 Dec 2002
Posts: 1361
Location: Germany

PostPosted: Sun May 07, 2006 6:17 am    Post subject: Reply with quote

if you looked at the complete error message, you would have seen what var name there should be written Razz. Thats why its recommned to use meaningfull var names.
_________________
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
Back to top
View user's profile Send private message MSN Messenger
darton
Op


Joined: 21 Jan 2006
Posts: 155

PostPosted: Sun May 07, 2006 6:32 am    Post subject: Reply with quote

EDIT: Hey it works, you are great De Kus. Thank you very much.

Last edited by darton on Sun May 07, 2006 7:03 am; edited 5 times in total
Back to top
View user's profile Send private message
De Kus
Revered One


Joined: 15 Dec 2002
Posts: 1361
Location: Germany

PostPosted: Sun May 07, 2006 6:34 am    Post subject: Reply with quote

darton wrote:
But it doesn't.
else? and make sure you used a copy from the last edit Razz.
_________________
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber