add seeds table
This commit is contained in:
parent
6077c64f06
commit
0bd0b42230
2 changed files with 52 additions and 24 deletions
38
database.py
38
database.py
|
@ -12,14 +12,17 @@ cur = conn.cursor()
|
||||||
# exit(0)
|
# exit(0)
|
||||||
|
|
||||||
## check if table exists, else create it
|
## check if table exists, else create it
|
||||||
cur.execute("SELECT EXISTS (SELECT FROM pg_tables WHERE schemaname = 'public' AND tablename = 'games');")
|
|
||||||
|
def create_games_table():
|
||||||
|
tablename = "games"
|
||||||
|
cur.execute("SELECT EXISTS (SELECT FROM pg_tables WHERE schemaname = 'public' AND tablename = '{}');".format(tablename))
|
||||||
a = cur.fetchone()
|
a = cur.fetchone()
|
||||||
|
|
||||||
if a[0] is False:
|
if a[0] is False:
|
||||||
print("creating table")
|
print("Creating table '{}'".format(tablename))
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"CREATE TABLE games ("
|
"CREATE TABLE {} ("
|
||||||
"id SERIAL PRIMARY KEY,"
|
"id INT PRIMARY KEY,"
|
||||||
"num_players SMALLINT NOT NULL,"
|
"num_players SMALLINT NOT NULL,"
|
||||||
"score SMALLINT NOT NULL,"
|
"score SMALLINT NOT NULL,"
|
||||||
"seed TEXT NOT NULL,"
|
"seed TEXT NOT NULL,"
|
||||||
|
@ -29,10 +32,31 @@ if a[0] is False:
|
||||||
"one_less_card BOOLEAN,"
|
"one_less_card BOOLEAN,"
|
||||||
"all_or_nothing BOOLEAN,"
|
"all_or_nothing BOOLEAN,"
|
||||||
"num_turns SMALLINT"
|
"num_turns SMALLINT"
|
||||||
");")
|
");".format(tablename))
|
||||||
conn.commit()
|
conn.commit()
|
||||||
else:
|
# else:
|
||||||
print("table already exists")
|
# 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():
|
class Game():
|
||||||
|
|
|
@ -2,14 +2,17 @@ import json
|
||||||
from site_api import get, api, replay
|
from site_api import get, api, replay
|
||||||
from database import Game, store, load, commit
|
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)
|
url = "variants/{}".format(variant_id)
|
||||||
r = api(url)
|
r = api(url)
|
||||||
if not r:
|
if not r:
|
||||||
print("Not a valid variant: {}".format(variant_id))
|
print("Not a valid variant: {}".format(variant_id))
|
||||||
return
|
return
|
||||||
num_entries = r['total_rows']
|
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
|
num_pages = (num_entries + 99) // 100
|
||||||
for page in range(0, num_pages):
|
for page in range(0, num_pages):
|
||||||
print("Downloading page {} of {}".format(page + 1, num_pages), end = '\r')
|
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
|
g.variant_id = variant_id
|
||||||
store(g)
|
store(g)
|
||||||
print()
|
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()
|
commit()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
download_games(1)
|
for var in variants:
|
||||||
|
download_games(var['id'], var['name'])
|
||||||
|
|
Loading…
Reference in a new issue