Merge pull request #20 from brrr2/stats-pr

Adds player and game stats commands
This commit is contained in:
nyuszika7h 2014-02-12 12:12:36 +01:00
commit 28d431a3c1
2 changed files with 135 additions and 4 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)
@ -2868,12 +2874,88 @@ def flastgame(cli, nick, rest):
if rest.strip(): if rest.strip():
aftergame(cli, rawnick, rest) aftergame(cli, rawnick, rest)
@cmd("flastgame", admin_only=True, raw_nick=True) @cmd("flastgame", admin_only=True, raw_nick=True)
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 or lists game totals for all game sizes if no game size is given."""
if var.PHASE not in ("none", "join"):
cli.notice(nick, "Wait until the game is over to view stats.")
return
# List all games sizes and totals if no size is given.
if rest == "":
cli.msg(chan, var.get_game_totals())
return
# Check for invalid input
rest = rest.strip()
if not rest.isdigit() or int(rest) > var.MAX_PLAYERS or int(rest) < 4:
cli.notice(nick, "Please enter an integer between {0} and {1}.".format(4, var.MAX_PLAYERS))
return
# Attempt to find game stats for the given game size.
size = int(rest)
msg = var.get_game_stats(size)
if msg == "":
cli.msg(chan, "No stats for \u0002{0}\u0002 player games.".format(size))
else:
cli.msg(chan, msg)
@cmd("player", "p")
def player_stats(cli, nick, chan, rest):
"""Gets the stats for the given player and role."""
if var.PHASE not in ("none", "join"):
cli.notice(nick, "Wait until the game is over to view stats.")
return
# Check if we have enough parameters.
params = rest.split()
if len(params) < 2:
cli.notice(nick, "Supply a nick and role name.")
return
# Find the player's account if possible.
if params[0] in var.USERS:
acc = var.USERS[params[0]]["account"]
else:
acc = params[0]
role = " ".join(params[1:]).lower()
# Attempt to find the player's stats.
msg = var.get_player_stats(acc, role)
if msg == "":
cli.notice(nick, "No stats for {0} as {1}.".format(acc, role))
else:
cli.notice(nick, msg)
@pmcmd("player", "p")
def player_stats_pm(cli, nick, rest):
player_stats(cli, nick, "", rest)
@pmcmd("mystats", "me")
def my_stats_pm(cli, nick, rest):
my_stats(cli, nick, "", rest)
@cmd("mystats", "me")
def my_stats(cli, nick, chan, rest):
"""Gets your own stats for the given role."""
# Check if role has been given
if rest == "":
cli.notice(nick, "Supply a role.")
return
# Check if player is identified
acc = var.USERS[nick]["account"]
if acc == "*":
cli.notice(nick, "You are not identified with NickServ.")
return
player_stats(cli, nick, chan, "{0} {1}".format(acc, rest))
before_debug_mode_commands = list(COMMANDS.keys()) before_debug_mode_commands = list(COMMANDS.keys())
before_debug_mode_pmcommands = list(PM_COMMANDS.keys()) before_debug_mode_pmcommands = list(PM_COMMANDS.keys())

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)')
@ -250,13 +255,13 @@ def add_simple_rolemsg(clk):
def remove_ping(clk): def remove_ping(clk):
with conn: with conn:
c.execute('DELETE from ping where cloak=?', (clk,)) c.execute('DELETE from ping where cloak=?', (clk,))
def add_ping(clk): def add_ping(clk):
with conn: with conn:
c.execute('INSERT into ping VALUES (?)', (clk,)) c.execute('INSERT into ping VALUES (?)', (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
@ -277,5 +282,49 @@ 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=?", (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(acc, role):
with conn:
for row in c.execute("SELECT * FROM rolestats WHERE player=? AND role=?", (acc, role)):
return "\u0002{0}\u0002 as \u0002{1}\u0002 | Team wins: {2}, Individual wins: {3}, Total games: {4}".format(*row)
else:
return ""
def get_game_stats(size):
with conn:
for row in c.execute("SELECT * FROM gamestats WHERE size=?", (size,)):
return "\u0002{0}\u0002 player games | Village wins: {1}, Wolf wins: {2}, Total games: {3}".format(*row)
else:
return ""
def get_game_totals():
sizeList = []
with conn:
for size in range(4, MAX_PLAYERS):
c.execute("SELECT size, totalgames FROM gamestats WHERE size=?", (size,))
row = c.fetchone()
if row:
sizeList.append("{0}p({1})".format(*row))
if len(sizeList) == 0:
return "No games have been played."
else:
return "Game totals: %s" % ", ".join(sizeList)