Files
front/netcode/index.ts
T
2024-04-25 20:54:40 +02:00

213 lines
5.8 KiB
TypeScript

import { type Card, create_card, type CardNetcode } from "./Card"
import { type Effect, create_effect, type EffectNetcode } from "./Effect"
import { type Game, create_game, type GameNetcode } from "./Game"
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"
export const socket = new WebSocket("ws://127.0.0.1:8765")
export class GameObjectRegister {
readonly effects: Map<string, Effect>
readonly cards: Map<string, Card>
readonly teams: Map<string, Team>
readonly players: Map<string, Player>
readonly turns: Map<string, Turn>
context: 'login' | 'pregame' | 'game' = "login"
private readonly objects: Map<string, GameObjectNetcode>
game?: Game
self?: Player
constructor() {
this.effects = reactive(new Map())
this.cards = reactive(new Map())
this.teams = reactive(new Map())
this.players = reactive(new Map())
this.turns = reactive(new Map())
this.objects = reactive(new Map())
}
update_object(obj: GameObjectNetcode) {
Object.assign(this.objects.get(obj.uuid) as GameNetcode, obj)
}
createObject = {
"effect": (data: EffectNetcode) => {
const effect = create_effect(data, this)
this.effects.set(data.uuid, effect)
this.objects.set(data.uuid, data)
},
"card": (data: CardNetcode) => {
const card = create_card(data, this)
this.cards.set(data.uuid, card)
this.objects.set(data.uuid, card)
},
"player": (data: PlayerNetcode) => {
const player = create_player(data, this)
this.players.set(data.uuid, player)
this.objects.set(data.uuid, player)
},
"team": (data: TeamNetcode) => {
const team = create_team(data, this)
this.teams.set(data.uuid, team)
this.objects.set(data.uuid, team)
},
"game": (data: GameNetcode) => {
this.game = create_game(data, this)
this.objects.set(data.uuid, this.game)
},
"turn": (data: any) => {
const turn = create_turn(data, this)
this.turns.set(data.uuid, turn)
this.objects.set(data.uuid, turn)
}
} as { [id: string]: (data: any) => any }
set_self(uuid: string) {
this.self = this.players.get(uuid)
}
}
const goStore = reactive(new GameObjectRegister())
socket.onopen = function (e) {
console.log("[open] Connection established");
};
socket.onclose = function (event) {
if (event.wasClean) {
console.warn(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
} else {
// e.g. server process killed or network down
// event.code is usually 1006 in this case
console.error('[close] Connection died');
}
};
socket.onerror = function (error) {
console.error(error);
};
socket.onmessage = function (event) {
const data = JSON.parse(event.data)
if (data.type == "create" && data.data_type == "object") {
if (!(data.data.object_type in goStore.createObject)) {
throw new Error(`Object ${data.data.object_type} couldn't be created`)
}
goStore.createObject[data.data.object_type](data.data)
} else if (data.type == "update" && data.data_type == "object") {
goStore.update_object(data.data)
} 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 {
console.log(data.type, data.data_type, data.data);
}
};
export async function register(username: string) {
socket.send(JSON.stringify({
type: "register",
data_type: "username",
data: username
}))
}
export async function login(username: string) {
socket.send(JSON.stringify({
type: "login",
data_type: "username",
data: username
}))
}
export async function setReady(value: boolean) {
socket.send(JSON.stringify({
type: "ready",
data_type: "ready_state",
data: value
}))
}
export async function buy(card: Card) {
socket.send(JSON.stringify({
type: "action",
data_type: "buy",
data: card.uuid
}))
}
export async function endTurn() {
socket.send(JSON.stringify({
type: "action",
data_type: "end_turn",
data: true
}))
}
export function playAllCards(cards: Card[]) {
cards.forEach((c: any) => {
playCard(c)
});
}
export function playCard(card: Card) {
console.log(card.uuid)
socket.send(JSON.stringify({
type: "action",
data_type: "play_card",
data: card.uuid
}))
}
export function lockCard(card: Card) {
socket.send(JSON.stringify({
type: "action",
data_type: "lock_card",
data: card.uuid
}))
}
export function useEffect(effect: Effect, target?: Effect | Card | Player | Team) {
console.log({ effect_id: effect.uuid, target: target?.uuid })
socket.send(JSON.stringify({
type: "action",
data_type: "use_effect",
data: { effect_id: effect.uuid, target: target?.uuid }
}))
}
export function discard(card: Card) {
socket.send(JSON.stringify({
type: "action",
data_type: "discard",
data: card.uuid
}))
}
h
export function heal() {
socket.send(JSON.stringify({
type: "action",
data_type: "heal",
data: 1
}))
}
export function damage(target: Team) {
socket.send(JSON.stringify({
type: "action",
data_type: "damage",
data: target.uuid
}))
}
export default goStore