rework variants file: use DB-based access to variants information
This commit is contained in:
parent
550df72a28
commit
a85d1e5f9a
1 changed files with 66 additions and 57 deletions
123
variants.py
123
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)
|
||||
|
|
Loading…
Reference in a new issue