From: Geoffrey Allott Date: Wed, 24 May 2023 21:23:45 +0000 (+0100) Subject: save auth and game when refreshing the page X-Git-Url: https://git.pointlesshacks.com/?a=commitdiff_plain;h=d84f5b4e060efe923989809ca806cc790e77d86b;p=pokerwave.git save auth and game when refreshing the page --- diff --git a/site/modules/auth.js b/site/modules/auth.js index b5f2323..a2c4411 100644 --- a/site/modules/auth.js +++ b/site/modules/auth.js @@ -29,4 +29,16 @@ export class Auth { signature: this.password }; } + + toString() { + return JSON.stringify({ + "username": this.username, + "password": this.password, + }); + } + + static fromString(string) { + const auth = JSON.parse(string); + return new Auth(auth.username, auth.password); + } } diff --git a/site/modules/socket.js b/site/modules/socket.js index 3a77a09..e640449 100644 --- a/site/modules/socket.js +++ b/site/modules/socket.js @@ -7,6 +7,14 @@ import { Chatroom } from "./chatroom.js"; import { KnockOutWhist } from "./whist.js"; import { TexasHoldEm } from "./poker.js"; +function create_search_params(key_value_pairs) { + const params = new URLSearchParams(); + for (const [key, value] of key_value_pairs) { + params.append(key, value); + } + return params.toString(); +} + export class Socket { constructor(container, login_all_sockets) { this.container = container; @@ -27,6 +35,12 @@ export class Socket { onopen() { console.log("WebSocket connected"); this.state = "Connected"; + + const auth = window.localStorage.getItem("auth"); + if (auth !== null) { + console.log("Logging in from saved credentials"); + this.login(Auth.fromString(auth)); + } } send(object) { @@ -68,6 +82,18 @@ export class Socket { this.state = "Connected"; break; case "LoginSuccess": + window.localStorage.setItem("auth", this.auth.toString()); + this.hide_login(); + this.game = new MainMenu(this.container, message => this.send(message)); + this.container.append(this.close_button()); + this.state = "LoggedIn"; + const params = new URLSearchParams(document.location.hash.substr(1)); + if (params.has("game")) { + this.send({type: "JoinGame", id: Number(params.get("game"))}); + } else if (params.has("lobby")) { + this.send({type: "JoinLobby", filter: params.get("lobby")}); + } + break; case "LeaveLobbySuccess": this.hide_login(); this.game = new MainMenu(this.container, message => this.send(message)); @@ -83,6 +109,7 @@ export class Socket { this.game = new GameList(this.container, message.filter, message.games, message => this.send(message)) this.container.append(this.close_button()); this.state = "InLobby"; + document.location.hash = create_search_params([["lobby", message.filter]]); break; case "NewGame": this.game.new_game(message.game); @@ -124,6 +151,20 @@ export class Socket { } break; } + switch (this.state) { + case "Connected": + case "LoginAuthResponseSent": + break; + case "InLobby": + document.location.hash = create_search_params([["lobby", this.last_filter]]); + break; + case "InGame": + document.location.hash = create_search_params([["game", this.game.summary.id]]); + break; + default: + document.location.hash = ""; + break; + } } pause_time_for_action(action) { @@ -235,6 +276,7 @@ export class Socket { switch (this.state) { case "LoggedIn": this.send({type: "Logout"}); + window.localStorage.removeItem("auth"); break; case "InLobby": this.send({type: "LeaveLobby"});