refactor typescript wiring

This commit is contained in:
seavor 2026-04-15 15:46:17 -05:00
parent cea9ae62d8
commit c62c336a11
286 changed files with 2999 additions and 3053 deletions

View file

@ -1,18 +1,10 @@
import { ProtoInit } from 'types';
import type { ServerInfo_Card } from 'generated/proto/serverinfo_card_pb';
import type { ServerInfo_Counter } from 'generated/proto/serverinfo_counter_pb';
import type { ServerInfo_Arrow } from 'generated/proto/serverinfo_arrow_pb';
import type { ServerInfo_PlayerProperties } from 'generated/proto/serverinfo_playerproperties_pb';
import type { MessageInitShape } from '@bufbuild/protobuf';
import { Data } from '@app/types';
import { create } from '@bufbuild/protobuf';
import { ServerInfo_CardSchema } from 'generated/proto/serverinfo_card_pb';
import { ServerInfo_CounterSchema } from 'generated/proto/serverinfo_counter_pb';
import { colorSchema } from 'generated/proto/color_pb';
import { ServerInfo_ArrowSchema } from 'generated/proto/serverinfo_arrow_pb';
import { ServerInfo_PlayerPropertiesSchema } from 'generated/proto/serverinfo_playerproperties_pb';
import { GameEntry, GamesState, PlayerEntry, ZoneEntry } from '../game.interfaces';
export function makeCard(overrides: ProtoInit<ServerInfo_Card> = {}): ServerInfo_Card {
return create(ServerInfo_CardSchema, {
export function makeCard(overrides: MessageInitShape<typeof Data.ServerInfo_CardSchema> = {}): Data.ServerInfo_Card {
return create(Data.ServerInfo_CardSchema, {
id: 1,
name: 'Test Card',
x: 0,
@ -34,19 +26,19 @@ export function makeCard(overrides: ProtoInit<ServerInfo_Card> = {}): ServerInfo
});
}
export function makeCounter(overrides: ProtoInit<ServerInfo_Counter> = {}): ServerInfo_Counter {
return create(ServerInfo_CounterSchema, {
export function makeCounter(overrides: MessageInitShape<typeof Data.ServerInfo_CounterSchema> = {}): Data.ServerInfo_Counter {
return create(Data.ServerInfo_CounterSchema, {
id: 1,
name: 'Life',
counterColor: create(colorSchema, { r: 0, g: 0, b: 0, a: 255 }),
counterColor: create(Data.colorSchema, { r: 0, g: 0, b: 0, a: 255 }),
radius: 1,
count: 20,
...overrides,
});
}
export function makeArrow(overrides: ProtoInit<ServerInfo_Arrow> = {}): ServerInfo_Arrow {
return create(ServerInfo_ArrowSchema, {
export function makeArrow(overrides: MessageInitShape<typeof Data.ServerInfo_ArrowSchema> = {}): Data.ServerInfo_Arrow {
return create(Data.ServerInfo_ArrowSchema, {
id: 1,
startPlayerId: 1,
startZone: 'table',
@ -54,7 +46,7 @@ export function makeArrow(overrides: ProtoInit<ServerInfo_Arrow> = {}): ServerIn
targetPlayerId: 1,
targetZone: 'table',
targetCardId: 2,
arrowColor: create(colorSchema, { r: 255, g: 0, b: 0, a: 255 }),
arrowColor: create(Data.colorSchema, { r: 255, g: 0, b: 0, a: 255 }),
...overrides,
});
}
@ -72,8 +64,10 @@ export function makeZoneEntry(overrides: Partial<ZoneEntry> = {}): ZoneEntry {
};
}
export function makePlayerProperties(overrides: ProtoInit<ServerInfo_PlayerProperties> = {}): ServerInfo_PlayerProperties {
return create(ServerInfo_PlayerPropertiesSchema, {
export function makePlayerProperties(
overrides: MessageInitShape<typeof Data.ServerInfo_PlayerPropertiesSchema> = {},
): Data.ServerInfo_PlayerProperties {
return create(Data.ServerInfo_PlayerPropertiesSchema, {
playerId: 1,
spectator: false,
conceded: false,

View file

@ -1,32 +1,13 @@
import { create } from '@bufbuild/protobuf';
import { Data } from '@app/types';
import { Actions } from './game.actions';
import { Types } from './game.types';
import {
makeArrow,
makeCard,
makeCounter,
makeGameEntry,
makePlayerProperties,
} from './__mocks__/fixtures';
import { Event_GameStateChangedSchema } from 'generated/proto/event_game_state_changed_pb';
import { Event_MoveCardSchema } from 'generated/proto/event_move_card_pb';
import { Event_FlipCardSchema } from 'generated/proto/event_flip_card_pb';
import { Event_DestroyCardSchema } from 'generated/proto/event_destroy_card_pb';
import { Event_AttachCardSchema } from 'generated/proto/event_attach_card_pb';
import { Event_CreateTokenSchema } from 'generated/proto/event_create_token_pb';
import { Event_SetCardAttrSchema } from 'generated/proto/event_set_card_attr_pb';
import { Event_SetCardCounterSchema } from 'generated/proto/event_set_card_counter_pb';
import { Event_CreateArrowSchema } from 'generated/proto/event_create_arrow_pb';
import { Event_DeleteArrowSchema } from 'generated/proto/event_delete_arrow_pb';
import { Event_CreateCounterSchema } from 'generated/proto/event_create_counter_pb';
import { Event_SetCounterSchema } from 'generated/proto/event_set_counter_pb';
import { Event_DelCounterSchema } from 'generated/proto/event_del_counter_pb';
import { Event_DrawCardsSchema } from 'generated/proto/event_draw_cards_pb';
import { Event_RevealCardsSchema } from 'generated/proto/event_reveal_cards_pb';
import { Event_ShuffleSchema } from 'generated/proto/event_shuffle_pb';
import { Event_RollDieSchema } from 'generated/proto/event_roll_die_pb';
import { Event_DumpZoneSchema } from 'generated/proto/event_dump_zone_pb';
import { Event_ChangeZonePropertiesSchema } from 'generated/proto/event_change_zone_properties_pb';
describe('Actions', () => {
it('clearStore', () => {
@ -34,8 +15,8 @@ describe('Actions', () => {
});
it('gameJoined', () => {
const entry = makeGameEntry();
expect(Actions.gameJoined(1, entry)).toEqual({ type: Types.GAME_JOINED, gameId: 1, gameEntry: entry });
const data = create(Data.Event_GameJoinedSchema, { hostId: 1, playerId: 2 });
expect(Actions.gameJoined(data)).toEqual({ type: Types.GAME_JOINED, data });
});
it('gameLeft', () => {
@ -51,7 +32,7 @@ describe('Actions', () => {
});
it('gameStateChanged', () => {
const data = create(Event_GameStateChangedSchema, {
const data = create(Data.Event_GameStateChangedSchema, {
playerList: [], gameStarted: true, activePlayerId: 1, activePhase: 0, secondsElapsed: 0
});
expect(Actions.gameStateChanged(1, data)).toEqual({ type: Types.GAME_STATE_CHANGED, gameId: 1, data });
@ -81,85 +62,85 @@ describe('Actions', () => {
});
it('cardMoved', () => {
const data = create(Event_MoveCardSchema, { cardId: 1 });
const data = create(Data.Event_MoveCardSchema, { cardId: 1 });
expect(Actions.cardMoved(1, 2, data)).toEqual({ type: Types.CARD_MOVED, gameId: 1, playerId: 2, data });
});
it('cardFlipped', () => {
const data = create(Event_FlipCardSchema, { cardId: 1 });
const data = create(Data.Event_FlipCardSchema, { cardId: 1 });
expect(Actions.cardFlipped(1, 2, data)).toEqual({ type: Types.CARD_FLIPPED, gameId: 1, playerId: 2, data });
});
it('cardDestroyed', () => {
const data = create(Event_DestroyCardSchema, { cardId: 1 });
const data = create(Data.Event_DestroyCardSchema, { cardId: 1 });
expect(Actions.cardDestroyed(1, 2, data)).toEqual({ type: Types.CARD_DESTROYED, gameId: 1, playerId: 2, data });
});
it('cardAttached', () => {
const data = create(Event_AttachCardSchema, { cardId: 1 });
const data = create(Data.Event_AttachCardSchema, { cardId: 1 });
expect(Actions.cardAttached(1, 2, data)).toEqual({ type: Types.CARD_ATTACHED, gameId: 1, playerId: 2, data });
});
it('tokenCreated', () => {
const data = create(Event_CreateTokenSchema, { cardId: 1 });
const data = create(Data.Event_CreateTokenSchema, { cardId: 1 });
expect(Actions.tokenCreated(1, 2, data)).toEqual({ type: Types.TOKEN_CREATED, gameId: 1, playerId: 2, data });
});
it('cardAttrChanged', () => {
const data = create(Event_SetCardAttrSchema, { cardId: 1 });
const data = create(Data.Event_SetCardAttrSchema, { cardId: 1 });
expect(Actions.cardAttrChanged(1, 2, data)).toEqual({ type: Types.CARD_ATTR_CHANGED, gameId: 1, playerId: 2, data });
});
it('cardCounterChanged', () => {
const data = create(Event_SetCardCounterSchema, { cardId: 1 });
const data = create(Data.Event_SetCardCounterSchema, { cardId: 1 });
expect(Actions.cardCounterChanged(1, 2, data)).toEqual({ type: Types.CARD_COUNTER_CHANGED, gameId: 1, playerId: 2, data });
});
it('arrowCreated', () => {
const arrow = makeArrow();
const data = create(Event_CreateArrowSchema, { arrowInfo: arrow });
const data = create(Data.Event_CreateArrowSchema, { arrowInfo: arrow });
expect(Actions.arrowCreated(1, 2, data)).toEqual({ type: Types.ARROW_CREATED, gameId: 1, playerId: 2, data });
});
it('arrowDeleted', () => {
const data = create(Event_DeleteArrowSchema, { arrowId: 3 });
const data = create(Data.Event_DeleteArrowSchema, { arrowId: 3 });
expect(Actions.arrowDeleted(1, 2, data)).toEqual({ type: Types.ARROW_DELETED, gameId: 1, playerId: 2, data });
});
it('counterCreated', () => {
const counter = makeCounter();
const data = create(Event_CreateCounterSchema, { counterInfo: counter });
const data = create(Data.Event_CreateCounterSchema, { counterInfo: counter });
expect(Actions.counterCreated(1, 2, data)).toEqual({ type: Types.COUNTER_CREATED, gameId: 1, playerId: 2, data });
});
it('counterSet', () => {
const data = create(Event_SetCounterSchema, { counterId: 1, value: 10 });
const data = create(Data.Event_SetCounterSchema, { counterId: 1, value: 10 });
expect(Actions.counterSet(1, 2, data)).toEqual({ type: Types.COUNTER_SET, gameId: 1, playerId: 2, data });
});
it('counterDeleted', () => {
const data = create(Event_DelCounterSchema, { counterId: 1 });
const data = create(Data.Event_DelCounterSchema, { counterId: 1 });
expect(Actions.counterDeleted(1, 2, data)).toEqual({ type: Types.COUNTER_DELETED, gameId: 1, playerId: 2, data });
});
it('cardsDrawn', () => {
const card = makeCard();
const data = create(Event_DrawCardsSchema, { number: 2, cards: [card] });
const data = create(Data.Event_DrawCardsSchema, { number: 2, cards: [card] });
expect(Actions.cardsDrawn(1, 2, data)).toEqual({ type: Types.CARDS_DRAWN, gameId: 1, playerId: 2, data });
});
it('cardsRevealed', () => {
const data = create(Event_RevealCardsSchema, { zoneName: 'hand', cards: [] });
const data = create(Data.Event_RevealCardsSchema, { zoneName: 'hand', cards: [] });
expect(Actions.cardsRevealed(1, 2, data)).toEqual({ type: Types.CARDS_REVEALED, gameId: 1, playerId: 2, data });
});
it('zoneShuffled', () => {
const data = create(Event_ShuffleSchema, { zoneName: 'deck', start: 0, end: 39 });
const data = create(Data.Event_ShuffleSchema, { zoneName: 'deck', start: 0, end: 39 });
expect(Actions.zoneShuffled(1, 2, data)).toEqual({ type: Types.ZONE_SHUFFLED, gameId: 1, playerId: 2, data });
});
it('dieRolled', () => {
const data = create(Event_RollDieSchema, { sides: 6, value: 4, values: [4] });
const data = create(Data.Event_RollDieSchema, { sides: 6, value: 4, values: [4] });
expect(Actions.dieRolled(1, 2, data)).toEqual({ type: Types.DIE_ROLLED, gameId: 1, playerId: 2, data });
});
@ -176,12 +157,12 @@ describe('Actions', () => {
});
it('zoneDumped', () => {
const data = create(Event_DumpZoneSchema, { zoneOwnerId: 1, zoneName: 'hand', numberCards: 3, isReversed: false });
const data = create(Data.Event_DumpZoneSchema, { zoneOwnerId: 1, zoneName: 'hand', numberCards: 3, isReversed: false });
expect(Actions.zoneDumped(1, 2, data)).toEqual({ type: Types.ZONE_DUMPED, gameId: 1, playerId: 2, data });
});
it('zonePropertiesChanged', () => {
const data = create(Event_ChangeZonePropertiesSchema, { zoneName: 'deck', alwaysRevealTopCard: true, alwaysLookAtTopCard: false });
const data = create(Data.Event_ChangeZonePropertiesSchema, { zoneName: 'deck', alwaysRevealTopCard: true, alwaysLookAtTopCard: false });
expect(Actions.zonePropertiesChanged(1, 2, data)).toEqual({
type: Types.ZONE_PROPERTIES_CHANGED,
gameId: 1,

View file

@ -1,24 +1,4 @@
import type { Event_AttachCard } from 'generated/proto/event_attach_card_pb';
import type { Event_ChangeZoneProperties } from 'generated/proto/event_change_zone_properties_pb';
import type { Event_CreateArrow } from 'generated/proto/event_create_arrow_pb';
import type { Event_CreateCounter } from 'generated/proto/event_create_counter_pb';
import type { Event_CreateToken } from 'generated/proto/event_create_token_pb';
import type { Event_DelCounter } from 'generated/proto/event_del_counter_pb';
import type { Event_DeleteArrow } from 'generated/proto/event_delete_arrow_pb';
import type { Event_DestroyCard } from 'generated/proto/event_destroy_card_pb';
import type { Event_DrawCards } from 'generated/proto/event_draw_cards_pb';
import type { Event_DumpZone } from 'generated/proto/event_dump_zone_pb';
import type { Event_FlipCard } from 'generated/proto/event_flip_card_pb';
import type { Event_GameStateChanged } from 'generated/proto/event_game_state_changed_pb';
import type { Event_MoveCard } from 'generated/proto/event_move_card_pb';
import type { ServerInfo_PlayerProperties } from 'generated/proto/serverinfo_playerproperties_pb';
import type { Event_RevealCards } from 'generated/proto/event_reveal_cards_pb';
import type { Event_RollDie } from 'generated/proto/event_roll_die_pb';
import type { Event_SetCardAttr } from 'generated/proto/event_set_card_attr_pb';
import type { Event_SetCardCounter } from 'generated/proto/event_set_card_counter_pb';
import type { Event_SetCounter } from 'generated/proto/event_set_counter_pb';
import type { Event_Shuffle } from 'generated/proto/event_shuffle_pb';
import { GameEntry } from './game.interfaces';
import type { Data } from '@app/types';
import { Types } from './game.types';
export const Actions = {
@ -26,10 +6,9 @@ export const Actions = {
type: Types.CLEAR_STORE,
}),
gameJoined: (gameId: number, gameEntry: GameEntry) => ({
gameJoined: (data: Data.Event_GameJoined) => ({
type: Types.GAME_JOINED,
gameId,
gameEntry,
data,
}),
gameLeft: (gameId: number) => ({
@ -48,13 +27,13 @@ export const Actions = {
hostId,
}),
gameStateChanged: (gameId: number, data: Event_GameStateChanged) => ({
gameStateChanged: (gameId: number, data: Data.Event_GameStateChanged) => ({
type: Types.GAME_STATE_CHANGED,
gameId,
data,
}),
playerJoined: (gameId: number, playerProperties: ServerInfo_PlayerProperties) => ({
playerJoined: (gameId: number, playerProperties: Data.ServerInfo_PlayerProperties) => ({
type: Types.PLAYER_JOINED,
gameId,
playerProperties,
@ -67,7 +46,7 @@ export const Actions = {
reason,
}),
playerPropertiesChanged: (gameId: number, playerId: number, properties: ServerInfo_PlayerProperties) => ({
playerPropertiesChanged: (gameId: number, playerId: number, properties: Data.ServerInfo_PlayerProperties) => ({
type: Types.PLAYER_PROPERTIES_CHANGED,
gameId,
playerId,
@ -79,112 +58,112 @@ export const Actions = {
gameId,
}),
cardMoved: (gameId: number, playerId: number, data: Event_MoveCard) => ({
cardMoved: (gameId: number, playerId: number, data: Data.Event_MoveCard) => ({
type: Types.CARD_MOVED,
gameId,
playerId,
data,
}),
cardFlipped: (gameId: number, playerId: number, data: Event_FlipCard) => ({
cardFlipped: (gameId: number, playerId: number, data: Data.Event_FlipCard) => ({
type: Types.CARD_FLIPPED,
gameId,
playerId,
data,
}),
cardDestroyed: (gameId: number, playerId: number, data: Event_DestroyCard) => ({
cardDestroyed: (gameId: number, playerId: number, data: Data.Event_DestroyCard) => ({
type: Types.CARD_DESTROYED,
gameId,
playerId,
data,
}),
cardAttached: (gameId: number, playerId: number, data: Event_AttachCard) => ({
cardAttached: (gameId: number, playerId: number, data: Data.Event_AttachCard) => ({
type: Types.CARD_ATTACHED,
gameId,
playerId,
data,
}),
tokenCreated: (gameId: number, playerId: number, data: Event_CreateToken) => ({
tokenCreated: (gameId: number, playerId: number, data: Data.Event_CreateToken) => ({
type: Types.TOKEN_CREATED,
gameId,
playerId,
data,
}),
cardAttrChanged: (gameId: number, playerId: number, data: Event_SetCardAttr) => ({
cardAttrChanged: (gameId: number, playerId: number, data: Data.Event_SetCardAttr) => ({
type: Types.CARD_ATTR_CHANGED,
gameId,
playerId,
data,
}),
cardCounterChanged: (gameId: number, playerId: number, data: Event_SetCardCounter) => ({
cardCounterChanged: (gameId: number, playerId: number, data: Data.Event_SetCardCounter) => ({
type: Types.CARD_COUNTER_CHANGED,
gameId,
playerId,
data,
}),
arrowCreated: (gameId: number, playerId: number, data: Event_CreateArrow) => ({
arrowCreated: (gameId: number, playerId: number, data: Data.Event_CreateArrow) => ({
type: Types.ARROW_CREATED,
gameId,
playerId,
data,
}),
arrowDeleted: (gameId: number, playerId: number, data: Event_DeleteArrow) => ({
arrowDeleted: (gameId: number, playerId: number, data: Data.Event_DeleteArrow) => ({
type: Types.ARROW_DELETED,
gameId,
playerId,
data,
}),
counterCreated: (gameId: number, playerId: number, data: Event_CreateCounter) => ({
counterCreated: (gameId: number, playerId: number, data: Data.Event_CreateCounter) => ({
type: Types.COUNTER_CREATED,
gameId,
playerId,
data,
}),
counterSet: (gameId: number, playerId: number, data: Event_SetCounter) => ({
counterSet: (gameId: number, playerId: number, data: Data.Event_SetCounter) => ({
type: Types.COUNTER_SET,
gameId,
playerId,
data,
}),
counterDeleted: (gameId: number, playerId: number, data: Event_DelCounter) => ({
counterDeleted: (gameId: number, playerId: number, data: Data.Event_DelCounter) => ({
type: Types.COUNTER_DELETED,
gameId,
playerId,
data,
}),
cardsDrawn: (gameId: number, playerId: number, data: Event_DrawCards) => ({
cardsDrawn: (gameId: number, playerId: number, data: Data.Event_DrawCards) => ({
type: Types.CARDS_DRAWN,
gameId,
playerId,
data,
}),
cardsRevealed: (gameId: number, playerId: number, data: Event_RevealCards) => ({
cardsRevealed: (gameId: number, playerId: number, data: Data.Event_RevealCards) => ({
type: Types.CARDS_REVEALED,
gameId,
playerId,
data,
}),
zoneShuffled: (gameId: number, playerId: number, data: Event_Shuffle) => ({
zoneShuffled: (gameId: number, playerId: number, data: Data.Event_Shuffle) => ({
type: Types.ZONE_SHUFFLED,
gameId,
playerId,
data,
}),
dieRolled: (gameId: number, playerId: number, data: Event_RollDie) => ({
dieRolled: (gameId: number, playerId: number, data: Data.Event_RollDie) => ({
type: Types.DIE_ROLLED,
gameId,
playerId,
@ -209,14 +188,14 @@ export const Actions = {
reversed,
}),
zoneDumped: (gameId: number, playerId: number, data: Event_DumpZone) => ({
zoneDumped: (gameId: number, playerId: number, data: Data.Event_DumpZone) => ({
type: Types.ZONE_DUMPED,
gameId,
playerId,
data,
}),
zonePropertiesChanged: (gameId: number, playerId: number, data: Event_ChangeZoneProperties) => ({
zonePropertiesChanged: (gameId: number, playerId: number, data: Data.Event_ChangeZoneProperties) => ({
type: Types.ZONE_PROPERTIES_CHANGED,
gameId,
playerId,

View file

@ -1,37 +1,16 @@
vi.mock('store/store', () => ({ store: { dispatch: vi.fn() } }));
vi.mock('../store', () => ({ store: { dispatch: vi.fn() } }));
import { create } from '@bufbuild/protobuf';
import { store } from 'store/store';
import { Data } from '@app/types';
import { store } from '..';
import { Actions } from './game.actions';
import { Dispatch } from './game.dispatch';
import {
makeArrow,
makeCard,
makeCounter,
makeGameEntry,
makePlayerProperties,
} from './__mocks__/fixtures';
import { Event_GameStateChangedSchema } from 'generated/proto/event_game_state_changed_pb';
import { Event_MoveCardSchema } from 'generated/proto/event_move_card_pb';
import { Event_FlipCardSchema } from 'generated/proto/event_flip_card_pb';
import { Event_DestroyCardSchema } from 'generated/proto/event_destroy_card_pb';
import { Event_AttachCardSchema } from 'generated/proto/event_attach_card_pb';
import { Event_CreateTokenSchema } from 'generated/proto/event_create_token_pb';
import { Event_SetCardAttrSchema } from 'generated/proto/event_set_card_attr_pb';
import { Event_SetCardCounterSchema } from 'generated/proto/event_set_card_counter_pb';
import { Event_CreateArrowSchema } from 'generated/proto/event_create_arrow_pb';
import { Event_DeleteArrowSchema } from 'generated/proto/event_delete_arrow_pb';
import { Event_CreateCounterSchema } from 'generated/proto/event_create_counter_pb';
import { Event_SetCounterSchema } from 'generated/proto/event_set_counter_pb';
import { Event_DelCounterSchema } from 'generated/proto/event_del_counter_pb';
import { Event_DrawCardsSchema } from 'generated/proto/event_draw_cards_pb';
import { Event_RevealCardsSchema } from 'generated/proto/event_reveal_cards_pb';
import { Event_ShuffleSchema } from 'generated/proto/event_shuffle_pb';
import { Event_RollDieSchema } from 'generated/proto/event_roll_die_pb';
import { Event_DumpZoneSchema } from 'generated/proto/event_dump_zone_pb';
import { Event_ChangeZonePropertiesSchema } from 'generated/proto/event_change_zone_properties_pb';
beforeEach(() => vi.clearAllMocks());
describe('Dispatch', () => {
it('clearStore dispatches Actions.clearStore()', () => {
@ -40,9 +19,9 @@ describe('Dispatch', () => {
});
it('gameJoined dispatches Actions.gameJoined()', () => {
const entry = makeGameEntry();
Dispatch.gameJoined(1, entry);
expect(store.dispatch).toHaveBeenCalledWith(Actions.gameJoined(1, entry));
const data = create(Data.Event_GameJoinedSchema, { hostId: 1, playerId: 2 });
Dispatch.gameJoined(data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.gameJoined(data));
});
it('gameLeft dispatches Actions.gameLeft()', () => {
@ -61,7 +40,7 @@ describe('Dispatch', () => {
});
it('gameStateChanged dispatches Actions.gameStateChanged()', () => {
const data = create(Event_GameStateChangedSchema, {
const data = create(Data.Event_GameStateChangedSchema, {
playerList: [], gameStarted: false, activePlayerId: 0, activePhase: 0, secondsElapsed: 0
});
Dispatch.gameStateChanged(1, data);
@ -91,97 +70,97 @@ describe('Dispatch', () => {
});
it('cardMoved dispatches Actions.cardMoved()', () => {
const data = create(Event_MoveCardSchema, { cardId: 1 });
const data = create(Data.Event_MoveCardSchema, { cardId: 1 });
Dispatch.cardMoved(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.cardMoved(1, 2, data));
});
it('cardFlipped dispatches Actions.cardFlipped()', () => {
const data = create(Event_FlipCardSchema, { cardId: 1 });
const data = create(Data.Event_FlipCardSchema, { cardId: 1 });
Dispatch.cardFlipped(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.cardFlipped(1, 2, data));
});
it('cardDestroyed dispatches Actions.cardDestroyed()', () => {
const data = create(Event_DestroyCardSchema, { cardId: 1 });
const data = create(Data.Event_DestroyCardSchema, { cardId: 1 });
Dispatch.cardDestroyed(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.cardDestroyed(1, 2, data));
});
it('cardAttached dispatches Actions.cardAttached()', () => {
const data = create(Event_AttachCardSchema, { cardId: 1 });
const data = create(Data.Event_AttachCardSchema, { cardId: 1 });
Dispatch.cardAttached(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.cardAttached(1, 2, data));
});
it('tokenCreated dispatches Actions.tokenCreated()', () => {
const data = create(Event_CreateTokenSchema, { cardId: 1 });
const data = create(Data.Event_CreateTokenSchema, { cardId: 1 });
Dispatch.tokenCreated(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.tokenCreated(1, 2, data));
});
it('cardAttrChanged dispatches Actions.cardAttrChanged()', () => {
const data = create(Event_SetCardAttrSchema, { cardId: 1 });
const data = create(Data.Event_SetCardAttrSchema, { cardId: 1 });
Dispatch.cardAttrChanged(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.cardAttrChanged(1, 2, data));
});
it('cardCounterChanged dispatches Actions.cardCounterChanged()', () => {
const data = create(Event_SetCardCounterSchema, { cardId: 1 });
const data = create(Data.Event_SetCardCounterSchema, { cardId: 1 });
Dispatch.cardCounterChanged(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.cardCounterChanged(1, 2, data));
});
it('arrowCreated dispatches Actions.arrowCreated()', () => {
const data = create(Event_CreateArrowSchema, { arrowInfo: makeArrow() });
const data = create(Data.Event_CreateArrowSchema, { arrowInfo: makeArrow() });
Dispatch.arrowCreated(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.arrowCreated(1, 2, data));
});
it('arrowDeleted dispatches Actions.arrowDeleted()', () => {
const data = create(Event_DeleteArrowSchema, { arrowId: 3 });
const data = create(Data.Event_DeleteArrowSchema, { arrowId: 3 });
Dispatch.arrowDeleted(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.arrowDeleted(1, 2, data));
});
it('counterCreated dispatches Actions.counterCreated()', () => {
const data = create(Event_CreateCounterSchema, { counterInfo: makeCounter() });
const data = create(Data.Event_CreateCounterSchema, { counterInfo: makeCounter() });
Dispatch.counterCreated(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.counterCreated(1, 2, data));
});
it('counterSet dispatches Actions.counterSet()', () => {
const data = create(Event_SetCounterSchema, { counterId: 1, value: 10 });
const data = create(Data.Event_SetCounterSchema, { counterId: 1, value: 10 });
Dispatch.counterSet(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.counterSet(1, 2, data));
});
it('counterDeleted dispatches Actions.counterDeleted()', () => {
const data = create(Event_DelCounterSchema, { counterId: 1 });
const data = create(Data.Event_DelCounterSchema, { counterId: 1 });
Dispatch.counterDeleted(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.counterDeleted(1, 2, data));
});
it('cardsDrawn dispatches Actions.cardsDrawn()', () => {
const data = create(Event_DrawCardsSchema, { number: 2, cards: [makeCard()] });
const data = create(Data.Event_DrawCardsSchema, { number: 2, cards: [makeCard()] });
Dispatch.cardsDrawn(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.cardsDrawn(1, 2, data));
});
it('cardsRevealed dispatches Actions.cardsRevealed()', () => {
const data = create(Event_RevealCardsSchema, { zoneName: 'hand', cards: [] });
const data = create(Data.Event_RevealCardsSchema, { zoneName: 'hand', cards: [] });
Dispatch.cardsRevealed(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.cardsRevealed(1, 2, data));
});
it('zoneShuffled dispatches Actions.zoneShuffled()', () => {
const data = create(Event_ShuffleSchema, { zoneName: 'deck', start: 0, end: 39 });
const data = create(Data.Event_ShuffleSchema, { zoneName: 'deck', start: 0, end: 39 });
Dispatch.zoneShuffled(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.zoneShuffled(1, 2, data));
});
it('dieRolled dispatches Actions.dieRolled()', () => {
const data = create(Event_RollDieSchema, { sides: 6, value: 4, values: [4] });
const data = create(Data.Event_RollDieSchema, { sides: 6, value: 4, values: [4] });
Dispatch.dieRolled(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.dieRolled(1, 2, data));
});
@ -202,13 +181,13 @@ describe('Dispatch', () => {
});
it('zoneDumped dispatches Actions.zoneDumped()', () => {
const data = create(Event_DumpZoneSchema, { zoneOwnerId: 1, zoneName: 'hand', numberCards: 3, isReversed: false });
const data = create(Data.Event_DumpZoneSchema, { zoneOwnerId: 1, zoneName: 'hand', numberCards: 3, isReversed: false });
Dispatch.zoneDumped(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.zoneDumped(1, 2, data));
});
it('zonePropertiesChanged dispatches Actions.zonePropertiesChanged()', () => {
const data = create(Event_ChangeZonePropertiesSchema, { zoneName: 'deck', alwaysRevealTopCard: true, alwaysLookAtTopCard: false });
const data = create(Data.Event_ChangeZonePropertiesSchema, { zoneName: 'deck', alwaysRevealTopCard: true, alwaysLookAtTopCard: false });
Dispatch.zonePropertiesChanged(1, 2, data);
expect(store.dispatch).toHaveBeenCalledWith(Actions.zonePropertiesChanged(1, 2, data));
});

View file

@ -1,34 +1,14 @@
import type { Event_AttachCard } from 'generated/proto/event_attach_card_pb';
import type { Event_ChangeZoneProperties } from 'generated/proto/event_change_zone_properties_pb';
import type { Event_CreateArrow } from 'generated/proto/event_create_arrow_pb';
import type { Event_CreateCounter } from 'generated/proto/event_create_counter_pb';
import type { Event_CreateToken } from 'generated/proto/event_create_token_pb';
import type { Event_DelCounter } from 'generated/proto/event_del_counter_pb';
import type { Event_DeleteArrow } from 'generated/proto/event_delete_arrow_pb';
import type { Event_DestroyCard } from 'generated/proto/event_destroy_card_pb';
import type { Event_DrawCards } from 'generated/proto/event_draw_cards_pb';
import type { Event_DumpZone } from 'generated/proto/event_dump_zone_pb';
import type { Event_FlipCard } from 'generated/proto/event_flip_card_pb';
import type { Event_GameStateChanged } from 'generated/proto/event_game_state_changed_pb';
import type { Event_MoveCard } from 'generated/proto/event_move_card_pb';
import type { ServerInfo_PlayerProperties } from 'generated/proto/serverinfo_playerproperties_pb';
import type { Event_RevealCards } from 'generated/proto/event_reveal_cards_pb';
import type { Event_RollDie } from 'generated/proto/event_roll_die_pb';
import type { Event_SetCardAttr } from 'generated/proto/event_set_card_attr_pb';
import type { Event_SetCardCounter } from 'generated/proto/event_set_card_counter_pb';
import type { Event_SetCounter } from 'generated/proto/event_set_counter_pb';
import type { Event_Shuffle } from 'generated/proto/event_shuffle_pb';
import { store } from 'store/store';
import type { Data } from '@app/types';
import { store } from '..';
import { Actions } from './game.actions';
import { GameEntry } from './game.interfaces';
export const Dispatch = {
clearStore: () => {
store.dispatch(Actions.clearStore());
},
gameJoined: (gameId: number, gameEntry: GameEntry) => {
store.dispatch(Actions.gameJoined(gameId, gameEntry));
gameJoined: (data: Data.Event_GameJoined) => {
store.dispatch(Actions.gameJoined(data));
},
gameLeft: (gameId: number) => {
@ -43,11 +23,11 @@ export const Dispatch = {
store.dispatch(Actions.gameHostChanged(gameId, hostId));
},
gameStateChanged: (gameId: number, data: Event_GameStateChanged) => {
gameStateChanged: (gameId: number, data: Data.Event_GameStateChanged) => {
store.dispatch(Actions.gameStateChanged(gameId, data));
},
playerJoined: (gameId: number, playerProperties: ServerInfo_PlayerProperties) => {
playerJoined: (gameId: number, playerProperties: Data.ServerInfo_PlayerProperties) => {
store.dispatch(Actions.playerJoined(gameId, playerProperties));
},
@ -55,7 +35,7 @@ export const Dispatch = {
store.dispatch(Actions.playerLeft(gameId, playerId, reason));
},
playerPropertiesChanged: (gameId: number, playerId: number, properties: ServerInfo_PlayerProperties) => {
playerPropertiesChanged: (gameId: number, playerId: number, properties: Data.ServerInfo_PlayerProperties) => {
store.dispatch(Actions.playerPropertiesChanged(gameId, playerId, properties));
},
@ -63,67 +43,67 @@ export const Dispatch = {
store.dispatch(Actions.kicked(gameId));
},
cardMoved: (gameId: number, playerId: number, data: Event_MoveCard) => {
cardMoved: (gameId: number, playerId: number, data: Data.Event_MoveCard) => {
store.dispatch(Actions.cardMoved(gameId, playerId, data));
},
cardFlipped: (gameId: number, playerId: number, data: Event_FlipCard) => {
cardFlipped: (gameId: number, playerId: number, data: Data.Event_FlipCard) => {
store.dispatch(Actions.cardFlipped(gameId, playerId, data));
},
cardDestroyed: (gameId: number, playerId: number, data: Event_DestroyCard) => {
cardDestroyed: (gameId: number, playerId: number, data: Data.Event_DestroyCard) => {
store.dispatch(Actions.cardDestroyed(gameId, playerId, data));
},
cardAttached: (gameId: number, playerId: number, data: Event_AttachCard) => {
cardAttached: (gameId: number, playerId: number, data: Data.Event_AttachCard) => {
store.dispatch(Actions.cardAttached(gameId, playerId, data));
},
tokenCreated: (gameId: number, playerId: number, data: Event_CreateToken) => {
tokenCreated: (gameId: number, playerId: number, data: Data.Event_CreateToken) => {
store.dispatch(Actions.tokenCreated(gameId, playerId, data));
},
cardAttrChanged: (gameId: number, playerId: number, data: Event_SetCardAttr) => {
cardAttrChanged: (gameId: number, playerId: number, data: Data.Event_SetCardAttr) => {
store.dispatch(Actions.cardAttrChanged(gameId, playerId, data));
},
cardCounterChanged: (gameId: number, playerId: number, data: Event_SetCardCounter) => {
cardCounterChanged: (gameId: number, playerId: number, data: Data.Event_SetCardCounter) => {
store.dispatch(Actions.cardCounterChanged(gameId, playerId, data));
},
arrowCreated: (gameId: number, playerId: number, data: Event_CreateArrow) => {
arrowCreated: (gameId: number, playerId: number, data: Data.Event_CreateArrow) => {
store.dispatch(Actions.arrowCreated(gameId, playerId, data));
},
arrowDeleted: (gameId: number, playerId: number, data: Event_DeleteArrow) => {
arrowDeleted: (gameId: number, playerId: number, data: Data.Event_DeleteArrow) => {
store.dispatch(Actions.arrowDeleted(gameId, playerId, data));
},
counterCreated: (gameId: number, playerId: number, data: Event_CreateCounter) => {
counterCreated: (gameId: number, playerId: number, data: Data.Event_CreateCounter) => {
store.dispatch(Actions.counterCreated(gameId, playerId, data));
},
counterSet: (gameId: number, playerId: number, data: Event_SetCounter) => {
counterSet: (gameId: number, playerId: number, data: Data.Event_SetCounter) => {
store.dispatch(Actions.counterSet(gameId, playerId, data));
},
counterDeleted: (gameId: number, playerId: number, data: Event_DelCounter) => {
counterDeleted: (gameId: number, playerId: number, data: Data.Event_DelCounter) => {
store.dispatch(Actions.counterDeleted(gameId, playerId, data));
},
cardsDrawn: (gameId: number, playerId: number, data: Event_DrawCards) => {
cardsDrawn: (gameId: number, playerId: number, data: Data.Event_DrawCards) => {
store.dispatch(Actions.cardsDrawn(gameId, playerId, data));
},
cardsRevealed: (gameId: number, playerId: number, data: Event_RevealCards) => {
cardsRevealed: (gameId: number, playerId: number, data: Data.Event_RevealCards) => {
store.dispatch(Actions.cardsRevealed(gameId, playerId, data));
},
zoneShuffled: (gameId: number, playerId: number, data: Event_Shuffle) => {
zoneShuffled: (gameId: number, playerId: number, data: Data.Event_Shuffle) => {
store.dispatch(Actions.zoneShuffled(gameId, playerId, data));
},
dieRolled: (gameId: number, playerId: number, data: Event_RollDie) => {
dieRolled: (gameId: number, playerId: number, data: Data.Event_RollDie) => {
store.dispatch(Actions.dieRolled(gameId, playerId, data));
},
@ -139,11 +119,11 @@ export const Dispatch = {
store.dispatch(Actions.turnReversed(gameId, reversed));
},
zoneDumped: (gameId: number, playerId: number, data: Event_DumpZone) => {
zoneDumped: (gameId: number, playerId: number, data: Data.Event_DumpZone) => {
store.dispatch(Actions.zoneDumped(gameId, playerId, data));
},
zonePropertiesChanged: (gameId: number, playerId: number, data: Event_ChangeZoneProperties) => {
zonePropertiesChanged: (gameId: number, playerId: number, data: Data.Event_ChangeZoneProperties) => {
store.dispatch(Actions.zonePropertiesChanged(gameId, playerId, data));
},

View file

@ -1,7 +1,4 @@
import type { ServerInfo_Card } from 'generated/proto/serverinfo_card_pb';
import type { ServerInfo_Counter } from 'generated/proto/serverinfo_counter_pb';
import type { ServerInfo_Arrow } from 'generated/proto/serverinfo_arrow_pb';
import type { ServerInfo_PlayerProperties } from 'generated/proto/serverinfo_playerproperties_pb';
import type { Data } from '@app/types';
export interface GamesState {
games: { [gameId: number]: GameEntry };
@ -32,14 +29,14 @@ export interface GameEntry {
/** Normalized from ServerInfo_Player — keyed collections for O(1) lookup. */
export interface PlayerEntry {
properties: ServerInfo_PlayerProperties;
properties: Data.ServerInfo_PlayerProperties;
deckList: string;
/** Zones keyed by zone name (e.g. "hand", "deck", "table"). */
zones: { [zoneName: string]: ZoneEntry };
/** Player-level counters (e.g. life) keyed by counter id. */
counters: { [counterId: number]: ServerInfo_Counter };
counters: { [counterId: number]: Data.ServerInfo_Counter };
/** Arrows keyed by arrow id. */
arrows: { [arrowId: number]: ServerInfo_Arrow };
arrows: { [arrowId: number]: Data.ServerInfo_Arrow };
}
/** Normalized from ServerInfo_Zone — card list is an ordered array matching proto. */
@ -51,7 +48,7 @@ export interface ZoneEntry {
/** Authoritative card count (used for hidden zones where cardList may be empty). */
cardCount: number;
/** Ordered card list; may be empty for hidden zones with no dump active. */
cards: ServerInfo_Card[];
cards: Data.ServerInfo_Card[];
alwaysRevealTopCard: boolean;
alwaysLookAtTopCard: boolean;
}

View file

@ -1,6 +1,5 @@
import { create } from '@bufbuild/protobuf';
import { CardAttribute } from 'generated/proto/card_attributes_pb';
import type { ServerInfo_Player } from 'generated/proto/serverinfo_player_pb';
import { Data } from '@app/types';
import { gamesReducer } from './game.reducer';
import { Types } from './game.types';
import {
@ -13,7 +12,6 @@ import {
makeState,
makeZoneEntry,
} from './__mocks__/fixtures';
import { ServerInfo_PlayerSchema } from 'generated/proto/serverinfo_player_pb';
// ── 2A: Initialisation & lifecycle ───────────────────────────────────────────
@ -30,9 +28,16 @@ describe('2A: Initialisation & lifecycle', () => {
});
it('GAME_JOINED → inserts gameEntry keyed by gameId', () => {
const entry = makeGameEntry({ gameId: 42 });
const result = gamesReducer({ games: {} }, { type: Types.GAME_JOINED, gameId: 42, gameEntry: entry });
expect(result.games[42]).toBe(entry);
const data = create(Data.Event_GameJoinedSchema, {
gameInfo: create(Data.ServerInfo_GameSchema, { gameId: 42, roomId: 1, description: 'test' }),
hostId: 5,
playerId: 2,
spectator: false,
judge: false,
resuming: false,
});
const result = gamesReducer({ games: {} }, { type: Types.GAME_JOINED, data });
expect(result.games[42]).toEqual(expect.objectContaining({ gameId: 42, hostId: 5, localPlayerId: 2 }));
});
it('GAME_LEFT → removes game by gameId', () => {
@ -69,8 +74,8 @@ describe('2B: Game state & player management', () => {
const card = makeCard({ id: 5 });
const counter = makeCounter({ id: 2 });
const arrow = makeArrow({ id: 3 });
const playerList: ServerInfo_Player[] = [
create(ServerInfo_PlayerSchema, {
const playerList: Data.ServerInfo_Player[] = [
create(Data.ServerInfo_PlayerSchema, {
properties: makePlayerProperties({ playerId: 7 }),
deckList: 'some deck',
zoneList: [
@ -550,7 +555,7 @@ describe('2E: CARD_ATTR_CHANGED', () => {
});
}
function dispatchAttr(state: ReturnType<typeof makeState>, attribute: CardAttribute, attrValue: string) {
function dispatchAttr(state: ReturnType<typeof makeState>, attribute: Data.CardAttribute, attrValue: string) {
return gamesReducer(state, {
type: Types.CARD_ATTR_CHANGED,
gameId: 1,
@ -560,37 +565,37 @@ describe('2E: CARD_ATTR_CHANGED', () => {
}
it('AttrTapped (1) → card.tapped = true when attrValue is "1"', () => {
const result = dispatchAttr(stateWithCard(), CardAttribute.AttrTapped, '1');
const result = dispatchAttr(stateWithCard(), Data.CardAttribute.AttrTapped, '1');
expect(result.games[1].players[1].zones['table'].cards[0].tapped).toBe(true);
});
it('AttrAttacking (2) → card.attacking = true when attrValue is "1"', () => {
const result = dispatchAttr(stateWithCard(), CardAttribute.AttrAttacking, '1');
const result = dispatchAttr(stateWithCard(), Data.CardAttribute.AttrAttacking, '1');
expect(result.games[1].players[1].zones['table'].cards[0].attacking).toBe(true);
});
it('AttrFaceDown (3) → card.faceDown = true when attrValue is "1"', () => {
const result = dispatchAttr(stateWithCard(), CardAttribute.AttrFaceDown, '1');
const result = dispatchAttr(stateWithCard(), Data.CardAttribute.AttrFaceDown, '1');
expect(result.games[1].players[1].zones['table'].cards[0].faceDown).toBe(true);
});
it('AttrColor (4) → card.color = attrValue', () => {
const result = dispatchAttr(stateWithCard(), CardAttribute.AttrColor, 'red');
const result = dispatchAttr(stateWithCard(), Data.CardAttribute.AttrColor, 'red');
expect(result.games[1].players[1].zones['table'].cards[0].color).toBe('red');
});
it('AttrPT (5) → card.pt = attrValue', () => {
const result = dispatchAttr(stateWithCard(), CardAttribute.AttrPT, '2/3');
const result = dispatchAttr(stateWithCard(), Data.CardAttribute.AttrPT, '2/3');
expect(result.games[1].players[1].zones['table'].cards[0].pt).toBe('2/3');
});
it('AttrAnnotation (6) → card.annotation = attrValue', () => {
const result = dispatchAttr(stateWithCard(), CardAttribute.AttrAnnotation, 'enchanted');
const result = dispatchAttr(stateWithCard(), Data.CardAttribute.AttrAnnotation, 'enchanted');
expect(result.games[1].players[1].zones['table'].cards[0].annotation).toBe('enchanted');
});
it('AttrDoesntUntap (7) → card.doesntUntap = true when attrValue is "1"', () => {
const result = dispatchAttr(stateWithCard(), CardAttribute.AttrDoesntUntap, '1');
const result = dispatchAttr(stateWithCard(), Data.CardAttribute.AttrDoesntUntap, '1');
expect(result.games[1].players[1].zones['table'].cards[0].doesntUntap).toBe(true);
});
});

View file

@ -1,12 +1,5 @@
import { CardAttribute } from 'generated/proto/card_attributes_pb';
import type { ServerInfo_CardCounter } from 'generated/proto/serverinfo_cardcounter_pb';
import type { ServerInfo_Card } from 'generated/proto/serverinfo_card_pb';
import type { ServerInfo_Counter } from 'generated/proto/serverinfo_counter_pb';
import type { ServerInfo_Arrow } from 'generated/proto/serverinfo_arrow_pb';
import type { ServerInfo_Player } from 'generated/proto/serverinfo_player_pb';
import { Data } from '@app/types';
import { create } from '@bufbuild/protobuf';
import { ServerInfo_CardSchema } from 'generated/proto/serverinfo_card_pb';
import { ServerInfo_CardCounterSchema } from 'generated/proto/serverinfo_cardcounter_pb';
import { GameAction } from './game.actions';
import { GameEntry, GameMessage, GamesState, PlayerEntry, ZoneEntry } from './game.interfaces';
import { Types } from './game.types';
@ -74,7 +67,7 @@ function removeGame(state: GamesState, gameId: number): GamesState {
}
/** Converts the proto PlayerInfo[] array into the keyed PlayerEntry map used in the store. */
function normalizePlayers(playerList: ServerInfo_Player[]): { [playerId: number]: PlayerEntry } {
function normalizePlayers(playerList: Data.ServerInfo_Player[]): { [playerId: number]: PlayerEntry } {
const players: { [playerId: number]: PlayerEntry } = {};
for (const player of playerList) {
const playerId = player.properties.playerId;
@ -92,12 +85,12 @@ function normalizePlayers(playerList: ServerInfo_Player[]): { [playerId: number]
};
}
const counters: { [counterId: number]: ServerInfo_Counter } = {};
const counters: { [counterId: number]: Data.ServerInfo_Counter } = {};
for (const counter of player.counterList) {
counters[counter.id] = counter;
}
const arrows: { [arrowId: number]: ServerInfo_Arrow } = {};
const arrows: { [arrowId: number]: Data.ServerInfo_Arrow } = {};
for (const arrow of player.arrowList) {
arrows[arrow.id] = arrow;
}
@ -120,8 +113,8 @@ function buildEmptyCard(
y: number,
faceDown: boolean,
providerId: string
): ServerInfo_Card {
return create(ServerInfo_CardSchema, {
): Data.ServerInfo_Card {
return create(Data.ServerInfo_CardSchema, {
id,
name,
x,
@ -157,9 +150,31 @@ export const gamesReducer = (state: GamesState = initialState, action: GameActio
}
case Types.GAME_JOINED: {
const { data } = action;
const gameInfo = data.gameInfo;
if (!gameInfo) {
return state;
}
const gameEntry: GameEntry = {
gameId: gameInfo.gameId,
roomId: gameInfo.roomId,
description: gameInfo.description,
hostId: data.hostId,
localPlayerId: data.playerId,
spectator: data.spectator,
judge: data.judge,
resuming: data.resuming,
started: gameInfo.started,
activePlayerId: -1,
activePhase: -1,
secondsElapsed: 0,
reversed: false,
players: {},
messages: [],
};
return {
...state,
games: { ...state.games, [action.gameId]: action.gameEntry },
games: { ...state.games, [gameEntry.gameId]: gameEntry },
};
}
@ -266,8 +281,8 @@ export const gamesReducer = (state: GamesState = initialState, action: GameActio
}
// Locate card in source zone (by id for visible zones, by position for hidden)
let removedCard: ServerInfo_Card | undefined;
let newSourceCards: ServerInfo_Card[];
let removedCard: Data.ServerInfo_Card | undefined;
let newSourceCards: Data.ServerInfo_Card[];
if (cardId >= 0) {
removedCard = sourceZoneEntry.cards.find(c => c.id === cardId);
newSourceCards = sourceZoneEntry.cards.filter(c => c.id !== cardId);
@ -280,7 +295,7 @@ export const gamesReducer = (state: GamesState = initialState, action: GameActio
}
const effectiveNewId = newCardId >= 0 ? newCardId : (removedCard?.id ?? -1);
const movedCard: ServerInfo_Card = removedCard
const movedCard: Data.ServerInfo_Card = removedCard
? {
...removedCard,
id: effectiveNewId,
@ -423,7 +438,7 @@ export const gamesReducer = (state: GamesState = initialState, action: GameActio
return state;
}
const newCard: ServerInfo_Card = create(ServerInfo_CardSchema, {
const newCard: Data.ServerInfo_Card = create(Data.ServerInfo_CardSchema, {
id: cardId,
name: cardName,
x,
@ -469,15 +484,15 @@ export const gamesReducer = (state: GamesState = initialState, action: GameActio
return state;
}
const attrPatch: Partial<ServerInfo_Card> = {};
switch (attribute as CardAttribute) {
case CardAttribute.AttrTapped: attrPatch.tapped = attrValue === '1'; break;
case CardAttribute.AttrAttacking: attrPatch.attacking = attrValue === '1'; break;
case CardAttribute.AttrFaceDown: attrPatch.faceDown = attrValue === '1'; break;
case CardAttribute.AttrColor: attrPatch.color = attrValue; break;
case CardAttribute.AttrPT: attrPatch.pt = attrValue; break;
case CardAttribute.AttrAnnotation: attrPatch.annotation = attrValue; break;
case CardAttribute.AttrDoesntUntap: attrPatch.doesntUntap = attrValue === '1'; break;
const attrPatch: Partial<Data.ServerInfo_Card> = {};
switch (attribute as Data.CardAttribute) {
case Data.CardAttribute.AttrTapped: attrPatch.tapped = attrValue === '1'; break;
case Data.CardAttribute.AttrAttacking: attrPatch.attacking = attrValue === '1'; break;
case Data.CardAttribute.AttrFaceDown: attrPatch.faceDown = attrValue === '1'; break;
case Data.CardAttribute.AttrColor: attrPatch.color = attrValue; break;
case Data.CardAttribute.AttrPT: attrPatch.pt = attrValue; break;
case Data.CardAttribute.AttrAnnotation: attrPatch.annotation = attrValue; break;
case Data.CardAttribute.AttrDoesntUntap: attrPatch.doesntUntap = attrValue === '1'; break;
}
const updatedCards = [...zone.cards];
@ -507,7 +522,7 @@ export const gamesReducer = (state: GamesState = initialState, action: GameActio
}
const card = zone.cards[cardIdx];
let newCounterList: ServerInfo_CardCounter[];
let newCounterList: Data.ServerInfo_CardCounter[];
if (counterValue <= 0) {
newCounterList = card.counterList.filter(c => c.id !== counterId);
} else {
@ -515,7 +530,7 @@ export const gamesReducer = (state: GamesState = initialState, action: GameActio
newCounterList =
existing >= 0
? card.counterList.map(c => (c.id === counterId ? { ...c, value: counterValue } : c))
: [...card.counterList, create(ServerInfo_CardCounterSchema, { id: counterId, value: counterValue })];
: [...card.counterList, create(Data.ServerInfo_CardCounterSchema, { id: counterId, value: counterValue })];
}
const updatedCards = [...zone.cards];

View file

@ -1,12 +1,12 @@
import { createSelector } from '@reduxjs/toolkit';
import type { ServerInfo_Card } from 'generated/proto/serverinfo_card_pb';
import type { Data } from '@app/types';
import { GamesState, GameEntry, PlayerEntry, ZoneEntry } from './game.interfaces';
interface State {
games: GamesState;
}
const EMPTY_ARRAY: ServerInfo_Card[] = [];
const EMPTY_ARRAY: Data.ServerInfo_Card[] = [];
const EMPTY_OBJECT = {} as Record<string, never>;
export const Selectors = {