Skip to content

Commit

Permalink
fix: hint being sent more than once
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenil committed Apr 30, 2024
1 parent bf71fe0 commit f09fe30
Showing 1 changed file with 47 additions and 40 deletions.
87 changes: 47 additions & 40 deletions html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,46 +111,53 @@ <h2>Chat</h2>
</div>

<script>
function startTimer(duration, display) {
var timer = duration, minutes, seconds;
var timerInterval = setInterval(function () {
minutes = parseInt(timer / 60, 10);
seconds = parseInt(timer % 60, 10);

minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;

display.textContent = minutes + ":" + seconds;

if (timer > 0 && timer % 30 === 0) {
sendHintRequest(); // Send a hint request every 30 seconds
}

if (--timer < 0) {
clearInterval(timerInterval);
alert("Time's up!");
display.textContent = "00:00"; // Reset timer display
}
}, 1000);
}

function sendHintRequest() {
var data = {
type: "giveMehint",
eventData: {}
};
connection.send(JSON.stringify(data));
console.log("Hint request sent.");
}

function startGameTimer() {
var twentyMinutes = 60 * 20; // 20 minutes in seconds
var display = document.querySelector('#timer');
startTimer(twentyMinutes, display);
}
window.onload = function() {
startGameTimer(); // You can also trigger this based on game start conditions
};
function startTimer(duration, display) {
var timer = duration, minutes, seconds;
var hintSent = false;

var timerInterval = setInterval(function() {
minutes = parseInt(timer / 60, 10);
seconds = parseInt(timer % 60, 10);

minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;

display.textContent = minutes + ":" + seconds;


if (timer > 0 && timer % 30 === 0 && !hintSent) {
sendHintRequest();
hintSent = true;
} else if (timer % 30 !== 0) {
hintSent = false;
}

if (--timer < 0) {
clearInterval(timerInterval);
alert("Time's up!");
display.textContent = "00:00";
}
}, 1000);
}

function sendHintRequest() {
var data = {
type: "giveMehint",
eventData: {}
};
connection.send(JSON.stringify(data));
console.log("Hint request sent.");
}

function startGameTimer() {
var twentyMinutes = 60 * 20; // 20 minutes in seconds
var display = document.querySelector('#timer');
startTimer(twentyMinutes, display);
}

window.onload = function() {
startGameTimer();
};
</script>


Expand Down

0 comments on commit f09fe30

Please sign in to comment.