42 lines
1.1 KiB
Python
42 lines
1.1 KiB
Python
from typing import List
|
|
|
|
import psycopg2.extras
|
|
from database import conn_manager
|
|
|
|
import hanabi.hanab_game
|
|
|
|
|
|
def store_actions(game_id: int, actions: List[hanabi.hanab_game.Action]):
|
|
vals = []
|
|
for turn, action in enumerate(actions):
|
|
vals.append((game_id, turn, action.type.value, action.target, action.value or 0))
|
|
|
|
conn = conn_manager.get_connection()
|
|
cur = conn.cursor()
|
|
psycopg2.extras.execute_values(
|
|
cur,
|
|
"INSERT INTO game_actions (game_id, turn, type, target, value) "
|
|
"VALUES %s "
|
|
"ON CONFLICT (game_id, turn) "
|
|
"DO NOTHING",
|
|
vals
|
|
)
|
|
conn.commit()
|
|
|
|
|
|
def store_deck_for_seed(seed: str, deck: List[hanabi.hanab_game.DeckCard]):
|
|
vals = []
|
|
for index, card in enumerate(deck):
|
|
vals.append((seed, index, card.suitIndex, card.rank))
|
|
|
|
conn = conn_manager.get_connection()
|
|
cur = conn.cursor()
|
|
psycopg2.extras.execute_values(
|
|
cur,
|
|
"INSERT INTO seeds (seed, card_index, suit_index, rank) "
|
|
"VALUES %s "
|
|
"ON CONFLICT (seed, card_index) "
|
|
"DO NOTHING",
|
|
vals
|
|
)
|
|
conn.commit()
|