Browse Source

normalized the database substantially

master
jcao219 13 years ago
parent
commit
fac7d6b01d
  1. 76
      var.py

76
var.py

@ -158,13 +158,31 @@ import sqlite3
import os import os
conn = sqlite3.connect("data.sqlite3", check_same_thread = False) conn = sqlite3.connect("data.sqlite3", check_same_thread = False)
c = conn.cursor()
c.execute('CREATE TABLE IF NOT EXISTS away (nick TEXT)')
c.execute('SELECT * FROM away') with conn:
c = conn.cursor()
c.execute('CREATE TABLE IF NOT EXISTS away (nick TEXT)')
for row in c: c.execute('SELECT * FROM away')
AWAY.append(row[0])
for row in c:
AWAY.append(row[0])
# populate the roles table
c.execute('DROP TABLE IF EXISTS roles')
c.execute('CREATE TABLE roles (id INTEGER PRIMARY KEY AUTOINCREMENT, role TEXT)')
for x in ["villager"]+list(ROLE_INDICES.values()):
c.execute("INSERT OR REPLACE INTO roles (role) VALUES (?)", (x,))
c.execute(('CREATE TABLE IF NOT EXISTS rolestats (playerid INTEGER, roleid INTEGER, '+
'teamwins SMALLINT, individualwins SMALLINT, totalgames SMALLINT, '+
'UNIQUE(playerid, roleid))'))
c.execute("CREATE TABLE IF NOT EXISTS players (id INTEGER PRIMARY KEY AUTOINCREMENT, cloak TEXT)")
def remove_away(clk): def remove_away(clk):
with conn: with conn:
@ -174,36 +192,40 @@ def add_away(clk):
with conn: with conn:
c.execute('INSERT into away VALUES (?)', (clk,)) c.execute('INSERT into away VALUES (?)', (clk,))
def update_role_stats(clk, role, won, iwon): def update_role_stats(clk, role, won, iwon):
role = role.replace(" ", "_")+'_stats'
with conn: with conn:
c.execute(('CREATE TABLE IF NOT EXISTS {0} (id INTEGER PRIMARY KEY AUTOINCREMENT, '+
'cloak TEXT UNIQUE, teamwins SMALLINT, individualwins SMALLINT, total SMALLINT)').format(role))
wins, iwins, totalgames = 0, 0, 0 wins, iwins, totalgames = 0, 0, 0
c.execute('SELECT * FROM {0} WHERE cloak=?'.format(role), (clk,)) c.execute('SELECT id FROM players WHERE cloak=?', (clk,))
row = c.fetchone()
if row:
plid = row[0]
else:
c.execute('INSERT INTO players (cloak) VALUES (?)', (clk,))
c.execute('SELECT id FROM players WHERE cloak=?', (clk,))
plid = c.fetchone()[0]
c.execute('SELECT id FROM roles WHERE role=?', (role,))
rid = c.fetchone()[0]
c.execute(("SELECT teamwins, individualwins, totalgames FROM rolestats "+
"WHERE playerid=? AND roleid=?"), (plid, rid))
row = c.fetchone() row = c.fetchone()
if row: if row:
_, __, wins, iwins, totalgames = row wins, iwins, total = row
else:
wins, iwins, total = 0,0,0
if won: if won:
wins += 1 wins += 1
if iwon: if iwon:
iwins += 1 iwins += 1
totalgames += 1 total += 1
c.execute(('INSERT OR REPLACE INTO {0} (cloak, teamwins, individualwins, total) '+ c.execute("INSERT OR REPLACE INTO rolestats VALUES (?,?,?,?,?)",
'VALUES (?,?,?,?)').format(role), (clk, wins, iwins, totalgames)) (plid, rid, wins, iwins, total))
def get_role_stats(clk, role):
role = role.replace(" ", "_")+'_stats'
with conn:
c.execute(('CREATE TABLE IF NOT EXISTS {0} (id INTEGER PRIMARY KEY AUTOINCREMENT, '+
'cloak TEXT UNIQUE, teamwins SMALLINT, individualwins SMALLINT, total SMALLINT)').format(role))
wins, iwins, totalgames = 0, 0, 0
c.execute('SELECT * FROM {0} WHERE cloak=?'.format(role), (clk,))
row = c.fetchone()
if row:
_, __, wins, iwins, totalgames = row
return wins, iwins, totalgames
Loading…
Cancel
Save