From 4ed0b8d626baef7b23b027dc71442b3e23837a6b Mon Sep 17 00:00:00 2001 From: Proxyfil Date: Wed, 22 Apr 2026 20:59:24 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20Created=20independant=20game=5Fc?= =?UTF-8?q?ontrol=20and=20game=5Fstate=20classes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/lib/play/admin/game_state.ts | 21 ++++++++ .../lib/play/admin/socket_game_controls.ts | 50 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 frontend/src/lib/play/admin/game_state.ts create mode 100644 frontend/src/lib/play/admin/socket_game_controls.ts diff --git a/frontend/src/lib/play/admin/game_state.ts b/frontend/src/lib/play/admin/game_state.ts new file mode 100644 index 0000000..ba03b7e --- /dev/null +++ b/frontend/src/lib/play/admin/game_state.ts @@ -0,0 +1,21 @@ +export class GameState { + public game_id: string; + public players: any[]; + public selected_question: number; + public timer_res: string; + public question_results: any; + public answer_count: number; + public shown_question_now: number; + public final_results: any; + + constructor(game_id: string, players: any[]) { + this.game_id = game_id; + this.players = players; + this.selected_question = -1; + this.timer_res = '0'; + this.question_results = null; + this.answer_count = 0; + this.shown_question_now = -1; + this.final_results = null; + } +} \ No newline at end of file diff --git a/frontend/src/lib/play/admin/socket_game_controls.ts b/frontend/src/lib/play/admin/socket_game_controls.ts new file mode 100644 index 0000000..a50269c --- /dev/null +++ b/frontend/src/lib/play/admin/socket_game_controls.ts @@ -0,0 +1,50 @@ + +import type { Socket } from 'socket.io-client'; + +/** + * Class that encapsulates all the socket calls related to game controls for the admin page. + * Still in development, but it will be used to avoid having socket calls directly in the svelte files, and to have a single place to manage all the game controls related socket calls. + */ +export class SocketGameControls { + socket: Socket; + + constructor(socket: Socket) { + this.socket = socket; + } + + set_question_number(q_number: number) { + this.socket.emit('set_question_number', q_number.toString()); + }; + + get_question_results(game_id: string, question_number: number) { + this.socket.emit('get_question_results', { + game_id, + question_number + }); + }; + + show_solutions() { + this.socket.emit('show_solutions', {}); + }; + + get_final_results() { + this.socket.emit('get_final_results', {}); + }; + + start_game() { + this.socket.emit('start_game', ''); + } + + kick_player(username: string, players: any[]) { + this.socket.emit('kick_player', { username: username }); + + for (let i = 0; i < players.length; i++) { + console.log(players[i].username, username); + if (players[i].username === username) { + players.splice(i, 1); + break; + } + } + return players; + } +} \ No newline at end of file