Refactored some stats code.

Made update_roles_stats() and update_game_stats() a bit cleaner.
This commit is contained in:
Yizhe Shen 2014-02-13 17:56:55 -05:00
parent 7f9def0b2c
commit 32fa38a6c9
2 changed files with 6 additions and 11 deletions

View File

@ -838,10 +838,7 @@ def stop_game(cli, winner = ""):
var.update_role_stats(acc, rol, won, iwon) var.update_role_stats(acc, rol, won, iwon)
size = len(var.list_players()) + len(var.DEAD) size = len(var.list_players()) + len(var.DEAD)
if winner == "wolves": var.update_game_stats(size, winner)
var.update_game_stats(size, False, True)
elif winner == "villagers":
var.update_game_stats(size, True, False)
reset(cli) reset(cli)

View File

@ -263,15 +263,13 @@ def add_ping(clk):
def update_role_stats(acc, role, won, iwon): def update_role_stats(acc, role, won, iwon):
with conn: with conn:
wins, iwins, totalgames = 0, 0, 0 wins, iwins, total = 0, 0, 0
c.execute(("SELECT teamwins, individualwins, totalgames FROM rolestats "+ c.execute(("SELECT teamwins, individualwins, totalgames FROM rolestats "+
"WHERE player=? AND role=?"), (acc, role)) "WHERE player=? AND role=?"), (acc, role))
row = c.fetchone() row = c.fetchone()
if row: if row:
wins, iwins, total = row wins, iwins, total = row
else:
wins, iwins, total = 0,0,0
if won: if won:
wins += 1 wins += 1
@ -282,7 +280,7 @@ def update_role_stats(acc, role, won, iwon):
c.execute("INSERT OR REPLACE INTO rolestats VALUES (?,?,?,?,?)", c.execute("INSERT OR REPLACE INTO rolestats VALUES (?,?,?,?,?)",
(acc, role, wins, iwins, total)) (acc, role, wins, iwins, total))
def update_game_stats(size, vwon, wwon): def update_game_stats(size, winner):
with conn: with conn:
vwins, wwins, total = 0, 0, 0 vwins, wwins, total = 0, 0, 0
@ -292,10 +290,10 @@ def update_game_stats(size, vwon, wwon):
if row: if row:
vwins, wwins, total = row vwins, wwins, total = row
if vwon: if winner == "wolves":
vwins += 1
if wwon:
wwins += 1 wwins += 1
elif winner == "villagers":
vwins += 1
total += 1 total += 1
c.execute("INSERT OR REPLACE INTO gamestats VALUES (?,?,?,?)", c.execute("INSERT OR REPLACE INTO gamestats VALUES (?,?,?,?)",