feat(game): Implement return to lobby functionality and enhance dog life handling

This commit is contained in:
2025-11-19 19:18:41 +01:00
parent e2a5cb9614
commit b260e18223
8 changed files with 143 additions and 45 deletions

View File

@@ -89,7 +89,7 @@ function pollForUpdates(gameId) {
const $lobbyElement = $('#lobbybackground');
const $mainmenuElement = $('#main-menu-screen')
if (!$handElement.length && !$lobbyElement.length && !$mainmenuElement.length) {
setTimeout(() => pollForUpdates(gameId), 5000);
setTimeout(() => pollForUpdates(gameId), 1000);
return;
}
const route = jsRoutes.controllers.PollingController.polling(gameId);
@@ -115,13 +115,15 @@ function pollForUpdates(gameId) {
$handElement.removeClass('ingame-cards-slide');
}
const dog = data.dog;
newHand.forEach((cardId, index) => {
const cardHtml = `
<div class="col-auto handcard" style="border-radius: 6px">
<div class="btn btn-outline-light p-0 border-0 shadow-none"
data-card-id="${index}"
style="border-radius: 6px"
onclick="handlePlayCard(this, '${gameId}')">
onclick="handlePlayCard(this, '${gameId}', '${dog}')">
<img src="/assets/images/cards/${cardId}.png" width="120px" style="border-radius: 6px"/>
</div>
@@ -130,6 +132,14 @@ function pollForUpdates(gameId) {
newHandHTML += cardHtml;
});
if (dog) {
newHandHTML += `
<div class="mt-2">
<button class="btn btn-danger" onclick="handleSkipDogLife(this, '${gameId}')">Skip Dog Life</button>
</div>
`;
}
$handElement.html(newHandHTML);
$('#current-player-name').text(data.currentPlayerName)
if (data.nextPlayer) {
@@ -248,12 +258,12 @@ function pollForUpdates(gameId) {
console.error(`Something unexpected happened while polling. ${jqXHR.status}, ${errorThrown}`)
}
}),
complete: ((jqXHR, textStatus) => {
complete: (() => {
if (!window.location.href.includes("game")) {
console.log("[DEBUG] Page URL changed. Stopping poll restart.");
return;
}
setTimeout(() => pollForUpdates(gameId), 500);
setTimeout(() => pollForUpdates(gameId), 200);
})
})
}
@@ -374,7 +384,7 @@ function sendLeavePlayerRequest(gameId) {
})
}
function handlePlayCard(cardobject, gameId) {
function handlePlayCard(cardobject, gameId, dog = false) {
const cardId = cardobject.dataset.cardId;
const jsonObj = {
cardID: cardId
@@ -382,6 +392,33 @@ function handlePlayCard(cardobject, gameId) {
sendPlayCardRequest(jsonObj, gameId, cardobject)
}
function handleSkipDogLife(cardobject, gameId) {
const route = jsRoutes.controllers.IngameController.playDogCard(gameId);
$.ajax({
url: route.url,
type: route.type,
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify({
cardID: 'skip'
}),
error: (jqXHR => {
let error;
try {
error = JSON.parse(jqXHR.responseText);
} catch (e) {
console.error("Failed to parse error response:", e);
}
if (error?.errorMessage) {
alert(`${error.errorMessage}`);
} else {
alert('An unexpected error occurred. Please try again.');
}
})
})
}
function sendPlayCardRequest(jsonObj, gameId, cardobject) {
const wiggleKeyframes = [
{ transform: 'translateX(0)' },
@@ -405,10 +442,6 @@ function sendPlayCardRequest(jsonObj, gameId, cardobject) {
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify(jsonObj),
success: (data => {
if (data.status === 'success') {
}
}),
error: (jqXHR => {
try {
error = JSON.parse(jqXHR.responseText);