Spaces:
Sleeping
Sleeping
File size: 2,758 Bytes
90cbf22 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
import { v, Infer, ObjectType } from 'convex/values';
import { Game } from './game';
import {
DAY_DURATION,
NIGHT_DURATION,
WWOLF_VOTE_DURATION,
PLAYER_KILL_VOTE_DURATION,
LLM_VOTE_DURATION,
} from '../constants';
import { processVotes } from './voting';
type CycleState = 'Day' | 'Night' | 'WerewolfVoting' | 'PlayerKillVoting' | 'LLMsVoting' | 'LobbyState'
const stateDurations: { [key in CycleState]: number } = {
Day: DAY_DURATION,
Night: NIGHT_DURATION,
WerewolfVoting: WWOLF_VOTE_DURATION,
PlayerKillVoting: PLAYER_KILL_VOTE_DURATION,
LLMsVoting: LLM_VOTE_DURATION,
LobbyState: Infinity
};
const normalCycle: CycleState[] = [
'Day',
'Night',
'WerewolfVoting',
'PlayerKillVoting',
];
export const gameCycleSchema = {
currentTime: v.number(),
cycleState: v.union(
v.literal('Day'),
v.literal('Night'),
v.literal('WerewolfVoting'),
v.literal('PlayerKillVoting'),
v.literal('LLMsVoting'),
v.literal('LobbyState'),
),
cycleIndex: v.number(),
};
export type SerializedGameCycle = ObjectType<typeof gameCycleSchema>;
const onStateChange = (prevState: CycleState, newState: CycleState, game: Game, now: number) => {
console.log(`state changed: ${ prevState } -> ${ newState }`);
if (prevState === 'PlayerKillVoting') {
const mostVotedPlayer = processVotes(game.world.votes, [...game.world.players.values()])[0];
// TODO: Kill the player
const playerToKill = game.world.players.get(mostVotedPlayer)
if (playerToKill != undefined) {
playerToKill.kill(game, now)
}
}
if (prevState === 'WerewolfVoting') {
const mostVotedPlayer = processVotes(game.world.votes, [...game.world.players.values()])[0];
// TODO: Check if most voted player is werewolf
}
// TODO: Implement LLM voting
};
export class GameCycle {
currentTime: number;
cycleState: CycleState;
cycleIndex: number;
constructor(serialized: SerializedGameCycle) {
const { currentTime, cycleState, cycleIndex } = serialized;
this.currentTime = currentTime;
this.cycleState = cycleState;
this.cycleIndex = cycleIndex;
}
// Tick method to increment the counter
tick(game: Game, tickDuration: number) {
this.currentTime += tickDuration;
if (this.currentTime >= stateDurations[this.cycleState]) {
const prevState = this.cycleState;
this.currentTime = 0;
this.cycleIndex = (this.cycleIndex + 1) % normalCycle.length;
this.cycleState = normalCycle[this.cycleIndex];
onStateChange(prevState, this.cycleState, game, tickDuration);
}
}
serialize(): SerializedGameCycle {
const { currentTime, cycleState, cycleIndex } = this;
return {
currentTime,
cycleState,
cycleIndex,
};
}
}
|