Skip to content

Commit

Permalink
feat(server): track statistics
Browse files Browse the repository at this point in the history
[TTTK-68]
  • Loading branch information
JM-Lemmi committed Mar 14, 2024
1 parent b5b2fc1 commit c5f6055
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Server/websocket_server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from Server.game import Game
from Server.player import Player
from Server.rulebase import RuleBase
from Server.statistics import Statistics

import asyncio
import websockets
import logging
Expand All @@ -19,6 +21,7 @@ def __init__(self, admin:Player, port: int = 8765) -> None:
self._inprogress = False
self._port = port
self._connections = set()
self._stats = Statistics()

with open("./json_schema/client_to_server.json", "r") as f:
self._json_schema = json.load(f)
Expand Down Expand Up @@ -97,6 +100,7 @@ async def handler(self, websocket):
# make move, catch illegal move
try:
self._game.move(self._game.players.index(self._players[message_json["player_uuid"]]), (message_json["move"]["x"], message_json["move"]["y"]))
self._stats.increment_moves(self._players[message_json["player_uuid"]])
except ValueError as e:
await websocket.send(json.dumps({"message_type": "game/error", "error_message": str(e)}))

Expand All @@ -119,6 +123,7 @@ async def handler(self, websocket):
"sender_uuid": message_json["player_uuid"],
"message": message_json["message"],
}))
self._stats.increment_emojis(self._players[message_json["player_uuid"]], message_json["message"])


case "server/terminate":
Expand Down Expand Up @@ -170,6 +175,8 @@ async def handler(self, websocket):
async def _end_game(self):
self._inprogress = False

self._stats.increment_games(self._players, self._game.state.winner)

await websockets.broadcast(self._connections, json.dumps({
"message_type": "game/end",
"winner_uuid": str(self._game.winner.uuid) if self._game.winner else None,
Expand Down

0 comments on commit c5f6055

Please sign in to comment.