From 35859db0512b36a05f863b26d6317f510f493ff7 Mon Sep 17 00:00:00 2001 From: starnakin Date: Tue, 9 Jan 2024 12:31:13 +0100 Subject: [PATCH] game: init --- frontend/static/js/api/Game.js | 34 +++++++++++++++++++ .../js/views/{Game.js => GameOfflineView.js} | 0 frontend/static/js/views/GameView.js | 33 ++++++++++++++++++ games/consumers.py | 2 +- 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 frontend/static/js/api/Game.js rename frontend/static/js/views/{Game.js => GameOfflineView.js} (100%) create mode 100644 frontend/static/js/views/GameView.js diff --git a/frontend/static/js/api/Game.js b/frontend/static/js/api/Game.js new file mode 100644 index 0000000..daf6cf7 --- /dev/null +++ b/frontend/static/js/api/Game.js @@ -0,0 +1,34 @@ +class Game +{ + /** + * @param {Client} client + */ + constructor(client, id) + { + /** + * @type {Client} + */ + this.client = client; + this.id = id; + } + + async init() + { + let response = await this.client._get(`/api/games/${this.id}`); + + if (response.status !== 200) + return response.status; + + let response_data = await response.json(); + + this.players_id = response_data.players_id; + this.state = response_data.state; + this.started = response_data.started; + this.finished = response_data.finished; + this.winner_id = this.finished ? response_data.winner_id : undefined; + + return 0; + } +} + +export { Game } \ No newline at end of file diff --git a/frontend/static/js/views/Game.js b/frontend/static/js/views/GameOfflineView.js similarity index 100% rename from frontend/static/js/views/Game.js rename to frontend/static/js/views/GameOfflineView.js diff --git a/frontend/static/js/views/GameView.js b/frontend/static/js/views/GameView.js new file mode 100644 index 0000000..bfd2a78 --- /dev/null +++ b/frontend/static/js/views/GameView.js @@ -0,0 +1,33 @@ +import { client } from "../index.js"; +import { Game } from "../api/Game.js"; +import AbstractView from "./abstracts/AbstractView.js"; + +export default class extends AbstractView +{ + constructor(params) + { + super(params, "Game"); + this.game = new Game(client, params.id); + } + + async postInit() + { + let error_code = await this.game.init(); + + if (error_code) + return error_code; + } + + async getHtml() + { + return ` +

Welcome back, Dom

+

+ Fugiat voluptate et nisi Lorem cillum anim sit do eiusmod occaecat irure do. Reprehenderit anim fugiat sint exercitation consequat. Sit anim laborum sit amet Lorem adipisicing ullamco duis. Anim in do magna ea pariatur et. +

+

+ View recent posts. +

+ `; + } +} diff --git a/games/consumers.py b/games/consumers.py index 814fe93..981d106 100644 --- a/games/consumers.py +++ b/games/consumers.py @@ -28,7 +28,7 @@ class GameWebSocket(AsyncWebsocketConsumer): self.room = game_room_manager.get(self.game_id) if (self.room is None): - self.member.send("Tournament not found") + self.member.send("Game not found.") self.disconnect(1017) self.room.append(self.member)