fix: gen content state shared.

This commit is contained in:
Rintim 2024-03-01 15:47:37 +08:00
parent 5fffd71852
commit 76eacc3f36
No known key found for this signature in database
GPG Key ID: BE9E1EA615BACFCF
2 changed files with 1725 additions and 1702 deletions

View File

@ -409,7 +409,18 @@ export class GameEvent {
catch { catch {
throw new Error(`Content ${item} may not exist.\nlib.element.content[${item}] = ${lib.element.content[item]}`); throw new Error(`Content ${item} may not exist.\nlib.element.content[${item}] = ${lib.element.content[item]}`);
} }
if (typeof lib.element.content[item] === "undefined")
throw new Error(`Cannot find lib.element.content[${item}]`)
// Generator的状态重置
else if (lib.element.content[item]._gen) {
this.content = lib.element.content[item].bind({
gen: null,
last: undefined
})
} else {
this.content = lib.element.content[item]; this.content = lib.element.content[item];
}
break; break;
} }
return this; return this;

View File

@ -1,13 +1,15 @@
import { nonameInitialized, assetURL, userAgent, Uninstantable, GeneratorFunction, AsyncFunction } from "../../util/index.js"; import { nonameInitialized, assetURL, userAgent, Uninstantable, GeneratorFunction, AsyncFunction } from "../../util/index.js";
import { AI as ai } from '../../ai/index.js'; import { AI as ai } from '../../ai/index.js'
import { Get as get } from '../../get/index.js'; import { Get as get } from '../../get/index.js'
import { Game as game } from '../../game/index.js'; import { Game, Game as game } from '../../game/index.js'
import { Library as lib } from "../index.js"; import { Library as lib } from "../index.js"
import { status as _status } from '../../status/index.js'; import { status as _status } from '../../status/index.js'
import { UI as ui } from '../../ui/index.js'; import { UI as ui } from '../../ui/index.js'
import { GNC as gnc } from '../../gnc/index.js'; import { GNC as gnc } from '../../gnc/index.js'
import { LibInitPromises } from "./promises.js"; import { LibInitPromises } from "./promises.js"
import { GameEvent } from "../element/gameEvent.js"
import { GameEventPromise } from "../element/gameEventPromise.js"
export class LibInit extends Uninstantable { export class LibInit extends Uninstantable {
/** /**
@ -693,30 +695,37 @@ export class LibInit extends Uninstantable {
} }
case "function": case "function":
if (gnc.is.generatorFunc(item)) { if (gnc.is.generatorFunc(item)) {
let gen, lastEvent; // let gen, lastEvent;
return function* (event, step, source, player, target, targets, card, cards, skill, forced, num, trigger, result, _status, lib, game, ui, get, ai) { return function* (event, step, source, player, target, targets, card, cards, skill, forced, num, trigger, result, _status, lib, game, ui, get, ai) {
event.step = NaN; event.step = NaN;
if (!gen) gen = item(event, { if (!this.gen) this.gen = item(event, {
event: event, event,
step: step, step,
source: source, source,
player: player, player,
target: target, target,
targets: targets, targets,
card: card, card,
cards: cards, cards,
skill: skill, skill,
forced: forced, forced,
num: num, num,
trigger: trigger, trigger,
result: result result
}); });
var res = gen.next((lastEvent && (typeof lastEvent == 'object') && ("result" in lastEvent)) ? lastEvent.result : lastEvent);
let res
if (!this.last) res = this.gen.next()
else if (typeof this.last !== "object") res = this.gen.next(this.last)
else if (this.last instanceof GameEvent || this.last instanceof GameEventPromise)
res = this.gen.next(this.last.result)
else res = this.gen.next(this.last)
if (res.done){ if (res.done){
gen = null; this.gen = null;
return event.finish(); return event.finish();
} }
var currentResult = res.value; let currentResult = res.value;
// TODO: use `event.debugger` to replace source // TODO: use `event.debugger` to replace source
if (typeof currentResult == "function") yield currentResult; if (typeof currentResult == "function") yield currentResult;
else { else {
@ -724,9 +733,12 @@ export class LibInit extends Uninstantable {
event.step = currentResult[1]; event.step = currentResult[1];
currentResult = currentResult[0]; currentResult = currentResult[0];
} }
lastEvent = currentResult; this.last = currentResult;
}
} }
}.bind({
gen: null,
last: undefined
})
} else if (item._parsed) return item; } else if (item._parsed) return item;
// falls through // falls through
default: default: