From 90e2daf30f8386bdc8444b2c9d21a16311bbb1fb Mon Sep 17 00:00:00 2001 From: Geoffrey Allott Date: Tue, 16 Mar 2021 23:01:19 +0000 Subject: [PATCH] add timeouts after certain actions to pause the game so the state may be viewed --- site/modules/socket.js | 56 +++++++++++++++++++++++++++++++++++++++++- site/modules/whist.js | 3 +-- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/site/modules/socket.js b/site/modules/socket.js index d0bd1de..5edded5 100644 --- a/site/modules/socket.js +++ b/site/modules/socket.js @@ -18,6 +18,9 @@ export class Socket { this.socket.onmessage = (msg) => this.onmessage(msg); this.socket.onclose = () => this.onclose(); this.state = "Connecting"; + + this.scheduled_actions = []; + this.schedule_timeout = null; } onopen() { @@ -97,7 +100,7 @@ export class Socket { break; case "TakeActionSuccess": case "NewAction": - this.game.take_action(message.action); + this.schedule_action(this.game, message.action); break; case "LogoutSuccess": delete this.auth; @@ -117,6 +120,57 @@ export class Socket { } } + pause_time_for_action(action) { + switch(action) { + case "Join": + case "Leave": + case "KnockedOut": + case "NextToDeal": + case "ChooseTrumps": + case "EndDeal": + case "WinGame": + return 0; + case "ReceiveCard": + case "CommunityCard": + return 200; + case "CutCard": + return 1000; + case "PlayCard": + return 500; + case "WinTrick": + case "WinCall": + case "WinHand": + return 1000; + case "PostBlind": + case "Fold": + case "TimeoutFold": + return 0; + case "Bet": + return 500; + default: + return 0; + } + } + + set_schedule_timeout(timeout) { + this.schedule_timeout = setTimeout(() => { + if (this.scheduled_actions.length == 0) { + this.schedule_timeout = null; + } else { + const {game, user_action} = this.scheduled_actions.shift(); + game.take_action(user_action); + this.set_schedule_timeout(this.pause_time_for_action(user_action.action.action)); + } + }, timeout); + } + + schedule_action(game, user_action) { + this.scheduled_actions.push({game, user_action}); + if (this.schedule_timeout === null) { + this.set_schedule_timeout(0); + } + } + hide_login() { document.getElementById("login-background").classList.add("hidden"); } diff --git a/site/modules/whist.js b/site/modules/whist.js index 3d731df..6a20745 100644 --- a/site/modules/whist.js +++ b/site/modules/whist.js @@ -7,7 +7,6 @@ export class KnockOutWhist { constructor(container, summary, actions, username, send) { this.container = container; this.summary = summary; - this.actions = actions; this.username = username; this.send = send; this.seats = new Map(); @@ -74,7 +73,7 @@ export class KnockOutWhist { } this.container.append(this.svg); - for (const user_action of this.actions) { + for (const user_action of actions) { this.take_action(user_action); } -- 2.34.1