Player async function and integration with app.py
This commit is contained in:
@ -1,4 +1,6 @@
|
||||
import logging
|
||||
import threading
|
||||
import time
|
||||
from typing import Literal, Dict, Any
|
||||
import config
|
||||
from dto.requests import GameStartRequestDTO, GameDecisionRequestDTO
|
||||
@ -9,18 +11,29 @@ class Player:
|
||||
|
||||
def __init__(self):
|
||||
self.client = JuliusBaerApiClient()
|
||||
self._thread = None
|
||||
|
||||
def start(self):
|
||||
self.play()
|
||||
|
||||
def play_on_separate_thread(self):
|
||||
if self._thread and self._thread.is_alive():
|
||||
logging.warning('Game loop already running.')
|
||||
return self._thread
|
||||
|
||||
self._thread = threading.Thread(target=self.play, daemon=True)
|
||||
self._thread.start()
|
||||
return self._thread
|
||||
|
||||
def play(self):
|
||||
print('playing')
|
||||
payload = GameStartRequestDTO(player_name=config.API_TEAM)
|
||||
start_response = self.client.start_game(payload)
|
||||
logging.info(start_response)
|
||||
|
||||
status = ''
|
||||
decision = self.make_decision(start_response.client_data)
|
||||
while status != 'gameover':
|
||||
while status not in ['gameover', 'complete']:
|
||||
|
||||
payload = GameDecisionRequestDTO(
|
||||
decision=decision,
|
||||
@ -29,10 +42,10 @@ class Player:
|
||||
)
|
||||
|
||||
decision_response = self.client.send_decision(payload)
|
||||
logging.info(decision_response)
|
||||
print(decision_response.status, decision_response.score)
|
||||
status = decision_response.status
|
||||
decision = self.make_decision(decision_response.client_data)
|
||||
|
||||
time.sleep(1.5)
|
||||
|
||||
def make_decision(self, client_data: Dict[str, Any]) -> Literal["Accept", "Reject"]:
|
||||
# Do your magic!
|
||||
|
Reference in New Issue
Block a user