From 310ac70a74af40bf68fd87883b2805197fd58b92 Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Fri, 19 May 2023 18:24:01 +0200 Subject: [PATCH] Add ability to fake the draw for admins Signed-off-by: Emmy D'Anello --- draw/consumers.py | 9 ++++++++ draw/static/draw.js | 50 ++++++++++++++++++++++++++++++++------------- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/draw/consumers.py b/draw/consumers.py index 26e0e9d..54d1802 100644 --- a/draw/consumers.py +++ b/draw/consumers.py @@ -315,6 +315,10 @@ class DrawConsumer(AsyncJsonWebsocketConsumer): # Launch the dice and get the result res = randint(1, 100) + if self.registration.is_admin and 'result' in kwargs \ + and isinstance(kwargs['result'], int) and (1 <= kwargs['result'] <= 100): + # Admins can force the result + res = int(kwargs['result']) if state == 'DICE_SELECT_POULES': team_draw.passage_dice = res else: @@ -591,6 +595,11 @@ class DrawConsumer(AsyncJsonWebsocketConsumer): while True: # Choose a random problem problem = randint(1, len(settings.PROBLEMS)) + if self.registration.is_admin and 'problem' in kwargs \ + and isinstance(kwargs['problem'], int) and (1 <= kwargs['problem'] <= len(settings.PROBLEMS)): + # Admins can force the draw + problem = int(kwargs['problem']) + # Check that the user didn't already accept this problem for the first round # if this is the second round if await TeamDraw.objects.filter(participation_id=td.participation_id, diff --git a/draw/static/draw.js b/draw/static/draw.js index f574b92..f45468c 100644 --- a/draw/static/draw.js +++ b/draw/static/draw.js @@ -34,17 +34,19 @@ function cancelLastStep(tid) { * The parameter `trigram` can be specified (by volunteers) to launch a dice for a specific team. * @param tid The tournament id * @param trigram The trigram of the team that a volunteer wants to force the dice launch (default: null) + * @param result The forced value. Null if unused (for regular people) */ -function drawDice(tid, trigram = null) { - socket.send(JSON.stringify({'tid': tid, 'type': 'dice', 'trigram': trigram})) +function drawDice(tid, trigram = null, result = null) { + socket.send(JSON.stringify({'tid': tid, 'type': 'dice', 'trigram': trigram, 'result': result})) } /** * Request to draw a new problem. * @param tid The tournament id + * @param problem The forced problem. Null if unused (for regular people) */ -function drawProblem(tid) { - socket.send(JSON.stringify({'tid': tid, 'type': 'draw_problem'})) +function drawProblem(tid, problem = null) { + socket.send(JSON.stringify({'tid': tid, 'type': 'draw_problem', 'problem': problem})) } /** @@ -196,8 +198,7 @@ document.addEventListener('DOMContentLoaded', () => { elem.classList.remove('text-bg-success') elem.classList.add('text-bg-warning') elem.innerText = `${trigram} 🎲 ??` - } - else { + } else { elem.classList.remove('text-bg-warning') elem.classList.add('text-bg-success') elem.innerText = `${trigram} 🎲 ${result}` @@ -510,8 +511,7 @@ document.addEventListener('DOMContentLoaded', () => { teamTr.append(reporterTd, opponentTd, defenderTd) break } - } - else if (poule.teams.length === 4) { + } else if (poule.teams.length === 4) { let emptyTd = document.createElement('td') switch (i) { case 0: @@ -527,8 +527,7 @@ document.addEventListener('DOMContentLoaded', () => { teamTr.append(emptyTd, reporterTd, opponentTd, defenderTd) break } - } - else if (poule.teams.length === 5) { + } else if (poule.teams.length === 5) { let emptyTd = document.createElement('td') let emptyTd2 = document.createElement('td') switch (i) { @@ -598,8 +597,7 @@ document.addEventListener('DOMContentLoaded', () => { if (problem !== null) { recapDiv.classList.remove('text-bg-warning') recapDiv.classList.add('text-bg-success') - } - else { + } else { recapDiv.classList.add('text-bg-warning') recapDiv.classList.remove('text-bg-success') } @@ -632,8 +630,7 @@ document.addEventListener('DOMContentLoaded', () => { recapDiv.parentNode.append(penaltyDiv) } penaltyDiv.textContent = `❌ ${0.5 * (rejected.length - (problems_count - 5))}` - } - else { + } else { // Eventually remove this div if (penaltyDiv !== null) penaltyDiv.remove() @@ -786,4 +783,29 @@ document.addEventListener('DOMContentLoaded', () => { } setupSocket() + + if (document.querySelector('a[href="/admin/"]')) { + // Administrators can fake the draw + // This is useful for debug purposes, or + document.getElementsByTagName('body')[0].addEventListener('keyup', event => { + if (event.key === 'f') { + let activeTab = document.querySelector('#tournaments-tab button.active') + let tid = activeTab.id.substring(4) + + let dice = document.getElementById(`launch-dice-${tid}`) + let box = document.getElementById(`draw-problem-${tid}`) + let value = NaN + if (!dice.classList.contains('d-none')) { + value = parseInt(prompt("Entrez la valeur du dé (laissez vide pour annuler) :")) + if (!isNaN(value) && 1 <= value && value <= 100) + drawDice(tid, null, value) + + } else if (!box.classList.contains('d-none')) { + value = parseInt(prompt("Entrez le numéro du problème à choisir (laissez vide pour annuler) :")) + if (!isNaN(value) && 1 <= value && value <= 8) + drawProblem(tid, value) + } + } + }) + } })