refactor: reformat code and introduce IGameContextExport
- Reformat `src/core/game.ts` and sample types to use 2-space indentation - Add `IGameContextExport` to hide internal test properties - Update `CombatGameContext` to use the exported context type
This commit is contained in:
parent
a82b6b0685
commit
601eb0f417
160
src/core/game.ts
160
src/core/game.ts
|
|
@ -1,80 +1,104 @@
|
||||||
import {MutableSignal, mutableSignal} from "@/utils/mutable-signal";
|
import { MutableSignal, mutableSignal } from "@/utils/mutable-signal";
|
||||||
import {
|
import {
|
||||||
Command,
|
Command,
|
||||||
CommandRegistry, CommandResult,
|
CommandRegistry,
|
||||||
CommandRunnerContextExport,
|
CommandResult,
|
||||||
CommandSchema,
|
CommandRunnerContextExport,
|
||||||
createCommandRegistry,
|
CommandSchema,
|
||||||
createCommandRunnerContext, parseCommandSchema,
|
createCommandRegistry,
|
||||||
|
createCommandRunnerContext,
|
||||||
|
parseCommandSchema,
|
||||||
} from "@/utils/command";
|
} from "@/utils/command";
|
||||||
import {PromptValidator} from "@/utils/command/command-runner";
|
import { PromptValidator } from "@/utils/command/command-runner";
|
||||||
import {Mulberry32RNG, ReadonlyRNG, RNG} from "@/utils/rng";
|
import { Mulberry32RNG, ReadonlyRNG, RNG } from "@/utils/rng";
|
||||||
|
|
||||||
export interface IGameContext<TState extends Record<string, unknown> = {} > {
|
export interface IGameContext<TState extends Record<string, unknown> = {}> {
|
||||||
get value(): TState;
|
get value(): TState;
|
||||||
get rng(): ReadonlyRNG;
|
get rng(): ReadonlyRNG;
|
||||||
produce(fn: (draft: TState) => void): void;
|
produce(fn: (draft: TState) => void): void;
|
||||||
produceAsync(fn: (draft: TState) => void): Promise<void>;
|
produceAsync(fn: (draft: TState) => void): Promise<void>;
|
||||||
run<T>(input: string): Promise<CommandResult<T>>;
|
run<T>(input: string): Promise<CommandResult<T>>;
|
||||||
runParsed<T>(command: Command): Promise<CommandResult<T>>;
|
runParsed<T>(command: Command): Promise<CommandResult<T>>;
|
||||||
prompt: <TResult,TArgs extends any[]=any[]>(def: PromptDef<TArgs>, validator: PromptValidator<TResult,TArgs>, currentPlayer?: string | null) => Promise<TResult>;
|
prompt: <TResult, TArgs extends any[] = any[]>(
|
||||||
|
def: PromptDef<TArgs>,
|
||||||
// test only
|
validator: PromptValidator<TResult, TArgs>,
|
||||||
_state: MutableSignal<TState>;
|
currentPlayer?: string | null,
|
||||||
_commands: CommandRunnerContextExport<IGameContext<TState>>;
|
) => Promise<TResult>;
|
||||||
_rng: RNG;
|
|
||||||
|
// test only
|
||||||
|
_state: MutableSignal<TState>;
|
||||||
|
_commands: CommandRunnerContextExport<IGameContext<TState>>;
|
||||||
|
_rng: RNG;
|
||||||
}
|
}
|
||||||
|
export type IGameContextExport<TState extends Record<string, unknown> = {}> =
|
||||||
|
Omit<IGameContext<TState>, "_state" | "_commands" | "_rng">;
|
||||||
|
|
||||||
export function createGameContext<TState extends Record<string, unknown> = {} >(
|
export function createGameContext<TState extends Record<string, unknown> = {}>(
|
||||||
commandRegistry: CommandRegistry<IGameContext<TState>>,
|
commandRegistry: CommandRegistry<IGameContext<TState>>,
|
||||||
initialState?: TState | (() => TState)
|
initialState?: TState | (() => TState),
|
||||||
): IGameContext<TState> {
|
): IGameContext<TState> {
|
||||||
const stateValue = typeof initialState === 'function' ? initialState() : initialState ?? {} as TState;
|
const stateValue =
|
||||||
const state = mutableSignal(stateValue);
|
typeof initialState === "function"
|
||||||
let commands: CommandRunnerContextExport<IGameContext<TState>> = null as any;
|
? initialState()
|
||||||
|
: (initialState ?? ({} as TState));
|
||||||
|
const state = mutableSignal(stateValue);
|
||||||
|
let commands: CommandRunnerContextExport<IGameContext<TState>> = null as any;
|
||||||
|
|
||||||
const context: IGameContext<TState> = {
|
const context: IGameContext<TState> = {
|
||||||
get value(): TState {
|
get value(): TState {
|
||||||
return state.value;
|
return state.value;
|
||||||
},
|
},
|
||||||
get rng() {
|
get rng() {
|
||||||
return this._rng;
|
return this._rng;
|
||||||
},
|
},
|
||||||
produce(fn) {
|
produce(fn) {
|
||||||
return state.produce(fn);
|
return state.produce(fn);
|
||||||
},
|
},
|
||||||
produceAsync(fn) {
|
produceAsync(fn) {
|
||||||
return state.produceAsync(fn);
|
return state.produceAsync(fn);
|
||||||
},
|
},
|
||||||
run<T>(input: string) {
|
run<T>(input: string) {
|
||||||
return commands.run<T>(input);
|
return commands.run<T>(input);
|
||||||
},
|
},
|
||||||
runParsed<T>(command: Command) {
|
runParsed<T>(command: Command) {
|
||||||
return commands.runParsed<T>(command);
|
return commands.runParsed<T>(command);
|
||||||
},
|
},
|
||||||
prompt(def, validator, currentPlayer) {
|
prompt(def, validator, currentPlayer) {
|
||||||
return commands.prompt(def.schema, validator, def.hintText, currentPlayer);
|
return commands.prompt(
|
||||||
},
|
def.schema,
|
||||||
|
validator,
|
||||||
_state: state,
|
def.hintText,
|
||||||
_commands: commands,
|
currentPlayer,
|
||||||
_rng: new Mulberry32RNG(),
|
);
|
||||||
};
|
},
|
||||||
|
|
||||||
context._commands = commands = createCommandRunnerContext(commandRegistry, context);
|
_state: state,
|
||||||
|
_commands: commands,
|
||||||
return context;
|
_rng: new Mulberry32RNG(),
|
||||||
|
};
|
||||||
|
|
||||||
|
context._commands = commands = createCommandRunnerContext(
|
||||||
|
commandRegistry,
|
||||||
|
context,
|
||||||
|
);
|
||||||
|
|
||||||
|
return context;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type PromptDef<TArgs extends any[]=any[]> = {
|
export type PromptDef<TArgs extends any[] = any[]> = {
|
||||||
schema: CommandSchema,
|
schema: CommandSchema;
|
||||||
hintText?: string,
|
hintText?: string;
|
||||||
}
|
};
|
||||||
export function createPromptDef<TArgs extends any[]=any[]>(schema: CommandSchema | string, hintText?: string): PromptDef<TArgs> {
|
export function createPromptDef<TArgs extends any[] = any[]>(
|
||||||
schema = typeof schema === 'string' ? parseCommandSchema(schema) : schema;
|
schema: CommandSchema | string,
|
||||||
return { schema, hintText };
|
hintText?: string,
|
||||||
|
): PromptDef<TArgs> {
|
||||||
|
schema = typeof schema === "string" ? parseCommandSchema(schema) : schema;
|
||||||
|
return { schema, hintText };
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createGameCommandRegistry<TState extends Record<string, unknown> = {} >() {
|
export function createGameCommandRegistry<
|
||||||
return createCommandRegistry<IGameContext<TState>>();
|
TState extends Record<string, unknown> = {},
|
||||||
}
|
>() {
|
||||||
|
return createCommandRegistry<IGameContext<TState>>();
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,53 +1,59 @@
|
||||||
import type { PlayerDeck } from "../deck/types";
|
import type { PlayerDeck } from "../deck/types";
|
||||||
import {EnemyData, IntentData} from "@/samples/slay-the-spire-like/system/types";
|
import {
|
||||||
import {EffectData} from "@/samples/slay-the-spire-like/system/types";
|
EnemyData,
|
||||||
import {GridInventory} from "@/samples/slay-the-spire-like/system/grid-inventory";
|
IntentData,
|
||||||
import {GameItemMeta} from "@/samples/slay-the-spire-like/system/progress";
|
} from "@/samples/slay-the-spire-like/system/types";
|
||||||
|
import { EffectData } from "@/samples/slay-the-spire-like/system/types";
|
||||||
|
import { GridInventory } from "@/samples/slay-the-spire-like/system/grid-inventory";
|
||||||
|
import { GameItemMeta } from "@/samples/slay-the-spire-like/system/progress";
|
||||||
|
|
||||||
export type EffectTable = Record<string, {data: EffectData, stacks: number}>;
|
export type EffectTable = Record<string, { data: EffectData; stacks: number }>;
|
||||||
|
|
||||||
export type CombatEntity = {
|
export type CombatEntity = {
|
||||||
id: string; // player is just "player"
|
id: string; // player is just "player"
|
||||||
effects: EffectTable;
|
effects: EffectTable;
|
||||||
hp: number;
|
hp: number;
|
||||||
maxHp: number;
|
maxHp: number;
|
||||||
isAlive: boolean;
|
isAlive: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type PlayerEntity = CombatEntity & {
|
export type PlayerEntity = CombatEntity & {
|
||||||
energy: number;
|
energy: number;
|
||||||
maxEnergy: number;
|
maxEnergy: number;
|
||||||
deck: PlayerDeck;
|
deck: PlayerDeck;
|
||||||
itemEffects: Record<string, EffectTable>;
|
itemEffects: Record<string, EffectTable>;
|
||||||
}
|
};
|
||||||
|
|
||||||
export type EnemyEntity = CombatEntity & {
|
export type EnemyEntity = CombatEntity & {
|
||||||
enemy: EnemyData;
|
enemy: EnemyData;
|
||||||
intents: Record<string, IntentData>;
|
intents: Record<string, IntentData>;
|
||||||
currentIntent: IntentData;
|
currentIntent: IntentData;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type CombatPhase = "playerTurn" | "enemyTurn" | "combatEnd";
|
export type CombatPhase = "playerTurn" | "enemyTurn" | "combatEnd";
|
||||||
export type CombatResult = "victory" | "defeat";
|
export type CombatResult = "victory" | "defeat";
|
||||||
|
|
||||||
export type LootEntry = {
|
export type LootEntry =
|
||||||
type: "gold";
|
| {
|
||||||
amount: number;
|
type: "gold";
|
||||||
} | {
|
amount: number;
|
||||||
type: "item",
|
}
|
||||||
itemId: string;
|
| {
|
||||||
};
|
type: "item";
|
||||||
|
itemId: string;
|
||||||
|
};
|
||||||
|
|
||||||
export type CombatState = {
|
export type CombatState = {
|
||||||
enemies: EnemyEntity[];
|
enemies: EnemyEntity[];
|
||||||
player: PlayerEntity;
|
player: PlayerEntity;
|
||||||
inventory: GridInventory<GameItemMeta>;
|
inventory: GridInventory<GameItemMeta>;
|
||||||
|
|
||||||
phase: CombatPhase;
|
phase: CombatPhase;
|
||||||
turnNumber: number;
|
turnNumber: number;
|
||||||
result: CombatResult | null;
|
result: CombatResult | null;
|
||||||
|
|
||||||
loot: LootEntry[];
|
loot: LootEntry[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export type CombatGameContext = import("@/core/game").IGameContext<CombatState>;
|
export type CombatGameContext =
|
||||||
|
import("@/core/game").IGameContextExport<CombatState>;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue