From a85d1e5f9a79fc309defeda4209391a33c180ad8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 12 May 2023 20:11:10 +0200 Subject: [PATCH] rework variants file: use DB-based access to variants information --- variants.py | 123 ++++++++++++++++++++++++++++------------------------ 1 file changed, 66 insertions(+), 57 deletions(-) diff --git a/variants.py b/variants.py index 834b53d..4976781 100644 --- a/variants.py +++ b/variants.py @@ -1,70 +1,79 @@ import json +import os +import networkx as nx +from collections import OrderedDict +import matplotlib.pyplot as plt + +from database import cur -# Some setup for conversion between variant id and name -with open("variants.json", 'r') as f: - VARIANTS = json.loads(f.read()) - -def variant_id(variant_name): - return next(var['id'] for var in VARIANTS if var['name'] == variant_name) - -def variant_name(variant_id): - return next(var['name'] for var in VARIANTS if var['id'] == variant_id) - -def num_suits(variant_id): - return next(len(var['suits']) for var in VARIANTS if var['id'] == variant_id) - -def properties(variant_id): - return next(var for var in VARIANTS if var['id'] == variant_id) +def variant_id(name): + cur.execute( + "SELECT id FROM variants WHERE name = %s", + (name,) + ) + return cur.fetchone()[0] -if __name__ == "__main__": - x = set() - c = set() - for var in VARIANTS: - for k in var.keys(): - x.add(k) - for s in var['suits']: - c.add(s) - for y in x: - print(y) +def variant_name(var_id): + cur.execute( + "SELECT name FROM variants WHERE id = %s", + (var_id,) + ) + return cur.fetchone()[0] - for s in c: - print(s) - # need: suit name -> colors +def num_suits(var_id): + cur.execute( + "SELECT num_suits FROM variants WHERE id = %s", + (var_id,) + ) + return cur.fetchone()[0] -""" -# actual changes of theoretical instance -clueStarved -throwItInHole (no clues for fives) -# general restrictions on what clues are allowed -alternatingClues -clueColors -clueRanks -synesthesia (no rank clused, but color touches rank as well) +class Variant: + def __init__( + self, name, clue_starved, throw_it_in_a_hole, alternating_clues, synesthesia, chimneys, funnels, + no_color_clues, no_rank_clues, odds_and_evens, up_or_down, critical_fours, num_suits, special_rank, + special_rank_ranks, special_rank_colors, suits + ): + self.name = name + self.clue_starved = clue_starved + self.throw_it_in_a_hole = throw_it_in_a_hole + self.alternating_clues = alternating_clues + self.synesthesia = synesthesia + self.chimneys = chimneys + self.funnels = funnels + self.no_color_clues = no_color_clues + self.no_rank_clues = no_rank_clues + self.odds_and_evens = odds_and_evens + self.up_or_down = up_or_down + self.critical_fours = critical_fours + self.num_suits = num_suits + self.special_rank = special_rank + self.special_rank_ranks = special_rank_ranks + self.special_rank_colors = special_rank_colors -# can be ignored -cowPig -duck + self.suits = suits -# -> use oracle? -# clue touch changed -chimneys -funnels -colorCluesTouchNothing -rankCluesTouchNothing -oddsAndEvens (ranks touch ranks of same parity) + @staticmethod + def from_db(var_id): + cur.execute( + "SELECT " + "name, clue_starved, throw_it_in_a_hole, alternating_clues, synesthesia, chimneys, funnels, " + "no_color_clues, no_rank_clues, odds_and_evens, up_or_down, critical_fours, num_suits, special_rank, " + "special_rank_ranks, special_rank_colors " + "FROM variants WHERE id = %s", + (var_id,) + ) + var_properties = cur.fetchone() -# changes behaviour of ones or fives -specialAllClueColors -specialAllClueRanks -specialNoClueColors -specialNoClueRanks -specialDeceptive -specialRank + cur.execute( + "SELECT suit_id FROM variant_suits " + "WHERE variant_id = %s " + "ORDER BY index", + (var_id,) + ) + var_suits = list(map(lambda x: x[0], cur.fetchall())) -upOrDown -criticalFours -""" + return Variant(*var_properties, var_suits)