rewrite instance_finder

now contains utilities to fetch data from hanab.live / update DBs
This commit is contained in:
Maximilian Keßler 2023-03-02 22:16:43 +01:00
parent 9b976f6552
commit d7387574ea
Signed by: max
GPG key ID: BCC5A619923C0BA5

View file

@ -1,61 +1,44 @@
import json import json
from site_api import get, api, replay from site_api import get, api, replay
from sat import COLORS, solve from sat import COLORS, solve
from database import Game, store, load, commit from database import Game, store, load, commit, conn
from download_data import export_game
def known_solvable(seed):
link = "seed/" + seed
r = api(link)
rows = r['rows']
if not rows:
print("invalid response to seed {}".format(seed))
return False
for row in rows:
if row["score"] == 25:
return True, row["id"]
for i in range(1, (r['total_rows'] + 99) // 100):
page = api(link + "?page=" + str(i))
rows = page['rows']
for row in rows:
if row["score"] == 25:
return True, row["id"]
# print("No solution found in database for seed {}".format(seed))
return False
def solvable(replay): def update_seeds_db():
deck = replay["deck"] cur2 = conn.cursor()
deck_str = " ".join(COLORS[c["suitIndex"]] + str(c["rank"]) for c in deck) with conn.cursor() as cur:
return solve(deck_str, len(replay["players"])) cur.execute("SELECT num_players, seed, variant_id from games;")
for (num_players, seed, variant_id) in cur:
cur2.execute("SELECT COUNT(*) from seeds WHERE seed = (%s);", (seed,))
if cur2.fetchone()[0] == 0:
print("new seed {}".format(seed))
cur2.execute("INSERT INTO seeds"
"(seed, num_players, variant_id)"
"VALUES"
"(%s, %s, %s)",
(seed, num_players, variant_id)
)
conn.commit()
else:
print("seed {} already found in DB".format(seed))
num_entries = 0 def get_decks_of_seeds():
for i in range(0,10000): cur = conn.cursor()
r = api("variants/0?page=" + str(i)) cur2 = conn.cursor()
for row in r['rows']: cur.execute("SELECT seed FROM seeds WHERE deck is NULL")
num_entries += 1 for (seed,) in cur:
row.pop('users') cur2.execute("SELECT id FROM games WHERE seed = (%s)", (seed,))
row.pop('datetime') (game_id,) = cur2.fetchone()
g = Game(row) print("Exporting game {} for seed {}.".format(game_id, seed))
g.variant_id = 0 export_game(game_id)
store(g) conn.commit()
print('considered {} entries'.format(num_entries)) get_decks_of_seeds()
commit()
exit(0) exit(0)
seeds = {2: [], 3: [], 4: [], 5: [], 6: []}
num_games = 0
for i in range(0,10000):
r = api("variants/0?page=" + str(i))
for row in r['rows']:
num_games += 1
if row['seed'] not in seeds[row['num_players']]:
seeds[row['num_players']].append(row['seed'])
# print('found new non-max-game in 5p: ' + row['seed'])
print('looked at {} games'.format(num_games)) print('looked at {} games'.format(num_games))
for i in range(2,7): for i in range(2,7):
print("Found {} seeds in {}-player in database".format(len(seeds[i]), i)) print("Found {} seeds in {}-player in database".format(len(seeds[i]), i))