Skip to content

Commit

Permalink
Merge pull request #94 from karlromets/fix/final-round-timer
Browse files Browse the repository at this point in the history
Fix final round timer state
  • Loading branch information
joshzcold authored Dec 27, 2024
2 parents ee4c576 + 602af30 commit 4b99b7a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 11 deletions.
37 changes: 33 additions & 4 deletions components/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ export default function Admin(props) {
const [error, setErrorVal] = useState("");
const [imageUploaded, setImageUploaded] = useState(null);
const [timerStarted, setTimerStarted] = useState(false);
const [timerCompleted, setTimerCompleted] = useState(false);
const [csvFileUpload, setCsvFileUpload] = useState(null);
const [csvFileUploadText, setCsvFileUploadText] = useState(null);
let ws = props.ws;
Expand Down Expand Up @@ -422,6 +423,9 @@ export default function Admin(props) {
} else if (json.action === "error") {
console.error(json.message);
setError(json.message);
} else if (json.action === "timer_complete") {
setTimerStarted(false);
setTimerCompleted(true);
} else {
console.debug("did not expect admin: ", json);
}
Expand Down Expand Up @@ -1019,7 +1023,8 @@ export default function Admin(props) {
{/* START FINAL ROUND 2 */}
{!game.is_final_second ? (
<button
className="border-4 rounded p-5 text-3xl bg-secondary-300 text-foreground"
className={`border-4 rounded p-5 text-3xl bg-secondary-300 text-foreground ${timerStarted ? 'opacity-50': ''}`}
disabled={timerStarted}
onClick={() => {
console.debug(game);
game.is_final_second = true;
Expand All @@ -1030,6 +1035,7 @@ export default function Admin(props) {
action: "set_timer",
data: game.final_round_timers[1],
});
setTimerCompleted(false);
}}
>
{t("start")} {t("Final Round")}{" "}
Expand All @@ -1039,7 +1045,8 @@ export default function Admin(props) {
<div className="flex py-5 items-center flex-row justify-evenly text-foreground space-x-5">
{/* GO BACK TO FINAL ROUND 1 */}
<button
className="border-4 rounded p-5 text-3xl bg-secondary-300"
className={`border-4 rounded p-5 text-3xl bg-secondary-300 ${timerStarted ? 'opacity-50': ''}`}
disabled={timerStarted}
onClick={() => {
game.is_final_round = true;
game.hide_first_round = false;
Expand Down Expand Up @@ -1086,11 +1093,12 @@ export default function Admin(props) {
) : null}
</div>
)}
<div className="px-2">
<div className="px-2 flex">
{!timerStarted ? (
/* START TIMER */
<button
className="border-4 rounded p-5 text-3xl bg-secondary-300 text-foreground"
className={`border-4 rounded p-5 text-3xl bg-secondary-300 text-foreground ${timerCompleted ? 'opacity-50' : ''}`}
disabled={timerCompleted}
onClick={() => {
if (game.is_final_second) {
send({
Expand All @@ -1104,6 +1112,7 @@ export default function Admin(props) {
});
}
setTimerStarted(true);
setTimerCompleted(false);
}}
>
{t("Start Timer")}
Expand All @@ -1120,6 +1129,26 @@ export default function Admin(props) {
{t("Stop Timer")}
</button>
)}
<button className={`border-4 rounded p-5 ml-2 text-3xl bg-secondary-300 text-foreground ${!timerStarted ? '' : 'opacity-50'}`}
disabled={timerStarted}
onClick={() => {
if(!timerStarted) {
if (game.is_final_second) {
send({
action: "set_timer",
data: game.final_round_timers[1],
});
} else {
send({
action: "set_timer",
data: game.final_round_timers[0],
});
}
}
setTimerCompleted(false);
}}>
{t("Reset Timer")}
</button>
</div>
</div>
<FinalRoundPointTotals game={game} />
Expand Down
38 changes: 33 additions & 5 deletions pages/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,49 @@ export default function Game(props) {
} else if (json.action === "stop_timer") {
clearInterval(timerInterval);
} else if (json.action === "start_timer") {
let limit = json.data;
timerInterval = setInterval(() => {
if (limit > 0) {
limit = limit - 1;
setTimer(limit);
setTimer(prevTimer => {
if (prevTimer > 0) {
return prevTimer - 1;
} else {
var audio = new Audio("try-again.mp3");
audio.play();
clearInterval(timerInterval);
setTimer(json.data);

// Send timer stop to admin.js
try {
let session = cookieCutter.get("session");
let [room, id] = session.split(":");

if(!session) {
console.error("No session cookie found");
return 0;
}

if(!room || !id) {
console.error("Invalid session cookie format");
return 0;
}

ws.current.send(JSON.stringify({
action: "timer_complete",
room: room,
id: id
}));
} catch (error) {
console.error("Error processing session cookie:", error);
}
return 0;
}
});
}, 1000);
} else if (json.action === "change_lang") {
console.debug("Language Change", json.data);
i18n.changeLanguage(json.data);
} else if (json.action === "timer_complete") {
console.debug("Timer complete");
} else if (json.action === "clearbuzzers") {
console.debug("Clear buzzers");
} else {
console.error("didn't expect", json);
}
Expand Down
1 change: 1 addition & 0 deletions public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"start": "Start",
"Start Timer": "Start Timer",
"Stop Timer": "Stop Timer",
"Reset Timer": "Reset Timer",
"submit": "Submit",
"Team Name": "Team Name",
"team": "Team",
Expand Down
5 changes: 3 additions & 2 deletions public/locales/et/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
"spanish": "Hispaania",
"Start Round 1": "Alusta vooru nr. 1",
"start": "Start",
"Start Timer": "K\u00e4ivita Timer",
"Stop Timer": "Peata Timer",
"Start Timer": "K\u00e4ivita Taimer",
"Stop Timer": "Peata Taimer",
"Reset Timer": "Taasta Taimer",
"submit": "Esita",
"Team Name": "Tiimi nimi",
"team": "Tiim",
Expand Down

0 comments on commit 4b99b7a

Please sign in to comment.