diff --git a/modules/wolfgame.py b/modules/wolfgame.py index 1d76580..b514694 100644 --- a/modules/wolfgame.py +++ b/modules/wolfgame.py @@ -837,6 +837,12 @@ def stop_game(cli, winner = ""): 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) # This must be after reset(cli) @@ -2872,6 +2878,27 @@ def flastgame(cli, nick, rest): def _flastgame(cli, nick, chan, 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") def player_stats(cli, nick, chan, rest): diff --git a/settings/wolfgame.py b/settings/wolfgame.py index 7300586..da9fb81 100644 --- a/settings/wolfgame.py +++ b/settings/wolfgame.py @@ -223,6 +223,11 @@ with conn: 'teamwins SMALLINT, individualwins SMALLINT, totalgames SMALLINT, '+ '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: 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): - with conn: 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 (?,?,?,?,?)", (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): with conn: 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: 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 "" \ No newline at end of file