add json response store manager
This commit is contained in:
@ -1,4 +1,5 @@
|
|||||||
API_URI=
|
API_URI=
|
||||||
API_KEY=
|
API_KEY=
|
||||||
API_TEAM=
|
API_TEAM=
|
||||||
|
GAME_FILES_DIR=/project_absolute_path/game_files
|
||||||
GROQ_API_KEY=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
|
GROQ_API_KEY=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
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -273,3 +273,5 @@ devenv.lock
|
|||||||
|
|
||||||
# pre-commit
|
# pre-commit
|
||||||
.pre-commit-config.yaml
|
.pre-commit-config.yaml
|
||||||
|
|
||||||
|
game_files
|
||||||
|
@ -5,4 +5,5 @@ from dotenv import load_dotenv
|
|||||||
load_dotenv()
|
load_dotenv()
|
||||||
API_URI = str(os.getenv("API_URI") or "")
|
API_URI = str(os.getenv("API_URI") or "")
|
||||||
API_KEY = str(os.getenv("API_KEY") or "")
|
API_KEY = str(os.getenv("API_KEY") or "")
|
||||||
API_TEAM = str(os.getenv("API_TEAM") or "")
|
API_TEAM = str(os.getenv("API_TEAM") or "")
|
||||||
|
GAME_FILES_DIR = str(os.getenv("GAME_FILES_DIR") or "")
|
@ -5,6 +5,7 @@ from typing import Literal, Dict, Any
|
|||||||
import config
|
import config
|
||||||
from dto.requests import GameStartRequestDTO, GameDecisionRequestDTO
|
from dto.requests import GameStartRequestDTO, GameDecisionRequestDTO
|
||||||
from services.julius_baer_api_client import JuliusBaerApiClient
|
from services.julius_baer_api_client import JuliusBaerApiClient
|
||||||
|
from utils.storage.game_files_manager import store_game_round_data
|
||||||
|
|
||||||
|
|
||||||
class Player:
|
class Player:
|
||||||
@ -33,8 +34,11 @@ class Player:
|
|||||||
|
|
||||||
status = ''
|
status = ''
|
||||||
decision = self.make_decision(start_response.client_data)
|
decision = self.make_decision(start_response.client_data)
|
||||||
while status not in ['gameover', 'complete']:
|
|
||||||
|
|
||||||
|
decision_counter = 0
|
||||||
|
store_game_round_data(start_response, decision_counter, str(start_response.session_id))
|
||||||
|
|
||||||
|
while status not in ['gameover', 'complete']:
|
||||||
payload = GameDecisionRequestDTO(
|
payload = GameDecisionRequestDTO(
|
||||||
decision=decision,
|
decision=decision,
|
||||||
session_id=start_response.session_id,
|
session_id=start_response.session_id,
|
||||||
@ -45,8 +49,13 @@ class Player:
|
|||||||
print(decision_response.status, decision_response.score)
|
print(decision_response.status, decision_response.score)
|
||||||
status = decision_response.status
|
status = decision_response.status
|
||||||
decision = self.make_decision(decision_response.client_data)
|
decision = self.make_decision(decision_response.client_data)
|
||||||
|
|
||||||
|
store_game_round_data(decision_response, decision_counter, str(start_response.session_id))
|
||||||
|
|
||||||
|
decision_counter += 1
|
||||||
time.sleep(1.5)
|
time.sleep(1.5)
|
||||||
|
|
||||||
|
|
||||||
def make_decision(self, client_data: Dict[str, Any]) -> Literal["Accept", "Reject"]:
|
def make_decision(self, client_data: Dict[str, Any]) -> Literal["Accept", "Reject"]:
|
||||||
# Do your magic!
|
# Do your magic!
|
||||||
|
|
||||||
|
0
utils/storage/__init__.py
Normal file
0
utils/storage/__init__.py
Normal file
35
utils/storage/game_files_manager.py
Normal file
35
utils/storage/game_files_manager.py
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import os
|
||||||
|
import logging
|
||||||
|
import config
|
||||||
|
import json
|
||||||
|
|
||||||
|
from dto.responses import GameStartResponseDTO, GameDecisionResponseDTO
|
||||||
|
|
||||||
|
GAME_FILES_DIR = config.GAME_FILES_DIR
|
||||||
|
|
||||||
|
# Define padding for round numbers (e.g., 6 digits for up to 999,999 rounds)
|
||||||
|
FOLDER_ROUND_PADDING = 6
|
||||||
|
|
||||||
|
def store_game_round_data(response: GameStartResponseDTO | GameDecisionResponseDTO, round_number: int, session_id: str):
|
||||||
|
"""
|
||||||
|
Logs structured response data and saves associated client files.
|
||||||
|
"""
|
||||||
|
logging.info(f"[+] Storing game round data in {GAME_FILES_DIR}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
padded_round = str(round_number).zfill(FOLDER_ROUND_PADDING)
|
||||||
|
round_folder_name = f"{padded_round}_decision"
|
||||||
|
|
||||||
|
# Construct the directory path: base_dir / session_id / decision_XXXXXX
|
||||||
|
round_dir = os.path.join(GAME_FILES_DIR, str(session_id), round_folder_name)
|
||||||
|
os.makedirs(round_dir, exist_ok=True) # Create the directory structure if it doesn't exist
|
||||||
|
|
||||||
|
json_file_path = os.path.join(round_dir, f"{padded_round}_response.json")
|
||||||
|
|
||||||
|
with open(json_file_path, "w") as json_file:
|
||||||
|
json.dump(response.model_dump_json(), json_file)
|
||||||
|
|
||||||
|
logging.info(f"[+] Successfully saved API response JSON to: {json_file_path}")
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"[!] Failed to save API response JSON: {e}")
|
||||||
|
|
Reference in New Issue
Block a user