From 85abc39632e80e876c9572c3f83802a0874eb240 Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Sun, 30 Oct 2022 02:32:40 +0200 Subject: [PATCH] dedup converting game state to json --- files/classes/casino_game.py | 7 +++++++ files/helpers/roulette.py | 4 ++-- files/helpers/twentyone.py | 4 ++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/files/classes/casino_game.py b/files/classes/casino_game.py index 5e1d9a4fa..4e74fd9d7 100644 --- a/files/classes/casino_game.py +++ b/files/classes/casino_game.py @@ -1,6 +1,8 @@ from sqlalchemy import * from files.__main__ import Base import time +from files.helpers.lazy import lazy +import json CASINO_GAME_KINDS = ['blackjack', 'slots', 'roulette'] @@ -24,3 +26,8 @@ class Casino_Game(Base): def __repr__(self): return f"" + + @property + @lazy + def game_state_json(self): + return json.loads(self.game_state) diff --git a/files/helpers/roulette.py b/files/helpers/roulette.py index f658e5cf3..e27c10154 100644 --- a/files/helpers/roulette.py +++ b/files/helpers/roulette.py @@ -95,7 +95,7 @@ def gambler_placed_roulette_bet(gambler, bet, which, amount, currency): if len(active_games) == 0: parent_id = int(time.time()) 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) @@ -130,7 +130,7 @@ def get_roulette_bets_and_betters(): participants.append(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'] bets[bet].append({ 'game_id': game.id, diff --git a/files/helpers/twentyone.py b/files/helpers/twentyone.py index 8a6c2c4ad..fee54c70f 100644 --- a/files/helpers/twentyone.py +++ b/files/helpers/twentyone.py @@ -76,7 +76,7 @@ def get_active_twentyone_game(gambler): def get_active_twentyone_game_state(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) @@ -286,7 +286,7 @@ def dispatch_action(gambler, action): raise Exception( 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 not can_purchase_insurance(state):