Move database/persistence initialization into a function

This allows us to postpone init_db(). One advantage of this is that
specifying AWAY in botconfig.py won't overwrite the away list from the
DB anymore, but properly get added to (as would be expected).
This commit is contained in:
Janik Kleinhoff 2014-12-08 13:18:34 +00:00
parent 7ad48ee412
commit 1d06b68d24
2 changed files with 47 additions and 44 deletions

View File

@ -87,6 +87,8 @@ var.DISCONNECTED = {} # players who got disconnected
var.LOGGER = WolfgameLogger(var.LOG_FILENAME, var.BARE_LOG_FILENAME) var.LOGGER = WolfgameLogger(var.LOG_FILENAME, var.BARE_LOG_FILENAME)
var.init_db()
var.OPPED = False # Keeps track of whether the bot is opped var.OPPED = False # Keeps track of whether the bot is opped
if botconfig.DEBUG_MODE: if botconfig.DEBUG_MODE:

View File

@ -658,71 +658,72 @@ class AleatoireMode(object):
import sqlite3 import sqlite3
conn = sqlite3.connect("data.sqlite3", check_same_thread = False) conn = sqlite3.connect("data.sqlite3", check_same_thread = False)
c = conn.cursor()
with conn: def init_db():
c = conn.cursor() with conn:
c.execute('CREATE TABLE IF NOT EXISTS away (nick TEXT)') # whoops, i mean cloak, not nick c.execute('CREATE TABLE IF NOT EXISTS away (nick TEXT)') # whoops, i mean cloak, not nick
c.execute('CREATE TABLE IF NOT EXISTS simple_role_notify (cloak TEXT)') # people who understand each role c.execute('CREATE TABLE IF NOT EXISTS simple_role_notify (cloak TEXT)') # people who understand each role
c.execute('CREATE TABLE IF NOT EXISTS prefer_notice (cloak TEXT)') # people who prefer /notice c.execute('CREATE TABLE IF NOT EXISTS prefer_notice (cloak TEXT)') # people who prefer /notice
c.execute('CREATE TABLE IF NOT EXISTS stasised (cloak TEXT, games INTEGER, UNIQUE(cloak))') # stasised people c.execute('CREATE TABLE IF NOT EXISTS stasised (cloak TEXT, games INTEGER, UNIQUE(cloak))') # stasised people
c.execute('CREATE TABLE IF NOT EXISTS denied (cloak TEXT, command TEXT, UNIQUE(cloak, command))') # botconfig.DENY c.execute('CREATE TABLE IF NOT EXISTS denied (cloak TEXT, command TEXT, UNIQUE(cloak, command))') # botconfig.DENY
c.execute('CREATE TABLE IF NOT EXISTS allowed (cloak TEXT, command TEXT, UNIQUE(cloak, command))') # botconfig.ALLOW c.execute('CREATE TABLE IF NOT EXISTS allowed (cloak TEXT, command TEXT, UNIQUE(cloak, command))') # botconfig.ALLOW
c.execute('SELECT * FROM away') c.execute('SELECT * FROM away')
for row in c: for row in c:
AWAY.append(row[0]) AWAY.append(row[0])
c.execute('SELECT * FROM simple_role_notify') c.execute('SELECT * FROM simple_role_notify')
for row in c: for row in c:
SIMPLE_NOTIFY.append(row[0]) SIMPLE_NOTIFY.append(row[0])
c.execute('SELECT * FROM prefer_notice') c.execute('SELECT * FROM prefer_notice')
for row in c: for row in c:
PREFER_NOTICE.append(row[0]) PREFER_NOTICE.append(row[0])
c.execute('SELECT * FROM stasised') c.execute('SELECT * FROM stasised')
for row in c: for row in c:
STASISED[row[0]] = row[1] STASISED[row[0]] = row[1]
c.execute('SELECT * FROM denied') c.execute('SELECT * FROM denied')
for row in c: for row in c:
if row[0] not in botconfig.DENY: if row[0] not in botconfig.DENY:
botconfig.DENY[row[0]] = [] botconfig.DENY[row[0]] = []
botconfig.DENY[row[0]].append(row[1]) botconfig.DENY[row[0]].append(row[1])
c.execute('SELECT * FROM allowed') c.execute('SELECT * FROM allowed')
for row in c: for row in c:
if row[0] not in botconfig.ALLOW: if row[0] not in botconfig.ALLOW:
botconfig.ALLOW[row[0]] = [] botconfig.ALLOW[row[0]] = []
botconfig.ALLOW[row[0]].append(row[1]) botconfig.ALLOW[row[0]].append(row[1])
# populate the roles table # populate the roles table
c.execute('DROP TABLE IF EXISTS roles') c.execute('DROP TABLE IF EXISTS roles')
c.execute('CREATE TABLE roles (id INTEGER PRIMARY KEY AUTOINCREMENT, role TEXT)') c.execute('CREATE TABLE roles (id INTEGER PRIMARY KEY AUTOINCREMENT, role TEXT)')
for x in list(ROLE_GUIDE.keys()): for x in list(ROLE_GUIDE.keys()):
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 (player TEXT, role TEXT, '+ c.execute(('CREATE TABLE IF NOT EXISTS rolestats (player TEXT, role TEXT, '+
'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 (gamemode TEXT, size SMALLINT, villagewins SMALLINT, ' + c.execute(('CREATE TABLE IF NOT EXISTS gamestats (gamemode TEXT, size SMALLINT, villagewins SMALLINT, ' +
'wolfwins SMALLINT, monsterwins SMALLINT, foolwins SMALLINT, totalgames SMALLINT, UNIQUE(gamemode, size))')) 'wolfwins SMALLINT, monsterwins SMALLINT, foolwins SMALLINT, totalgames SMALLINT, UNIQUE(gamemode, size))'))
c.execute('CREATE TABLE IF NOT EXISTS ping (cloak text)') c.execute('CREATE TABLE IF NOT EXISTS ping (cloak text)')
c.execute('SELECT * FROM ping') c.execute('SELECT * FROM ping')
for row in c: for row in c:
PING_IN.append(row[0]) PING_IN.append(row[0])
def remove_away(clk): def remove_away(clk):