Split doAction into multiple functions
This commit is contained in:
parent
1116942031
commit
d6fa74455e
@ -5,8 +5,8 @@ type Setter<T> = React.Dispatch<React.SetStateAction<T>>;
|
|||||||
type WebSocketData = string | ArrayBufferLike | Blob | ArrayBufferView;
|
type WebSocketData = string | ArrayBufferLike | Blob | ArrayBufferView;
|
||||||
|
|
||||||
type ActionMessage<T = any> = {
|
type ActionMessage<T = any> = {
|
||||||
Action: import("../utils/actions").GameAction,
|
readonly Action: import("../utils/actions").GameAction,
|
||||||
Data?: T
|
readonly Data?: T
|
||||||
}
|
}
|
||||||
|
|
||||||
type Action<T> = (obj: T) => void;
|
type Action<T> = (obj: T) => void;
|
||||||
|
@ -20,47 +20,39 @@ const ghosts = [
|
|||||||
|
|
||||||
const store = getDefaultStore();
|
const store = getDefaultStore();
|
||||||
|
|
||||||
export const doAction: MessageEventFunction<string> = (message): void => { // TODO divide into smaller functions
|
export const doAction: MessageEventFunction<string> = (event): void => { // TODO divide into smaller functions
|
||||||
const parsed: ActionMessage = JSON.parse(message.data);
|
const message: ActionMessage = JSON.parse(event.data);
|
||||||
|
|
||||||
switch (parsed.Action as GameAction) {
|
switch (message.Action as GameAction) {
|
||||||
case GameAction.rollDice:
|
case GameAction.rollDice:
|
||||||
store.set(diceAtom, parsed.Data as number[]);
|
setDice(message.Data);
|
||||||
break;
|
break;
|
||||||
case GameAction.moveCharacter:
|
case GameAction.moveCharacter:
|
||||||
store.set(diceAtom, parsed.Data?.dice as number[]);
|
moveCharacter(message.Data);
|
||||||
updateCharacters(parsed);
|
|
||||||
removeEatenPellets(parsed);
|
|
||||||
break;
|
break;
|
||||||
case GameAction.playerInfo:
|
case GameAction.playerInfo:
|
||||||
const playerProps = parsed.Data as PlayerProps[];
|
playerInfo(message.Data);
|
||||||
console.log(playerProps);
|
|
||||||
store.set(playersAtom, playerProps.map(p => new Player(p)));
|
|
||||||
const pacMen = playerProps.filter(p => p.PacMan).map(p => new PacMan(p.PacMan!));
|
|
||||||
console.log(pacMen);
|
|
||||||
// TODO find spawn points
|
|
||||||
store.set(charactersAtom, [...pacMen, ...ghosts]);
|
|
||||||
break;
|
break;
|
||||||
case GameAction.ready:
|
case GameAction.ready:
|
||||||
const isReady = parsed.Data.AllReady as boolean;
|
ready(message.Data);
|
||||||
if (isReady) {
|
|
||||||
store.set(currentPlayerAtom, new Player(parsed.Data.Starter as PlayerProps));
|
|
||||||
}
|
|
||||||
store.set(playersAtom, (parsed.Data.Players as PlayerProps[]).map(p => new Player(p)));
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function removeEatenPellets(parsed: ActionMessage): void {
|
function setDice(data?: number[]): void {
|
||||||
const pellets = parsed.Data?.eatenPellets as Position[];
|
store.set(diceAtom, data);
|
||||||
|
|
||||||
for (const pellet of pellets) {
|
|
||||||
testMap[pellet.y][pellet.x] = TileType.empty;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateCharacters(parsed: ActionMessage): void {
|
type MoveCharacterData = { dice: number[], characters: CharacterProps[], eatenPellets: Position[] };
|
||||||
const updatedCharacters = parsed.Data?.characters as CharacterProps[] | undefined;
|
|
||||||
|
function moveCharacter(data?: MoveCharacterData): void {
|
||||||
|
store.set(diceAtom, data?.dice);
|
||||||
|
updateCharacters(data);
|
||||||
|
removeEatenPellets(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateCharacters(data?: MoveCharacterData): void {
|
||||||
|
const updatedCharacters = data?.characters;
|
||||||
|
|
||||||
if (updatedCharacters) {
|
if (updatedCharacters) {
|
||||||
const newList: Character[] = [];
|
const newList: Character[] = [];
|
||||||
@ -70,3 +62,36 @@ function updateCharacters(parsed: ActionMessage): void {
|
|||||||
store.set(charactersAtom, newList);
|
store.set(charactersAtom, newList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function removeEatenPellets(data?: MoveCharacterData): void {
|
||||||
|
const pellets = data?.eatenPellets;
|
||||||
|
|
||||||
|
for (const pellet of pellets ?? []) {
|
||||||
|
testMap[pellet.y][pellet.x] = TileType.empty;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function playerInfo(data?: PlayerProps[]): void {
|
||||||
|
const playerProps = data ?? [];
|
||||||
|
console.log(playerProps);
|
||||||
|
store.set(playersAtom, playerProps.map(p => new Player(p)));
|
||||||
|
const pacMen = playerProps.filter(p => p.PacMan).map(p => new PacMan(p.PacMan!));
|
||||||
|
console.log(pacMen);
|
||||||
|
// TODO find spawn points
|
||||||
|
store.set(charactersAtom, [...pacMen, ...ghosts]);
|
||||||
|
}
|
||||||
|
|
||||||
|
type ReadyData =
|
||||||
|
| { AllReady: true, Starter: PlayerProps, Players: PlayerProps[] }
|
||||||
|
| { AllReady: false, Players: PlayerProps[] }
|
||||||
|
| string;
|
||||||
|
|
||||||
|
function ready(data?: ReadyData): void {
|
||||||
|
if (data && typeof data !== "string") {
|
||||||
|
const isReady = data.AllReady;
|
||||||
|
if (isReady) {
|
||||||
|
store.set(currentPlayerAtom, new Player(data.Starter));
|
||||||
|
}
|
||||||
|
store.set(playersAtom, (data.Players).map(p => new Player(p)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user