Added game stats to database and "gamestats" command.

Added a game stats table to game DB to store wolf/village wins for
each game size. Added "gamestats" command to display stored stats.
This commit is contained in:
Yizhe Shen 2014-02-09 18:44:59 -05:00
parent 1994e7330f
commit cbae0291f8
2 changed files with 57 additions and 1 deletions

View File

@ -837,6 +837,12 @@ 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)
if winner == "wolves":
var.update_game_stats(size, False, True)
elif winner == "villagers":
var.update_game_stats(size, True, False)
reset(cli) reset(cli)
# This must be after reset(cli) # This must be after reset(cli)
@ -2872,6 +2878,27 @@ def flastgame(cli, nick, rest):
def _flastgame(cli, nick, chan, rest): def _flastgame(cli, nick, chan, rest):
flastgame(cli, nick, rest) flastgame(cli, nick, rest)
@cmd("gamestats", "gstats")
def game_stats(cli, nick, chan, rest):
"""Gets the game stats for a given game size"""
if var.PHASE not in ("none", "join"):
cli.notice(nick, "Wait until the game is over to view stats.")
return
if rest == "":
cli.notice(nick, "Supply a game size")
return
if not rest.isdigit():
cli.notice(nick, "Please enter an integer.")
return
size = int(rest.strip())
msg = var.get_game_stats(size)
if msg == "":
cli.msg(chan, "No stats for {0} player games.".format(size))
else:
cli.msg(chan, msg)
@cmd("player", "p") @cmd("player", "p")
def player_stats(cli, nick, chan, rest): def player_stats(cli, nick, chan, rest):

View File

@ -223,6 +223,11 @@ with conn:
'teamwins SMALLINT, individualwins SMALLINT, totalgames SMALLINT, '+ 'teamwins SMALLINT, individualwins SMALLINT, totalgames SMALLINT, '+
'UNIQUE(player, role))')) 'UNIQUE(player, role))'))
c.execute(('CREATE TABLE IF NOT EXISTS gamestats (size SMALLINT, villagewins SMALLINT, ' +
'wolfwins SMALLINT, totalgames SMALLINT, UNIQUE(size))'))
if OPT_IN_PING: if OPT_IN_PING:
c.execute('CREATE TABLE IF NOT EXISTS ping (cloak text)') c.execute('CREATE TABLE IF NOT EXISTS ping (cloak text)')
@ -257,7 +262,6 @@ 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, totalgames = 0, 0, 0
@ -278,6 +282,25 @@ 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):
with conn:
vwins, wwins, total = 0, 0, 0
c.execute("SELECT villagewins, wolfwins, totalgames FROM gamestats "+
"WHERE size = %d" % size)
row = c.fetchone()
if row:
vwins, wwins, total = row
if vwon:
vwins += 1
if wwon:
wwins += 1
total += 1
c.execute("INSERT OR REPLACE INTO gamestats VALUES (?,?,?,?)",
(size, vwins, wwins, total))
def get_player_stats(player, role): def get_player_stats(player, role):
with conn: with conn:
for row in c.execute("SELECT * FROM rolestats WHERE player = '%s' AND role = '%s'" % (player, role)): for row in c.execute("SELECT * FROM rolestats WHERE player = '%s' AND role = '%s'" % (player, role)):
@ -285,3 +308,9 @@ def get_player_stats(player, role):
else: else:
return "" return ""
def get_game_stats(size):
with conn:
for row in c.execute("SELECT * FROM gamestats WHERE size = %d" % (size)):
return "{0} player games: {1} village wins, {2} wolf wins, and {3} total games.".format(*row)
else:
return ""