counting-on

CountingOn Game for Eggdrop
Log | Files | Refs | README

commit 7fe3c350a5113c85955d02cd650a698755556e36
parent 49c8600dc9d1cb4b0ca7a2d74c6a355b4acb1734
Author: Miniontoby <miniontoby@ircnow.org>
Date:   Tue, 30 Mar 2021 12:16:15 +0000

Updated code

Diffstat:
Mcounton.tcl | 207+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 197 insertions(+), 10 deletions(-)

diff --git a/counton.tcl b/counton.tcl @@ -1,17 +1,50 @@ #.chanset #channel +countOn namespace eval CountOn { - bind pub "-|-" s CountOn::CountSay - bind pub "-|-" c CountOn::CountCurrent + bind pub "-|-" !start CountOn::Init setudef flag countOn + variable gameOn 0 variable last 0 variable lastNick "" + variable tries 0 variable scriptVersion 1.0 + variable StartTimer "" + variable chan "" + variable players "" + variable scores "" - proc CountSay {nick host user chan text} { + proc checkChan {chan} { + if {[channel get $chan countOn]} { + return 1 + } + return 0 + } + proc BindCommands {} { + bind pub "-|-" s CountOn::Say + + variable players + variable chan + set pcount 0 + while {[lindex $players $pcount] != ""} { + set nick [lindex $players $pcount] + pushmode $chan +v $nick + incr pcount + } + pushmode $chan +m + return 1 + } + proc UnbindCommands {} { + unbind pub "-|-" s CountOn::Say + + return 1 + } + + proc Say {nick host user chan text} { variable last variable lastNick + variable players + variable scores set now $text if {[channel get $chan countOn]} { if { $lastNick == $nick } { @@ -22,24 +55,178 @@ namespace eval CountOn { putserv "PRIVMSG $chan :Ok!" set last $now set lastNick $nick + set idx [lsearch $players $nick] + set val [lindex $scores $idx] + set scores [lreplace $scores $idx $idx [expr ($val+1)]] return 0 } else { putserv "PRIVMSG $chan :$nick: You failed!, start over" set last 0 + set pcount 0 + while {[lindex $players $pcount] != ""} { + if {[lindex $players $pcount] == $nick} { + set players [lremove $players $pcount] + set scores [lremove $scores $pcount] + pushmode $chan -v $nick + } + incr pcount + } + if { [CountOn::checkOver] == 1} { + CountOn::UnbindCommands + CountOn::GameOver + return + } set lastNick $nick + putserv "PRIVMSG $chan :Start with 's 1'" return 1 } } } } - proc CountCurrent {nick host user chan text} { - variable last + proc Start {} { + variable last + variable lastNick + variable players + variable chan + variable tries + variable gameOn + if {[checkChan $chan] == 1} { + if {![llength $players]} { + putserv "PRIVMSG $chan :No players, need 3 players to play" + if {$tries < 3} { + set tries [expr ($tries+1)] + set StartTimer [utimer 30 CountOn::Start] + } else { + set tries 0 + set gameOn 0 + putserv "PRIVMSG $chan :Stopping game, do !start again to start an game" + } + return + } + if {[llength $players] < 3} { + putserv "PRIVMSG $chan :Not enough players, need 3 players to play" + if {$tries < 3} { + set tries [expr ($tries+1)] + set StartTimer [utimer 30 CountOn::Start] + } else { + set tries 0 + set gameOn 0 + putserv "PRIVMSG $chan :Stopping game, do !start again to start an game" + } + return + } + CountOn::BindCommands + set gameOn 2 + putserv "PRIVMSG $chan :Game Started, start with 's 1'" + } + } + proc Init {nick host user chana text} { + variable gameOn + variable chan + if { [checkChan $chana] == 1} { + if {[botisop $chana] == 0} { + putserv "PRIVMSG $chana :I'm not op'd. Plz give me op to start a game" + return 0 + } + if { $gameOn == 0 } { + set chan $chana + putserv "PRIVMSG $chan :You got 30 seconds to join CountingOn" + set gameOn 1 + bind pub "-|-" j CountOn::Join + set StartTimer [utimer 30 CountOn::Start] + return 1 + } else { + putserv "NOTICE $nick :A game is already running" + return 0 + } + } + } + proc GameOver {} { + variable gameOn + variable chan + variable players + variable scores + variable StartTimer variable lastNick - if {[channel get $chan countOn]} { - putserv "PRIVMSG $chan :$lastNick said $last" - return 0 + if { $gameOn == 2 } { + catch {killutimer $StartTimer} + set pcount 0 + set nick "" + set score "" + putlog "Players: $players, Scores: $scores" + if {[lindex $scores 0] > [lindex $scores 1]} { + set nick [lindex $players 0] + set score [lindex $scores 0] + } elseif {[lindex $scores 0] < [lindex $scores 1]} { + set nick [lindex $players 1] + set score [lindex $scores 1] + } else { + set nick "No-one" + set score "[lindex $scores 1] (tie)" + } + putserv "PRIVMSG $chan :The game is over" + putserv "PRIVMSG $chan :$nick has won with $score points" + CountOn::reset + return 1 + } + } + proc reset {} { + variable gameOn + variable players + variable scores + variable last + variable lastNick + variable chan + + set pcount 0 + while {[lindex $players $pcount] != ""} { + set nick [lindex $players $pcount] + pushmode $chan -v $nick + incr pcount } - return 1 + pushmode $chan -m + + set gameOn 0 + set players "" + set scores "" + set last 0 + set lastNick "" + set chan "" + return + } + proc Join {nick host user chan text} { + variable players + variable scores + variable gameOn + if { $gameOn == 0 } { return } + set pcount 0 + while {[lindex $players $pcount] != ""} { + if {[lindex $players $pcount] == $nick} { + return + } + incr pcount + } + lappend players $nick + lappend scores 0 + putserv "PRIVMSG $chan :$nick joined the game" + } + + proc checkOver {} { + variable players + putlog [llength $players] + if {[llength $players] < 3} { + return 1 + } + return 0 + } + + proc lremove {list idx_list} { + foreach idx [lreverse $idx_list] { + set list [lreplace $list $idx $idx] + } + return $list } - putlog "CountOn loaded!" + + putlog "CountingOn loaded!" } +