🐛 Fixed #82

This commit is contained in:
Mawoka
2022-08-26 18:46:03 +02:00
parent 6d3a27f566
commit f1c463c2d7
6 changed files with 128 additions and 82 deletions
+13 -7
View File
@@ -6,7 +6,7 @@
<script lang="ts"> <script lang="ts">
import type { QuizData } from '$lib/quiz_types'; import type { QuizData } from '$lib/quiz_types';
import { getLocalization } from '$lib/i18n'; import { getLocalization } from '$lib/i18n';
import { get_question_title, getWinnersSorted } from '$lib/admin.ts'; import { get_question_title } from '$lib/admin.ts';
import type { PlayerAnswer } from '$lib/admin.ts'; import type { PlayerAnswer } from '$lib/admin.ts';
import { socket } from './socket'; import { socket } from './socket';
@@ -48,12 +48,14 @@
socket.on('final_results', (data) => { socket.on('final_results', (data) => {
// data = JSON.parse(data); // data = JSON.parse(data);
final_results = data; final_results = data;
console.log(getWinnersSorted(quiz_data, final_results));
}); });
socket.on('question_results', (data) => { socket.on('question_results', (data) => {
question_results = JSON.parse(data); try {
question_results = JSON.parse(data);
} catch {
question_results = undefined;
}
}); });
const timer = (time: string) => { const timer = (time: string) => {
@@ -71,9 +73,7 @@
}; };
</script> </script>
{#if timer_res === undefined} {#if timer_res !== undefined && !final_results_clicked}
<span>Select a question to start!</span>
{:else if !final_results_clicked}
<div class="w-full flex justify-center"> <div class="w-full flex justify-center">
<span>{$t('admin_page.time_left')}: {timer_res}</span> <span>{$t('admin_page.time_left')}: {timer_res}</span>
</div> </div>
@@ -89,6 +89,12 @@
>{$t('admin_page.get_results')}</button >{$t('admin_page.get_results')}</button
> >
</div> </div>
{:else if question_results === undefined}
{#if !final_results_clicked}
<div class="w-full flex justify-center">
<h1 class="text-3xl">{$t('admin_page.no_answers')}</h1>
</div>
{/if}
{:else} {:else}
<div class="w-full flex justify-center"> <div class="w-full flex justify-center">
<div class="relative overflow-x-auto shadow-md rounded-lg"> <div class="relative overflow-x-auto shadow-md rounded-lg">
+21 -16
View File
@@ -23,21 +23,6 @@ export const getWinnersSorted = (
) => { ) => {
const winners = {}; const winners = {};
const q_count = quiz_data.questions.length; const q_count = quiz_data.questions.length;
for (let i = 0; i < q_count; i++) {
const q_res = final_results[i];
if (q_res === null) {
continue;
}
for (let j = 0; j < q_res.length; j++) {
const res = q_res[j];
if (res['right']) {
if (winners[res['username']] === undefined) {
winners[res['username']] = 0;
}
winners[res['username']] += 1;
}
}
}
function sortObjectbyValue(obj) { function sortObjectbyValue(obj) {
const asc = false; const asc = false;
@@ -48,7 +33,27 @@ export const getWinnersSorted = (
return ret; return ret;
} }
return sortObjectbyValue(winners); try {
for (let i = 0; i < q_count; i++) {
const q_res = final_results[i];
if (q_res === null) {
continue;
}
for (let j = 0; j < q_res.length; j++) {
const res = q_res[j];
if (res['right']) {
if (winners[res['username']] === undefined) {
winners[res['username']] = 0;
}
winners[res['username']] += 1;
}
}
}
return sortObjectbyValue(winners);
} catch {
return undefined;
}
}; };
export interface Player { export interface Player {
+2 -1
View File
@@ -132,7 +132,8 @@
"get_final_results": "Get final results", "get_final_results": "Get final results",
"export_results": "Export results", "export_results": "Export results",
"show_next_question": "Show next question", "show_next_question": "Show next question",
"start_by_showing_first_question": "Start by showing the first question!" "start_by_showing_first_question": "Start by showing the first question!",
"no_answers": "No answers!"
}, },
"password_reset_page": { "password_reset_page": {
"reset_password": "Reset password" "reset_password": "Reset password"
+68 -47
View File
@@ -18,24 +18,17 @@
right: string; right: string;
} }
let data_available = false;
const getWinnersSorted = () => { const getWinnersSorted = () => {
let winners = {}; let winners = {};
let q_count = quiz_data.questions.length; let q_count = quiz_data.questions.length;
for (let i = 0; i < q_count; i++) { console.log(
let q_res = final_results[i]; JSON.stringify(final_results),
if (q_res === null) { JSON.stringify(final_results) === '{}',
continue; 'HI!!!!!',
} JSON.stringify(final_results) === JSON.stringify({})
for (let j = 0; j < q_res.length; j++) { );
let res = q_res[j];
if (res['right']) {
if (winners[res['username']] === undefined) {
winners[res['username']] = 0;
}
winners[res['username']] += 1;
}
}
}
function sortObjectbyValue(obj) { function sortObjectbyValue(obj) {
const asc = false; const asc = false;
@@ -46,43 +39,71 @@
return ret; return ret;
} }
return sortObjectbyValue(winners); try {
for (let i = 0; i < q_count; i++) {
let q_res = final_results[i];
if (q_res === null) {
continue;
}
for (let j = 0; j < q_res.length; j++) {
let res = q_res[j];
if (res['right']) {
if (winners[res['username']] === undefined) {
winners[res['username']] = 0;
}
winners[res['username']] += 1;
}
}
}
winners_arr = Object.keys(winners);
data_available = true;
return sortObjectbyValue(winners);
} catch {
data_available = false;
}
}; };
let winners = getWinnersSorted(); let winners = getWinnersSorted();
let winners_arr = Object.keys(winners); let winners_arr;
console.log(winners);
</script> </script>
<div> <div>
<h1 class="mx-auto text-center text-6xl mt-8">{$t('play_page.end_sentence')}</h1> <h1 class="mx-auto text-center text-6xl mt-8">{$t('play_page.end_sentence')}</h1>
{#if data_available}
<div class="flex mx-auto w-fit flex-col pt-8 gap-2"> <div class="flex mx-auto w-fit flex-col pt-8 gap-2">
<div> <div>
<p class="text-3xl text-center"> <p class="text-3xl text-center">
{$t('play_page.1st_place')}: <span class="underline">{winners_arr[0]}</span> {$t('play_page.1st_place')}: <span class="underline">{winners_arr[0]}</span>
<span>with {winners[winners_arr[0]]} out of {quiz_data.questions.length}</span> <span>with {winners[winners_arr[0]]} out of {quiz_data.questions.length}</span>
</p> </p>
</div>
{#if winners_arr.length >= 2}
<div>
<p class="text-2xl text-center">
{$t('play_page.2nd_place')}: <span class="underline">{winners_arr[1]}</span>
<span
>with {winners[winners_arr[1]]} out of {quiz_data.questions
.length}</span
>
</p>
</div>
{/if}
{#if winners_arr.length >= 3}
<div>
<p class="text-xl text-center">
{$t('play_page.3rd place')}: <span class="underline">{winners_arr[2]}</span>
<span>
{$t('play_page.with_out_of', {
correct_questions: winners[winners_arr[2]],
total_question_count: quiz_data.questions.length
})}
</span>
</p>
</div>
{/if}
</div> </div>
{#if winners_arr.length >= 2} {:else}
<div> <div class="flex justify-center">
<p class="text-2xl text-center"> <h1 class="text-3xl">{$t('admin_page.no_answers')}</h1>
{$t('play_page.2nd_place')}: <span class="underline">{winners_arr[1]}</span> </div>
<span>with {winners[winners_arr[1]]} out of {quiz_data.questions.length}</span> {/if}
</p>
</div>
{/if}
{#if winners_arr.length >= 3}
<div>
<p class="text-xl text-center">
{$t('play_page.3rd place')}: <span class="underline">{winners_arr[2]}</span>
<span>
{$t('play_page.with_out_of', {
correct_questions: winners[winners_arr[2]],
total_question_count: quiz_data.questions.length
})}
</span>
</p>
</div>
{/if}
</div>
</div> </div>
+3 -3
View File
@@ -29,7 +29,7 @@
let game_started = false; let game_started = false;
let quiz_data: QuizData; let quiz_data: QuizData;
//let question_number = '0'; //let question_number = '0';
let question_results = null; // let question_results = null;
let final_results: Array<null> | Array<Array<PlayerAnswer>> = [null]; let final_results: Array<null> | Array<Array<PlayerAnswer>> = [null];
let success = false; let success = false;
let dataexport_download_a; let dataexport_download_a;
@@ -58,7 +58,7 @@
errorMessage = $t('admin_page.already_registered_as_admin'); errorMessage = $t('admin_page.already_registered_as_admin');
}); });
socket.on('question_results', (int_data) => { /* socket.on('question_results', (int_data) => {
try { try {
int_data = JSON.parse(int_data); int_data = JSON.parse(int_data);
} catch (e) { } catch (e) {
@@ -66,7 +66,7 @@
return; return;
} }
question_results = int_data; question_results = int_data;
}); });*/
socket.on('export_token', (int_data) => { socket.on('export_token', (int_data) => {
warnToLeave = false; warnToLeave = false;
dataexport_download_a.href = `/api/v1/quiz/export_data/${int_data}?ts=${new Date().getTime()}&game_pin=${game_pin}`; dataexport_download_a.href = `/api/v1/quiz/export_data/${int_data}?ts=${new Date().getTime()}&game_pin=${game_pin}`;
+21 -8
View File
@@ -9,6 +9,9 @@
import { navbarVisible } from '$lib/stores'; import { navbarVisible } from '$lib/stores';
import ShowEndScreen from '$lib/play/end.svelte'; import ShowEndScreen from '$lib/play/end.svelte';
import { QuizQuestionType } from '$lib/quiz_types'; import { QuizQuestionType } from '$lib/quiz_types';
import { getLocalization } from '$lib/i18n';
const { t } = getLocalization();
// Exports // Exports
export let data; export let data;
@@ -83,7 +86,11 @@
socket.on('question_results', (data) => { socket.on('question_results', (data) => {
restart(); restart();
answer_results = JSON.parse(data); try {
answer_results = JSON.parse(data);
} catch {
answer_results = null;
}
}); });
socket.on('final_results', (data) => { socket.on('final_results', (data) => {
@@ -124,12 +131,18 @@
/> />
{/key} {/key}
{:else if gameMeta.started && answer_results !== undefined} {:else if gameMeta.started && answer_results !== undefined}
{#key unique} {#if answer_results === null}
<ShowResults <div class="w-full flex justify-center">
bind:results={answer_results} <h1 class="text-3xl">{$t('admin_page.no_answers')}</h1>
bind:game_data={gameData} </div>
bind:question_index {:else}
/> {#key unique}
{/key} <ShowResults
bind:results={answer_results}
bind:game_data={gameData}
bind:question_index
/>
{/key}
{/if}
{/if} {/if}
</div> </div>