Add the ability of chain calling for events, and fix several connect mode bugs.

This commit is contained in:
Tipx-L 2023-10-11 02:41:25 -07:00
parent bc14608256
commit e0020298ab
2 changed files with 103 additions and 59 deletions

View File

@ -8236,7 +8236,7 @@
lib.ui=ui; lib.ui=ui;
lib.ai=ai; lib.ai=ai;
lib.game=game; lib.game=game;
_status.event=new lib.element.Event(); _status.event=new lib.element.Event().finish();
HTMLDivElement.prototype.animate=function(name,time){ HTMLDivElement.prototype.animate=function(name,time){
var that; var that;
@ -30354,45 +30354,49 @@
changeToZero(){ changeToZero(){
this.num=0; this.num=0;
this.numFixed=true; this.numFixed=true;
return this;
} }
finish(){ finish(){
this.finished=true; this.finished=true;
return this;
} }
putStepCache(key,value){ putStepCache(key,value){
if(!this._stepCache){ if(!this._stepCache){
this._stepCache = {}; this._stepCache={};
} }
this._stepCache[key] = value; this._stepCache[key]=value;
return this;
} }
getStepCache(key){ getStepCache(key){
if(!this._stepCache)return undefined; if(!this._stepCache) return undefined;
return this._stepCache[key]; return this._stepCache[key];
} }
clearStepCache(key){ clearStepCache(key){
if(key !== undefined && key !== null){ if(key!==undefined&&key!==null){
delete this._stepCache[key]; delete this._stepCache[key];
} }
delete this._stepCache; delete this._stepCache;
return this;
} }
callFuncUseStepCache(prefix,func,params){ callFuncUseStepCache(prefix,func,params){
if(typeof func != 'function')return; if(typeof func!='function') return;
if(_status.closeStepCache)return func.apply(null,params); if(_status.closeStepCache) return func.apply(null,params);
var cacheKey = "["+prefix+"]"+get.paramToCacheKey.apply(null,params); var cacheKey="["+prefix+"]"+get.paramToCacheKey.apply(null,params);
var ret = this.getStepCache(cacheKey); var ret=this.getStepCache(cacheKey);
if(ret === undefined || ret === null){ if(ret===undefined||ret===null){
ret = func.apply(null,params); ret=func.apply(null,params);
this.putStepCache(cacheKey,ret); this.putStepCache(cacheKey,ret);
} }
return ret; return ret;
} }
putTempCache(key1,key2,value){ putTempCache(key1,key2,value){
if(!this._tempCache){ if(!this._tempCache){
this._tempCache = {}; this._tempCache={};
} }
if(!this._tempCache[key1]){ if(!this._tempCache[key1]){
this._tempCache[key1] = {}; this._tempCache[key1]={};
} }
this._tempCache[key1][key2] = value; this._tempCache[key1][key2]=value;
return value; return value;
} }
getTempCache(key1,key2){ getTempCache(key1,key2){
@ -30409,7 +30413,9 @@
this.finish(); this.finish();
if(notrigger!='notrigger'){ if(notrigger!='notrigger'){
this.trigger(this.name+'Cancelled'); this.trigger(this.name+'Cancelled');
if(this.player&&lib.phaseName.contains(this.name)) this.player.getHistory('skipped').add(this.name)} if(this.player&&lib.phaseName.contains(this.name)) this.player.getHistory('skipped').add(this.name);
}
return this;
} }
neutralize(event){ neutralize(event){
this.untrigger(); this.untrigger();
@ -30417,18 +30423,22 @@
this._neutralized=true; this._neutralized=true;
this.trigger('eventNeutralized'); this.trigger('eventNeutralized');
this._neutralize_event=event||_status.event; this._neutralize_event=event||_status.event;
return this;
} }
unneutralize(){ unneutralize(){
this.untrigger(); this.untrigger();
delete this._neutralized; delete this._neutralized;
delete this.finished; delete this.finished;
if(this.type=='card'&&this.card&&this.name=='sha') this.directHit=true; if(this.type=='card'&&this.card&&this.name=='sha') this.directHit=true;
return this;
} }
goto(step){ goto(step){
this.step=step-1; this.step=step-1;
return this;
} }
redo(){ redo(){
this.step--; this.step--;
return this;
} }
setHiddenSkill(skill){ setHiddenSkill(skill){
if(!this.player) return this; if(!this.player) return this;
@ -30500,11 +30510,13 @@
get.stringifiedResult(this.parent),get.skillState(this.player)); get.stringifiedResult(this.parent),get.skillState(this.player));
this.player.wait(); this.player.wait();
game.pause(); game.pause();
return this;
} }
resume(){ resume(){
delete this._cardChoice; delete this._cardChoice;
delete this._targetChoice; delete this._targetChoice;
delete this._skillChoice; delete this._skillChoice;
return this;
} }
getParent(level,forced){ getParent(level,forced){
var parent,historys=[]; var parent,historys=[];
@ -30552,21 +30564,18 @@
if(!this._rand) this._rand=Math.random(); if(!this._rand) this._rand=Math.random();
return this._rand; return this._rand;
} }
insert(func,map){ insert(content,map){
var next=game.createEvent(this.name+'Inserted',false,this); const next=new lib.element.Event(`${this.name}Inserted`,false);
next.setContent(func); this.next.push(next);
for(var i in map){ next.setContent(content);
next.set(i,map[i]); Object.entries(map).forEach(entry=>next.set(entry[0],entry[1]));
}
return next; return next;
} }
insertAfter(func,map){ insertAfter(content,map){
var next=game.createEvent(this.name+'Inserted',false,{next:[]}); const next=new lib.element.Event(`${this.name}Inserted`,false);
this.after.push(next); this.after.push(next);
next.setContent(func); next.setContent(content);
for(var i in map){ Object.entries(map).forEach(entry=>next.set(entry[0],entry[1]));
next.set(i,map[i]);
}
return next; return next;
} }
backup(skill){ backup(skill){
@ -30690,6 +30699,7 @@
delete this._cardChoice; delete this._cardChoice;
delete this._targetChoice; delete this._targetChoice;
delete this._skillChoice; delete this._skillChoice;
return this;
} }
restore(){ restore(){
if(this._backup){ if(this._backup){
@ -30716,6 +30726,7 @@
delete this.skill; delete this.skill;
delete this.ignoreMod; delete this.ignoreMod;
delete this.filterCard2; delete this.filterCard2;
return this;
} }
isMine(){ isMine(){
return (this.player&&this.player==game.me&&!_status.auto&&!this.player.isMad()&&!game.notMe); return (this.player&&this.player==game.me&&!_status.auto&&!this.player.isMad()&&!game.notMe);
@ -30732,13 +30743,13 @@
return !player||player==evt.player; return !player||player==evt.player;
} }
addTrigger(skill,player){ addTrigger(skill,player){
if(!player||!skill) return; if(!player||!skill) return this;
var evt=this; var evt=this;
if(typeof skill=='string') skill=[skill]; if(typeof skill=='string') skill=[skill];
game.expandSkills(skill); game.expandSkills(skill);
while(true){ while(true){
var evt=evt.getParent('arrangeTrigger'); var evt=evt.getParent('arrangeTrigger');
if(!evt||evt.name!='arrangeTrigger'||!evt.map) return; if(!evt||evt.name!='arrangeTrigger'||!evt.map) return this;
var filter=function(content){ var filter=function(content){
if(typeof content=='string') return content==triggername; if(typeof content=='string') return content==triggername;
return content.contains(triggername); return content.contains(triggername);
@ -30752,7 +30763,7 @@
if(evt.map[i].player==player){map=evt.map[i];break;} if(evt.map[i].player==player){map=evt.map[i];break;}
} }
} }
if(!map) return; if(!map) return this;
var func=function(skillx){ var func=function(skillx){
var info=lib.skill[skillx]; var info=lib.skill[skillx];
var bool=false; var bool=false;
@ -30779,6 +30790,43 @@
func(skill[j]); func(skill[j]);
} }
} }
return this;
}
removeTrigger(skill,player){
if(!player||!skill) return;
var evt=this;
if(typeof skill=='string') skill=[skill];
game.expandSkills(skill);
while(true){
var evt=evt.getParent('arrangeTrigger');
if(!evt||evt.name!='arrangeTrigger'||!evt.map) return;
var filter=function(content){
if(typeof content=='string') return content==triggername;
return content.contains(triggername);
};
var trigger=evt._trigger;
var triggername=evt.triggername;
var map=false;
if(evt.doing&&evt.doing.player==player) map=evt.doing;
else{
for(var i=0;i<evt.map.length;i++){
if(evt.map[i].player==player){
map=evt.map[i];
break;
}
}
}
if(!map) return;
var func=function(skillx){
var toremove=map.list.filter(i=>{
return i[0]==skillx&&i[1]==player;
});
if(toremove.length>0) map.list.removeArray(toremove);
}
for(var j=0;j<skill.length;j++){
func(skill[j]);
}
}
} }
removeTrigger(skill,player){ removeTrigger(skill,player){
if(!player||!skill) return; if(!player||!skill) return;
@ -30817,8 +30865,8 @@
} }
} }
trigger(name){ trigger(name){
if(_status.video) return; if(_status.video) return this;
if((this.name==='gain'||this.name==='lose')&&!_status.gameDrawed) return; if((this.name==='gain'||this.name==='lose')&&!_status.gameDrawed) return this;
if(name==='gameDrawEnd') _status.gameDrawed=true; if(name==='gameDrawEnd') _status.gameDrawed=true;
if(name==='gameStart'){ if(name==='gameStart'){
lib.announce.publish('gameStart',{}); lib.announce.publish('gameStart',{});
@ -30831,8 +30879,8 @@
_status.gameStarted=true; _status.gameStarted=true;
game.showHistory(); game.showHistory();
} }
if(!lib.hookmap[name]&&!lib.config.compatiblemode) return; if(!lib.hookmap[name]&&!lib.config.compatiblemode) return this;
if(!game.players||!game.players.length) return; if(!game.players||!game.players.length) return this;
var event=this; var event=this;
var start=false; var start=false;
var starts=[_status.currentPhase,event.source,event.player,game.me,game.players[0]]; var starts=[_status.currentPhase,event.source,event.player,game.me,game.players[0]];
@ -30841,7 +30889,7 @@
start=starts[i];break; start=starts[i];break;
} }
} }
if(!start) return; if(!start) return this;
if(!game.players.contains(start)&&!game.dead.contains(start)){ if(!game.players.contains(start)&&!game.dead.contains(start)){
start=game.findNext(start); start=game.findNext(start);
} }
@ -31010,6 +31058,7 @@
//next.starter=start; //next.starter=start;
event._triggering=next; event._triggering=next;
} }
return this;
} }
untrigger(all,player){ untrigger(all,player){
if(typeof all=='undefined') all=true; if(typeof all=='undefined') all=true;
@ -31024,15 +31073,14 @@
}; };
this._triggered=5; this._triggered=5;
} }
else{ else if(player){
if(player){ this._notrigger.add(player);
this._notrigger.add(player); if(!evt||!evt.map) return this;
if(!evt||!evt.map) return; for(var i=0;i<evt.map.length;i++){
for(var i=0;i<evt.map.length;i++){ if(evt.map[i].player==player) evt.map[i].list.length=0;
if(evt.map[i].player==player) evt.map[i].list.length=0;
}
} }
} }
return this;
} }
}, },
Dialog:class extends HTMLDivElement{ Dialog:class extends HTMLDivElement{
@ -38523,6 +38571,9 @@
next._trigger=event; next._trigger=event;
next.setContent('createTrigger'); next.setContent('createTrigger');
}, },
/**
* @legacy Use `new lib.element.Event(name, trigger)` (and optionally `(triggerEvent || _status.event).next.push(next)`) instead.
*/
createEvent:(name,trigger,triggerEvent)=>{ createEvent:(name,trigger,triggerEvent)=>{
const next=new lib.element.Event(name,trigger); const next=new lib.element.Event(name,trigger);
(triggerEvent||_status.event).next.push(next); (triggerEvent||_status.event).next.push(next);

View File

@ -35,21 +35,21 @@ game.import('mode',function(lib,game,ui,get,ai,_status){
node.style.left='calc(50% - 210px)'; node.style.left='calc(50% - 210px)';
node.style.top='calc(50% - 20px)'; node.style.top='calc(50% - 20px)';
node.style.whiteSpace='nowrap'; node.style.whiteSpace='nowrap';
node.innerHTML=lib.config.last_ip||lib.hallURL; node.textContent=lib.config.last_ip||lib.hallURL;
node.contentEditable=true; node.contentEditable=true;
node.style.webkitUserSelect='text'; node.style.webkitUserSelect='text';
node.style.textAlign='center'; node.style.textAlign='center';
node.style.overflow='hidden'; node.style.overflow='hidden';
var connect=function(e){ var connect=function(e){
event.textnode.innerHTML='正在连接...'; event.textnode.textContent='正在连接...';
clearTimeout(event.timeout); clearTimeout(event.timeout);
if(e) e.preventDefault(); if(e) e.preventDefault();
game.saveConfig('last_ip',node.innerHTML); game.saveConfig('last_ip',node.textContent=node.textContent);
game.connect(node.innerHTML,function(success){ game.connect(node.textContent,function(success){
if(!success&&event.textnode){ if(!success&&event.textnode){
alert('连接失败'); alert('连接失败');
event.textnode.innerHTML='输入联机地址'; event.textnode.textContent='输入联机地址';
} }
}); });
}; };
@ -82,7 +82,7 @@ game.import('mode',function(lib,game,ui,get,ai,_status){
ui.ipbutton=button; ui.ipbutton=button;
ui.hall_button=ui.create.system('联机大厅',function(){ ui.hall_button=ui.create.system('联机大厅',function(){
node.innerHTML=get.config('hall_ip')||lib.hallURL; node.textContent=get.config('hall_ip')||lib.hallURL;
connect(); connect();
},true); },true);
if(!get.config('hall_button')){ if(!get.config('hall_button')){
@ -90,7 +90,7 @@ game.import('mode',function(lib,game,ui,get,ai,_status){
} }
ui.recentIP=ui.create.system('最近连接',null,true); ui.recentIP=ui.create.system('最近连接',null,true);
var clickLink=function(){ var clickLink=function(){
node.innerHTML=this.innerHTML; node.textContent=this.textContent;
connect(); connect();
}; };
lib.setPopped(ui.recentIP,function(){ lib.setPopped(ui.recentIP,function(){
@ -101,7 +101,7 @@ game.import('mode',function(lib,game,ui,get,ai,_status){
}); });
var list=ui.create.div('.caption'); var list=ui.create.div('.caption');
for(var i=0;i<lib.config.recentIP.length;i++){ for(var i=0;i<lib.config.recentIP.length;i++){
ui.create.div('.text.textlink',list,clickLink).innerHTML=get.trimip(lib.config.recentIP[i]); ui.create.div('.text.textlink',list,clickLink).textContent=get.trimip(lib.config.recentIP[i]);
} }
uiintro.add(list); uiintro.add(list);
var clear=uiintro.add('<div class="text center">清除</div>'); var clear=uiintro.add('<div class="text center">清除</div>');
@ -128,14 +128,14 @@ game.import('mode',function(lib,game,ui,get,ai,_status){
} }
var n=5; var n=5;
var connect=function(){ var connect=function(){
event.textnode.innerHTML='正在连接...'; event.textnode.textContent='正在连接...';
game.connect(info[0],function(success){ game.connect(info[0],function(success){
if(!success&&n--){ if(!success&&n--){
createNode(); createNode();
event.timeout=setTimeout(connect,1000); event.timeout=setTimeout(connect,1000);
} }
else{ else{
event.textnode.innerHTML='输入联机地址'; event.textnode.textContent='输入联机地址';
} }
}); });
}; };
@ -153,13 +153,6 @@ game.import('mode',function(lib,game,ui,get,ai,_status){
} }
} }
_status.connectDenied=createNode; _status.connectDenied=createNode;
for(var i in lib.element.event){
event.parent[i]=lib.element.event[i];
}
event.parent.custom={
add:{},
replace:{}
};
setTimeout(lib.init.onfree,1000); setTimeout(lib.init.onfree,1000);
} }
}; };