Merge pull request #140 from mawoka-myblock/132-add-a-field-for-optional-data-to-be-entered-by-the-players
This commit is contained in:
@@ -51,10 +51,13 @@ class GetLiveDataResponse(BaseModel):
|
|||||||
|
|
||||||
@router.get("/")
|
@router.get("/")
|
||||||
async def get_live_game_data(
|
async def get_live_game_data(
|
||||||
game_pin: int, api_key: str, player_count_as_a_string: bool = False, in_array: bool = False
|
game_pin: str, api_key: str, player_count_as_a_string: bool = False, in_array: bool = False
|
||||||
):
|
):
|
||||||
user_id = await check_api_key(api_key)
|
user_id = await check_api_key(api_key)
|
||||||
redis_res = await redis.get(f"game:{game_pin}")
|
redis_res = await redis.get(f"game:{game_pin}")
|
||||||
|
if redis_res is None:
|
||||||
|
game_pin = await redis.get(f"game_pin:{user_id}:{game_pin}")
|
||||||
|
redis_res = await redis.get(f"game:{game_pin}")
|
||||||
if redis_res is None or user_id is None:
|
if redis_res is None or user_id is None:
|
||||||
raise HTTPException(status_code=404, detail="Game not found or API key not found")
|
raise HTTPException(status_code=404, detail="Game not found or API key not found")
|
||||||
game = PlayGame.parse_raw(redis_res)
|
game = PlayGame.parse_raw(redis_res)
|
||||||
@@ -96,9 +99,13 @@ async def get_live_game_data(
|
|||||||
|
|
||||||
|
|
||||||
@router.get("/user_count")
|
@router.get("/user_count")
|
||||||
async def get_game_user_count(game_pin: int, as_string: bool = False):
|
async def get_game_user_count(game_pin: str, api_key: str, as_string: bool = False):
|
||||||
# if redis_res is None:
|
# if redis_res is None:
|
||||||
# raise HTTPException(status_code=404, detail="Game not found")
|
# raise HTTPException(status_code=404, detail="Game not found")
|
||||||
|
user_id = await check_api_key(api_key)
|
||||||
|
redis_res = await redis.get(f"game_session:{game_pin}")
|
||||||
|
if redis_res is None:
|
||||||
|
game_pin = await redis.get(f"game_pin:{user_id}:{game_pin}")
|
||||||
player_count = await redis.scard(f"game_session:{game_pin}:players")
|
player_count = await redis.scard(f"game_session:{game_pin}:players")
|
||||||
if as_string:
|
if as_string:
|
||||||
return {"players": {"count": str(player_count)}}
|
return {"players": {"count": str(player_count)}}
|
||||||
@@ -115,9 +122,12 @@ async def get_game_user_count(game_pin: int, as_string: bool = False):
|
|||||||
@router.get(
|
@router.get(
|
||||||
"/players",
|
"/players",
|
||||||
)
|
)
|
||||||
async def get_game_session(game_pin: int, api_key: str):
|
async def get_game_session(game_pin: str, api_key: str):
|
||||||
user_id = await check_api_key(api_key)
|
user_id = await check_api_key(api_key)
|
||||||
redis_res = await redis.get(f"game_session:{game_pin}")
|
redis_res = await redis.get(f"game_session:{game_pin}")
|
||||||
|
if redis_res is None:
|
||||||
|
game_pin = await redis.get(f"game_pin:{user_id}:{game_pin}")
|
||||||
|
redis_res = await redis.get(f"game_session:{game_pin}")
|
||||||
if redis_res is None or user_id is None:
|
if redis_res is None or user_id is None:
|
||||||
raise HTTPException(status_code=404, detail="Game not found or API key not found")
|
raise HTTPException(status_code=404, detail="Game not found or API key not found")
|
||||||
data = GameSession.parse_raw(redis_res)
|
data = GameSession.parse_raw(redis_res)
|
||||||
@@ -140,9 +150,12 @@ async def get_game_session(game_pin: int, api_key: str):
|
|||||||
|
|
||||||
|
|
||||||
@router.post("/set_question")
|
@router.post("/set_question")
|
||||||
async def set_next_question(game_pin: int, question_number: int, api_key: str):
|
async def set_next_question(game_pin: str, question_number: int, api_key: str):
|
||||||
user_id = await check_api_key(api_key)
|
user_id = await check_api_key(api_key)
|
||||||
redis_res = await redis.get(f"game:{game_pin}")
|
redis_res = await redis.get(f"game:{game_pin}")
|
||||||
|
if redis_res is None:
|
||||||
|
game_pin = await redis.get(f"game_pin:{user_id}:{game_pin}")
|
||||||
|
redis_res = await redis.get(f"game:{game_pin}")
|
||||||
if redis_res is None or user_id is None:
|
if redis_res is None or user_id is None:
|
||||||
raise HTTPException(status_code=404, detail="Game not found or API key not found")
|
raise HTTPException(status_code=404, detail="Game not found or API key not found")
|
||||||
game_data = PlayGame.parse_raw(redis_res)
|
game_data = PlayGame.parse_raw(redis_res)
|
||||||
@@ -161,7 +174,14 @@ async def set_next_question(game_pin: int, question_number: int, api_key: str):
|
|||||||
|
|
||||||
|
|
||||||
@router.get("/scores")
|
@router.get("/scores")
|
||||||
async def get_live_player_scores(game_pin: int):
|
async def get_live_player_scores(game_pin: str, api_key: str):
|
||||||
|
user_id = await check_api_key(api_key)
|
||||||
|
redis_res = await redis.get(f"game:{game_pin}")
|
||||||
|
if redis_res is None:
|
||||||
|
game_pin = await redis.get(f"game_pin:{user_id}:{game_pin}")
|
||||||
|
redis_res = await redis.get(f"game:{game_pin}")
|
||||||
|
if redis_res is None or user_id is None:
|
||||||
|
raise HTTPException(status_code=404, detail="Game not found or API key not found")
|
||||||
res = await redis.hgetall(f"game_session:{game_pin}:player_scores")
|
res = await redis.hgetall(f"game_session:{game_pin}:player_scores")
|
||||||
return_arr = []
|
return_arr = []
|
||||||
for username in res:
|
for username in res:
|
||||||
|
|||||||
@@ -115,6 +115,7 @@ async def start_quiz(
|
|||||||
background_color=quiz.background_color,
|
background_color=quiz.background_color,
|
||||||
)
|
)
|
||||||
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)
|
||||||
|
await redis.set(f"game_pin:{user.id}:{quiz_id}", game_pin, ex=18000)
|
||||||
return {**quiz.dict(exclude={"id"}), **game.dict(exclude={"questions"})}
|
return {**quiz.dict(exclude={"id"}), **game.dict(exclude={"questions"})}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -194,7 +194,8 @@ class ReturnQuestion(QuizQuestion):
|
|||||||
if values["type"] == QuizQuestionType.RANGE and type(v) != RangeQuizAnswerWithoutSolution:
|
if values["type"] == QuizQuestionType.RANGE and type(v) != RangeQuizAnswerWithoutSolution:
|
||||||
raise ValueError("Answer must be from type RangeQuizAnswer if type is RANGE")
|
raise ValueError("Answer must be from type RangeQuizAnswer if type is RANGE")
|
||||||
if values["type"] == QuizQuestionType.VOTING and type(v[0]) != VotingQuizAnswer:
|
if values["type"] == QuizQuestionType.VOTING and type(v[0]) != VotingQuizAnswer:
|
||||||
print("Answer must be from type VotingQuizAnswer if type is VOTING")
|
# print("Answer must be from type VotingQuizAnswer if type is VOTING")
|
||||||
|
pass
|
||||||
return v
|
return v
|
||||||
|
|
||||||
|
|
||||||
@@ -208,12 +209,10 @@ async def set_question_number(sid, data: str):
|
|||||||
game_data.current_question = int(float(data))
|
game_data.current_question = int(float(data))
|
||||||
await redis.set(f"game:{session['game_pin']}", game_data.json())
|
await redis.set(f"game:{session['game_pin']}", game_data.json())
|
||||||
await redis.set(f"game:{session['game_pin']}:current_time", datetime.now().isoformat())
|
await redis.set(f"game:{session['game_pin']}:current_time", datetime.now().isoformat())
|
||||||
# print(game_data.dict(include={"questions"})["questions"][int(float(data))])
|
|
||||||
temp_return = game_data.dict(include={"questions"})["questions"][int(float(data))]
|
temp_return = game_data.dict(include={"questions"})["questions"][int(float(data))]
|
||||||
if game_data.questions[int(float(data))].type == QuizQuestionType.VOTING:
|
if game_data.questions[int(float(data))].type == QuizQuestionType.VOTING:
|
||||||
for i in range(len(temp_return["answers"])):
|
for i in range(len(temp_return["answers"])):
|
||||||
temp_return["answers"][i] = VotingQuizAnswer(**temp_return["answers"][i])
|
temp_return["answers"][i] = VotingQuizAnswer(**temp_return["answers"][i])
|
||||||
print(temp_return)
|
|
||||||
await sio.emit(
|
await sio.emit(
|
||||||
"set_question_number",
|
"set_question_number",
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,129 +0,0 @@
|
|||||||
<!--
|
|
||||||
- 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/.
|
|
||||||
-->
|
|
||||||
<script lang="ts">
|
|
||||||
import { navbarVisible } from '$lib/stores';
|
|
||||||
|
|
||||||
navbarVisible.set(false);
|
|
||||||
const data = [
|
|
||||||
{
|
|
||||||
username: 'Mawoka',
|
|
||||||
answer: 'A very long answe-thing',
|
|
||||||
right: false,
|
|
||||||
time_taken: 1681.631,
|
|
||||||
score: 0
|
|
||||||
},
|
|
||||||
{
|
|
||||||
username: 'Mawoka',
|
|
||||||
answer: 'A very long answe-thing',
|
|
||||||
right: false,
|
|
||||||
time_taken: 1681.631,
|
|
||||||
score: 0
|
|
||||||
},
|
|
||||||
{
|
|
||||||
username: 'Mawoka',
|
|
||||||
answer: 'A very long answe-thing',
|
|
||||||
right: false,
|
|
||||||
time_taken: 1681.631,
|
|
||||||
score: 0
|
|
||||||
},
|
|
||||||
{
|
|
||||||
username: 'Mawoka',
|
|
||||||
answer: 'A very long answe-thing',
|
|
||||||
right: false,
|
|
||||||
time_taken: 1681.631,
|
|
||||||
score: 0
|
|
||||||
},
|
|
||||||
{
|
|
||||||
username: 'Mawoka',
|
|
||||||
answer: 'A very long answe-thing',
|
|
||||||
right: false,
|
|
||||||
time_taken: 1681.631,
|
|
||||||
score: 0
|
|
||||||
},
|
|
||||||
{
|
|
||||||
username: 'Mawoka',
|
|
||||||
answer: 'A very long answe-thing',
|
|
||||||
right: false,
|
|
||||||
time_taken: 1681.631,
|
|
||||||
score: 0
|
|
||||||
},
|
|
||||||
{
|
|
||||||
username: 'Mawoka',
|
|
||||||
answer: 'A very long answe-thing',
|
|
||||||
right: false,
|
|
||||||
time_taken: 1681.631,
|
|
||||||
score: 0
|
|
||||||
},
|
|
||||||
{
|
|
||||||
username: 'Mawoka',
|
|
||||||
answer: 'A very long answe-thing',
|
|
||||||
right: false,
|
|
||||||
time_taken: 1681.631,
|
|
||||||
score: 0
|
|
||||||
},
|
|
||||||
{
|
|
||||||
username: 'Mawoka',
|
|
||||||
answer: 'A very long answe-thing',
|
|
||||||
right: false,
|
|
||||||
time_taken: 1681.631,
|
|
||||||
score: 0
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
username: 'Mawoka',
|
|
||||||
answer: 'A very long answe-thing',
|
|
||||||
right: false,
|
|
||||||
time_taken: 1681.631,
|
|
||||||
score: 0
|
|
||||||
},
|
|
||||||
{
|
|
||||||
username: 'dewads',
|
|
||||||
answer: 'A very long answe-thing',
|
|
||||||
right: false,
|
|
||||||
time_taken: 1681.631,
|
|
||||||
score: 0
|
|
||||||
},
|
|
||||||
{
|
|
||||||
username: 'dsadsa',
|
|
||||||
answer: 'der',
|
|
||||||
right: false,
|
|
||||||
time_taken: 2613.532,
|
|
||||||
score: 0
|
|
||||||
}
|
|
||||||
];
|
|
||||||
const quiz_answers = ['A very long answe-thing', 'dieter', 'moin', 'der'];
|
|
||||||
let sorted_data = {};
|
|
||||||
for (const i of quiz_answers) {
|
|
||||||
sorted_data[i] = 0;
|
|
||||||
}
|
|
||||||
for (const i of data) {
|
|
||||||
sorted_data[i.answer] += 1;
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div class="flex justify-center h-screen w-full">
|
|
||||||
<div
|
|
||||||
class="m-auto grid grid-rows-3 w-fit gap-4"
|
|
||||||
style="grid-template-columns: repeat({quiz_answers.length}, minmax(0, 1fr));"
|
|
||||||
>
|
|
||||||
{#each quiz_answers as answer}
|
|
||||||
<span class="text-center self-end"
|
|
||||||
>{#if sorted_data[answer] > 0}{sorted_data[answer]}{/if}</span
|
|
||||||
>
|
|
||||||
{/each}
|
|
||||||
{#each quiz_answers as answer}
|
|
||||||
<div
|
|
||||||
class="w-20 bg-black self-end flex justify-center"
|
|
||||||
style="height: {(sorted_data[answer] * 20) / data.length}rem"
|
|
||||||
/>
|
|
||||||
{/each}
|
|
||||||
{#each quiz_answers as answer}
|
|
||||||
<div class="w-20">
|
|
||||||
<p class="-rotate-45">{answer}</p>
|
|
||||||
</div>
|
|
||||||
{/each}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
Reference in New Issue
Block a user