Skip to content

Commit

Permalink
Question counter funcionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoDiNu committed Apr 8, 2024
1 parent 391a382 commit fa421b3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/java/com/uniovi/controllers/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ public String getPoints(HttpSession session) {
return "0";
}

@GetMapping("/game/currentQuestion")
@ResponseBody
public String getCurrentQuestion(HttpSession session) {
GameSession gameSession = (GameSession) session.getAttribute("gameSession");
if (gameSession != null)
return String.valueOf(gameSession.getAnsweredQuestions().size()+1);
else
return "0";
}

private Player getLoggedInPlayer(Principal principal) {
Optional<Player> player = playerService.getUserByUsername(principal.getName());
return player.orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ <h1 class="text-center" th:text="#{game.finish}"></h1>
<script>
$(document).ready(function () {
$(".points").remove();
$(".questionCounter").remove();
});
</script>
</div>
13 changes: 13 additions & 0 deletions src/main/resources/templates/game/fragments/questionResult.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ <h1 class="text-center" th:text="#{${messageKey}}" style="font-size: 2em;"></h1>
let activeTimeout = setTimeout(function () {
clearInterval(interval); // Ensure the interval is cleared when the timeout completes
$("#gameFrame").load('/game/update');
updateQuestionCounter();
}, timeoutPeriod);

timeoutPeriod = timeoutPeriod - updateInterval * 2; // Adjust the timeout period to account for the update interval
Expand All @@ -43,12 +44,24 @@ <h1 class="text-center" th:text="#{${messageKey}}" style="font-size: 2em;"></h1>
}
}

function updateQuestionCounter() {
$.ajax({
type: "GET",
url: "/game/currentQuestion",
success: function (response) {
if (!isNaN(response))
$("#currentQuestion").text(response);
}
});
}

$("#continueBtn").off('click').on('click', function () {
if (activeTimeout) {
clearTimeout(activeTimeout);
clearInterval(interval);
}
$("#gameFrame").load("/game/update");
updateQuestionCounter();
});
}

Expand Down

0 comments on commit fa421b3

Please sign in to comment.