Use defaultdict in transition_day and stats

This commit is contained in:
Vgr E.Barry 2015-05-28 15:42:57 -04:00
parent 590406d00a
commit 7d652b457d

View File

@ -25,6 +25,7 @@ import botconfig
import traceback import traceback
from src import decorators from src import decorators
from datetime import datetime, timedelta from datetime import datetime, timedelta
from collections import defaultdict
import threading import threading
import copy import copy
import time import time
@ -1241,7 +1242,7 @@ def stats(cli, nick, chan, rest):
rs.append(var.DEFAULT_ROLE) rs.append(var.DEFAULT_ROLE)
amn_roles = {"amnesiac": 0} amn_roles = defaultdict(int)
for amn in var.ORIGINAL_ROLES["amnesiac"]: for amn in var.ORIGINAL_ROLES["amnesiac"]:
if amn not in pl: if amn not in pl:
continue continue
@ -1255,17 +1256,11 @@ def stats(cli, nick, chan, rest):
amnrole = var.DEFAULT_ROLE amnrole = var.DEFAULT_ROLE
if amnrole != "amnesiac": if amnrole != "amnesiac":
amn_roles["amnesiac"] += 1 amn_roles["amnesiac"] += 1
if amnrole in amn_roles:
amn_roles[amnrole] -= 1 amn_roles[amnrole] -= 1
else:
amn_roles[amnrole] = -1
bitten_roles = {} bitten_roles = defaultdict(int)
for bitten, role in var.BITTEN_ROLES.items(): for role in var.BITTEN_ROLES.values():
if role in bitten_roles:
bitten_roles[role] += 1 bitten_roles[role] += 1
else:
bitten_roles[role] = 1
vb = "are" vb = "are"
for role in rs: for role in rs:
@ -1277,31 +1272,31 @@ def stats(cli, nick, chan, rest):
continue continue
elif role == "lycan": elif role == "lycan":
count += len([p for p in var.CURED_LYCANS if p in var.ROLES["villager"]]) count += len([p for p in var.CURED_LYCANS if p in var.ROLES["villager"]])
count += bitten_roles["lycan"] if "lycan" in bitten_roles else 0 count += bitten_roles["lycan"]
elif role == var.DEFAULT_ROLE: elif role == var.DEFAULT_ROLE:
if var.HIDDEN_TRAITOR: if var.HIDDEN_TRAITOR:
count += len(var.ROLES["traitor"]) count += len(var.ROLES["traitor"])
count += bitten_roles["traitor"] if "traitor" in bitten_roles else 0 count += bitten_roles["traitor"]
if var.DEFAULT_ROLE == "villager": if var.DEFAULT_ROLE == "villager":
count += len(var.ROLES["village elder"] + var.ROLES["time lord"] + var.ROLES["vengeful ghost"]) count += len(var.ROLES["village elder"] + var.ROLES["time lord"] + var.ROLES["vengeful ghost"])
count -= len([p for p in var.CURED_LYCANS if p in var.ROLES["villager"]]) count -= len([p for p in var.CURED_LYCANS if p in var.ROLES["villager"]])
count += bitten_roles["village elder"] if "village elder" in bitten_roles else 0 count += bitten_roles["village elder"]
count += bitten_roles["time lord"] if "time lord" in bitten_roles else 0 count += bitten_roles["time lord"]
count += bitten_roles["vengeful ghost"] if "vengeful ghost" in bitten_roles else 0 count += bitten_roles["vengeful ghost"]
else: else:
count += len(var.ROLES["vengeful ghost"]) count += len(var.ROLES["vengeful ghost"])
count += bitten_roles["vengeful ghost"] if "vengeful ghost" in bitten_roles else 0 count += bitten_roles["vengeful ghost"]
count += bitten_roles[var.DEFAULT_ROLE] if var.DEFAULT_ROLE in bitten_roles else 0 count += bitten_roles[var.DEFAULT_ROLE]
elif role == "villager": elif role == "villager":
count += len(var.ROLES["village elder"] + var.ROLES["time lord"]) count += len(var.ROLES["village elder"] + var.ROLES["time lord"])
count -= len([p for p in var.CURED_LYCANS if p in var.ROLES["villager"]]) count -= len([p for p in var.CURED_LYCANS if p in var.ROLES["villager"]])
count += bitten_roles["villager"] if "villager" in bitten_roles else 0 count += bitten_roles["villager"]
count += bitten_roles["village elder"] if "village elder" in bitten_roles else 0 count += bitten_roles["village elder"]
count += bitten_roles["time lord"] if "time lord" in bitten_roles else 0 count += bitten_roles["time lord"]
elif role == "wolf": elif role == "wolf":
count -= sum(bitten_roles.values()) count -= sum(bitten_roles.values())
else: else:
count += bitten_roles[role] if role in bitten_roles else 0 count += bitten_roles[role]
if role in amn_roles: if role in amn_roles:
count += amn_roles[role] count += amn_roles[role]
@ -3093,18 +3088,15 @@ def transition_day(cli, gameid=0):
new_wolf = True new_wolf = True
break break
found = {} found = defaultdict(int)
for v in var.KILLS.values(): for v in var.KILLS.values():
for p in v: for p in v:
if p in found:
found[p] += 1 found[p] += 1
else:
found[p] = 1
maxc = 0 maxc = 0
victims = [] victims = []
bitten = [] bitten = []
killers = {} # dict of victim: list of killers (for retribution totem) killers = defaultdict(list) # dict of victim: list of killers (for retribution totem)
bywolves = set() # wolves targeted, others may have as well (needed for harlot visit and maybe other things) bywolves = set() # wolves targeted, others may have as well (needed for harlot visit and maybe other things)
onlybywolves = set() # wolves and nobody else targeted (needed for lycan) onlybywolves = set() # wolves and nobody else targeted (needed for lycan)
dups = [] dups = []
@ -3120,10 +3112,8 @@ def transition_day(cli, gameid=0):
victims.append(victim) victims.append(victim)
bywolves.add(victim) bywolves.add(victim)
onlybywolves.add(victim) onlybywolves.add(victim)
if victim in killers:
killers[victim].append("@wolves") # special key to let us know to randomly select a wolf killers[victim].append("@wolves") # special key to let us know to randomly select a wolf
else:
killers[victim] = ["@wolves"]
if victims and var.ANGRY_WOLVES: if victims and var.ANGRY_WOLVES:
# they got a 2nd kill # they got a 2nd kill
@ -3141,10 +3131,7 @@ def transition_day(cli, gameid=0):
victims.append(victim) victims.append(victim)
bywolves.add(victim) bywolves.add(victim)
onlybywolves.add(victim) onlybywolves.add(victim)
if victim in killers:
killers[victim].append("@wolves") # special key to let us know to randomly select a wolf killers[victim].append("@wolves") # special key to let us know to randomly select a wolf
else:
killers[victim] = ["@wolves"]
if var.ALPHA_ENABLED: # check for bites if var.ALPHA_ENABLED: # check for bites
for (alpha, desired) in var.BITE_PREFERENCES.items(): for (alpha, desired) in var.BITE_PREFERENCES.items():
@ -3191,29 +3178,20 @@ def transition_day(cli, gameid=0):
for ghost, target in var.VENGEFUL_GHOSTS.items(): for ghost, target in var.VENGEFUL_GHOSTS.items():
if target == "villagers": if target == "villagers":
victim = var.OTHER_KILLS[ghost] victim = var.OTHER_KILLS[ghost]
if victim in killers:
killers[victim].append(ghost) killers[victim].append(ghost)
else:
killers[victim] = [ghost]
if victim not in var.DYING: # wolf ghost killing ghost will take precedence over everything except death totem and elder if victim not in var.DYING: # wolf ghost killing ghost will take precedence over everything except death totem and elder
wolfghostvictims.append(victim) wolfghostvictims.append(victim)
for k, d in var.OTHER_KILLS.items(): for k, d in var.OTHER_KILLS.items():
victims.append(d) victims.append(d)
onlybywolves.discard(d) onlybywolves.discard(d)
if d in killers:
killers[d].append(k) killers[d].append(k)
else:
killers[d] = [k]
for d in var.DYING: for d in var.DYING:
victims.append(d) victims.append(d)
onlybywolves.discard(d) onlybywolves.discard(d)
for s, v in var.LASTGIVEN.items(): for s, v in var.LASTGIVEN.items():
if v == d and var.TOTEMS[s] == "death": if v == d and var.TOTEMS[s] == "death":
if d in killers:
killers[d].append(s) killers[d].append(s)
else:
killers[d] = [s]
victims_set = set(victims) # remove duplicates victims_set = set(victims) # remove duplicates
victims_set.discard(None) # in the event that ever happens victims_set.discard(None) # in the event that ever happens
victims = [] victims = []