fix(polling): Improve polling mechanism and delay handling (#60)
Reviewed-on: #60
This commit is contained in:
Submodule knockoutwhist updated: 372f20ca6c...e4322839d1
@@ -2,11 +2,12 @@ package components
|
|||||||
|
|
||||||
import de.knockoutwhist.components.DefaultConfiguration
|
import de.knockoutwhist.components.DefaultConfiguration
|
||||||
import de.knockoutwhist.ui.UI
|
import de.knockoutwhist.ui.UI
|
||||||
|
import de.knockoutwhist.utils.DelayHandler
|
||||||
import de.knockoutwhist.utils.events.EventListener
|
import de.knockoutwhist.utils.events.EventListener
|
||||||
|
|
||||||
class WebApplicationConfiguration extends DefaultConfiguration {
|
class WebApplicationConfiguration extends DefaultConfiguration {
|
||||||
|
|
||||||
override def uis: Set[UI] = Set()
|
override def uis: Set[UI] = Set()
|
||||||
override def listener: Set[EventListener] = Set()
|
override def listener: Set[EventListener] = Set(DelayHandler)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,7 +59,6 @@ class IngameController @Inject() (
|
|||||||
case Some(g) =>
|
case Some(g) =>
|
||||||
val results = Try {
|
val results = Try {
|
||||||
returnInnerHTML(g, request.user)
|
returnInnerHTML(g, request.user)
|
||||||
|
|
||||||
}
|
}
|
||||||
if (results.isSuccess) {
|
if (results.isSuccess) {
|
||||||
Ok(views.html.main("In-Game - Knockout Whist")(results.get))
|
Ok(views.html.main("In-Game - Knockout Whist")(results.get))
|
||||||
@@ -67,7 +66,7 @@ class IngameController @Inject() (
|
|||||||
InternalServerError(results.failed.get.getMessage)
|
InternalServerError(results.failed.get.getMessage)
|
||||||
}
|
}
|
||||||
case None =>
|
case None =>
|
||||||
NotFound("Game not found")
|
Redirect(routes.MainMenuController.mainMenu())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
def startGame(gameId: String): Action[AnyContent] = authAction { implicit request: AuthenticatedRequest[AnyContent] =>
|
def startGame(gameId: String): Action[AnyContent] = authAction { implicit request: AuthenticatedRequest[AnyContent] =>
|
||||||
|
|||||||
@@ -78,7 +78,8 @@ class PollingController @Inject() (
|
|||||||
"name" -> u.name,
|
"name" -> u.name,
|
||||||
"id" -> u.id,
|
"id" -> u.id,
|
||||||
"self" -> (u.id == userSession.id)
|
"self" -> (u.id == userSession.id)
|
||||||
))
|
)),
|
||||||
|
"maxPlayers" -> game.maxPlayers
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -119,15 +119,6 @@ class GameLobby private(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
waitingPromises.keys.foreach { playerId =>
|
|
||||||
val queue = eventsPerPlayer(playerId)
|
|
||||||
if (queue.nonEmpty) {
|
|
||||||
val promise = waitingPromises(playerId)
|
|
||||||
promise.success(queue.dequeue())
|
|
||||||
waitingPromises.remove(playerId)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col">
|
<div class="col">
|
||||||
<div class="p-3 text-center fs-4">Playeramount: @gamelobby.getPlayers.size / @gamelobby.maxPlayers</div>
|
<div class="p-3 text-center fs-4" id="playerAmount">Playeramount: @gamelobby.getPlayers.size / @gamelobby.maxPlayers</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="row justify-content-center align-items-center flex-grow-1">
|
<div class="row justify-content-center align-items-center flex-grow-1">
|
||||||
|
|||||||
@@ -79,7 +79,13 @@
|
|||||||
})
|
})
|
||||||
})()
|
})()
|
||||||
|
|
||||||
|
let polling = false;
|
||||||
function pollForUpdates(gameId) {
|
function pollForUpdates(gameId) {
|
||||||
|
if (polling) {
|
||||||
|
console.log("[DEBUG] Polling already in progress. Skipping this cycle.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
polling = true;
|
||||||
console.log(`[DEBUG] Starting poll cycle for Game ID: ${gameId} at ${new Date().toISOString()}`);
|
console.log(`[DEBUG] Starting poll cycle for Game ID: ${gameId} at ${new Date().toISOString()}`);
|
||||||
if (!gameId) {
|
if (!gameId) {
|
||||||
console.error("[DEBUG] Game ID is missing. Stopping poll.");
|
console.error("[DEBUG] Game ID is missing. Stopping poll.");
|
||||||
@@ -90,7 +96,7 @@ function pollForUpdates(gameId) {
|
|||||||
const $mainmenuElement = $('#main-menu-screen')
|
const $mainmenuElement = $('#main-menu-screen')
|
||||||
const $mainbody = $('#main-body')
|
const $mainbody = $('#main-body')
|
||||||
if (!$handElement.length && !$lobbyElement.length && !$mainmenuElement.length && !$mainbody.length) {
|
if (!$handElement.length && !$lobbyElement.length && !$mainmenuElement.length && !$mainbody.length) {
|
||||||
setTimeout(() => pollForUpdates(gameId), 1000);
|
setTimeout(() => { polling = false; pollForUpdates(gameId) }, 1000);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const route = jsRoutes.controllers.PollingController.polling(gameId);
|
const route = jsRoutes.controllers.PollingController.polling(gameId);
|
||||||
@@ -254,6 +260,7 @@ function pollForUpdates(gameId) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
$("#players").html(newHtml);
|
$("#players").html(newHtml);
|
||||||
|
$('#playerAmount').text(`Playeramount: ${data.users.length} / ${data.maxPlayers}`);
|
||||||
} else {
|
} else {
|
||||||
console.warn(`[DEBUG] Received unknown status: ${data.status}`);
|
console.warn(`[DEBUG] Received unknown status: ${data.status}`);
|
||||||
}
|
}
|
||||||
@@ -267,11 +274,7 @@ function pollForUpdates(gameId) {
|
|||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
complete: (() => {
|
complete: (() => {
|
||||||
if (!window.location.href.includes("game")) {
|
setTimeout(() => { polling = false; pollForUpdates(gameId) }, 200);
|
||||||
console.log("[DEBUG] Page URL changed. Stopping poll restart.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
setTimeout(() => pollForUpdates(gameId), 200);
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user