add seeds table
This commit is contained in:
parent
6077c64f06
commit
0bd0b42230
64
database.py
64
database.py
@ -12,27 +12,51 @@ cur = conn.cursor()
|
||||
# exit(0)
|
||||
|
||||
## check if table exists, else create it
|
||||
cur.execute("SELECT EXISTS (SELECT FROM pg_tables WHERE schemaname = 'public' AND tablename = 'games');")
|
||||
a = cur.fetchone()
|
||||
|
||||
if a[0] is False:
|
||||
print("creating table")
|
||||
cur.execute(
|
||||
"CREATE TABLE games ("
|
||||
"id SERIAL PRIMARY KEY,"
|
||||
"num_players SMALLINT NOT NULL,"
|
||||
"score SMALLINT NOT NULL,"
|
||||
"seed TEXT NOT NULL,"
|
||||
"variant_id SMALLINT NOT NULL,"
|
||||
"deck_plays BOOLEAN,"
|
||||
"one_extra_card BOOLEAN,"
|
||||
"one_less_card BOOLEAN,"
|
||||
"all_or_nothing BOOLEAN,"
|
||||
"num_turns SMALLINT"
|
||||
");")
|
||||
conn.commit()
|
||||
else:
|
||||
print("table already exists")
|
||||
def create_games_table():
|
||||
tablename = "games"
|
||||
cur.execute("SELECT EXISTS (SELECT FROM pg_tables WHERE schemaname = 'public' AND tablename = '{}');".format(tablename))
|
||||
a = cur.fetchone()
|
||||
|
||||
if a[0] is False:
|
||||
print("Creating table '{}'".format(tablename))
|
||||
cur.execute(
|
||||
"CREATE TABLE {} ("
|
||||
"id INT PRIMARY KEY,"
|
||||
"num_players SMALLINT NOT NULL,"
|
||||
"score SMALLINT NOT NULL,"
|
||||
"seed TEXT NOT NULL,"
|
||||
"variant_id SMALLINT NOT NULL,"
|
||||
"deck_plays BOOLEAN,"
|
||||
"one_extra_card BOOLEAN,"
|
||||
"one_less_card BOOLEAN,"
|
||||
"all_or_nothing BOOLEAN,"
|
||||
"num_turns SMALLINT"
|
||||
");".format(tablename))
|
||||
conn.commit()
|
||||
# else:
|
||||
# print("table already exists")
|
||||
|
||||
def create_seeds_table():
|
||||
tablename = 'seeds'
|
||||
cur.execute("SELECT EXISTS (SELECT FROM pg_tables WHERE schemaname = 'public' AND tablename = '{}');".format(tablename))
|
||||
a = cur.fetchone()
|
||||
|
||||
if a[0] is False:
|
||||
print("Creating table '{}'".format(tablename))
|
||||
cur.execute(
|
||||
"CREATE TABLE {} ("
|
||||
"seed TEXT NOT NULL PRIMARY KEY,"
|
||||
"num_players SMALLINT NOT NULL,"
|
||||
"variant_id SMALLINT NOT NULL,"
|
||||
"feasible BOOLEAN," # theoretical solvability
|
||||
"max_score_theoretical SMALLINT" # if infeasible, max score
|
||||
");".format(tablename))
|
||||
conn.commit()
|
||||
|
||||
create_games_table()
|
||||
create_seeds_table()
|
||||
|
||||
|
||||
|
||||
class Game():
|
||||
|
@ -2,14 +2,17 @@ import json
|
||||
from site_api import get, api, replay
|
||||
from database import Game, store, load, commit
|
||||
|
||||
def download_games(variant_id):
|
||||
with open('variants.json') as f:
|
||||
variants = json.loads(f.read())
|
||||
|
||||
def download_games(variant_id, name=None):
|
||||
url = "variants/{}".format(variant_id)
|
||||
r = api(url)
|
||||
if not r:
|
||||
print("Not a valid variant: {}".format(variant_id))
|
||||
return
|
||||
num_entries = r['total_rows']
|
||||
print("Downloading {} entries for variant {}".format(num_entries, variant_id))
|
||||
print("Downloading {} entries for variant {} ({})".format(num_entries, variant_id, name))
|
||||
num_pages = (num_entries + 99) // 100
|
||||
for page in range(0, num_pages):
|
||||
print("Downloading page {} of {}".format(page + 1, num_pages), end = '\r')
|
||||
@ -21,9 +24,10 @@ def download_games(variant_id):
|
||||
g.variant_id = variant_id
|
||||
store(g)
|
||||
print()
|
||||
print('Downloaded and stored {} entries for variant {}'.format(num_entries, variant_id))
|
||||
print('Downloaded and stored {} entries for variant {} ({})'.format(num_entries, variant_id, name))
|
||||
commit()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
download_games(1)
|
||||
for var in variants:
|
||||
download_games(var['id'], var['name'])
|
||||
|
Loading…
Reference in New Issue
Block a user