新增game.doAsyncInOrder方法,并修改孙休“兴学”为范例

This commit is contained in:
kuangthree 2024-01-20 11:11:24 +08:00
parent e810ac7618
commit 92bee0717e
2 changed files with 42 additions and 45 deletions

View File

@ -8067,61 +8067,42 @@ game.import('character',function(lib,game,ui,get,ai,_status){
audio:2, audio:2,
trigger:{player:'phaseJieshuBegin'}, trigger:{player:'phaseJieshuBegin'},
direct:true, direct:true,
content:function(){ async content(event,trigger,player){
'step 0'
var num=player.hp; var num=player.hp;
if(!player.hasSkill('yanzhu')){ if(!player.hasSkill('yanzhu')){
num=player.maxHp; num=player.maxHp;
} }
player.chooseTarget([1,num],get.prompt2('xingxue')).set('ai',function(target){ const {result:{targets,bool}}=
await player.chooseTarget([1,num],get.prompt2('xingxue')).set('ai',function(target){
var att=get.attitude(_status.event.player,target); var att=get.attitude(_status.event.player,target);
if(target.countCards('he')) return att; if(target.countCards('he')) return att;
return att/10; return att/10;
}); });
'step 1' if(bool){
if(result.bool){ player.logSkill('xingxue',targets);
player.logSkill('xingxue',result.targets); const chooseToPutCard = async function(target){
event.targets=result.targets; await target.draw();
event.targets.sort(lib.sort.seat); if(target.countCards('he')){
const {result:{cards,bool}} =
await target.chooseCard('选择一张牌置于牌堆顶','he',true);
if(bool){
await target.lose(cards,ui.cardPile,'insert');
} }
else{
event.finish();
}
'step 2'
if(event.targets.length){
var target=event.targets.shift();
target.draw();
event.current=target;
}
else{
event.finish();
}
'step 3'
if(event.current&&event.current.countCards('he')){
event.current.chooseCard('选择一张牌置于牌堆顶','he',true);
}
else{
event.goto(2);
}
'step 4'
if(result&&result.cards){
event.card=result.cards[0];
event.current.lose(result.cards,ui.cardPile,'insert');
game.broadcastAll(function(player){ game.broadcastAll(function(player){
var cardx=ui.create.card(); var cardx=ui.create.card();
cardx.classList.add('infohidden'); cardx.classList.add('infohidden');
cardx.classList.add('infoflip'); cardx.classList.add('infoflip');
player.$throw(cardx,1000,'nobroadcast'); player.$throw(cardx,1000,'nobroadcast');
},event.current); },target);
if(player == game.me){
game.delay(0.5);
} }
else{
event.card=null;
} }
'step 5' };
if(event.current==game.me) game.delay(0.5); await game.doAsyncInOrder(targets,chooseToPutCard);
event.goto(2);
} }
}, },
},
yanzhu:{ yanzhu:{
audio:2, audio:2,
enable:'phaseUse', enable:'phaseUse',

View File

@ -8434,6 +8434,22 @@ export class Game extends Uninstantable {
return true; return true;
}); });
} }
/**
* 此方法用于对所有targets按顺序执行一个async函数
*
* @param { Player[] } targets 需要执行async方法的目标
* @param { AsyncFunction } asyncFunc 需要执行的async方法
* @param { sort } function 排序器默认为lib.sort.seat
*/
static async doAsyncInOrder(targets,asyncFunc,sort){
if(!sort)sort = lib.sort.seat;
let sortedTargets = targets.sort(sort);
for(let i=0;i<sortedTargets.length;i++){
let target = sortedTargets[i];
await Promise.resolve(asyncFunc(target,i));
}
}
}; };
export const game = Game; export const game = Game;