Compare commits

..

2 Commits

Author SHA1 Message Date
hypercross 5235ba7def feat(slay-the-spire-like): support source entity in effect targeting
Update `getEffectTargets` to accept a `sourceEntityKey`. This allows
"self" targets to correctly resolve to the entity triggering the
effect (e.g., a player or a specific enemy) rather than defaulting
to a hardcoded player reference.
2026-04-22 08:56:06 +08:00
hypercross 6cb0626f05 refactor: update produce signature to return undefined
Change the return type of `produce` and `produceAsync` from `void` to
`undefined` in `MutableSignal` and `IGameContext` to more accurately
reflect that the mutation functions should not return a value.
2026-04-22 01:13:39 +08:00
5 changed files with 43 additions and 22 deletions

View File

@ -15,8 +15,8 @@ import { Mulberry32RNG, ReadonlyRNG, RNG } from "@/utils/rng";
export interface IGameContext<TState extends Record<string, unknown> = {}> {
get value(): TState;
get rng(): ReadonlyRNG;
produce(fn: (draft: TState) => void): void;
produceAsync(fn: (draft: TState) => void): Promise<void>;
produce(fn: (draft: TState) => undefined): void;
produceAsync(fn: (draft: TState) => undefined): Promise<void>;
run<T>(input: string): Promise<CommandResult<T>>;
runParsed<T>(command: Command): Promise<CommandResult<T>>;
prompt: <TResult, TArgs extends any[] = any[]>(
@ -51,10 +51,10 @@ export function createGameContext<TState extends Record<string, unknown> = {}>(
get rng() {
return this._rng;
},
produce(fn) {
produce(fn: (draft: TState) => undefined) {
return state.produce(fn);
},
produceAsync(fn) {
produceAsync(fn: (draft: TState) => undefined) {
return state.produceAsync(fn);
},
run<T>(input: string) {

View File

@ -107,12 +107,16 @@ export function* getEffectTargets(
target: CardEffectTarget | EffectTarget,
game: CombatGameContext,
targetId?: string,
sourceEntityKey: "player" | string = "player",
) {
if (target === "all" || target === "team") {
for (const enemy of getAliveEnemies(game.value)) {
yield enemy;
}
} else if (target === "self") {
const entity = getCombatEntity(game.value, sourceEntityKey);
if (entity) yield entity;
} else if (target === "player") {
yield game.value.player;
} else if (target === "target") {
if (!targetId) return;

View File

@ -65,9 +65,9 @@ export function createTriggers(run: IRunContext) {
for (const cardId of Object.values(regions.hand.childIds)) {
await triggers.onCardDiscarded.execute(ctx.game, { cardId });
}
await ctx.game.produceAsync(
(draft) => (draft.player.energy = draft.player.maxEnergy),
);
await ctx.game.produceAsync((draft) => {
draft.player.energy = draft.player.maxEnergy;
});
await triggers.onDraw.execute(ctx.game, { count: 5 });
}),
onShuffle: createTrigger("onShuffle", async (ctx) => {
@ -98,7 +98,12 @@ export function createTriggers(run: IRunContext) {
for (const { trigger, target, effects } of card.cardData.effects) {
if (trigger !== "onPlay") continue;
for (const [effect, stacks] of effects)
for (const entity of getEffectTargets(target, ctx.game, ctx.targetId))
for (const entity of getEffectTargets(
target,
ctx.game,
ctx.targetId,
source,
))
await triggers.onEffectApplied.execute(ctx.game, {
effect,
entityKey: entity.id,
@ -121,7 +126,12 @@ export function createTriggers(run: IRunContext) {
for (const { trigger, target, effects } of card.cardData.effects) {
if (trigger !== "onDiscard") continue;
for (const [effect, stacks] of effects)
for (const entity of getEffectTargets(target, ctx.game))
for (const entity of getEffectTargets(
target,
ctx.game,
undefined,
source,
))
await triggers.onEffectApplied.execute(ctx.game, {
effect,
entityKey: entity.id,
@ -142,7 +152,12 @@ export function createTriggers(run: IRunContext) {
for (const { trigger, target, effects } of card.cardData.effects) {
if (trigger !== "onDraw") continue;
for (const [effect, stacks] of effects)
for (const entity of getEffectTargets(target, ctx.game))
for (const entity of getEffectTargets(
target,
ctx.game,
undefined,
source,
))
await triggers.onEffectApplied.execute(ctx.game, {
effect,
entityKey: entity.id,
@ -236,7 +251,12 @@ export function createTriggers(run: IRunContext) {
const source = ctx.sourceEntityKey ?? enemy.id;
for (const [target, effect, stacks] of intent.effects) {
for (const entity of getEffectTargets(target, ctx.game))
for (const entity of getEffectTargets(
target,
ctx.game,
undefined,
source,
))
await triggers.onEffectApplied.execute(ctx.game, {
effect,
entityKey: entity.id,

View File

@ -1,5 +1,5 @@
import {Signal, SignalOptions} from '@preact/signals-core';
import {create} from 'mutative';
import { Signal, SignalOptions } from "@preact/signals-core";
import { create } from "mutative";
export class MutableSignal<T> extends Signal<T> {
private _interruptions: Promise<void>[] = [];
@ -7,7 +7,7 @@ export class MutableSignal<T> extends Signal<T> {
public constructor(t?: T, options?: SignalOptions<T>) {
super(t, options);
}
produce(fn: (draft: T) => void) {
produce(fn: (draft: T) => undefined) {
this.value = create(this.value, fn);
}
@ -30,13 +30,16 @@ export class MutableSignal<T> extends Signal<T> {
* produce `addInterruption` Promise
*
*/
async produceAsync(fn: (draft: T) => void): Promise<void> {
async produceAsync(fn: (draft: T) => undefined): Promise<void> {
await Promise.allSettled(this._interruptions);
this._interruptions = [];
this.produce(fn);
}
}
export function mutableSignal<T>(initial?: T, options?: SignalOptions<T>): MutableSignal<T> {
export function mutableSignal<T>(
initial?: T,
options?: SignalOptions<T>,
): MutableSignal<T> {
return new MutableSignal<T>(initial, options);
}

View File

@ -382,12 +382,6 @@ describe("desert triggers", () => {
}),
);
const triggers = getTriggers();
const stormEffect = createEffect("storm", "permanent");
ctx._state.produce((draft) => {
const enemy = draft.enemies[0];
enemy.effects.storm = { data: stormEffect, stacks: 2 };
});
await triggers.onEnemyIntent.execute(ctx, { enemyId: "风暴之灵-0" });