dedup converting game state to json

remotes/1693176582716663532/tmp_refs/heads/watchparty
Aevann1 2022-10-30 02:32:40 +02:00
parent a18c2e24e9
commit 85abc39632
3 changed files with 11 additions and 4 deletions

View File

@ -1,6 +1,8 @@
from sqlalchemy import * from sqlalchemy import *
from files.__main__ import Base from files.__main__ import Base
import time import time
from files.helpers.lazy import lazy
import json
CASINO_GAME_KINDS = ['blackjack', 'slots', 'roulette'] CASINO_GAME_KINDS = ['blackjack', 'slots', 'roulette']
@ -24,3 +26,8 @@ class Casino_Game(Base):
def __repr__(self): def __repr__(self):
return f"<CasinoGame(id={self.id})>" return f"<CasinoGame(id={self.id})>"
@property
@lazy
def game_state_json(self):
return json.loads(self.game_state)

View File

@ -95,7 +95,7 @@ def gambler_placed_roulette_bet(gambler, bet, which, amount, currency):
if len(active_games) == 0: if len(active_games) == 0:
parent_id = int(time.time()) parent_id = int(time.time())
else: else:
parent_id = json.loads(active_games[0].game_state)['parent_id'] parent_id = active_games[0].game_state_json['parent_id']
charge_gambler(gambler, amount, currency) charge_gambler(gambler, amount, currency)
@ -130,7 +130,7 @@ def get_roulette_bets_and_betters():
participants.append(game.user_id) participants.append(game.user_id)
user = get_account(game.user_id) user = get_account(game.user_id)
game_state = json.loads(game.game_state) game_state = game.game_state_json
bet = game_state['bet'] bet = game_state['bet']
bets[bet].append({ bets[bet].append({
'game_id': game.id, 'game_id': game.id,

View File

@ -76,7 +76,7 @@ def get_active_twentyone_game(gambler):
def get_active_twentyone_game_state(gambler): def get_active_twentyone_game_state(gambler):
active_game = get_active_twentyone_game(gambler) active_game = get_active_twentyone_game(gambler)
full_state = json.loads(active_game.game_state) full_state = active_game.game_state_json
return remove_exploitable_information(full_state) return remove_exploitable_information(full_state)
@ -286,7 +286,7 @@ def dispatch_action(gambler, action):
raise Exception( raise Exception(
f'Illegal action {action} passed to Blackjack#dispatch_action.') f'Illegal action {action} passed to Blackjack#dispatch_action.')
state = json.loads(game.game_state) state = game.game_state_json
if action == BlackjackAction.BUY_INSURANCE: if action == BlackjackAction.BUY_INSURANCE:
if not can_purchase_insurance(state): if not can_purchase_insurance(state):