Change the ping_join thread to be a timer instead.

This commit is contained in:
Vgr E.Barry 2015-02-19 16:44:30 -05:00
parent 1796c8d615
commit 375e34eb3f

View File

@ -333,7 +333,6 @@ def reset():
var.ROLES = {"person" : []}
var.JOINED_THIS_GAME = [] # keeps track of who already joined this game at least once (cloaks)
var.JOINED_THIS_GAME_ACCS = [] # same, except accounts
var.PING_JOIN_TIMER = 0
var.PINGED_ALREADY = []
var.PINGED_ALREADY_ACCS = []
var.NO_LYNCH = []
@ -983,13 +982,10 @@ def toggle_altpinged_status(nick, value, old=None):
var.PING_IF_NUMS[old].remove(cloak)
def join_timer_handler(cli):
while var.PHASE == "join":
with var.GRAVEYARD_LOCK:
if var.PING_JOIN_TIMER and var.PING_JOIN_TIMER + timedelta(seconds=10) <= datetime.now():
var.PINGING_IFS = True
to_ping = []
pl = var.list_players()
var.PINGING_IFS = True
checker = []
chk_acc = []
@ -1012,7 +1008,6 @@ def join_timer_handler(cli):
if not chk_acc and not checker:
var.PINGING_IFS = False
var.PING_JOIN_TIMER = 0
return
@hook("whospcrpl", hookid=387)
@ -1033,8 +1028,15 @@ def join_timer_handler(cli):
@hook("endofwho", hookid=387)
def fetch_altpingers(*stuff):
# fun fact: if someone joined 10 seconds after someone else, the bot would break.
# effectively, the join would delete join_pinger from var.TIMERS and this function
# here would be reached before it was created again, thus erroring and crashing.
# this is one of the multiple reasons we need unit testing
# I was lucky to catch this in testing, as it requires precise timing
# it only failed if a join happened while this outer func had started
if 'join_pinger' in var.TIMERS:
del var.TIMERS['join_pinger']
var.PINGING_IFS = False
var.PING_JOIN_TIMER = 0
decorators.unhook(HOOKS, 387)
if to_ping:
to_ping.sort(key=lambda x: x.lower())
@ -1042,8 +1044,6 @@ def join_timer_handler(cli):
cli.who(botconfig.CHANNEL, "%nushaf")
time.sleep(5)
@cmd("join", "j", none=True, join=True)
def join(cli, nick, chan, rest):
"""Either starts a new game of Werewolf or joins an existing game that has not started yet."""
@ -1096,12 +1096,8 @@ def join_player(cli, player, chan, who = None, forced = False):
var.PHASE = "join"
var.WAITED = 0
var.GAME_ID = time.time()
var.PING_JOIN_TIMER = datetime.now()
var.PINGED_ALREADY_ACCS = []
var.PINGED_ALREADY = []
join_pinger = threading.Thread(None, join_timer_handler, args=(cli,))
join_pinger.daemon = True
join_pinger.start()
if cloak:
var.JOINED_THIS_GAME.append(cloak)
if acc:
@ -1156,8 +1152,14 @@ def join_player(cli, player, chan, who = None, forced = False):
var.LAST_GSTATS = None
var.LAST_PSTATS = None
var.LAST_TIME = None
with var.GRAVEYARD_LOCK:
var.PING_JOIN_TIMER = datetime.now()
if 'join_pinger' in var.TIMERS:
var.TIMERS['join_pinger'][0].cancel()
t = threading.Timer(10, join_timer_handler, (cli,))
var.TIMERS['join_pinger'] = (t, time.time(), 10)
t.daemon = True
t.start()
def kill_join(cli, chan):
pl = var.list_players()
@ -5572,6 +5574,10 @@ def start(cli, nick, chan, forced = False):
if chan != botconfig.CHANNEL:
return
if 'join_pinger' in var.TIMERS:
var.TIMERS['join_pinger'][0].cancel()
del var.TIMERS['join_pinger']
villagers = var.list_players()
pl = villagers[:]