Feat : move code into pages for routing
This commit is contained in:
+61
-21
@@ -15,6 +15,7 @@ import { type Lobby, create_lobby, type LobbyNetcode } from "./Lobby";
|
||||
|
||||
import { type Ref } from "vue";
|
||||
|
||||
const socketTimeout = 1000
|
||||
let socket: WebSocket;
|
||||
|
||||
export class GameObjectRegister {
|
||||
@@ -25,6 +26,8 @@ export class GameObjectRegister {
|
||||
readonly turns: Map<string, Turn>;
|
||||
readonly games: Map<string, Game>;
|
||||
|
||||
readonly gameRoutingCallbacks: Map<string, [() => void, () => void]>;
|
||||
readonly loginCallbacks: Set<() => void>;
|
||||
context: "lobby" | "login" | "pregame" | "game" | "rules" = "login";
|
||||
private readonly objects: Map<string, GameObjectNetcode>;
|
||||
current_game: Ref<Game | null>;
|
||||
@@ -37,6 +40,9 @@ export class GameObjectRegister {
|
||||
clientMouseY: Ref<number>
|
||||
discordId: Ref<string>
|
||||
constructor() {
|
||||
this.gameRoutingCallbacks = new Map();
|
||||
this.loginCallbacks = new Set();
|
||||
|
||||
this.effects = reactive(new Map());
|
||||
this.cards = reactive(new Map());
|
||||
this.teams = reactive(new Map());
|
||||
@@ -117,6 +123,12 @@ export class GameObjectRegister {
|
||||
}
|
||||
setCurrentGame = (uuid: string) => {
|
||||
this.current_game.value = this.games.get(uuid) ?? null;
|
||||
this.gameRoutingCallbacks.get(uuid)?.[0]()
|
||||
this.gameRoutingCallbacks.delete(uuid)
|
||||
for (let [_key, value] of this.gameRoutingCallbacks) {
|
||||
value[1]()
|
||||
this.gameRoutingCallbacks.delete(_key)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -179,6 +191,8 @@ export const setup_websocket = () =>
|
||||
heron_session.value = data.data;
|
||||
} else if (data.data_type == "self_discord_id") {
|
||||
goStore.setDiscordId(data.data)
|
||||
goStore.loginCallbacks.forEach(c => c())
|
||||
goStore.loginCallbacks.clear()
|
||||
}
|
||||
} else {
|
||||
// console.log(data.type, data.data_type, data.data);
|
||||
@@ -188,24 +202,42 @@ export const setup_websocket = () =>
|
||||
|
||||
export async function heron_cookie_login(cookie: string) {
|
||||
await setup_websocket();
|
||||
socket.send(
|
||||
JSON.stringify({
|
||||
type: "cookie_login",
|
||||
data_type: "cookie",
|
||||
data: cookie,
|
||||
})
|
||||
);
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
goStore.loginCallbacks.add(resolve)
|
||||
socket.send(
|
||||
JSON.stringify({
|
||||
type: "cookie_login",
|
||||
data_type: "cookie",
|
||||
data: cookie,
|
||||
})
|
||||
);
|
||||
setTimeout(() => {
|
||||
goStore.loginCallbacks.delete(resolve);
|
||||
reject()
|
||||
}
|
||||
, socketTimeout)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
export async function oauth2_register_discord(code: string) {
|
||||
await setup_websocket();
|
||||
socket.send(
|
||||
JSON.stringify({
|
||||
type: "discord_login",
|
||||
data_type: "code",
|
||||
data: code,
|
||||
})
|
||||
);
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
goStore.loginCallbacks.add(resolve)
|
||||
|
||||
socket.send(
|
||||
JSON.stringify({
|
||||
type: "discord_login",
|
||||
data_type: "code",
|
||||
data: code,
|
||||
})
|
||||
);
|
||||
setTimeout(() => {
|
||||
goStore.loginCallbacks.delete(resolve);
|
||||
reject()
|
||||
}
|
||||
, socketTimeout)
|
||||
})
|
||||
}
|
||||
|
||||
export async function register(username: string) {
|
||||
@@ -364,12 +396,20 @@ export async function netcode_create_game() {
|
||||
|
||||
export async function join_game(game_uuid: string) {
|
||||
await setup_websocket();
|
||||
socket.send(
|
||||
JSON.stringify({
|
||||
type: "join",
|
||||
data_type: "game",
|
||||
data: game_uuid,
|
||||
})
|
||||
);
|
||||
return new Promise<void>(((resolve, reject) => {
|
||||
goStore.gameRoutingCallbacks.set(game_uuid, [resolve, reject])
|
||||
setTimeout(() => {
|
||||
const cb = goStore.gameRoutingCallbacks.get(game_uuid)
|
||||
if (!cb) { return }
|
||||
cb[1]()
|
||||
}, socketTimeout)
|
||||
socket.send(
|
||||
JSON.stringify({
|
||||
type: "join",
|
||||
data_type: "game",
|
||||
data: game_uuid,
|
||||
})
|
||||
);
|
||||
}))
|
||||
}
|
||||
export default goStore;
|
||||
|
||||
Reference in New Issue
Block a user