add timeouts after certain actions to pause the game so the state may be viewed
authorGeoffrey Allott <geoffrey@allott.email>
Tue, 16 Mar 2021 23:01:19 +0000 (23:01 +0000)
committerGeoffrey Allott <geoffrey@allott.email>
Tue, 16 Mar 2021 23:01:19 +0000 (23:01 +0000)
site/modules/socket.js
site/modules/whist.js

index d0bd1defcf16dc28f347aced7a6c19f999fca841..5edded5cf78c8a5579eafbd5e062e12cbee02c87 100644 (file)
@@ -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");
     }
index 3d731df8f3116d31bc4621bf2de041e267ba29fc..6a207458e1d54010a08a6144db7248e00e5830e5 100644 (file)
@@ -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);
         }