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 {
throw new Error(`Content ${item} may not exist.\nlib.element.content[${item}] = ${lib.element.content[item]}`);
}
this.content = 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];
}
break;
}
return this;

View File

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