import sqlite3 import os DB_PATH = os.path.join(os.path.dirname(__file__), 'grepo.db') def get_db(): conn = sqlite3.connect(DB_PATH) conn.row_factory = sqlite3.Row return conn def init_db(): conn = get_db() c = conn.cursor() # Commands queue — sent from dashboard, consumed by Tampermonkey c.execute(''' CREATE TABLE IF NOT EXISTS commands ( id INTEGER PRIMARY KEY AUTOINCREMENT, town_id TEXT NOT NULL, town_name TEXT, type TEXT NOT NULL, -- 'build' | 'recruit' payload TEXT NOT NULL, -- JSON string status TEXT NOT NULL DEFAULT 'pending', -- pending | executing | done | failed result_msg TEXT, created_at TEXT NOT NULL DEFAULT (datetime('now')), updated_at TEXT NOT NULL DEFAULT (datetime('now')) ) ''') # Town state — pushed by Tampermonkey every poll cycle c.execute(''' CREATE TABLE IF NOT EXISTS town_state ( town_id TEXT PRIMARY KEY, town_name TEXT, player TEXT, player_id TEXT, alliance_id TEXT, world_id TEXT, x REAL, y REAL, sea INTEGER, data TEXT NOT NULL, -- full JSON snapshot updated_at TEXT NOT NULL DEFAULT (datetime('now')) ) ''') # Key-value store — generic flags (e.g. captcha_active) c.execute(''' CREATE TABLE IF NOT EXISTS kv_store ( key TEXT PRIMARY KEY, value TEXT, updated_at TEXT NOT NULL DEFAULT (datetime('now')) ) ''') # Migration: add new columns if upgrading an existing database for _col in [ 'ALTER TABLE town_state ADD COLUMN player_id TEXT', 'ALTER TABLE town_state ADD COLUMN alliance_id TEXT', 'ALTER TABLE town_state ADD COLUMN x REAL', 'ALTER TABLE town_state ADD COLUMN y REAL', 'ALTER TABLE town_state ADD COLUMN sea INTEGER', ]: try: c.execute(_col) except Exception: pass # column already exists conn.commit() conn.close()