Added get_player_totals(). Some code cleanup.

If the "player" or "mystats" command is called without a role,
then the bot will now list the total games that the player has
played in each role.
This commit is contained in:
Yizhe Shen 2014-02-13 21:54:17 -05:00
parent 04a4f00f80
commit 04d2702f36
2 changed files with 29 additions and 25 deletions

View File

@ -2926,24 +2926,19 @@ def game_stats(cli, nick, chan, rest):
return return
# Attempt to find game stats for the given game size. # Attempt to find game stats for the given game size.
size = int(rest) cli.msg(chan, var.get_game_stats(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") @cmd("player", "p")
def player_stats(cli, nick, chan, rest): def player_stats(cli, nick, chan, rest):
"""Gets the stats for the given player and role.""" """Gets the stats for the given player and role or a list of role totals if no role is given."""
if var.PHASE not in ("none", "join"): if var.PHASE not in ("none", "join"):
cli.notice(nick, "Wait until the game is over to view stats.") cli.notice(nick, "Wait until the game is over to view stats.")
return return
# Check if we have enough parameters. # Check if we have enough parameters.
params = rest.split() params = rest.split()
if len(params) < 2: if len(params) < 1:
cli.notice(nick, "Supply a nick and role name.") cli.notice(nick, "Supply at least a nick.")
return return
# Find the player's account if possible. # Find the player's account if possible.
@ -2951,14 +2946,14 @@ def player_stats(cli, nick, chan, rest):
acc = var.USERS[params[0]]["account"] acc = var.USERS[params[0]]["account"]
else: else:
acc = params[0] acc = params[0]
role = " ".join(params[1:]).lower()
# Attempt to find the player's stats. # List the player's total games for all roles if no role is given
msg = var.get_player_stats(acc, role) if len(params) == 1:
if msg == "": cli.notice(nick, var.get_player_totals(acc))
cli.notice(nick, "No stats for {0} as {1}.".format(acc, role))
else: else:
cli.notice(nick, msg) role = " ".join(params[1:]).lower()
# Attempt to find the player's stats.
cli.notice(nick, var.get_player_stats(acc, role))
@pmcmd("player", "p") @pmcmd("player", "p")
def player_stats_pm(cli, nick, rest): def player_stats_pm(cli, nick, rest):
@ -2970,12 +2965,7 @@ def my_stats_pm(cli, nick, rest):
@cmd("mystats", "me") @cmd("mystats", "me")
def my_stats(cli, nick, chan, rest): def my_stats(cli, nick, chan, rest):
"""Gets your own stats for the given role.""" """Gets your own stats for the given role or a list of role totals if no role is given."""
# Check if role has been given
if rest == "":
cli.notice(nick, "Supply a role.")
return
# Check if player is identified # Check if player is identified
acc = var.USERS[nick]["account"] acc = var.USERS[nick]["account"]
if acc == "*": if acc == "*":

View File

@ -305,15 +305,29 @@ def get_player_stats(acc, role):
msg = "\u0002{0}\u0002 as \u0002{1}\u0002 | Team wins: {2} (%d%%), Individual wins: {3} (%d%%), Total games: {4}".format(*row) msg = "\u0002{0}\u0002 as \u0002{1}\u0002 | Team wins: {2} (%d%%), Individual wins: {3} (%d%%), Total games: {4}".format(*row)
return msg % (round(row[2]/row[4] * 100), round(row[3]/row[4] * 100)) return msg % (round(row[2]/row[4] * 100), round(row[3]/row[4] * 100))
else: else:
return "" return "No stats for {0} as {1}.".format(acc, role)
def get_player_totals(acc):
roleTotals = []
with conn:
for role in ["villager"] + [v for k, v in ROLE_INDICES.items()]:
c.execute("SELECT totalgames FROM rolestats WHERE player=? AND role=?", (acc, role))
row = c.fetchone()
if row:
roleTotals.append("\u0002{0}\u0002: {1}".format(role, *row))
if len(roleTotals) == 0:
return "{0} has not played any games.".format(acc)
else:
return "\u0002{0}\u0002's totals | {1}".format(acc, ", ".join(roleTotals))
def get_game_stats(size): def get_game_stats(size):
with conn: with conn:
for row in c.execute("SELECT * FROM gamestats WHERE size=?", (size,)): for row in c.execute("SELECT * FROM gamestats WHERE size=?", (size,)):
msg = "\u0002{0}\u0002 player games | Village wins: {1} (%d%%), Wolf wins: {2} (%d%%), Total games: {3}".format(*row) msg = "\u0002{0}\u0002 player games | Village wins: {1} (%d%%), Wolf wins: {2} (%d%%), Total games: {3}".format(*row)
return msg % (round(row[1]/row[3] * 100), round(row[2]/row[3] * 100)) return msg % (round(row[1]/row[3] * 100), round(row[2]/row[3] * 100))
else: else:
return "" return "No stats for \u0002{0}\u0002 player games.".format(size)
def get_game_totals(): def get_game_totals():
sizeList = [] sizeList = []
@ -322,9 +336,9 @@ def get_game_totals():
c.execute("SELECT size, totalgames FROM gamestats WHERE size=?", (size,)) c.execute("SELECT size, totalgames FROM gamestats WHERE size=?", (size,))
row = c.fetchone() row = c.fetchone()
if row: if row:
sizeList.append("\02{0}p\02: {1}".format(*row)) sizeList.append("\u0002{0}p\u0002: {1}".format(*row))
if len(sizeList) == 0: if len(sizeList) == 0:
return "No games have been played." return "No games have been played."
else: else:
return "Game totals: %s" % ", ".join(sizeList) return "Game totals | %s" % ", ".join(sizeList)