counting-on

CountingOn Game for Eggdrop
Log | Files | Refs | README

counton.tcl (5181B)


      1 #.chanset #channel +countOn
      2 
      3 namespace eval CountOn {
      4 	bind pub "-|-" !start CountOn::Init
      5 
      6 	setudef flag countOn
      7 	variable gameOn 0
      8 	variable last 0
      9 	variable lastNick ""
     10 	variable tries 0
     11 	variable scriptVersion 1.0
     12 	variable StartTimer ""
     13 	variable chan ""
     14 	variable players ""
     15 	variable scores ""
     16 
     17 	proc checkChan {chan} {
     18 		if {[channel get $chan countOn]} {
     19 			return 1
     20 		}
     21 		return 0
     22 	}
     23 	proc BindCommands {} {
     24 		bind pub "-|-" s CountOn::Say
     25 
     26 		variable players
     27 		variable chan
     28 		set pcount 0
     29 		while {[lindex $players $pcount] != ""} {
     30 			set nick [lindex $players $pcount]
     31 			pushmode $chan +v $nick
     32 			incr pcount
     33 		}
     34 		pushmode $chan +m
     35 		return 1
     36 	}
     37 	proc UnbindCommands {} {
     38 		unbind pub "-|-" s CountOn::Say
     39 
     40 		return 1
     41 	}
     42 
     43 	proc Say {nick host user chan text} {
     44 		variable last
     45 		variable lastNick
     46 		variable players
     47 		variable scores
     48 		set now $text
     49 		if {[channel get $chan countOn]} {
     50 			if { $lastNick == $nick } {
     51 				putserv "PRIVMSG $chan :$nick: You already said a number!"
     52 				return 1
     53 			} else {
     54 				if { [expr ($last+1)] == $now} {
     55 					putserv "PRIVMSG $chan :Ok!"
     56 					set last $now
     57 					set lastNick $nick
     58 					set idx [lsearch $players $nick]
     59 					set val [lindex $scores $idx]
     60 					set scores [lreplace $scores $idx $idx [expr ($val+1)]]
     61 					return 0
     62 				} else {
     63 					putserv "PRIVMSG $chan :$nick: You failed!, start over"
     64 					set last 0
     65 					set pcount 0
     66 					while {[lindex $players $pcount] != ""} {
     67 						if {[lindex $players $pcount] == $nick} {
     68 							set players [lremove $players $pcount]
     69 							set scores [lremove $scores $pcount]
     70 							pushmode $chan -v $nick
     71 						}
     72 						incr pcount
     73 					}
     74 					if { [CountOn::checkOver] == 1} {
     75 						CountOn::UnbindCommands
     76 						CountOn::GameOver
     77 						return
     78 					}
     79 					set lastNick $nick
     80 					putserv "PRIVMSG $chan :Start with 's 1'"
     81 					return 1
     82 				}
     83 			}
     84 		}
     85 	}
     86 	proc Start {} {
     87 		variable last 
     88 		variable lastNick 
     89 		variable players
     90 		variable chan
     91 		variable tries
     92 		variable gameOn
     93 		if {[checkChan $chan] == 1} {
     94 			if {![llength $players]} {
     95 				putserv "PRIVMSG $chan :No players, need 3 players to play"
     96 				if {$tries < 3} {
     97 					set tries [expr ($tries+1)]
     98 					set StartTimer [utimer 30 CountOn::Start]
     99 				} else {
    100 					set tries 0
    101 					set gameOn 0
    102 					putserv "PRIVMSG $chan :Stopping game, do !start again to start an game"
    103 				}
    104 				return
    105 			}
    106 			if {[llength $players] < 3} {
    107 				putserv "PRIVMSG $chan :Not enough players, need 3 players to play"
    108 				if {$tries < 3} {
    109 					set tries [expr ($tries+1)]
    110 					set StartTimer [utimer 30 CountOn::Start]
    111 				} else {
    112 					set tries 0
    113 					set gameOn 0
    114 					putserv "PRIVMSG $chan :Stopping game, do !start again to start an game"
    115 				}
    116 				return
    117 			}
    118 			CountOn::BindCommands
    119 			set gameOn 2
    120 			putserv "PRIVMSG $chan :Game Started, start with 's 1'"
    121 		}
    122 	}
    123 	proc Init {nick host user chana text} {
    124 		variable gameOn
    125 		variable chan
    126 		if { [checkChan $chana] == 1} { 
    127 			if {[botisop $chana] == 0} {
    128   		  putserv "PRIVMSG $chana :I'm not op'd. Plz give me op to start a game"
    129   		  return 0
    130   		}
    131 			if { $gameOn == 0 } {
    132 				set chan $chana
    133 				putserv "PRIVMSG $chan :You got 30 seconds to join CountingOn"
    134 				set gameOn 1
    135 				bind pub "-|-" j CountOn::Join
    136 				set StartTimer [utimer 30 CountOn::Start]
    137 				return 1
    138 			} else {
    139 				putserv "NOTICE $nick :A game is already running"
    140 				return 0
    141 			}
    142 		}
    143 	}
    144 	proc GameOver {} {
    145 		variable gameOn 
    146 		variable chan
    147 		variable players
    148 		variable scores
    149 		variable StartTimer
    150 		variable lastNick
    151 		if { $gameOn == 2 } {
    152 			catch {killutimer $StartTimer}
    153 			set pcount 0
    154 			set nick ""
    155 			set score ""
    156 			putlog "Players: $players, Scores: $scores"
    157 			if {[lindex $scores 0] > [lindex $scores 1]} {
    158 				set nick [lindex $players 0]
    159 				set score [lindex $scores 0]
    160 			} elseif {[lindex $scores 0] < [lindex $scores 1]} {
    161 				set nick [lindex $players 1]
    162 				set score [lindex $scores 1]
    163 			} else {
    164 				set nick "No-one"
    165 				set score "[lindex $scores 1] (tie)"
    166 			}
    167 			putserv "PRIVMSG $chan :The game is over"
    168 			putserv "PRIVMSG $chan :$nick has won with $score points"
    169 			CountOn::reset
    170 			return 1
    171 		} 
    172 	}
    173 	proc reset {} {
    174 		variable gameOn 
    175 		variable players 
    176 		variable scores
    177 		variable last 
    178 		variable lastNick
    179 		variable chan
    180 
    181 		set pcount 0
    182 		while {[lindex $players $pcount] != ""} {
    183 			set nick [lindex $players $pcount]
    184 			pushmode $chan -v $nick
    185 			incr pcount
    186 		}
    187 		pushmode $chan -m
    188 
    189 		set gameOn 0
    190 		set players ""
    191 		set scores ""
    192 		set last 0
    193 		set lastNick ""
    194 		set chan ""
    195 		return 
    196 	}
    197 	proc Join {nick host user chan text} {
    198 		variable players 
    199 		variable scores
    200 		variable gameOn
    201 		if { $gameOn == 0 } { return }
    202 		set pcount 0
    203 		while {[lindex $players $pcount] != ""} {
    204 			if {[lindex $players $pcount] == $nick} {
    205 				return
    206 			}
    207 			incr pcount
    208 		}
    209 		lappend players $nick
    210 		lappend scores 0
    211 		putserv "PRIVMSG $chan :$nick joined the game"
    212 	}
    213 
    214 	proc checkOver {} {
    215 		variable players
    216 		putlog [llength $players]
    217 		if {[llength $players] < 3} {
    218 			return 1
    219 		} 
    220 		return 0
    221 	}	
    222 
    223 	proc lremove {list idx_list} {
    224 		foreach idx [lreverse $idx_list] {
    225 			set list [lreplace $list $idx $idx]
    226 		}
    227 		return $list
    228 	}
    229 
    230 	putlog "CountingOn loaded!"
    231 }
    232