Added Kahoot!-Mode

This commit is contained in:
Mawoka
2022-09-17 22:32:18 +02:00
parent 4f2478c8e5
commit 1651cd7b2c
16 changed files with 161 additions and 28 deletions
+1
View File
@@ -158,6 +158,7 @@ class PlayGame(BaseModel):
started: bool = False started: bool = False
captcha_enabled: bool = False captcha_enabled: bool = False
cover_image: str | None cover_image: str | None
game_mode: str | None
class GamePlayer(BaseModel): class GamePlayer(BaseModel):
+10 -5
View File
@@ -90,7 +90,9 @@ async def get_public_quiz(quiz_id: str):
@router.post("/start/{quiz_id}") @router.post("/start/{quiz_id}")
async def start_quiz(quiz_id: str, captcha_enabled: bool = True, user: User = Depends(get_current_user)): async def start_quiz(
quiz_id: str, game_mode: str, captcha_enabled: bool = True, user: User = Depends(get_current_user)
):
try: try:
quiz_id = uuid.UUID(quiz_id) quiz_id = uuid.UUID(quiz_id)
except ValueError: except ValueError:
@@ -110,6 +112,7 @@ async def start_quiz(quiz_id: str, captcha_enabled: bool = True, user: User = De
description=quiz.description, description=quiz.description,
captcha_enabled=captcha_enabled, captcha_enabled=captcha_enabled,
cover_image=quiz.cover_image, cover_image=quiz.cover_image,
game_mode=game_mode,
) )
await redis.set(f"game:{str(game.game_pin)}", (game.json()), ex=18000) await redis.set(f"game:{str(game.game_pin)}", (game.json()), ex=18000)
return {**quiz.dict(exclude={"id"}), **game.dict(exclude={"questions"})} return {**quiz.dict(exclude={"id"}), **game.dict(exclude={"questions"})}
@@ -117,6 +120,7 @@ async def start_quiz(quiz_id: str, captcha_enabled: bool = True, user: User = De
class CheckIfCaptchaEnabledResponse(BaseModel): class CheckIfCaptchaEnabledResponse(BaseModel):
enabled: bool enabled: bool
game_mode: str | None
@router.get("/play/check_captcha/{game_pin}", response_model=CheckIfCaptchaEnabledResponse) @router.get("/play/check_captcha/{game_pin}", response_model=CheckIfCaptchaEnabledResponse)
@@ -124,10 +128,11 @@ async def check_if_captcha_enabled(game_pin: str):
game = await redis.get(f"game:{game_pin}") game = await redis.get(f"game:{game_pin}")
if game is None: if game is None:
return JSONResponse(status_code=404, content={"detail": "game not found"}) return JSONResponse(status_code=404, content={"detail": "game not found"})
try: game = PlayGame.parse_raw(game)
return CheckIfCaptchaEnabledResponse(**{"enabled": json.loads(game)["captcha_enabled"]}) if game.captcha_enabled:
except (KeyError, TypeError): return CheckIfCaptchaEnabledResponse(enabled=True, game_mode=game.game_mode)
return CheckIfCaptchaEnabledResponse(**{"enabled": True}) else:
return CheckIfCaptchaEnabledResponse(enabled=False, game_mode=game.game_mode)
@router.get("/join/{game_pin}", deprecated=True) @router.get("/join/{game_pin}", deprecated=True)
+19 -1
View File
@@ -9,9 +9,12 @@
import { get_question_title } 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';
import { QuizQuestionType } from '$lib/quiz_types';
import { kahoot_icons } from './play/kahoot_mode_assets/kahoot_icons';
export let game_token: string; export let game_token: string;
export let quiz_data: QuizData; export let quiz_data: QuizData;
export let game_mode;
const { t } = getLocalization(); const { t } = getLocalization();
@@ -84,11 +87,26 @@
<div> <div>
<img <img
src={quiz_data.questions[selected_question].image} src={quiz_data.questions[selected_question].image}
class="h-2/5 object-cover mx-auto mb-8" class="max-h-[20vh] object-cover mx-auto mb-8 w-auto"
alt="Content for Question" alt="Content for Question"
/> />
</div> </div>
{/if} {/if}
{#if game_mode === 'kahoot'}
{#if quiz_data.questions[selected_question].type === QuizQuestionType.ABCD}
<div class="grid grid-cols-2 gap-2 w-full p-4">
{#each quiz_data.questions[selected_question].answers as answer, i}
<div
class="rounded-lg h-fit flex align-middle"
style="background-color: {answer.color ?? '#B45309'}"
>
<img class="w-10 inline-block" alt="icon" src={kahoot_icons[i]} />
<span class="text-center text-2xl px-2 py-4">{answer.answer}</span>
</div>
{/each}
</div>
{/if}
{/if}
{/if} {/if}
<br /> <br />
{#if timer_res === '0'} {#if timer_res === '0'}
@@ -11,8 +11,9 @@
import { Pagination, EffectCoverflow, Keyboard, Mousewheel, Navigation } from 'swiper'; import { Pagination, EffectCoverflow, Keyboard, Mousewheel, Navigation } from 'swiper';
import { QuizQuestionType } from '$lib/quiz_types.js'; import { QuizQuestionType } from '$lib/quiz_types.js';
import { getLocalization } from '../i18n'; import { getLocalization } from '../i18n';
import { start_game } from './start_game'; import StartGamePopup from './start_game.svelte';
let start_game = null;
const { t } = getLocalization(); const { t } = getLocalization();
export let quizzes; export let quizzes;
@@ -149,7 +150,7 @@
> >
<button <button
on:click={() => { on:click={() => {
start_game(quiz.id); start_game = quiz.id;
}} }}
class="px-4 py-2 leading-5 text-black dark:text-white transition-colors duration-200 transform bg-gray-50 dark:bg-gray-700 rounded text-center hover:bg-gray-300 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50 dark:hover:bg-gray-600" class="px-4 py-2 leading-5 text-black dark:text-white transition-colors duration-200 transform bg-gray-50 dark:bg-gray-700 rounded text-center hover:bg-gray-300 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50 dark:hover:bg-gray-600"
> >
@@ -248,3 +249,7 @@
{/each} {/each}
</Swiper> </Swiper>
</div> </div>
{#if start_game !== null}
<StartGamePopup bind:quiz_id={start_game} />
{/if}
File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 32 KiB

@@ -9,7 +9,6 @@
import { reach } from 'yup'; import { reach } from 'yup';
import { ABCDQuestionSchema } from '$lib/yupSchemas'; import { ABCDQuestionSchema } from '$lib/yupSchemas';
import { getLocalization } from '$lib/i18n'; import { getLocalization } from '$lib/i18n';
import { invertColor } from '$lib/helpers';
const { t } = getLocalization(); const { t } = getLocalization();
+4 -1
View File
@@ -13,6 +13,7 @@
const { t } = getLocalization(); const { t } = getLocalization();
export let game_pin: string; export let game_pin: string;
export let game_mode;
let username = ''; let username = '';
let hcaptchaSitekey = import.meta.env.VITE_HCAPTCHA; let hcaptchaSitekey = import.meta.env.VITE_HCAPTCHA;
@@ -54,8 +55,10 @@
let captcha_resp: string; let captcha_resp: string;
const res = await fetch(`/api/v1/quiz/play/check_captcha/${game_pin}`); const res = await fetch(`/api/v1/quiz/play/check_captcha/${game_pin}`);
let captcha_enabled; let captcha_enabled;
const json = await res.json();
game_mode = json.game_mode;
if (res.status === 200) { if (res.status === 200) {
captcha_enabled = (await res.json()).enabled; captcha_enabled = json.enabled;
} }
if (res.status === 404) { if (res.status === 404) {
alertModal.set({ alertModal.set({
@@ -0,0 +1,12 @@
<!--
- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at https://mozilla.org/MPL/2.0/.
-->
<svg fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path d="M12 14l9-5-9-5-9 5 9 5z"></path>
<path d="M12 14l6.16-3.422a12.083 12.083 0 01.665 6.479A11.952 11.952 0 0012 20.055a11.952 11.952 0 00-6.824-2.998 12.078 12.078 0 01.665-6.479L12 14z"></path>
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M12 14l9-5-9-5-9 5 9 5zm0 0l6.16-3.422a12.083 12.083 0 01.665 6.479A11.952 11.952 0 0012 20.055a11.952 11.952 0 00-6.824-2.998 12.078 12.078 0 01.665-6.479L12 14zm-4 6v-7.5l4-2.222"></path>
</svg>

After

Width:  |  Height:  |  Size: 807 B

@@ -0,0 +1,12 @@
<!--
- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at https://mozilla.org/MPL/2.0/.
-->
<svg fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path d="M12 14l9-5-9-5-9 5 9 5z"></path>
<path d="M12 14l6.16-3.422a12.083 12.083 0 01.665 6.479A11.952 11.952 0 0012 20.055a11.952 11.952 0 00-6.824-2.998 12.078 12.078 0 01.665-6.479L12 14z"></path>
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M12 14l9-5-9-5-9 5 9 5zm0 0l6.16-3.422a12.083 12.083 0 01.665 6.479A11.952 11.952 0 0012 20.055a11.952 11.952 0 00-6.824-2.998 12.078 12.078 0 01.665-6.479L12 14zm-4 6v-7.5l4-2.222"></path>
</svg>

After

Width:  |  Height:  |  Size: 807 B

@@ -0,0 +1,12 @@
<!--
- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at https://mozilla.org/MPL/2.0/.
-->
<svg fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path d="M12 14l9-5-9-5-9 5 9 5z"></path>
<path d="M12 14l6.16-3.422a12.083 12.083 0 01.665 6.479A11.952 11.952 0 0012 20.055a11.952 11.952 0 00-6.824-2.998 12.078 12.078 0 01.665-6.479L12 14z"></path>
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M12 14l9-5-9-5-9 5 9 5zm0 0l6.16-3.422a12.083 12.083 0 01.665 6.479A11.952 11.952 0 0012 20.055a11.952 11.952 0 00-6.824-2.998 12.078 12.078 0 01.665-6.479L12 14zm-4 6v-7.5l4-2.222"></path>
</svg>

After

Width:  |  Height:  |  Size: 807 B

@@ -0,0 +1,12 @@
<!--
- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at https://mozilla.org/MPL/2.0/.
-->
<svg fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path d="M12 14l9-5-9-5-9 5 9 5z"></path>
<path d="M12 14l6.16-3.422a12.083 12.083 0 01.665 6.479A11.952 11.952 0 0012 20.055a11.952 11.952 0 00-6.824-2.998 12.078 12.078 0 01.665-6.479L12 14z"></path>
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M12 14l9-5-9-5-9 5 9 5zm0 0l6.16-3.422a12.083 12.083 0 01.665 6.479A11.952 11.952 0 0012 20.055a11.952 11.952 0 00-6.824-2.998 12.078 12.078 0 01.665-6.479L12 14zm-4 6v-7.5l4-2.222"></path>
</svg>

After

Width:  |  Height:  |  Size: 807 B

@@ -0,0 +1,12 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import KahootModeIcon0 from '$lib/play/kahoot_mode_assets/0.svg';
import KahootModeIcon1 from '$lib/play/kahoot_mode_assets/1.svg';
import KahootModeIcon2 from '$lib/play/kahoot_mode_assets/2.svg';
import KahootModeIcon3 from '$lib/play/kahoot_mode_assets/3.svg';
export const kahoot_icons = [KahootModeIcon0, KahootModeIcon1, KahootModeIcon2, KahootModeIcon3];
+29 -11
View File
@@ -9,11 +9,12 @@
import { socket } from '$lib/socket'; import { socket } from '$lib/socket';
import Spinner from '../Spinner.svelte'; import Spinner from '../Spinner.svelte';
import { getLocalization } from '$lib/i18n'; import { getLocalization } from '$lib/i18n';
import { invertColor } from '$lib/helpers'; import { kahoot_icons } from './kahoot_mode_assets/kahoot_icons';
const { t } = getLocalization(); const { t } = getLocalization();
export let question: Question; export let question: Question;
export let game_mode;
export let question_index: string | number; export let question_index: string | number;
if (typeof question_index === 'string') { if (typeof question_index === 'string') {
@@ -84,16 +85,33 @@
{/if} {/if}
{#if timer_res !== '0'} {#if timer_res !== '0'}
{#if question.type === QuizQuestionType.ABCD} {#if question.type === QuizQuestionType.ABCD}
<div class="flex flex-wrap"> {#if game_mode === 'normal'}
{#each question.answers as answer} <div class="flex flex-wrap">
<button {#each question.answers as answer}
class="w-1/2 text-3xl my-2 disabled:opacity-60 border border-white" <button
style="background-color: {answer.color ?? '#B45309'}" class="w-1/2 text-3xl my-2 disabled:opacity-60 border border-white"
disabled={selected_answer !== undefined} style="background-color: {answer.color ?? '#B45309'}"
on:click={() => selectAnswer(answer.answer)}>{answer.answer}</button disabled={selected_answer !== undefined}
> on:click={() => selectAnswer(answer.answer)}>{answer.answer}</button
{/each} >
</div> {/each}
</div>
{:else if game_mode === 'kahoot'}
<div class="grid grid-cols-2 gap-2 w-full p-4">
{#each question.answers as answer, i}
<button
class="rounded-lg h-fit flex align-middle justify-center disabled:opacity-60"
style="background-color: {answer.color ?? '#B45309'}"
disabled={selected_answer !== undefined}
on:click={() => selectAnswer(answer.answer)}
>
<img class="w-10 inline-block" alt="Icon" src={kahoot_icons[i]} />
</button>
{/each}
</div>
{:else}
<p>Error</p>
{/if}
{:else if question.type === QuizQuestionType.RANGE} {:else if question.type === QuizQuestionType.RANGE}
{#await import('svelte-range-slider-pips')} {#await import('svelte-range-slider-pips')}
<Spinner /> <Spinner />
+19 -5
View File
@@ -12,6 +12,7 @@
import type { PlayerAnswer, Player } from '$lib/admin.ts'; import type { PlayerAnswer, Player } from '$lib/admin.ts';
import SomeAdminScreen from '$lib/admin.svelte'; import SomeAdminScreen from '$lib/admin.svelte';
import { browser } from '$app/environment'; import { browser } from '$app/environment';
import { onMount } from 'svelte';
navbarVisible.set(false); navbarVisible.set(false);
@@ -22,6 +23,7 @@
// game_pin: '66190765' // game_pin: '66190765'
// }; // };
export let data; export let data;
let game_mode;
let { game_pin, auto_connect, game_token } = data; let { game_pin, auto_connect, game_token } = data;
let players: Array<Player> = []; let players: Array<Player> = [];
@@ -35,15 +37,21 @@
let dataexport_download_a; let dataexport_download_a;
let warnToLeave = true; let warnToLeave = true;
const connect = () => { const connect = async () => {
socket.emit('register_as_admin', { socket.emit('register_as_admin', {
game_pin: game_pin, game_pin: game_pin,
game_id: game_token game_id: game_token
}); });
const res = await fetch(`/api/v1/quiz/play/check_captcha/${game_pin}`);
const json = await res.json();
game_mode = json.game_mode;
}; };
if (auto_connect) { onMount(() => {
connect(); if (auto_connect) {
} connect();
}
});
socket.on('registered_as_admin', (data) => { socket.on('registered_as_admin', (data) => {
quiz_data = JSON.parse(data['game']); quiz_data = JSON.parse(data['game']);
console.log(quiz_data); console.log(quiz_data);
@@ -152,7 +160,13 @@
</div> </div>
{/if} {/if}
{:else} {:else}
<SomeAdminScreen bind:final_results bind:game_pin bind:game_token bind:quiz_data /> <SomeAdminScreen
bind:final_results
bind:game_pin
bind:game_token
bind:quiz_data
bind:game_mode
/>
{/if} {/if}
<a <a
+3 -1
View File
@@ -22,6 +22,7 @@
started: boolean; started: boolean;
} }
let game_mode;
let final_results: Array<null> | Array<Array<PlayerAnswer>> = [null]; let final_results: Array<null> | Array<Array<PlayerAnswer>> = [null];
interface PlayerAnswer { interface PlayerAnswer {
@@ -118,7 +119,7 @@
</svelte:head> </svelte:head>
<div> <div>
{#if !gameMeta.started && gameData === undefined} {#if !gameMeta.started && gameData === undefined}
<JoinGame {game_pin} /> <JoinGame {game_pin} bind:game_mode />
{:else if JSON.stringify(final_results) !== JSON.stringify([null])} {:else if JSON.stringify(final_results) !== JSON.stringify([null])}
<ShowEndScreen bind:final_results bind:quiz_data={gameData} /> <ShowEndScreen bind:final_results bind:quiz_data={gameData} />
{:else if gameData !== undefined && question_index === ''} {:else if gameData !== undefined && question_index === ''}
@@ -130,6 +131,7 @@
{:else if gameMeta.started && gameData !== undefined && question_index !== '' && answer_results === undefined} {:else if gameMeta.started && gameData !== undefined && question_index !== '' && answer_results === undefined}
{#key unique} {#key unique}
<Question <Question
bind:game_mode
bind:question={gameData.questions[parseInt(question_index)]} bind:question={gameData.questions[parseInt(question_index)]}
bind:question_index bind:question_index
/> />
@@ -9,7 +9,7 @@
import { createTippy } from 'svelte-tippy'; import { createTippy } from 'svelte-tippy';
import ImportedOrNot from '$lib/view_quiz/imported_or_not.svelte'; import ImportedOrNot from '$lib/view_quiz/imported_or_not.svelte';
import { QuizQuestionType } from '$lib/quiz_types.js'; import { QuizQuestionType } from '$lib/quiz_types.js';
import { start_game } from '$lib/dashboard/start_game'; import { start_game } from '../../../lib/dashboard/start_game.svelte';
const tippy = createTippy({ const tippy = createTippy({
arrow: true, arrow: true,