diff --git a/instance_finder.py b/instance_finder.py index bca5419..29bbdf3 100644 --- a/instance_finder.py +++ b/instance_finder.py @@ -1,61 +1,44 @@ import json from site_api import get, api, replay from sat import COLORS, solve -from database import Game, store, load, commit - -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 +from database import Game, store, load, commit, conn +from download_data import export_game -def solvable(replay): - deck = replay["deck"] - deck_str = " ".join(COLORS[c["suitIndex"]] + str(c["rank"]) for c in deck) - return solve(deck_str, len(replay["players"])) +def update_seeds_db(): + cur2 = conn.cursor() + with conn.cursor() as cur: + 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 -for i in range(0,10000): - r = api("variants/0?page=" + str(i)) - for row in r['rows']: - num_entries += 1 - row.pop('users') - row.pop('datetime') - g = Game(row) - g.variant_id = 0 - store(g) +def get_decks_of_seeds(): + cur = conn.cursor() + cur2 = conn.cursor() + cur.execute("SELECT seed FROM seeds WHERE deck is NULL") + for (seed,) in cur: + cur2.execute("SELECT id FROM games WHERE seed = (%s)", (seed,)) + (game_id,) = cur2.fetchone() + print("Exporting game {} for seed {}.".format(game_id, seed)) + export_game(game_id) + conn.commit() -print('considered {} entries'.format(num_entries)) -commit() +get_decks_of_seeds() 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)) for i in range(2,7): print("Found {} seeds in {}-player in database".format(len(seeds[i]), i))