33
44from time import time
55from collections import defaultdict
6- from sqlalchemy import Table , Column , String , Integer , PrimaryKeyConstraint , desc
6+ from sqlalchemy import Table , Column , String , Integer , PrimaryKeyConstraint , desc , Boolean
77from sqlalchemy .sql import select
88from cloudbot import hook
99from cloudbot .event import EventType
3232 PrimaryKeyConstraint ('chan' ,'network' )
3333 )
3434
35-
35+ status_table = Table (
36+ 'duck_status' ,
37+ database .metadata ,
38+ Column ('network' , String ),
39+ Column ('chan' , String ),
40+ Column ('active' , Boolean , default = False ),
41+ Column ('duck_kick' , Boolean , default = False ),
42+ PrimaryKeyConstraint ('network' , 'chan' )
43+ )
3644
3745"""
3846game_status structure
@@ -70,6 +78,35 @@ def load_optout(db):
7078 chan = row ["chan" ]
7179 opt_out .append (chan )
7280
81+
82+ @hook .on_start
83+ def load_status (db ):
84+ rows = db .execute (status_table .select ())
85+ for row in rows :
86+ net = row ['network' ]
87+ chan = row ['chan' ]
88+ status = game_status [net ][chan ]
89+ status ["game_on" ] = int (row ['active' ])
90+ status ["no_duck_kick" ] = int (row ['duck_kick' ])
91+ set_ducktime (chan , net )
92+
93+
94+ @hook .on_unload
95+ @hook .periodic (5 * 60 , initial_interval = 10 * 60 )
96+ def save_status (db ):
97+ for network in game_status :
98+ for chan , status in game_status [network ].items ():
99+ active = bool (status ['game_on' ])
100+ duck_kick = bool (status ['no_duck_kick' ])
101+ res = db .execute (status_table .update ().where (status_table .c .network == network ).where (status_table .c .chan == chan ).values (
102+ active = active , duck_kick = duck_kick
103+ ))
104+ if not res .rowcount :
105+ db .execute (status_table .insert ().values (network = network , chan = chan , active = active , duck_kick = duck_kick ))
106+
107+ db .commit ()
108+
109+
73110@hook .event ([EventType .message , EventType .action ], singlethread = True )
74111def incrementMsgCounter (event , conn ):
75112 """Increment the number of messages said in an active game channel. Also keep track of the unique masks that are speaking."""
@@ -94,19 +131,21 @@ def start_hunt(bot, chan, message, conn):
94131 return "there is already a game running in {}." .format (chan )
95132 else :
96133 game_status [conn .name ][chan ]['game_on' ] = 1
97- set_ducktime (chan , conn )
134+ set_ducktime (chan , conn . name )
98135 message ("Ducks have been spotted nearby. See how many you can shoot or save. use .bang to shoot or .befriend to save them. NOTE: Ducks now appear as a function of time and channel activity." , chan )
99136
137+
100138def set_ducktime (chan , conn ):
101139 global game_status
102- game_status [conn . name ][chan ]['next_duck_time' ] = random .randint (int (time ()) + 480 , int (time ()) + 3600 )
103- #game_status[conn.name ][chan]['flyaway'] = game_status[conn.name][chan]['next_duck_time'] + 600
104- game_status [conn . name ][chan ]['duck_status' ] = 0
140+ game_status [conn ][chan ]['next_duck_time' ] = random .randint (int (time ()) + 480 , int (time ()) + 3600 )
141+ #game_status[conn][chan]['flyaway'] = game_status[conn.name][chan]['next_duck_time'] + 600
142+ game_status [conn ][chan ]['duck_status' ] = 0
105143 # let's also reset the number of messages said and the list of masks that have spoken.
106- game_status [conn . name ][chan ]['messages' ] = 0
107- game_status [conn . name ][chan ]['masks' ] = []
144+ game_status [conn ][chan ]['messages' ] = 0
145+ game_status [conn ][chan ]['masks' ] = []
108146 return
109147
148+
110149@hook .command ("stophunt" , autohelp = False , permissions = ["chanop" ])
111150def stop_hunt (chan , conn ):
112151 """This command stops the duck hunt in your channel. Scores will be preserved"""
@@ -280,7 +319,7 @@ def bang(nick, chan, message, db, conn, notice):
280319 timer = "{:.3f}" .format (shoot - deploy )
281320 duck = "duck" if score == 1 else "ducks"
282321 message ("{} you shot a duck in {} seconds! You have killed {} {} in {}." .format (nick , timer , score , duck , chan ))
283- set_ducktime (chan , conn )
322+ set_ducktime (chan , conn . name )
284323
285324@hook .command ("befriend" , autohelp = False )
286325def befriend (nick , chan , message , db , conn , notice ):
@@ -336,7 +375,7 @@ def befriend(nick, chan, message, db, conn, notice):
336375 duck = "duck" if score == 1 else "ducks"
337376 timer = "{:.3f}" .format (shoot - deploy )
338377 message ("{} you befriended a duck in {} seconds! You have made friends with {} {} in {}." .format (nick , timer , score , duck , chan ))
339- set_ducktime (chan ,conn )
378+ set_ducktime (chan ,conn . name )
340379
341380def smart_truncate (content , length = 320 , suffix = '...' ):
342381 if len (content ) <= length :
0 commit comments