Skip to content

Commit

Permalink
Save player names in game results
Browse files Browse the repository at this point in the history
  • Loading branch information
alrusdi committed May 3, 2023
1 parent 4d0a87c commit e59d5fd
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
6 changes: 3 additions & 3 deletions client/common.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const POSSIBLE_PLAYERS = [
{'id': '09eabde7-3a21-43f6-b197-c75452e7c214', 'name': 'Саша', 'points': 0, 'current_break': 0, 'max_break': 0},
{'id': '1d5dd8e0-b411-43ac-a872-25747fce3c12', 'name': 'Сергей', 'points': 0, 'current_break': 0, 'max_break': 0},
{'id': '578137b1-b118-4a9d-85b0-b5a108b5e333', 'name': 'Гузель', 'points': 0, 'current_break': 0, 'max_break': 0},
{"id": "09eabde7-3a21-43f6-b197-c75452e7c214", "name": "Саша", "points": 0, "current_break": 0, "max_break": 0},
{"id": "1d5dd8e0-b411-43ac-a872-25747fce3c12", "name": "Сергей", "points": 0, "current_break": 0, "max_break": 0},
{"id": "578137b1-b118-4a9d-85b0-b5a108b5e333", "name": "Гузель", "points": 0, "current_break": 0, "max_break": 0}
]
5 changes: 2 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def new_game_page(request: Request):
@app.get("/game-results/{game_id}/", response_class=HTMLResponse)
async def game_results_page(request: Request, game_id:str):
game_results = await game_results_manager.get_game_results(game_id=game_id)
if not game_results and game_manager.is_game_exists(game_id=game_id):
is_game_exists = await game_manager.is_game_exists(game_id=game_id)
if not game_results and is_game_exists:
game = await game_manager.get_game(game_id=game_id)
if game:
await game_manager.finalize_game(game, game_results_manager)
Expand Down Expand Up @@ -79,9 +80,7 @@ async def websocket_endpoint(websocket: WebSocket, client_id: int):

def parse_ws_request(data: str) -> GameEventRequest:
try:
print(data)
req = GameEventRequest.from_json(data)
print(req)
return req
except:
import traceback
Expand Down
8 changes: 8 additions & 0 deletions server/game_results.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import datetime
import json
import os
from dataclasses import dataclass

from dataclasses_json import DataClassJsonMixin

from server.models.player import Player
from server.settings import BASE_DIR

SAVE_GAME_RESULTS_DIR = "server/data/results"

Expand Down Expand Up @@ -42,6 +44,12 @@ async def get_game_results(self, game_id: str) -> GameResults | None:

async def save_game_results(self, game_results: GameResults) -> None:
self.results[game_results.game_id] = game_results
with open(f"{BASE_DIR}/client/common.js", encoding="utf8") as f:
raw_data = f.read().replace("const POSSIBLE_PLAYERS = ", "")
player_names = {p["id"]: p["name"] for p in json.loads(raw_data)}
for pstat in game_results.player_stats:
if not pstat.name and pstat.id and pstat.id in player_names:
pstat.name = player_names[pstat.id]
data_raw = game_results.to_json(ensure_ascii=False, indent=4)
with open(f"{SAVE_GAME_RESULTS_DIR}/result_{game_results.game_id}.json", "w", encoding="utf8") as f:
f.write(data_raw)
3 changes: 3 additions & 0 deletions server/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import datetime
from pathlib import Path

timezone_offset = 5
tzinfo = datetime.timezone(datetime.timedelta(hours=timezone_offset))
DEFAULT_TIMEZONE = tzinfo # YEKT

BASE_DIR = str(Path(__file__).parent.parent)

0 comments on commit e59d5fd

Please sign in to comment.