/******************************************************************************* * * Nx功能服务 * * * * 2021.12.10 ******************************************************************************/ const NxMod = require( "nx.mod" ); /*** * ********************************************************************* * 类定义 * ********************************************************************* */ var NxFunc = cc.Class( { extends : NxMod, name : "NxFunc", // 初始化 initialize: function( _args ) { // USPER this._super( _args ); // 访问接口(设置) nx.func = this; // 变量 this.fcaches = {}; this.fbinders = {}; this.fconders = {}; return true; }, // 销毁 uninitialize: function() { // 访问接口(关闭) nx.func = null; // USPER return this._super(); }, /*** * ********************************************************************* * 查询支持 * ********************************************************************* */ // 获取指定功能 queryFunc: function( _fid ) { _fid = parseInt( _fid ); // 查询缓存 let cfg = this.fcaches[ _fid + '' ]; if( cfg ) { return cfg; } // 配置查询 cfg = nx.queryID( _fid ); return this.addFunc( cfg ); }, // 添加功能 addFunc: function( _cfgs ) { // 配置无效 let fid = _cfgs ? _cfgs.id : 0; if( !nx.dt.numPositive( fid, false ) ) { nx.error( "[功能]添加功能失败,无效配置!" ); return null; } // 重复添加 let tmp = this.fcaches[ fid + '' ]; if( nx.dt.objNEmpty( tmp ) ) { nx.error( "[功能]添加功能失败,重复添加!" ); return tmp; } // 加入 tmp = nx.dt.objClone( _cfgs ); this.fcaches[ fid + '' ] = tmp; nx.debug( "[功能]添加功能:" + _fid ); return tmp; }, // 删除功能 delFunc: function( _fid ) { _fid = parseInt( _fid ); let tmp = this.fcaches[ _fid + '' ]; if( tmp ) { nx.debug( "[功能]删除功能:" + _fid ); delete this.fcaches[ _fid + '' ]; } }, // 功能条件判断 checkFunc: function( _fid ) { _fid = parseInt( _fid ); // 未找到 let func = this.queryFunc( _fid ); if( !func ) { return { open: false, desc: "FunCondNoExist" }; } // 条件遍历 let conds = func.conds || []; for( let i = 0; i < conds.length; ++i ) { let cd = conds[ i ]; let reason = this.checkCodition( cd.key, cd.p1, cd.p2 ); if( nx.dt.strNEmpty( reason ) ) { return { open: false, desc: reason }; } } // 全通过 return { open: true }; }, /*** * ********************************************************************* * 监听支持 * ********************************************************************* */ // 功能开关监听 bindFunc: function( _fid, _cb, _tar ) { _fid = parseInt( _fid ); // 参数判断 if( !nx.dt.numPositive( _fid, false ) || !nx.dt.fnGood( _cb ) ) { nx.error( "[功能]监听绑定失败!" ); return; } // 追加监听 let item = this.fbinders[ '' + _fid ] || { state: this.checkFunc( _fid ), list: [], }; item.list.push( { tar: _tar, cb: _cb } ); this.fbinders[ '' + _fid ] = item; // 直接通告一次 _cb.call( _tar, item.state ); }, // 删除功能开关监听 unbindFunc: function( _fid, _cb, _tar ) { _fid = parseInt( _fid ); // 参数无效 if( !nx.dt.numPositive( _fid, false ) || !nx.dt.fnGood( _cb ) ) { nx.error( "[功能]删除监听失败,参数无效!" ); return; } // 查找监听列表 let group = this.fbinders[ '' + _fid ]; if( !group ) { nx.warn( "[功能]删除监听失败,未找到:" + _fid ); return; } // 指定删除 nx.dt.arrDelete( group.list, ( _t ) => { return ( _t && _t.cb == _cb && _t.tar == _tar ); }, false ); }, // 监听更新通告 updateAndNotifyBinders: function( _fid = 0 ) { _fid = parseInt( _fid ); // 通知 let notify = function( _group, _state ) { // 未有变化不通知 if( _group.state.open == _state.open ) { return; } // 通知 _group.state = _state; _group.list.forEach( ( _t ) => { if( _t && _t.cb && _t.tar ) { _t.cb.call( _t.tar, _binder.state ); } } ); }; // 指定通告 if( nx.dt.numPositive( _fid, false ) ) { let group = this.fbinders[_fid+'']; if( group ) { notify( group, this.checkCodition( _fid ) ); } return; } // 全体通告 for( let id in this.fbinders ) { let group = this.fbinders[id]; if( group ) { notify( group, this.checkCodition( id ) ); } } }, /*** * ********************************************************************* * 条件支持 * ********************************************************************* */ // 添加条件 addCondition: function( _key, _handle, _desc ) { // 参数合法检查 if( nx.dt.strEmpty( _key ) || !nx.dt.fnGood( _handle ) ) { nx.error( "[功能]无效的条件,添加失败!" ); return false; } // 重复添加:警告&&覆盖 if( this.fconders[ _key ] ) { nx.warn( "[功能]条件重复,覆盖原有处理方法:" + _key ); } // 添加 nx.debug( "[功能]添加条件:" + _key ); this.fconders[ _key ] = { key : _key, handle : _handle, desc : _desc }; return true; }, // 删除条件 delCondition: function( _key ) { let cond = this.fconders[ _key ]; if( cond ) { nx.debug( "[功能]删除条件:" + _key ); delete this.fconders[ _key ]; } }, // 条件检测 checkCodition: function( _key, _param1, _param2 ) { // 无效 let cond = this.fconders[ _key ]; if( !cond ) { nx.error( "[功能]条件无效,检测失败:" + _key ); return "FunCondNoExist"; } return cond.handle( _param1, _param2 ); }, // 条件输出 dumpConditions: function() { nx.debug( '[功能]全条件列表:' ); for( let k in this.fconders ) { let cd = this.fconders[k]; nx.debug( '\t\t%s %s', k, cd.desc || "" ); } }, } ); module.exports = NxFunc;