Fix reactive with new system
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import type { GameObjectRegister } from "."
|
||||
import type { Effect } from "./Effect"
|
||||
import type { GameObjectNetcode } from "./GameObject"
|
||||
|
||||
enum CardRole {
|
||||
PERSONAL = "personal",
|
||||
MARKET = "market",
|
||||
FIRE_GEM = "fire_gem"
|
||||
}
|
||||
|
||||
enum CardType {
|
||||
HERO = "hero",
|
||||
HERO_ABILITY = "hero_ability",
|
||||
CURRENCY = "currency",
|
||||
WEAPON = "weapon",
|
||||
CHAMPION = "champion",
|
||||
ACTION = "action",
|
||||
ITEM = "item"
|
||||
}
|
||||
|
||||
enum CardFaction {
|
||||
BASE = "base",
|
||||
|
||||
BLUE = "blue",
|
||||
RED = "red",
|
||||
GREEN = "green",
|
||||
YELLOW = "yellow"
|
||||
}
|
||||
|
||||
export interface CardNetcode extends GameObjectNetcode {
|
||||
card_id: number
|
||||
sprite: string
|
||||
name: string
|
||||
cost?: number
|
||||
role: CardRole
|
||||
card_type: CardType
|
||||
faction: CardFaction
|
||||
effects: Array<string>
|
||||
defense?: number
|
||||
guard?: boolean
|
||||
}
|
||||
|
||||
export type Card = Omit<CardNetcode, 'effects'> & {
|
||||
effects: Array<Effect>
|
||||
health_as_champion?: number
|
||||
}
|
||||
export function create_card(input: CardNetcode, register: GameObjectRegister) {
|
||||
const card = new Proxy<any>(input, {
|
||||
get(target: CardNetcode, prop, reciever) {
|
||||
if (prop == "effects") {
|
||||
return target.effects.map((e) => register.effects[e])
|
||||
}
|
||||
if (prop == "health_as_champion") {
|
||||
return Object.values(register.turns).find(e => e.champions_health.has(card))?.champions_health.get(card)
|
||||
}
|
||||
return Reflect.get(target, prop, reciever);
|
||||
},
|
||||
set(target, prop, val, receiver) {
|
||||
return Reflect.set(target, prop, val, receiver);
|
||||
}
|
||||
}) as Card
|
||||
return card
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import { GameObjectRegister } from "."
|
||||
import type { GameObjectNetcode } from "./GameObject"
|
||||
|
||||
enum CardEffects {
|
||||
GOLD = "gold",
|
||||
DAMAGE = "damage",
|
||||
HEAL = "heal",
|
||||
PERPARE = "prepare",
|
||||
STUN = "stun",
|
||||
SACRIFICE = "sacrifice",
|
||||
DRAW = "draw",
|
||||
DRAW_AND_DISCARD = "draw_and_discard",
|
||||
DISCARD = "discard",
|
||||
RESTACK_DISCARDED_CHAMPION = "restack_discarded_champion",
|
||||
RESTACK_DISCARDED_CARD = "restack_discarded_card",
|
||||
STACK_NEXT_ACTION_BOUGHT = "stack_next_action_bought",
|
||||
STACK_NEXT_CARD_BOUGHT = "stack_next_card_bought",
|
||||
PLAY_NEXT_CARD_BOUGHT = "play_next_card_bought"
|
||||
}
|
||||
|
||||
enum EffectTimes {
|
||||
PER_CHAMPION = "per_champion",
|
||||
PER_OTHER_CHAMPION = "per_other_champion",
|
||||
PER_OTHER_GUARD = "per_other_guard",
|
||||
PER_CARD_OF_SAME_FACTION = "per_card_of_same_faction"
|
||||
}
|
||||
|
||||
enum EffectType {
|
||||
CHAMPION_ACTION = "champion_action",
|
||||
SUICIDE = "suicide",
|
||||
FACTION_COMBO = "faction_combo"
|
||||
}
|
||||
|
||||
export interface EffectNetcode extends GameObjectNetcode {
|
||||
effect: CardEffects
|
||||
amount: number
|
||||
sub_effects: Array<EffectNetcode>
|
||||
mutex: Array<string>
|
||||
effect_type?: EffectType
|
||||
times?: EffectTimes
|
||||
}
|
||||
|
||||
export type Effect = Omit<EffectNetcode, 'sub_effects' | 'mutex'> & {
|
||||
sub_effects: { [key: string]: Effect }
|
||||
mutex: { [key: string]: Effect }
|
||||
}
|
||||
|
||||
export function create_effect(input: EffectNetcode, register: GameObjectRegister) {
|
||||
return new Proxy<any>(input, {
|
||||
get(target, prop, reciever) {
|
||||
if (prop == "sub_effects") {
|
||||
return Object.fromEntries(Object.entries(register.effects).filter(([i, v]) => i in target.sub_effects))
|
||||
}
|
||||
if (prop == "mutex") {
|
||||
return Object.fromEntries(Object.entries(register.effects).filter(([i, v]) => i in target.mutex))
|
||||
}
|
||||
return Reflect.get(target, prop, reciever);
|
||||
},
|
||||
set(target, prop, val, receiver) {
|
||||
return Reflect.set(target, prop, val, receiver);
|
||||
}
|
||||
}) as Effect
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import { type GameObjectRegister } from "."
|
||||
import { type Team } from "./Team"
|
||||
import { type GameObjectNetcode } from "./GameObject"
|
||||
import type { Card } from "./Card"
|
||||
|
||||
export interface GameNetcode extends GameObjectNetcode {
|
||||
market_stack: Array<string>
|
||||
market: Array<string>
|
||||
gem_stack: Array<string>
|
||||
started: boolean
|
||||
current_turn: string
|
||||
}
|
||||
|
||||
|
||||
export type Game = Omit<GameNetcode, 'market_stack' | 'market' | 'gem_stack' | 'current_turn'> & {
|
||||
market_stack: Array<Card>
|
||||
market: Array<Card | null>
|
||||
gem_stack: Array<Card>
|
||||
current_turn: Team
|
||||
}
|
||||
export function create_game(input: GameNetcode, register: GameObjectRegister) {
|
||||
return new Proxy<any>(input, {
|
||||
get(target, prop, reciever) {
|
||||
if (prop == "market_stack") {
|
||||
return input.market_stack.map(e => register.cards.get(e))
|
||||
}
|
||||
if (prop == "market") {
|
||||
return input.market.map(e => register.cards.get(e))
|
||||
}
|
||||
if (prop == "gem_stack") {
|
||||
return input.gem_stack.map(e => register.cards.get(e))
|
||||
}
|
||||
if (prop == "current_turn") {
|
||||
return register.teams.get(input.current_turn)
|
||||
}
|
||||
return Reflect.get(target, prop, reciever);
|
||||
},
|
||||
set(target, prop, val, receiver) {
|
||||
return Reflect.set(target, prop, val, receiver);
|
||||
}
|
||||
}) as Game
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export type GameObjectNetcode = {
|
||||
uuid: string
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import { type GameObjectRegister } from "."
|
||||
import type { Card } from "./Card"
|
||||
import type { GameObjectNetcode } from "./GameObject"
|
||||
import { type Team } from "./Team"
|
||||
import { type Turn } from "./Turn"
|
||||
export interface PlayerNetcode extends GameObjectNetcode {
|
||||
stack_pile: Array<string>
|
||||
discard_pile: Array<string>
|
||||
hand: Array<string>
|
||||
board: Array<string>
|
||||
team: string
|
||||
username: string
|
||||
turn: string
|
||||
}
|
||||
|
||||
|
||||
export type Player = Omit<PlayerNetcode, 'discard_pile' | 'stack_pile' | 'hand' | 'board' | 'team' | 'turn'> & {
|
||||
stack_pile: Array<Card | null>
|
||||
discard_pile: Array<Card>
|
||||
hand: Array<Card | null>
|
||||
board: Array<Card>
|
||||
team: Team
|
||||
turn: Turn
|
||||
}
|
||||
export function create_player(input: PlayerNetcode, register: GameObjectRegister) {
|
||||
return new Proxy<any>(input, {
|
||||
get(target: PlayerNetcode, prop, reciever) {
|
||||
if (prop == "stack_pile") {
|
||||
return target.stack_pile.map(e => register.cards.get(e))
|
||||
}
|
||||
if (prop == "discard_pile") {
|
||||
return target.discard_pile.map(e => register.cards.get(e))
|
||||
}
|
||||
if (prop == "hand") {
|
||||
return target.hand.map(e => register.cards.get(e))
|
||||
}
|
||||
if (prop == "board") {
|
||||
return target.board.map(e => register.cards.get(e))
|
||||
}
|
||||
if (prop == "team") {
|
||||
return register.teams.get(target.team)
|
||||
}
|
||||
if (prop == "turn") {
|
||||
return register.turns.get(target.turn)
|
||||
}
|
||||
return Reflect.get(target, prop, reciever);
|
||||
},
|
||||
set(target, prop, val, receiver) {
|
||||
return Reflect.set(target, prop, val, receiver);
|
||||
}
|
||||
}) as Player
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import { type GameObjectRegister } from "."
|
||||
import type { GameObjectNetcode } from "./GameObject"
|
||||
import { type Player } from "./Player"
|
||||
|
||||
export interface TeamNetcode extends GameObjectNetcode {
|
||||
health: number
|
||||
color: string
|
||||
name: string
|
||||
}
|
||||
|
||||
export type Team = TeamNetcode & {
|
||||
players: Array<Player>
|
||||
}
|
||||
|
||||
export function create_team(input: TeamNetcode, register: GameObjectRegister) {
|
||||
const team = new Proxy<any>(input, {
|
||||
get(target: TeamNetcode, prop, reciever) {
|
||||
if (prop == "players") {
|
||||
return Object.values(register.players).filter((p) => p.team == team)
|
||||
}
|
||||
return Reflect.get(target, prop, reciever);
|
||||
},
|
||||
set(target, prop, val, receiver) {
|
||||
return Reflect.set(target, prop, val, receiver);
|
||||
}
|
||||
}) as Team
|
||||
|
||||
return team
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import { type GameObjectRegister } from "."
|
||||
import type { Card } from "./Card"
|
||||
import { type Effect } from "./Effect"
|
||||
import type { GameObjectNetcode } from "./GameObject"
|
||||
export interface TurnNetcode extends GameObjectNetcode {
|
||||
cards_locked: Array<string>
|
||||
effects_availables: Array<string>
|
||||
champions_health: { [key: string]: string }
|
||||
discard_markers: number
|
||||
damage_reserve: number
|
||||
heal_reserve: number
|
||||
gold_reserve: number
|
||||
}
|
||||
|
||||
|
||||
export type Turn = Omit<TurnNetcode, 'cards_locked' | 'effects_availables' | 'champions_health'> & {
|
||||
cards_locked: { [key: string]: Card }
|
||||
effects_availables: Array<Effect>
|
||||
champions_health: Map<Card, number>
|
||||
}
|
||||
export function create_turn(input: TurnNetcode, register: GameObjectRegister) {
|
||||
return new Proxy<any>(input, {
|
||||
get(target: TurnNetcode, prop, reciever) {
|
||||
if (prop == "card_locked") {
|
||||
return target.cards_locked.map(e => register.cards.get(e))
|
||||
}
|
||||
if (prop == "effects_availables") {
|
||||
return target.effects_availables.map(e => register.effects.get(e))
|
||||
}
|
||||
if (prop == "champions_health") {
|
||||
const healths = new Map()
|
||||
for (const c in target.champions_health) {
|
||||
healths.set(register.cards.get(c), target.champions_health[c])
|
||||
}
|
||||
return healths
|
||||
}
|
||||
return Reflect.get(target, prop, reciever);
|
||||
},
|
||||
set(target, prop, val, receiver) {
|
||||
console.log(isReactive(target))
|
||||
return Reflect.set(target, prop, val, receiver);
|
||||
}
|
||||
}) as Turn
|
||||
}
|
||||
@@ -0,0 +1,216 @@
|
||||
import type { RefSymbol } from "@vue/reactivity"
|
||||
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"
|
||||
|
||||
import { isReactive } from "vue"
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user