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