Merge pull request #651 from nofficalfs/Dev-GlobalHandlerMove

Move `lib.handler` to fit position and fix its bugs.
This commit is contained in:
Spmario233 2023-11-18 22:15:47 +08:00 committed by GitHub
commit 4279ad3b0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 61 additions and 47 deletions

View File

@ -462,52 +462,6 @@
return method;
}
},
handler:{
_handlers:{},
getHandler:function(name,type){
if(!type)type = this.getDefaultHandlerType(name);
if(!this._handlers[name])return null;
if(!this._handlers[name][type])return null;
return this._handlers[name][type];
},
ensureHandlerList:function(name,type){
if(!type)type = this.getDefaultHandlerType(name);
if(!this._handlers[name])this._handlers[name] = {};
if(!this._handlers[name][type])this._handlers[name][type] = [];
return this._handlers[name][type];
},
removeHandler:function(name,type,func){
var list = this.ensureHandlerList(name,type);
list.remove(func);
if(list.length == 0){
delete this._handlers[name][type];
if(Object.keys(this._handlers[name]) == 0){
delete this._handlers[name];
}
}
},
pushHandler:function(name,type){
let functions = (typeof type == 'string' ? arguments.slice(2):arguments.slice(1));
type = (typeof type == 'string'?type:this.getDefaultHandlerType(name));
this.ensureHandlerList(name,type).addArray(functions);
},
getDefaultHandlerType:(name)=>{
return `on${name[0].toUpperCase()}${name.slice(1)}`;
},
addHandlerToEvent:function(event){
if(typeof event.name == 'string'){
let handlerMap = this._handlers[event.name];
if(handlerMap){
Object.keys(handlerMap).forEach((key)=>{
let list = handlerMap[key];
if(list){
list.forEach(handler=>event.pushHandler(key,handler));
}
});
}
}
}
},
objectURL:new Map(),
hookmap:{},
imported:{},
@ -31262,7 +31216,7 @@
const type=`onNext${name[0].toUpperCase()}${name.slice(1)}`;
if(gameEvent.hasHandler(type)) this.pushHandler(...gameEvent.getHandler(type));
}
lib.handler.addHandlerToEvent(this);
game.globalEventHandlers.addHandlerToEvent(this);
}
this.step=0;
this.finished=false;
@ -36169,6 +36123,66 @@
}
};
const game={
globalEventHandlers: new class {
constructor() {
this._handlers = {};
}
getHandler(name, type) {
if (!type)
type = this.getDefaultHandlerType(name);
if(!this._handlers[name])
return null;
if(!this._handlers[name][type])
return null;
return this._handlers[name][type];
}
ensureHandlerList(name, type) {
if(!type)
type = this.getDefaultHandlerType(name);
if(!this._handlers[name])
this._handlers[name] = {};
if(!this._handlers[name][type])
this._handlers[name][type] = [];
return this._handlers[name][type];
}
removeHandler(name, type, func) {
const list = this.ensureHandlerList(name,type);
list.remove(func);
if(list.length == 0){
delete this._handlers[name][type];
if(Object.keys(this._handlers[name]).length == 0){
delete this._handlers[name];
}
}
}
pushHandler(name, type) {
const args = Array.from(arguments);
const functions = (typeof type == 'string' ? args.slice(2) : args.slice(1));
type = (typeof type == 'string' ? type : this.getDefaultHandlerType(name));
this.ensureHandlerList(name, type).addArray(functions);
}
getDefaultHandlerType(name) {
return `on${ name[0].toUpperCase() }${ name.slice(1) }`;
}
addHandlerToEvent(event) {
if (typeof event.name != "string") return;
const handlerMap = this._handlers[event.name];
if (!handlerMap) return;
Object.keys(handlerMap).forEach((key) => {
const list = handlerMap[key];
if (!list) return;
list.forEach((handler) => {
event.pushHandler(key, handler);
});
});
}
},
//Stratagem
//谋攻
setStratagemBuffCost:(cardName,cost)=>game.broadcastAll((clientCardName,clientCost)=>lib.stratagemBuff.cost.set(clientCardName,clientCost),cardName,cost),