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;
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) {
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));
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);
}
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) {
switch (this.state) {
case "LoggedIn":
this.send({type: "Logout"});
+ window.localStorage.removeItem("auth");
break;
case "InLobby":
this.send({type: "LeaveLobby"});