Skip to content

Commit

Permalink
refactor: Optimize GameUI showGamePhase method for multiple message h…
Browse files Browse the repository at this point in the history
…andling

- Improved the `showGamePhase` method by clearing existing timers to ensure multiple messages fade out properly.
- This prevents overlapping text errors by resetting the phase display timer before showing a new phase.
  • Loading branch information
TKanX committed Aug 14, 2024
1 parent d4d11ea commit 99a9736
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 12 deletions.
13 changes: 2 additions & 11 deletions public/js/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -578,27 +578,18 @@ class GameUI {
*/
showGamePhase(phase, timeout) {
const gamePhaseElement = document.getElementById("game-phase");
const gamePhaseText = document.createElement("span");
const gamePhaseText = gamePhaseElement.querySelector("span");

gamePhaseText.innerText = phase;
gamePhaseText.classList.add(
"font-bold",
"text-2xl",
"text-center",
"drop-shadow"
);
gamePhaseElement.style.display = "block";

gamePhaseElement.style.opacity = 1;
gamePhaseElement.style.transition = "opacity 0.5s ease-in-out";

gamePhaseElement.innerHTML = "";
gamePhaseElement.appendChild(gamePhaseText);

clearTimeout(this.gamePhaseTimeout);
this.gamePhaseTimeout = setTimeout(() => {
gamePhaseElement.style.opacity = 0;
setTimeout(() => {
this.gamePhaseTimeout = setTimeout(() => {
gamePhaseElement.style.display = "none";
}, 500);
}, timeout);
Expand Down
4 changes: 3 additions & 1 deletion views/components/game/game.ejs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
<div id="game-phase" class="hidden fixed top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 flex flex-col items-center justify-center space-y-4 z-10"></div>
<div id="game-phase" class="hidden fixed top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 flex flex-col items-center justify-center space-y-4 z-10">
<span class="font-bold text-2xl text-center drop-shadow"></span>
</div>
<div id="game-players" class="fixed top-5 left-5 flex space-x-4"></div>

0 comments on commit 99a9736

Please sign in to comment.