diff --git a/frontend/src/lib/admin.svelte b/frontend/src/lib/admin.svelte index 7ee5f1c..76da064 100644 --- a/frontend/src/lib/admin.svelte +++ b/frontend/src/lib/admin.svelte @@ -14,6 +14,8 @@ SPDX-License-Identifier: MPL-2.0 import Spinner from '$lib/Spinner.svelte'; import Controls from '$lib/play/admin/controls.svelte'; import Question from '$lib/play/admin/question.svelte'; + import { SocketGameControls } from '$lib/play/admin/socket_game_controls.ts'; + import { GameState } from '$lib/play/admin/game_state.ts'; const { t } = getLocalization(); const default_colors = ['#D6EDC9', '#B07156', '#7F7057', '#4E6E58']; @@ -33,6 +35,7 @@ SPDX-License-Identifier: MPL-2.0 final_results?: Array | Array>; control_visible: boolean; player_scores: any; + game_state: GameState; } let { @@ -41,7 +44,8 @@ SPDX-License-Identifier: MPL-2.0 bg_color, final_results = $bindable([null]), control_visible, - player_scores = $bindable() + player_scores = $bindable(), + game_state = $bindable() }: Props = $props(); socket.on('get_question_results', () => { @@ -56,11 +60,20 @@ SPDX-License-Identifier: MPL-2.0 selected_question = selected_question + 1; answer_count = 0; timer(timer_res); + + game_state.timer_res = '0'; + game_state.question_results = null; + game_state.shown_question_now = data.question_index; + game_state.timer_res = quiz_data.questions[data.question_index].time; + game_state.selected_question = game_state.selected_question + 1; + game_state.answer_count = 0; }); socket.on('solutions', (_) => { timer_res = '0'; clearInterval(timer_interval); + + game_state.timer_res = '0'; }); socket.on('final_results', (data) => { @@ -68,20 +81,28 @@ SPDX-License-Identifier: MPL-2.0 final_results_clicked = true; timer_res = '0'; final_results = data; + + game_state.final_results = data; + game_state.timer_res = '0'; }); socket.on('everyone_answered', (_) => { timer_res = '0'; + game_state.timer_res = '0'; }); socket.on('question_results', (data) => { console.log('question_results:', data); question_results = data; timer_res = '0'; + + game_state.question_results = data; + game_state.timer_res = '0'; }); socket.on('player_answer', (_) => { answer_count += 1; + game_state.answer_count += 1; }); const timer = (time: string) => { @@ -95,8 +116,11 @@ SPDX-License-Identifier: MPL-2.0 } timer_res = seconds.toString(); + game_state.timer_res = seconds.toString(); }, 1000); }; + + const socket_game_controls: SocketGameControls = new SocketGameControls(socket); {#if control_visible} @@ -106,10 +130,11 @@ SPDX-License-Identifier: MPL-2.0 {quiz_data} bind:timer_res {final_results} - {socket} + {socket_game_controls} {question_results} {game_token} {shown_question_now} + bind:game_state /> {/if} {#if timer_res !== '0' && selected_question >= 0} diff --git a/frontend/src/lib/play/admin/controls.svelte b/frontend/src/lib/play/admin/controls.svelte index dcf44c7..1346870 100644 --- a/frontend/src/lib/play/admin/controls.svelte +++ b/frontend/src/lib/play/admin/controls.svelte @@ -9,6 +9,8 @@ SPDX-License-Identifier: MPL-2.0 import type { QuizData } from '$lib/quiz_types'; import type { Socket } from 'socket.io-client'; import { getLocalization } from '$lib/i18n'; + import { SocketGameControls } from '$lib/play/admin/socket_game_controls.ts'; + import { GameState } from '$lib/play/admin/game_state.ts'; interface Props { bg_color: string; @@ -16,10 +18,11 @@ SPDX-License-Identifier: MPL-2.0 quiz_data: QuizData; timer_res: string; final_results: any; - socket: Socket; + socket_game_controls: SocketGameControls; game_token: string; question_results: any; shown_question_now: number; + game_state: GameState; } let { @@ -28,30 +31,19 @@ SPDX-License-Identifier: MPL-2.0 quiz_data, timer_res = $bindable(), final_results, - socket, + socket_game_controls, game_token, question_results, - shown_question_now + shown_question_now, + game_state = $bindable() }: Props = $props(); const { t } = getLocalization(); - const set_question_number = (q_number: number) => { - socket.emit('set_question_number', q_number.toString()); - }; - const get_question_results = () => { - socket.emit('get_question_results', { - game_id: game_token, - question_number: shown_question_now - }); - }; const show_solutions = () => { - socket.emit('show_solutions', {}); + socket_game_controls.show_solutions(); timer_res = '0'; - }; - - const get_final_results = () => { - socket.emit('get_final_results', {}); + game_state.timer_res = '0'; }; @@ -67,7 +59,7 @@ SPDX-License-Identifier: MPL-2.0
{#if selected_question + 1 === quiz_data.questions.length && ((timer_res === '0' && question_results !== null) || quiz_data?.questions?.[selected_question]?.type === QuizQuestionType.SLIDE)} {#if JSON.stringify(final_results) === JSON.stringify([null])} - {/if} @@ -75,7 +67,7 @@ SPDX-License-Identifier: MPL-2.0 {#if (selected_question + 1 !== quiz_data.questions.length && question_results !== null) || selected_question === -1} {:else} - {/if} @@ -111,7 +103,7 @@ SPDX-License-Identifier: MPL-2.0 {#if quiz_data.questions[selected_question].type === QuizQuestionType.SLIDE}