不通过throw error结束async content

This commit is contained in:
nonameShijian 2024-05-21 11:07:53 +08:00
parent d09b270b7f
commit 0a72799c87
4 changed files with 76 additions and 37 deletions

View File

@ -258,7 +258,7 @@ export class GameEvent {
* @param {ArrayLike<Function> | Function | keyof typeof lib.element.content} item
*/
setContent(item: ArrayLike<Function> | Function | keyof typeof lib.element.content): this;
content: any;
content: ContentFuncByAll | GeneratorContentFuncByAll | OldContentFuncByAll;
/**
*
* @param {Function | keyof typeof lib.element.contents} contents

View File

@ -6027,15 +6027,7 @@ export class Game {
}
});
event.resolveContent = resolveContent;
event.content(event, trigger, player)
.then(() => resolveContent())
.catch((error) => {
resolveContent();
//再抛出异常
if (error !== "event_finish") {
throw error;
}
});
event.content(event, trigger, player).finally(() => resolveContent());
} else {
event.content(event, step, source, player, target, targets, card, cards, skill, forced, num, trigger, result, _status, lib, game, ui, get, ai);
resolve();

View File

@ -196,6 +196,10 @@ export class GameEvent {
* @type { string }
*/
triggername;
/**
* @type { ContentFuncByAll | GeneratorContentFuncByAll | OldContentFuncByAll }
*/
content;
/**
* @param {keyof this} key
* @param {number} [value]
@ -306,15 +310,6 @@ export class GameEvent {
this.numFixed = true;
return this;
}
forceFinish() {
if (!this.finished) {
this.finished = true;
if (this.content instanceof AsyncFunction) {
throw "event_finish";
}
}
return this;
}
finish() {
this.finished = true;
return this;
@ -369,7 +364,11 @@ export class GameEvent {
}
cancel(arg1, arg2, notrigger) {
this.untrigger(arg1, arg2);
this.forceFinish();
// this.forceFinish();
this.finish();
if (typeof this.resolveContent == 'function') {
this.resolveContent();
}
if (notrigger != "notrigger") {
if (this.player && lib.phaseName.includes(this.name))
this.player.getHistory("skipped").add(this.name);
@ -440,9 +439,11 @@ export class GameEvent {
try {
if (
!(lib.element.content[item] instanceof AsyncFunction) &&
// @ts-ignore
!lib.element.content[item]._parsed
) {
lib.element.content[item] = lib.init.parsex(lib.element.content[item]);
// @ts-ignore
lib.element.content[item]._parsed = true;
}
} catch {

View File

@ -64,20 +64,49 @@ export class GameEventPromise extends Promise {
// 不直接game.loop(event)是因为需要让别人可以手动set()和setContent()
// 再执行game.loop是因为原有的game.loop被await卡住了
// 得新执行一个只执行这个异步事件的game.loop
// 事件自行处理skip情况
_status.event.next.remove(eventPromise);
if (event.player && event.player.skipList.includes(event.name)) {
_status.event.trigger(event.name + "Skipped");
event.player.skipList.remove(event.name);
if (lib.phaseName.includes(event.name))
event.player.getHistory("skipped").add(event.name);
_status.event.next.remove(eventPromise);
event.finish();
// @ts-ignore
resolve();
/**
* @param { import("noname-typings/nonameModules/noname/library/index.js").GameEventPromise } event
*/
const check = event => {
if (event.player) {
const { player } = event;
if (player.skipList.includes(event.name)) {
_status.event.trigger(event.name + "Skipped");
player.skipList.remove(event.name);
if (lib.phaseName.includes(event.name))
player.getHistory("skipped").add(event.name);
_status.event.next.remove(event);
event.finish();
resolve(event.toEvent());
} else if (player.classList.contains("dead") && !event.forceDie && event.name != "phaseLoop") {
game.broadcastAll(function () {
while (_status.dieClose.length) {
_status.dieClose.shift().close();
}
});
if (event._oncancel) event._oncancel();
event.finish();
resolve(event.toEvent());
} else if (player.removed && event.name != "phaseLoop") {
event.finish();
resolve(event.toEvent());
} else if (player.isOut() && event.name != "phaseLoop" && !event.includeOut) {
if (event.name == "phase" && player == _status.roundStart && !event.skill) {
_status.roundSkipped = true;
}
event.finish();
resolve(event.toEvent());
}
}
};
check(eventPromise)
if (eventPromise.finished) {
return eventPromise;
}
const oldEvent = _status.event;
// 处理eventNeutralized反复横跳
if (!oldEvent.finished) {
@ -89,9 +118,13 @@ export class GameEventPromise extends Promise {
return game.loop(eventPromise).then(() => {
// 有时候event.finished还是false
return eventPromise;
}).then(event => {
check(event.toPromise());
return event.toPromise();
});
}
else {
// event.neutralize()执行后只允许带有eventNeutralized的arrangeTrigger执行
if (eventPromise.name == "arrangeTrigger" &&
eventPromise.triggername == "eventNeutralized") {
if (_status.event != eventPromise) {
@ -102,17 +135,30 @@ export class GameEventPromise extends Promise {
return game.loop(eventPromise).then(() => {
// 有时候event.finished还是false
return eventPromise;
}).then(() => {
// 如果它终于不给我跳了
// 就给老子停
if (oldEvent.finished === true && oldEvent.resolveContent) {
}).then(event => {
// 如果它终于不给我跳了就给老子停
if (oldEvent.finished === true && typeof oldEvent.resolveContent == 'function') {
oldEvent.resolveContent();
}
return eventPromise;
return event.toPromise();
});
}
// 否则以其他形式使finished为true的时候停止oldEvent
else {
console.log('不继续执行event', eventPromise.toEvent());
console.log(`不继续执行event(${eventPromise.name})`, eventPromise.toEvent());
// 给老子停
if (oldEvent.finished === true && typeof oldEvent.resolveContent == 'function') {
oldEvent.resolveContent();
console.log(`事件结束(${oldEvent.name})`, oldEvent.toEvent());
}
else {
if (oldEvent.finished === true) {
console.log(`事件未结束(${oldEvent.name})`, oldEvent.toEvent());
}
if (typeof oldEvent.resolveContent != 'function') {
console.log(`事件未结束, event.resolveContent不是一个函数`, oldEvent.toEvent());
}
}
}
}
}