import GameController from "../../../game/GameController"; import { LOG_TAG, Log } from "../../log/Log"; import { RDPlatformManager } from "../../platform/RDPlatformManager"; export default class IDataModel { protected modelName: string = 'default'; /** * 本地缓存的数据 */ private _dLocalData = {}; constructor(modelName = 'default') { this.modelName = modelName; this.LoadStorage(); this.registerListeners(); } clear() { } /** * 注册网络监听事件 */ private registerListeners() { let tbMsg = this.getMessageListeners(); for (const key in tbMsg) { if (tbMsg.hasOwnProperty(key)) { // EventMng.on(key.toString(), function (msg) { // tbMsg[key](msg); // }) } } } /** * 子类需要重写此方法,返回需要注册的监听事件 */ getMessageListeners() { return {}; }; /** * 发送协议 * @param msg */ sendProtocolMsg(msg) { try { //GameController.network.send(msg); } catch (e) { console.error('send proto', msg, e); } } protected LoadStorage() { let self = this; RDPlatformManager.getInstance().loadStorageData([`model_${this.modelName}`], function (data) { if (!data || data === "") { self._dLocalData = {} self.initData(); self.Save(); } else { self._dLocalData = data; } //cc.log("数据获取" + `model_${self.modelName}`) // cc.log(JSON.stringify(self._dLocalData)); }, function (err) { }); } /** * 如果是第一次初始化数据用 */ protected initData() { //todo 子类实现 Log.log(LOG_TAG.DEBUG, "如果是第一次初始化数据用 父类"); } /** * protected 只让实现类操作数据 也就是model类型操作数据 对外提供别的方法 * @param sKey * @param defaultValue */ protected Query(sKey: string, defaultValue: any = null) { if (this._dLocalData[sKey] != undefined) { return this._dLocalData[sKey]; } return defaultValue; } /** * 设置成功返回 true,反之返回 false 用于是否保存数据 * @param sKey * @param value */ protected Set(sKey: string, value: string | number) { if (this._dLocalData[sKey] && this._dLocalData[sKey] == value) { return false;//一样就不要改了 } this._dLocalData[sKey] = value; return true; } protected Save() { RDPlatformManager.getInstance().flushStorageData({key:`model_${this.modelName}`,data:this._dLocalData},function(){}), //cc.log( "数据保存" + `model_${this.modelName}`) //let self = this; RDPlatformManager.getInstance().loadStorageData([`model_${this.modelName}`],function(data){ // cc.log( "数据获取" + `model_${self.modelName}`) // cc.log(JSON.stringify(self._dLocalData)); },function(err){}); //cc.log(JSON.stringify(this._dLocalData)); } }