From 12edbdfb433bc4793ccf89d698464fc77967e634 Mon Sep 17 00:00:00 2001 From: nyuszika7h Date: Sat, 24 Sep 2016 23:22:33 +0200 Subject: [PATCH] Make "!gstats " also show total of all gamemodes Also capitalized the role names and "Total wins" for consistency with !pstats and upgraded the schema to create a new index for game lookups without specifying the game mode. --- src/db.py | 72 ++++++++++++++++++++++++++++++++++--------------- src/db/db.sql | 1 + src/wolfgame.py | 4 +-- 3 files changed, 54 insertions(+), 23 deletions(-) diff --git a/src/db.py b/src/db.py index 279dda3..8b5a061 100644 --- a/src/db.py +++ b/src/db.py @@ -14,7 +14,7 @@ from src.utilities import irc_lower, break_long_message, role_order, singular # increment this whenever making a schema change so that the schema upgrade functions run on start # they do not run by default for performance reasons -SCHEMA_VERSION = 4 +SCHEMA_VERSION = 5 _ts = threading.local() @@ -366,30 +366,57 @@ def get_player_totals(acc, hostmask): def get_game_stats(mode, size): conn = _conn() c = conn.cursor() - c.execute("SELECT COUNT(1) FROM game WHERE gamemode = ? AND gamesize = ?", (mode, size)) + + if mode == "all": + c.execute("SELECT COUNT(1) FROM game WHERE gamesize = ?", (size,)) + else: + c.execute("SELECT COUNT(1) FROM game WHERE gamemode = ? AND gamesize = ?", (mode, size)) + total_games = c.fetchone()[0] if not total_games: return "No stats for \u0002{0}\u0002 player games.".format(size) - c.execute("""SELECT - winner AS team, - COUNT(1) AS games, - CASE winner - WHEN 'villagers' THEN 0 - WHEN 'wolves' THEN 1 - ELSE 2 END AS ord - FROM game - WHERE - gamemode = ? - AND gamesize = ? - AND winner IS NOT NULL - GROUP BY team - ORDER BY ord ASC, team ASC""", (mode, size)) - msg = "\u0002{0}\u0002 player games | {1}" + + if mode == "all": + c.execute("""SELECT + winner AS team, + COUNT(1) AS games, + CASE winner + WHEN 'villagers' THEN 0 + WHEN 'wolves' THEN 1 + ELSE 2 END AS ord + FROM game + WHERE + gamesize = ? + AND winner IS NOT NULL + GROUP BY team + ORDER BY ord ASC, team ASC""", (size,)) + else: + c.execute("""SELECT + winner AS team, + COUNT(1) AS games, + CASE winner + WHEN 'villagers' THEN 0 + WHEN 'wolves' THEN 1 + ELSE 2 END AS ord + FROM game + WHERE + gamemode = ? + AND gamesize = ? + AND winner IS NOT NULL + GROUP BY team + ORDER BY ord ASC, team ASC""", (mode, size)) + + if mode == "all": + msg = "\u0002{0}\u0002 player games | ".format(size) + else: + msg = "\u0002{0}\u0002 player games (\u0002{1}\u0002) | ".format(size, mode) + bits = [] for row in c: - bits.append("%s wins: %d (%d%%)" % (singular(row[0]), row[1], round(row[1]/total_games * 100))) - bits.append("total games: {0}".format(total_games)) - return msg.format(size, ", ".join(bits)) + bits.append("{0} wins: {1} ({2}%)".format(singular(row[0]).title(), row[1], round(row[1]/total_games * 100))) + bits.append("Total games: {0}".format(total_games)) + + return msg + ", ".join(bits) def get_game_totals(mode): conn = _conn() @@ -831,8 +858,11 @@ def _upgrade(oldversion): with open(os.path.join(dn, "db", "upgrade3.sql"), "rt") as f: c.executescript(f.read()) if oldversion < 4: - print ("Upgrade from verison 3 to 4...", file=sys.stderr) + print ("Upgrade from version 3 to 4...", file=sys.stderr) # no actual upgrades, just wanted to force an index rebuild + if oldversion < 5: + print ("Upgrade from version 4 to 5...", file=sys.stderr) + c.execute("CREATE INDEX game_gamesize_idx ON game (gamesize)") print ("Rebuilding indexes...", file=sys.stderr) c.execute("REINDEX") diff --git a/src/db/db.sql b/src/db/db.sql index d1ba79b..f6e8a6f 100644 --- a/src/db/db.sql +++ b/src/db/db.sql @@ -111,6 +111,7 @@ CREATE TABLE game ( ); CREATE INDEX game_idx ON game (gamemode, gamesize); +CREATE INDEX game_gamesize_idx ON game (gamesize); -- List of people who played in each game CREATE TABLE game_player ( diff --git a/src/wolfgame.py b/src/wolfgame.py index fecc2d4..8dd69a3 100644 --- a/src/wolfgame.py +++ b/src/wolfgame.py @@ -7100,9 +7100,9 @@ def game_stats(cli, nick, chan, rest): return rest.pop(0) # Check for invalid input - if gamemode != "all" and len(rest) and rest[0].isdigit(): + if len(rest) and rest[0].isdigit(): gamesize = int(rest[0]) - if gamesize > var.GAME_MODES[gamemode][2] or gamesize < var.GAME_MODES[gamemode][1]: + if gamemode != "all" and (gamesize > var.GAME_MODES[gamemode][2] or gamesize < var.GAME_MODES[gamemode][1]): cli.notice(nick, messages["integer_range"].format(var.GAME_MODES[gamemode][1], var.GAME_MODES[gamemode][2])) return