Make "!gstats <num>" 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.
This commit is contained in:
nyuszika7h 2016-09-24 23:22:33 +02:00
parent 3ce7e1d72f
commit 12edbdfb43
3 changed files with 54 additions and 23 deletions

View File

@ -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 # 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 # they do not run by default for performance reasons
SCHEMA_VERSION = 4 SCHEMA_VERSION = 5
_ts = threading.local() _ts = threading.local()
@ -366,10 +366,31 @@ def get_player_totals(acc, hostmask):
def get_game_stats(mode, size): def get_game_stats(mode, size):
conn = _conn() conn = _conn()
c = conn.cursor() c = conn.cursor()
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)) c.execute("SELECT COUNT(1) FROM game WHERE gamemode = ? AND gamesize = ?", (mode, size))
total_games = c.fetchone()[0] total_games = c.fetchone()[0]
if not total_games: if not total_games:
return "No stats for \u0002{0}\u0002 player games.".format(size) return "No stats for \u0002{0}\u0002 player games.".format(size)
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 c.execute("""SELECT
winner AS team, winner AS team,
COUNT(1) AS games, COUNT(1) AS games,
@ -384,12 +405,18 @@ def get_game_stats(mode, size):
AND winner IS NOT NULL AND winner IS NOT NULL
GROUP BY team GROUP BY team
ORDER BY ord ASC, team ASC""", (mode, size)) ORDER BY ord ASC, team ASC""", (mode, size))
msg = "\u0002{0}\u0002 player games | {1}"
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 = [] bits = []
for row in c: for row in c:
bits.append("%s wins: %d (%d%%)" % (singular(row[0]), row[1], round(row[1]/total_games * 100))) 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)) bits.append("Total games: {0}".format(total_games))
return msg.format(size, ", ".join(bits))
return msg + ", ".join(bits)
def get_game_totals(mode): def get_game_totals(mode):
conn = _conn() conn = _conn()
@ -831,8 +858,11 @@ def _upgrade(oldversion):
with open(os.path.join(dn, "db", "upgrade3.sql"), "rt") as f: with open(os.path.join(dn, "db", "upgrade3.sql"), "rt") as f:
c.executescript(f.read()) c.executescript(f.read())
if oldversion < 4: 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 # 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) print ("Rebuilding indexes...", file=sys.stderr)
c.execute("REINDEX") c.execute("REINDEX")

View File

@ -111,6 +111,7 @@ CREATE TABLE game (
); );
CREATE INDEX game_idx ON game (gamemode, gamesize); CREATE INDEX game_idx ON game (gamemode, gamesize);
CREATE INDEX game_gamesize_idx ON game (gamesize);
-- List of people who played in each game -- List of people who played in each game
CREATE TABLE game_player ( CREATE TABLE game_player (

View File

@ -7100,9 +7100,9 @@ def game_stats(cli, nick, chan, rest):
return return
rest.pop(0) rest.pop(0)
# Check for invalid input # 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]) 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])) cli.notice(nick, messages["integer_range"].format(var.GAME_MODES[gamemode][1], var.GAME_MODES[gamemode][2]))
return return