From 899d0ceca7c7bcc5a28b924b9e18b672e4838a71 Mon Sep 17 00:00:00 2001 From: Rintim Date: Sat, 18 Nov 2023 00:04:56 +0800 Subject: [PATCH] pref(game): move `lib.handler` to `game.globalEventHandlers` and fix bugs. --- game/game.js | 108 +++++++++++++++++++++++++++++---------------------- 1 file changed, 61 insertions(+), 47 deletions(-) diff --git a/game/game.js b/game/game.js index 1dc51af7b..649cc8e1b 100644 --- a/game/game.js +++ b/game/game.js @@ -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:{}, @@ -31259,7 +31213,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; @@ -36166,6 +36120,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),