System.register(["__unresolved_0", "cc", "__unresolved_1", "__unresolved_2"], function (_export, _context) { "use strict"; var _reporterNs, _cclegacy, ch, PeriodData, PeriodDataUpdate, GameData, _crd; function _reportPossibleCrUseOfch(extras) { _reporterNs.report("ch", "../../ch/ch", _context.meta, extras); } function _reportPossibleCrUseOfPeriodData(extras) { _reporterNs.report("PeriodData", "./PeriodData", _context.meta, extras); } function _reportPossibleCrUseOfPeriodDataUpdate(extras) { _reporterNs.report("PeriodDataUpdate", "./PeriodData", _context.meta, extras); } _export("default", void 0); return { setters: [function (_unresolved_) { _reporterNs = _unresolved_; }, function (_cc) { _cclegacy = _cc.cclegacy; }, function (_unresolved_2) { ch = _unresolved_2.ch; }, function (_unresolved_3) { PeriodData = _unresolved_3.default; PeriodDataUpdate = _unresolved_3.PeriodDataUpdate; }], execute: function () { _crd = true; _cclegacy._RF.push({}, "c88bd5RDxxIJ7YUSwv6V1cm", "GameData", undefined); /**游戏数据 * 有额外自定义数据可以继承此类 */ _export("default", GameData = class GameData { constructor(gid, uid, name, limt, day_limt, week_limt, month_limt) { this._key = void 0; this._uid = void 0; this._gid = void 0; this._save_time = void 0; this._ver = void 0; this._data = void 0; this._day_data = void 0; this._week_data = void 0; this._month_data = void 0; // this._dirty = false; this._key = name; this._gid = gid; this._uid = uid; this._data = new (_crd && PeriodData === void 0 ? (_reportPossibleCrUseOfPeriodData({ error: Error() }), PeriodData) : PeriodData)((_crd && PeriodDataUpdate === void 0 ? (_reportPossibleCrUseOfPeriodDataUpdate({ error: Error() }), PeriodDataUpdate) : PeriodDataUpdate).none, limt); this._day_data = new (_crd && PeriodData === void 0 ? (_reportPossibleCrUseOfPeriodData({ error: Error() }), PeriodData) : PeriodData)((_crd && PeriodDataUpdate === void 0 ? (_reportPossibleCrUseOfPeriodDataUpdate({ error: Error() }), PeriodDataUpdate) : PeriodDataUpdate).day, day_limt); this._week_data = new (_crd && PeriodData === void 0 ? (_reportPossibleCrUseOfPeriodData({ error: Error() }), PeriodData) : PeriodData)((_crd && PeriodDataUpdate === void 0 ? (_reportPossibleCrUseOfPeriodDataUpdate({ error: Error() }), PeriodDataUpdate) : PeriodDataUpdate).week, week_limt); this._month_data = new (_crd && PeriodData === void 0 ? (_reportPossibleCrUseOfPeriodData({ error: Error() }), PeriodData) : PeriodData)((_crd && PeriodDataUpdate === void 0 ? (_reportPossibleCrUseOfPeriodDataUpdate({ error: Error() }), PeriodDataUpdate) : PeriodDataUpdate).month, month_limt); } // get data() { return this._data; } get day_data() { return this._day_data; } get week_data() { return this._week_data; } get month_data() { return this._month_data; } get uid() { return this._uid; } checkNewDay() { var _this$_day_data, _this$_week_data, _this$_month_data; let now = (_crd && ch === void 0 ? (_reportPossibleCrUseOfch({ error: Error() }), ch) : ch).date.now(); (_this$_day_data = this._day_data) == null || _this$_day_data.check(now); (_this$_week_data = this._week_data) == null || _this$_week_data.check(now); (_this$_month_data = this._month_data) == null || _this$_month_data.check(now); } // compareVersion(version1, version2) { // 移除前面的 'v' 字符 const v1 = version1.replace(/^V/, ''); const v2 = version2.replace(/^V/, ''); // 分割版本号 const parts1 = v1.split('.').map(Number); // 将版本号分割并转为数字 const parts2 = v2.split('.').map(Number); // 比较每一部分 for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) { const num1 = parts1[i] || 0; // 如果没有该部分,默认为 0 const num2 = parts2[i] || 0; if (num1 < num2) { return -1; // version1 < version2 } if (num1 > num2) { return 1; // version1 > version2 } } return 0; // 两个版本号相等 } // async loadGameDataWithRetry(maxRetries = 3, delayMs = 1000) { let attempt = 0; while (attempt < maxRetries) { const data = await this.load_data(); if (data != null) { return data != null ? data : null; } else { attempt++; await new Promise(resolve => setTimeout(resolve, delayMs)); } } return null; } // async saveGameDataWithRetry(save_data, maxRetries = 3, delayMs = 1000) { let attempt = 0; while (attempt < maxRetries) { const ret = await this.save_data(save_data); if (ret) { return true; } else { attempt++; await new Promise(resolve => setTimeout(resolve, delayMs)); } } return false; } /**加载数据*/ async load(ver) { this.on_load(0); let load_data = (_crd && ch === void 0 ? (_reportPossibleCrUseOfch({ error: Error() }), ch) : ch).storage.getObject(this._key, this._gid); if (load_data) { if ((_crd && ch === void 0 ? (_reportPossibleCrUseOfch({ error: Error() }), ch) : ch).sdk.get_inited() && load_data.uid != this._uid) load_data = null; } // if (!load_data) { const remote_data = await this.loadGameDataWithRetry(); load_data = remote_data; } // } else if (remote_data && this.on_check(load_data, remote_data)) { // load_data = remote_data; // } // if (!load_data) { this.on_init(); console.log("无数据所以初始化"); } else { this.unserialize(load_data); console.log("有数据初始化"); } this.checkVer(ver); this.checkNewDay(); this.on_load(1); } /**远程调用保存,如果不是强制远程保存,必并先设置脏数据*/ async save(force = false) { this.on_save(0); if (force) { this.setDirty(); } else if (!this._dirty) { this.on_save(1); return false; } let ret = await this.saveGameDataWithRetry(this.serialize()); this.on_save(1); if (ret) this._dirty = false; return ret; } /**设置脏数据后保存到本地*/ setDirty() { this._dirty = true; (_crd && ch === void 0 ? (_reportPossibleCrUseOfch({ error: Error() }), ch) : ch).storage.set(this._key, this.serialize(), this._gid); } checkVer(new_v) { if (!new_v) return; if (!this._ver) { this.on_ver(true, this._ver, new_v); } let k = this.compareVersion(this._ver, new_v); this.on_ver(k < 0, this._ver, new_v); this._ver = new_v; } /** * 序列化(数据库存储) */ serialize() { const save_data = {}; if (this._data) save_data.data = this._data.serialize(); if (this._day_data) save_data.day_data = this._day_data.serialize(); if (this._week_data) save_data.week_data = this._week_data.serialize(); if (this._month_data) save_data.month_data = this._month_data.serialize(); save_data.uid = this._uid; save_data.gid = this._gid; this._save_time = (_crd && ch === void 0 ? (_reportPossibleCrUseOfch({ error: Error() }), ch) : ch).date.now(); save_data.save_time = this._save_time; if (this._ver) save_data.ver = this._ver; this.on_serialize(save_data); return save_data; } /** * 反序列化 */ unserialize(load_data) { var _this$_data, _this$_day_data2, _this$_week_data2, _this$_month_data2, _load_data$uid, _load_data$gid, _ref; if (!load_data) return; (_this$_data = this._data) == null || _this$_data.unserialize(load_data.data); (_this$_day_data2 = this._day_data) == null || _this$_day_data2.unserialize(load_data.day_data); (_this$_week_data2 = this._week_data) == null || _this$_week_data2.unserialize(load_data.week_data); (_this$_month_data2 = this._month_data) == null || _this$_month_data2.unserialize(load_data.month_data); this._uid = (_load_data$uid = load_data.uid) != null ? _load_data$uid : this._uid; this._gid = (_load_data$gid = load_data.gid) != null ? _load_data$gid : this._gid; this._save_time = (_ref = load_data.save_time) != null ? _ref : (_crd && ch === void 0 ? (_reportPossibleCrUseOfch({ error: Error() }), ch) : ch).date.now(); if (load_data.ver) this._ver = load_data.ver; this.on_unserialize(load_data); } /**重写此方法初始自定义数据*/ on_init() {} /**重写此方法检测是否使用远程数据*/ on_check(local, remote) { return true; } /**重写 版本检测数据处理*/ on_ver(is_new, old_v, new_v) {} /**重写序列化加入自定义的数据*/ on_serialize(data) {} /**重写反序列化*/ on_unserialize(data) {} /**保存数据(0开始 1结束)*/ on_save(step) {} /**加载数据(0开始 1结束)*/ on_load(step) { if (step == 0) { console.log("开始加载数据"); } else if (step == 1) { console.log("加载数据结束"); } } /**重写成其它加载方式*/ async load_data() { const ret = await (_crd && ch === void 0 ? (_reportPossibleCrUseOfch({ error: Error() }), ch) : ch).sdk.loadGameData(this._key); if (ret.code === 0) { return ret.data; } else { (_crd && ch === void 0 ? (_reportPossibleCrUseOfch({ error: Error() }), ch) : ch).log.warn(`尝试加载数据失败,错误代码: ${ret.code} ${ret.err}`); } return null; } /**重写成其它保存方式*/ async save_data(save_data) { const ret = await (_crd && ch === void 0 ? (_reportPossibleCrUseOfch({ error: Error() }), ch) : ch).sdk.saveGameData(this._key, save_data); if (ret.code == 0) { return true; } else { (_crd && ch === void 0 ? (_reportPossibleCrUseOfch({ error: Error() }), ch) : ch).log.warn(`尝试保存数据失败,错误代码: ${ret.code} ${ret.err}`); return false; } } }); _cclegacy._RF.pop(); _crd = false; } }; }); //# sourceMappingURL=69708f163aeb7f1d3d93c5d9454dca6576ef2581.js.map