Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render timer on page #97

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ <h2>Players</h2>
</form>
</template>

<section id="timer-section"></section>

<script
src="https://cdn.socket.io/4.1.2/socket.io.min.js"
crossorigin="anonymous"
Expand Down
35 changes: 35 additions & 0 deletions client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,37 @@ const nameFormElement = getElementById("name-form") as NameFormElement;
const playerListElement = getElementById("player-list");
const playerNameElement = getElementById("player-name");

const renderTimer = (): void => {
const timerSectionElement = getElementById("timer-section");
renderInitialTimer(timerSectionElement);
renderCountdown(timerSectionElement);
};

const renderInitialTimer = (timerElement: HTMLElement): void => {
const secondsUntilRoundEnd = document.createElement("p");
secondsUntilRoundEnd.innerText = "15";
timerElement.appendChild(secondsUntilRoundEnd);
appendTimerSuffix(secondsUntilRoundEnd);
};

const renderCountdown = (timerElement: HTMLElement) => {
let secondsElapsed = 0;
const updateTimer = setInterval(() => {
secondsElapsed++;
timerElement.innerText = (15 - secondsElapsed).toString();
appendTimerSuffix(timerElement);
if (secondsElapsed === 15) {
clearInterval(updateTimer);
}
}, 1000);
};

const appendTimerSuffix = (timerElement: HTMLElement) => {
const description = document.createElement("span");
description.innerText = " seconds left";
timerElement.appendChild(description);
};

let currentPlayer: Player;
let playerNames: Player["name"][] = [];

Expand Down Expand Up @@ -130,6 +161,10 @@ socket.on("round:startable", () => {
showStartButton();
});

socket.on("round:countdown", () => {
renderTimer();
});

nameFormElement.addEventListener("submit", (e) => {
e.preventDefault();
addPlayer(nameFormElement.elements.name.value);
Expand Down
1 change: 1 addition & 0 deletions server/@types/events.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Colour, Player, Question } from "./entities";
export interface ClientboundSocketServerEvents {
"round:startable": () => void;
"round:start": () => void;
"round:countdown": () => void;
"player:set": (player: Player) => void;
"players:get": (playerNames: Player["name"][]) => void;
"question:get": (question: Question) => void;
Expand Down
1 change: 0 additions & 1 deletion server/machines/round.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const roundMachine = createMachine(
types: {
context: {} as Context,
events: {} as Event,
typegen: {} as import("./round.typegen").Typegen0,
},
states: {
roundStart: {
Expand Down
24 changes: 0 additions & 24 deletions server/machines/round.typegen.ts

This file was deleted.

4 changes: 4 additions & 0 deletions server/models/round.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class Round {
);
break;
}
case "countdown": {
this.server.onCountdown()
break;
}
default:
break;
}
Expand Down
4 changes: 4 additions & 0 deletions server/socketServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ export class SocketServer {
onShowStartButton() {
this.server.emit("round:startable");
}

onCountdown() {
this.server.emit("round:countdown")
}
}
Loading