Add threading locks to deal with the ping-if timer properly.

This commit is contained in:
Vgr E.Barry 2015-02-19 22:16:48 -05:00
parent 375e34eb3f
commit 26dd0b9c26

View File

@ -85,6 +85,7 @@ var.LAST_SAID_TIME = {}
var.GAME_START_TIME = datetime.now() # for idle checker only
var.CAN_START_TIME = 0
var.GRAVEYARD_LOCK = threading.RLock()
var.WARNING_LOCK = threading.RLock()
var.STARTED_DAY_PLAYERS = 0
var.DISCONNECTED = {} # players who got disconnected
@ -304,6 +305,7 @@ def reset_settings():
def reset_modes_timers(cli):
# Reset game timers
with var.WARNING_LOCK: # make sure it isn't being used by the ping join handler
for x, timr in var.TIMERS.items():
timr[0].cancel()
var.TIMERS = {}
@ -830,6 +832,7 @@ def altpinger(cli, nick, chan, rest):
if not rest:
if altpinged:
msg.append("Your ping preferences are currently set to {0}.".format(players))
with var.WARNING_LOCK:
if acc and acc != "*" and acc in var.PING_PREFS_ACCS:
msg.append("When your desired player count is reached, you will be {0}.".format(pref_mean[var.PING_PREFS_ACCS[acc]]))
elif not var.ACCOUNTS_ONLY and cloak in var.PING_PREFS:
@ -866,6 +869,7 @@ def altpinger(cli, nick, chan, rest):
else:
pref = rest[0]
with var.WARNING_LOCK:
if pref.lower() in ("once", "one", "first", "onjoin"):
if acc and acc != "*":
if acc in var.PING_PREFS_ACCS.keys() and var.PING_PREFS_ACCS[acc] == "once":
@ -910,6 +914,7 @@ def altpinger(cli, nick, chan, rest):
msg.append("You will now be added to the {0}ping list as well as being pinged immediately when your preferred amount of players is reached.")
var.PING_PREFS[cloak] = "all"
var.set_ping_pref(cloak, "all")
elif not msg: # only warn if there was no message to avoid false positives
msg.append("Invalid parameter. Please enter a non-negative integer or a valid preference.")
@ -943,6 +948,7 @@ def toggle_altpinged_status(nick, value, old=None):
del var.PING_IF_PREFS_ACCS[acc]
var.set_ping_if_status_acc(acc, 0)
if old is not None:
with var.WARNING_LOCK:
if old in var.PING_IF_NUMS_ACCS.keys():
if acc in var.PING_IF_NUMS_ACCS[old]:
var.PING_IF_NUMS_ACCS[old].remove(acc)
@ -950,6 +956,7 @@ def toggle_altpinged_status(nick, value, old=None):
del var.PING_IF_PREFS[cloak]
var.set_ping_if_status(cloak, 0)
if old is not None:
with var.WARNING_LOCK:
if old in var.PING_IF_NUMS.keys():
if cloak in var.PING_IF_NUMS[old]:
var.PING_IF_NUMS[old].remove(cloak)
@ -960,6 +967,7 @@ def toggle_altpinged_status(nick, value, old=None):
if acc not in var.PING_PREFS_ACCS:
var.PING_PREFS_ACCS[acc] = "once"
var.set_ping_pref_acc(acc, "once")
with var.WARNING_LOCK:
if value not in var.PING_IF_NUMS_ACCS.keys():
var.PING_IF_NUMS_ACCS[value] = []
var.PING_IF_NUMS_ACCS[value].append(acc)
@ -973,6 +981,7 @@ def toggle_altpinged_status(nick, value, old=None):
if cloak not in var.PING_PREFS:
var.PING_PREFS[cloak] = "once"
var.set_ping_pref(cloak, "once")
with var.WARNING_LOCK:
if value not in var.PING_IF_NUMS.keys():
var.PING_IF_NUMS[value] = []
var.PING_IF_NUMS[value].append(cloak)
@ -982,6 +991,7 @@ def toggle_altpinged_status(nick, value, old=None):
var.PING_IF_NUMS[old].remove(cloak)
def join_timer_handler(cli):
with var.WARNING_LOCK:
var.PINGING_IFS = True
to_ping = []
pl = var.list_players()
@ -1034,7 +1044,7 @@ def join_timer_handler(cli):
# 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:
# possible underlying bugs were squashed with the proper use of a reentrant lock
del var.TIMERS['join_pinger']
var.PINGING_IFS = False
decorators.unhook(HOOKS, 387)
@ -1153,6 +1163,7 @@ def join_player(cli, player, chan, who = None, forced = False):
var.LAST_PSTATS = None
var.LAST_TIME = None
with var.WARNING_LOCK:
if 'join_pinger' in var.TIMERS:
var.TIMERS['join_pinger'][0].cancel()
@ -5574,10 +5585,6 @@ 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[:]
@ -5630,11 +5637,6 @@ def start(cli, nick, chan, forced = False):
cli.msg(chan, "{0}: No game settings are defined for \u0002{1}\u0002 player games.".format(nick, len(villagers)))
return
# Cancel join timer
if 'join' in var.TIMERS:
var.TIMERS['join'][0].cancel()
del var.TIMERS['join']
if var.ORIGINAL_SETTINGS: # Custom settings
while True:
wvs = sum(addroles[r] for r in var.WOLFCHAT_ROLES)
@ -5784,6 +5786,12 @@ def start(cli, nick, chan, forced = False):
if var.GOAT_HERDER:
var.SPECIAL_ROLES["goat herder"] = [ nick ]
with var.WARNING_LOCK: # cancel timers
for name in ("join", "join_pinger"):
if name in var.TIMERS:
var.TIMERS[name][0].cancel()
del var.TIMERS[name]
cli.msg(chan, ("{0}: Welcome to Werewolf, the popular detective/social party "+
"game (a theme of Mafia). Using the \002{1}\002 game mode.").format(", ".join(pl), var.CURRENT_GAMEMODE))
cli.mode(chan, "+m")