This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
import { type GameObjectRegister } from "."
|
||||
import { type GameObjectNetcode } from "./GameObject"
|
||||
import type { Game } from "./Game"
|
||||
|
||||
export interface LobbyNetcode extends GameObjectNetcode {
|
||||
games: Array<string>
|
||||
}
|
||||
|
||||
|
||||
export type Lobby = LobbyNetcode & {
|
||||
games: Array<Game>
|
||||
}
|
||||
export function create_lobby(input: LobbyNetcode, register: GameObjectRegister) {
|
||||
return new Proxy<any>(input, {
|
||||
get(target: LobbyNetcode, prop, reciever) {
|
||||
// if (prop == "games") {
|
||||
// return target.games.map(e => register.games.get(e))
|
||||
// }
|
||||
return Reflect.get(target, prop, reciever);
|
||||
},
|
||||
set(target, prop, val, receiver) {
|
||||
return Reflect.set(target, prop, val, receiver);
|
||||
}
|
||||
}) as Lobby
|
||||
}
|
||||
+46
-8
@@ -11,6 +11,7 @@ import type { GameObjectNetcode } from "./GameObject";
|
||||
import { type Player, create_player, type PlayerNetcode } from "./Player";
|
||||
import { type Team, create_team, type TeamNetcode } from "./Team";
|
||||
import { type Turn, create_turn } from "./Turn";
|
||||
import { type Lobby, create_lobby, type LobbyNetcode } from "./Lobby";
|
||||
|
||||
import { type Ref } from "vue";
|
||||
|
||||
@@ -22,10 +23,12 @@ export class GameObjectRegister {
|
||||
readonly teams: Map<string, Team>;
|
||||
readonly players: Map<string, Player>;
|
||||
readonly turns: Map<string, Turn>;
|
||||
readonly games: Map<string, Game>
|
||||
|
||||
context: "login" | "pregame" | "game" = "login";
|
||||
context: "lobby" | "login" | "pregame" | "game" = "login";
|
||||
private readonly objects: Map<string, GameObjectNetcode>;
|
||||
game: Ref<Game | null>;
|
||||
current_game: Ref<Game | null>
|
||||
lobby: Ref<Lobby | null>;
|
||||
self: Ref<Player | null>;
|
||||
constructor() {
|
||||
this.effects = reactive(new Map());
|
||||
@@ -33,14 +36,18 @@ export class GameObjectRegister {
|
||||
this.teams = reactive(new Map());
|
||||
this.players = reactive(new Map());
|
||||
this.turns = reactive(new Map());
|
||||
this.games = reactive(new Map())
|
||||
|
||||
this.objects = reactive(new Map());
|
||||
|
||||
this.game = ref(null);
|
||||
this.current_game = ref(null)
|
||||
this.lobby = ref(null);
|
||||
this.self = ref(null);
|
||||
}
|
||||
|
||||
update_object(obj: GameObjectNetcode) {
|
||||
console.log(obj)
|
||||
console.log(obj.uuid)
|
||||
console.log(this.objects.get(obj.uuid))
|
||||
Object.assign(this.objects.get(obj.uuid) as GameNetcode, obj);
|
||||
}
|
||||
|
||||
@@ -66,19 +73,28 @@ export class GameObjectRegister {
|
||||
this.objects.set(data.uuid, team);
|
||||
},
|
||||
game: (data: GameNetcode) => {
|
||||
this.game.value = create_game(data, this);
|
||||
this.objects.set(data.uuid, this.game.value);
|
||||
const game = create_game(data, this);
|
||||
this.games.set(data.uuid, game);
|
||||
this.objects.set(data.uuid, game);
|
||||
},
|
||||
turn: (data: any) => {
|
||||
const turn = create_turn(data, this);
|
||||
this.turns.set(data.uuid, turn);
|
||||
this.objects.set(data.uuid, turn);
|
||||
},
|
||||
lobby: (data: LobbyNetcode) => {
|
||||
this.lobby.value = create_lobby(data, this);
|
||||
this.objects.set(data.uuid, this.lobby.value);
|
||||
},
|
||||
} as { [id: string]: (data: any) => any };
|
||||
|
||||
set_self = (uuid: string) => {
|
||||
this.self.value = this.players.get(uuid) ?? null;
|
||||
};
|
||||
|
||||
set_current_game = (uuid: string) => {
|
||||
this.current_game.value = this.games.get(uuid) ?? null;
|
||||
};
|
||||
}
|
||||
|
||||
const goStore = reactive(new GameObjectRegister());
|
||||
@@ -130,8 +146,13 @@ export const setup_websocket = () =>
|
||||
} else if (data.type === "context") {
|
||||
goStore.context = data.data;
|
||||
// console.log(data.data);
|
||||
} else if (data.type === "set" && data.data_type == "self") {
|
||||
goStore.set_self(data.data);
|
||||
} else if (data.type === "set") {
|
||||
if(data.data_type == "self"){
|
||||
goStore.set_self(data.data);
|
||||
} else if (data.data_type == "current_game"){
|
||||
goStore.set_current_game(data.data)
|
||||
}
|
||||
|
||||
} else {
|
||||
// console.log(data.type, data.data_type, data.data);
|
||||
}
|
||||
@@ -292,4 +313,21 @@ export async function damage_champion(target: Card) {
|
||||
);
|
||||
}
|
||||
|
||||
export async function netcode_create_game(){
|
||||
await setup_websocket()
|
||||
socket.send(JSON.stringify({
|
||||
type: "create",
|
||||
data_type: "game",
|
||||
data: "bite"
|
||||
}))
|
||||
}
|
||||
|
||||
export async function join_game(game_uuid: string){
|
||||
await setup_websocket()
|
||||
socket.send(JSON.stringify({
|
||||
type: "join",
|
||||
data_type: "game",
|
||||
data: game_uuid
|
||||
}))
|
||||
}
|
||||
export default goStore;
|
||||
|
||||
Reference in New Issue
Block a user