chore/cleanup : move game handlers into netcode directory
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
import { playSound } from "~/helpers/audioHelpers";
|
||||
import type { Container, Game, Player } from "./interfaces";
|
||||
import type { Store } from "pinia";
|
||||
|
||||
export function delay(delay: number) {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(resolve, delay);
|
||||
@@ -13,3 +17,174 @@ export function delay(delay: number) {
|
||||
// };
|
||||
|
||||
// export const containerEvents = reactive(new Map<string, ContainerHandler>());
|
||||
|
||||
type handlerInput = {
|
||||
game: Game;
|
||||
containers: Ref<Record<string, Container>>;
|
||||
settingsStore: ReturnType<typeof useSettingsStore>;
|
||||
playerList: Ref<Array<Player>>;
|
||||
refresh: () => Promise<void>;
|
||||
focusPlayer: (player: Player) => void;
|
||||
};
|
||||
export const handlers: {
|
||||
[key: string]: (data: any, input: handlerInput) => Promise<void>;
|
||||
} = {
|
||||
start: async (data: any, { game }) => {
|
||||
game.started = true;
|
||||
game.currentTurn = data.game.currentTurn;
|
||||
},
|
||||
populateContainer: async (data: any, { containers }) => {
|
||||
const fromContainer = containers.value[data.container._id as string];
|
||||
playSound("/sounds/distribute.mp3");
|
||||
for (const card of data.container.cards) {
|
||||
fromContainer.cards.push(card);
|
||||
// if (!settingsStore.fastAnimations) {
|
||||
// await delay(settingsStore.animationSpeed);
|
||||
// }
|
||||
}
|
||||
},
|
||||
shuffleContainer: async (data: any, { containers, settingsStore }) => {
|
||||
playSound("/sounds/shuffle.mp3");
|
||||
const container = containers.value[data.container as string];
|
||||
container.shuffling = true;
|
||||
await delay(settingsStore.animationSpeed * 2);
|
||||
container.shuffling = false;
|
||||
await delay(settingsStore.animationSpeed * 2);
|
||||
},
|
||||
distributeCards: async (data: any, { containers, settingsStore }) => {
|
||||
const fromContainer = containers.value[data.from as string];
|
||||
const toContainer = containers.value[data.to as string];
|
||||
if (settingsStore.fastAnimations) {
|
||||
playSound("/sounds/distribute.mp3");
|
||||
}
|
||||
for (const card of data.cards) {
|
||||
const index = fromContainer.cards.findIndex(
|
||||
(c) => !c || c._id == card._id
|
||||
);
|
||||
fromContainer.cards.splice(index, 1);
|
||||
if (!settingsStore.fastAnimations) {
|
||||
await delay(settingsStore.animationSpeed);
|
||||
playSound("/sounds/distribute.mp3");
|
||||
}
|
||||
toContainer.cards.push(card);
|
||||
if (!settingsStore.fastAnimations) {
|
||||
await delay(settingsStore.animationSpeed);
|
||||
}
|
||||
}
|
||||
await delay(settingsStore.animationSpeed * 2);
|
||||
},
|
||||
endTurn: async (data: any, { game, settingsStore, focusPlayer }) => {
|
||||
game.currentTurn = data.currentTurn;
|
||||
await delay(settingsStore.animationSpeed);
|
||||
const playingPlayer = game.teams.find(
|
||||
(t) => t._id == game.currentTurn.currentTeam
|
||||
)?.players[0];
|
||||
if (playingPlayer) {
|
||||
focusPlayer(playingPlayer);
|
||||
await delay(settingsStore.animationSpeed);
|
||||
}
|
||||
},
|
||||
lockCard: async (data: any, { game, settingsStore }) => {
|
||||
playSound("/sounds/lock.mp3");
|
||||
game.currentTurn.cardsLocked.push(data.card);
|
||||
await delay(settingsStore.animationSpeed);
|
||||
},
|
||||
destroyCard: async (data: any, { containers, settingsStore }) => {
|
||||
const fromContainer = containers.value[data.from as string];
|
||||
const index = fromContainer.cards.findIndex(
|
||||
(c) => !c || c._id == data.card
|
||||
);
|
||||
fromContainer.cards.splice(index, 1);
|
||||
playSound("/sounds/destroy.mp3");
|
||||
await delay(settingsStore.animationSpeed);
|
||||
},
|
||||
joinGame: async (data: any, { game }) => {
|
||||
playSound("/sounds/teleport.mp3");
|
||||
game.teams.push(data.team);
|
||||
},
|
||||
lockEffect: async (data: any, { game, settingsStore }) => {
|
||||
game.currentTurn.lockedEffects.push(data.effect);
|
||||
await delay(settingsStore.animationSpeed);
|
||||
},
|
||||
unlockEffect: async (data: any, { game, settingsStore }) => {
|
||||
const effectIndex = game.currentTurn.lockedEffects.indexOf(data.effect);
|
||||
game.currentTurn.lockedEffects.splice(effectIndex, 1);
|
||||
await delay(settingsStore.animationSpeed);
|
||||
},
|
||||
"currentTurn.updateGold": async (data: any, { game }) => {
|
||||
console.log(`adding gold amount : ` + data.operation);
|
||||
game.currentTurn.gold = data.gold;
|
||||
},
|
||||
"currentTurn.updateDamage": async (data: any, { game }) => {
|
||||
console.log(`adding damage amount : ` + data.operation);
|
||||
game.currentTurn.damage = data.damage;
|
||||
},
|
||||
"currentTurn.addToken": async (data: any, { game, settingsStore }) => {
|
||||
game.currentTurn.tokens.push(data.token);
|
||||
await delay(settingsStore.animationSpeed);
|
||||
},
|
||||
"currentTurn.useToken": async (data: any, { game, settingsStore }) => {
|
||||
const index = game.currentTurn.tokens.findIndex(
|
||||
(t) => t._id == data.tokenId
|
||||
);
|
||||
game.currentTurn.tokens.splice(index, 1);
|
||||
if (!settingsStore.fastAnimations) {
|
||||
await delay(settingsStore.animationSpeed);
|
||||
}
|
||||
},
|
||||
"team.updateHealth": async (data: any, { game }) => {
|
||||
const team = game.teams.find((t) => t._id == data.team);
|
||||
if (!team) {
|
||||
throw new Error("Team not found");
|
||||
}
|
||||
console.log(`adding health amount : ` + data.operation);
|
||||
team.health = data.health;
|
||||
},
|
||||
"player.addFuckUMarker": async (data: any, { playerList }) => {
|
||||
console.log(`adding fuckUMarkers : ` + data.operation);
|
||||
const targetPlayer = playerList.value?.find((p) => p._id == data.playerId);
|
||||
if (!targetPlayer) {
|
||||
throw new Error("Cannot add fuckU Markers : player not found");
|
||||
}
|
||||
targetPlayer.fuckUMarkers = data.fuckUMarkers;
|
||||
},
|
||||
"player.removeFuckUMarker": async (data: any, { playerList }) => {
|
||||
console.log(`adding fuckUMarkers : ` + data.operation);
|
||||
const targetPlayer = playerList.value?.find((p) => p._id == data.playerId);
|
||||
if (!targetPlayer) {
|
||||
throw new Error("Cannot remove fuckU Markers : player not found");
|
||||
}
|
||||
targetPlayer.fuckUMarkers = data.fuckUMarkers;
|
||||
},
|
||||
"player.addDiscardMarker": async (data: any, { playerList }) => {
|
||||
console.log(`adding discardMarkers : ` + data.operation);
|
||||
const targetPlayer = playerList.value?.find((p) => p._id == data.playerId);
|
||||
if (!targetPlayer) {
|
||||
throw new Error("Cannot add discard Markers : player not found");
|
||||
}
|
||||
targetPlayer.discardMarkers = data.discardMarkers;
|
||||
},
|
||||
"player.removeDiscardMarker": async (data: any, { playerList }) => {
|
||||
console.log(`adding fuckUMarkers : ` + data.operation);
|
||||
const targetPlayer = playerList.value?.find((p) => p._id == data.playerId);
|
||||
if (!targetPlayer) {
|
||||
throw new Error("Cannot remove discard Markers : player not found");
|
||||
}
|
||||
targetPlayer.discardMarkers = data.discardMarkers;
|
||||
},
|
||||
killTeam: async (data: any, { game, refresh }) => {
|
||||
const index = game.teams.findIndex((t) => t._id == data.teamId);
|
||||
if (!index) {
|
||||
console.error("team index not found. Desyncs might happen");
|
||||
await refresh();
|
||||
return;
|
||||
}
|
||||
game.teams.splice(index, 1);
|
||||
},
|
||||
consumeEffect: async (data: any) => {
|
||||
console.warn("consumeEffect not implemented");
|
||||
},
|
||||
endGame: async () => {
|
||||
alert("game ended");
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user