Browse Source

better stats (needs testing)

master
Jimmy Cao 13 years ago
parent
commit
a7e8ad3831
  1. 28
      var.py
  2. 12
      wolfgame.py

28
var.py

@ -185,18 +185,13 @@ with conn:
c.execute("INSERT OR REPLACE INTO roles (role) VALUES (?)", (x,)) c.execute("INSERT OR REPLACE INTO roles (role) VALUES (?)", (x,))
c.execute(('CREATE TABLE IF NOT EXISTS rolestats (playerid INTEGER, roleid INTEGER, '+ c.execute(('CREATE TABLE IF NOT EXISTS rolestats (player TEXT, roleid INTEGER, '+
'teamwins SMALLINT, individualwins SMALLINT, totalgames SMALLINT, '+ 'teamwins SMALLINT, individualwins SMALLINT, totalgames SMALLINT, '+
'UNIQUE(playerid, roleid))')) 'UNIQUE(player, roleid))'))
# create the players table
c.execute("CREATE TABLE IF NOT EXISTS players (id INTEGER PRIMARY KEY AUTOINCREMENT, nick TEXT, cloak TEXT, "+
"UNIQUE(nick, cloak))")
# create nick change table
c.execute("CREATE TABLE IF NOT EXISTS nick_changes (old INTEGER, new INTEGER, UNIQUE(new))")
@ -209,30 +204,17 @@ def add_away(clk):
c.execute('INSERT into away VALUES (?)', (clk,)) c.execute('INSERT into away VALUES (?)', (clk,))
def add_player_record(nick, cloak):
with conn:
c.execute('INSERT OR IGNORE INTO players (nick, cloak) VALUES (?,?)', (nick, cloak))
def update_role_stats(nick, clk, 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
c.execute('SELECT id FROM players WHERE nick=? AND cloak=?', (nick, clk))
row = c.fetchone()
if row:
plid = row[0]
else:
c.execute('INSERT INTO players (nick, cloak) VALUES (?,?)', (nick, clk))
c.execute('SELECT id FROM players WHERE nick=? AND cloak=?', (nick, clk))
plid = c.fetchone()[0]
c.execute('SELECT id FROM roles WHERE role=?', (role,)) c.execute('SELECT id FROM roles WHERE role=?', (role,))
rid = c.fetchone()[0] rid = c.fetchone()[0]
c.execute(("SELECT teamwins, individualwins, totalgames FROM rolestats "+ c.execute(("SELECT teamwins, individualwins, totalgames FROM rolestats "+
"WHERE playerid=? AND roleid=?"), (plid, rid)) "WHERE player=? AND roleid=?"), (acc, rid))
row = c.fetchone() row = c.fetchone()
if row: if row:
wins, iwins, total = row wins, iwins, total = row
@ -246,7 +228,7 @@ def update_role_stats(nick, clk, role, won, iwon):
total += 1 total += 1
c.execute("INSERT OR REPLACE INTO rolestats VALUES (?,?,?,?,?)", c.execute("INSERT OR REPLACE INTO rolestats VALUES (?,?,?,?,?)",
(plid, rid, wins, iwins, total)) (acc, rid, wins, iwins, total))

12
wolfgame.py

@ -332,8 +332,6 @@ def join(cli, nick, chan, rest):
nick, _, __, cloak = parse_nick(nick) nick, _, __, cloak = parse_nick(nick)
if var.PHASE == "none": if var.PHASE == "none":
if cloak:
var.add_player_record(nick, cloak)
cli.mode(chan, "+v", nick, nick+"!*@*") cli.mode(chan, "+v", nick, nick+"!*@*")
var.ROLES["person"].append(nick) var.ROLES["person"].append(nick)
@ -351,8 +349,6 @@ def join(cli, nick, chan, rest):
elif var.PHASE != "join": elif var.PHASE != "join":
cli.notice(nick, "Sorry but the game is already running. Try again next time.") cli.notice(nick, "Sorry but the game is already running. Try again next time.")
else: else:
if cloak:
var.add_player_record(nick, cloak)
cli.mode(chan, "+v", nick, nick+"!*@*") cli.mode(chan, "+v", nick, nick+"!*@*")
var.ROLES["person"].append(nick) var.ROLES["person"].append(nick)
@ -710,12 +706,14 @@ def stop_game(cli, winner = ""):
for plr, rol in plrl: for plr, rol in plrl:
if plr not in var.USERS.keys(): # he died TODO: when a player leaves, count the game as lost for him if plr not in var.USERS.keys(): # he died TODO: when a player leaves, count the game as lost for him
if plr in var.DEAD_USERS.keys(): if plr in var.DEAD_USERS.keys():
clk = var.DEAD_USERS[plr] acc = var.DEAD_USERS[plr]["account"]
else: else:
continue # something wrong happened continue # something wrong happened
else: else:
clk = var.USERS[plr]["cloak"] acc = var.USERS[plr]["account"]
if acc == "*":
continue # not logged in
# determine if this player's team won # determine if this player's team won
if plr in (var.ORIGINAL_ROLES["wolf"] + var.ORIGINAL_ROLES["traitor"] + if plr in (var.ORIGINAL_ROLES["wolf"] + var.ORIGINAL_ROLES["traitor"] +
var.ORIGINAL_ROLES["werecrow"]): # the player was wolf-aligned var.ORIGINAL_ROLES["werecrow"]): # the player was wolf-aligned
@ -735,7 +733,7 @@ def stop_game(cli, winner = ""):
iwon = won and plr in var.list_players() # survived, team won = individual win iwon = won and plr in var.list_players() # survived, team won = individual win
var.update_role_stats(plr, clk, rol, won, iwon) var.update_role_stats(acc, rol, won, iwon)
reset(cli) reset(cli)

Loading…
Cancel
Save