Added decision logging (#2)

* Added decision logging
This commit is contained in:
Luca De Laurentiis
2025-04-12 15:41:45 +02:00
committed by GitHub
parent 8bc59aeae6
commit 7d8b34210c
3 changed files with 39 additions and 16 deletions

View File

@ -0,0 +1,10 @@
client_id,decision
cdd60b2d-34eb-4db4-9d5e-8e903bbb4057,Accept
7b48afa1-6db3-4762-8faf-e73b5158f6b0,Accept
32eeffdc-91ab-4518-8dc6-4d805c5e4aed,Accept
1b97cefc-48ab-437f-8cd0-ffd4eaa707df,Accept
5471d561-b89b-4317-b3ed-499b5334e424,Accept
a482b28b-6f4c-4f2d-a167-42c071a92470,Accept
2d7d0293-291c-46ec-91fb-75351be347f8,Accept
3006006d-1060-4b53-afd3-b702a8fc2358,Accept
f4a42e3e-75a8-43dc-92fe-e38fe23b1d82,Accept
1 client_id decision
2 cdd60b2d-34eb-4db4-9d5e-8e903bbb4057 Accept
3 7b48afa1-6db3-4762-8faf-e73b5158f6b0 Accept
4 32eeffdc-91ab-4518-8dc6-4d805c5e4aed Accept
5 1b97cefc-48ab-437f-8cd0-ffd4eaa707df Accept
6 5471d561-b89b-4317-b3ed-499b5334e424 Accept
7 a482b28b-6f4c-4f2d-a167-42c071a92470 Accept
8 2d7d0293-291c-46ec-91fb-75351be347f8 Accept
9 3006006d-1060-4b53-afd3-b702a8fc2358 Accept
10 f4a42e3e-75a8-43dc-92fe-e38fe23b1d82 Accept

View File

@ -5,7 +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 from utils.storage.game_files_manager import store_game_round_data, store_decision
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -34,21 +34,29 @@ class Player:
start_response = self.client.start_game(payload) start_response = self.client.start_game(payload)
log.info('game started, session id: %s', start_response.session_id) log.info('game started, session id: %s', start_response.session_id)
status = ''
decision = self.make_decision(start_response.client_data) decision = self.make_decision(start_response.client_data)
decision_counter = 0 decision_counter = 0
client_id = start_response.client_id
while status not in ['gameover', 'complete']: is_game_running = True
while is_game_running:
payload = GameDecisionRequestDTO( payload = GameDecisionRequestDTO(
decision=decision, decision=decision,
session_id=start_response.session_id, session_id=start_response.session_id,
client_id=start_response.client_id, client_id=client_id,
) )
log.info('client id: %s', client_id)
decision_response = self.client.send_decision(payload) decision_response = self.client.send_decision(payload)
log.info(f'decision: {decision}, response status: {decision_response.status}, score: {decision_response.score}') log.info(f'decision: {decision}, response status: {decision_response.status}, score: {decision_response.score}')
status = decision_response.status status = decision_response.status
is_game_running = status not in ['gameover', 'complete']
if is_game_running:
store_decision(str(client_id), decision)
client_id = decision_response.client_id
decision = self.make_decision(decision_response.client_data) decision = self.make_decision(decision_response.client_data)
# Handle first response from game initialization logic # Handle first response from game initialization logic
@ -60,22 +68,12 @@ class Player:
store_game_round_data(decision, decision_response, decision_counter, str(start_response.session_id), status) store_game_round_data(decision, decision_response, decision_counter, str(start_response.session_id), status)
decision_counter += 1 decision_counter += 1
time.sleep(1.5) time.sleep(1)
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!
return 'Accept' return 'Accept' # Replace me!!
# import random
# return random.choice(["Accept", "Reject"])
if __name__ == '__main__':
player = Player()
player.start()

View File

@ -4,6 +4,8 @@ import logging
import config import config
import json import json
from typing import Dict, Any from typing import Dict, Any
import csv
from pathlib import Path
from dto.responses import GameStartResponseDTO, GameDecisionResponseDTO from dto.responses import GameStartResponseDTO, GameDecisionResponseDTO
@ -74,3 +76,16 @@ def store_game_round_data(decision: str, response: GameStartResponseDTO | GameDe
logging.info(f"[+] Successfully saved API response JSON to: {json_file_path}") logging.info(f"[+] Successfully saved API response JSON to: {json_file_path}")
except Exception as e: except Exception as e:
logging.error(f"[!] Failed to save API response JSON: {e}") logging.error(f"[!] Failed to save API response JSON: {e}")
def store_decision(client_id: str, decision: str):
path = Path('./resources/decision_log2.csv') # TODO clean me!!
path.parent.mkdir(parents=True, exist_ok=True) # create dirs if needed
exists = path.exists()
with open(path, 'a', newline='') as f:
writer = csv.writer(f)
if not exists:
writer.writerow(['client_id', 'decision']) # header
writer.writerow([client_id, decision])