c06beeae8a596cc2026e942e11b3062f57cc4dbb.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. System.register(["__unresolved_0", "cc", "__unresolved_1", "__unresolved_2"], function (_export, _context) {
  2. "use strict";
  3. var _reporterNs, _cclegacy, ch, PeriodData, PeriodDataUpdate, GameData, _crd;
  4. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
  5. function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
  6. function _reportPossibleCrUseOfch(extras) {
  7. _reporterNs.report("ch", "../../ch/ch", _context.meta, extras);
  8. }
  9. function _reportPossibleCrUseOfPeriodData(extras) {
  10. _reporterNs.report("PeriodData", "./PeriodData", _context.meta, extras);
  11. }
  12. function _reportPossibleCrUseOfPeriodDataUpdate(extras) {
  13. _reporterNs.report("PeriodDataUpdate", "./PeriodData", _context.meta, extras);
  14. }
  15. _export("default", void 0);
  16. return {
  17. setters: [function (_unresolved_) {
  18. _reporterNs = _unresolved_;
  19. }, function (_cc) {
  20. _cclegacy = _cc.cclegacy;
  21. }, function (_unresolved_2) {
  22. ch = _unresolved_2.ch;
  23. }, function (_unresolved_3) {
  24. PeriodData = _unresolved_3.default;
  25. PeriodDataUpdate = _unresolved_3.PeriodDataUpdate;
  26. }],
  27. execute: function () {
  28. _crd = true;
  29. _cclegacy._RF.push({}, "c88bd5RDxxIJ7YUSwv6V1cm", "GameData", undefined);
  30. /**游戏数据
  31. * 有额外自定义数据可以继承此类
  32. */
  33. _export("default", GameData = class GameData {
  34. constructor(gid, uid, name, limt, day_limt, week_limt, month_limt) {
  35. this._key = void 0;
  36. this._uid = void 0;
  37. this._gid = void 0;
  38. this._save_time = void 0;
  39. this._ver = void 0;
  40. this._data = void 0;
  41. this._day_data = void 0;
  42. this._week_data = void 0;
  43. this._month_data = void 0;
  44. //
  45. this._dirty = false;
  46. this._key = name;
  47. this._gid = gid;
  48. this._uid = uid;
  49. this._data = new (_crd && PeriodData === void 0 ? (_reportPossibleCrUseOfPeriodData({
  50. error: Error()
  51. }), PeriodData) : PeriodData)((_crd && PeriodDataUpdate === void 0 ? (_reportPossibleCrUseOfPeriodDataUpdate({
  52. error: Error()
  53. }), PeriodDataUpdate) : PeriodDataUpdate).none, limt);
  54. this._day_data = new (_crd && PeriodData === void 0 ? (_reportPossibleCrUseOfPeriodData({
  55. error: Error()
  56. }), PeriodData) : PeriodData)((_crd && PeriodDataUpdate === void 0 ? (_reportPossibleCrUseOfPeriodDataUpdate({
  57. error: Error()
  58. }), PeriodDataUpdate) : PeriodDataUpdate).day, day_limt);
  59. this._week_data = new (_crd && PeriodData === void 0 ? (_reportPossibleCrUseOfPeriodData({
  60. error: Error()
  61. }), PeriodData) : PeriodData)((_crd && PeriodDataUpdate === void 0 ? (_reportPossibleCrUseOfPeriodDataUpdate({
  62. error: Error()
  63. }), PeriodDataUpdate) : PeriodDataUpdate).week, week_limt);
  64. this._month_data = new (_crd && PeriodData === void 0 ? (_reportPossibleCrUseOfPeriodData({
  65. error: Error()
  66. }), PeriodData) : PeriodData)((_crd && PeriodDataUpdate === void 0 ? (_reportPossibleCrUseOfPeriodDataUpdate({
  67. error: Error()
  68. }), PeriodDataUpdate) : PeriodDataUpdate).month, month_limt);
  69. } //
  70. get data() {
  71. return this._data;
  72. }
  73. get day_data() {
  74. return this._day_data;
  75. }
  76. get week_data() {
  77. return this._week_data;
  78. }
  79. get month_data() {
  80. return this._month_data;
  81. }
  82. get uid() {
  83. return this._uid;
  84. }
  85. checkNewDay() {
  86. var _this$_day_data, _this$_week_data, _this$_month_data;
  87. var now = (_crd && ch === void 0 ? (_reportPossibleCrUseOfch({
  88. error: Error()
  89. }), ch) : ch).date.now();
  90. (_this$_day_data = this._day_data) == null || _this$_day_data.check(now);
  91. (_this$_week_data = this._week_data) == null || _this$_week_data.check(now);
  92. (_this$_month_data = this._month_data) == null || _this$_month_data.check(now);
  93. } //
  94. compareVersion(version1, version2) {
  95. // 移除前面的 'v' 字符
  96. var v1 = version1.replace(/^V/, '');
  97. var v2 = version2.replace(/^V/, ''); // 分割版本号
  98. var parts1 = v1.split('.').map(Number); // 将版本号分割并转为数字
  99. var parts2 = v2.split('.').map(Number); // 比较每一部分
  100. for (var i = 0; i < Math.max(parts1.length, parts2.length); i++) {
  101. var num1 = parts1[i] || 0; // 如果没有该部分,默认为 0
  102. var num2 = parts2[i] || 0;
  103. if (num1 < num2) {
  104. return -1; // version1 < version2
  105. }
  106. if (num1 > num2) {
  107. return 1; // version1 > version2
  108. }
  109. }
  110. return 0; // 两个版本号相等
  111. } //
  112. loadGameDataWithRetry(maxRetries, delayMs) {
  113. var _this = this;
  114. return _asyncToGenerator(function* () {
  115. if (maxRetries === void 0) {
  116. maxRetries = 3;
  117. }
  118. if (delayMs === void 0) {
  119. delayMs = 1000;
  120. }
  121. var attempt = 0;
  122. while (attempt < maxRetries) {
  123. var _data = yield _this.load_data();
  124. if (_data != null) {
  125. return _data != null ? _data : null;
  126. } else {
  127. attempt++;
  128. yield new Promise(resolve => setTimeout(resolve, delayMs));
  129. }
  130. }
  131. return null;
  132. })();
  133. } //
  134. saveGameDataWithRetry(save_data, maxRetries, delayMs) {
  135. var _this2 = this;
  136. return _asyncToGenerator(function* () {
  137. if (maxRetries === void 0) {
  138. maxRetries = 3;
  139. }
  140. if (delayMs === void 0) {
  141. delayMs = 1000;
  142. }
  143. var attempt = 0;
  144. while (attempt < maxRetries) {
  145. var ret = yield _this2.save_data(save_data);
  146. if (ret) {
  147. return true;
  148. } else {
  149. attempt++;
  150. yield new Promise(resolve => setTimeout(resolve, delayMs));
  151. }
  152. }
  153. return false;
  154. })();
  155. }
  156. /**加载数据*/
  157. load(ver) {
  158. var _this3 = this;
  159. return _asyncToGenerator(function* () {
  160. _this3.on_load(0);
  161. var load_data = (_crd && ch === void 0 ? (_reportPossibleCrUseOfch({
  162. error: Error()
  163. }), ch) : ch).storage.getObject(_this3._key, _this3._gid);
  164. if (load_data) {
  165. if ((_crd && ch === void 0 ? (_reportPossibleCrUseOfch({
  166. error: Error()
  167. }), ch) : ch).sdk.get_inited() && load_data.uid != _this3._uid) load_data = null;
  168. } //
  169. if (!load_data) {
  170. var remote_data = yield _this3.loadGameDataWithRetry();
  171. load_data = remote_data;
  172. } // } else if (remote_data && this.on_check(load_data, remote_data)) {
  173. // load_data = remote_data;
  174. // }
  175. //
  176. if (!load_data) {
  177. _this3.on_init();
  178. } else {
  179. _this3.unserialize(load_data);
  180. }
  181. _this3.checkVer(ver);
  182. _this3.checkNewDay();
  183. _this3.on_load(1);
  184. })();
  185. }
  186. /**远程调用保存,如果不是强制远程保存,必并先设置脏数据*/
  187. save(force) {
  188. var _this4 = this;
  189. return _asyncToGenerator(function* () {
  190. if (force === void 0) {
  191. force = false;
  192. }
  193. _this4.on_save(0);
  194. if (force) {
  195. _this4.setDirty();
  196. } else if (!_this4._dirty) {
  197. _this4.on_save(1);
  198. return false;
  199. }
  200. var ret = yield _this4.saveGameDataWithRetry(_this4.serialize());
  201. _this4.on_save(1);
  202. if (ret) _this4._dirty = false;
  203. return ret;
  204. })();
  205. }
  206. /**设置脏数据后保存到本地*/
  207. setDirty() {
  208. this._dirty = true;
  209. (_crd && ch === void 0 ? (_reportPossibleCrUseOfch({
  210. error: Error()
  211. }), ch) : ch).storage.set(this._key, this.serialize(), this._gid);
  212. }
  213. checkVer(new_v) {
  214. if (!new_v) return;
  215. if (!this._ver) {
  216. this.on_ver(true, this._ver, new_v);
  217. }
  218. var k = this.compareVersion(this._ver, new_v);
  219. this.on_ver(k < 0, this._ver, new_v);
  220. this._ver = new_v;
  221. }
  222. /**
  223. * 序列化(数据库存储)
  224. */
  225. serialize() {
  226. var save_data = {};
  227. if (this._data) save_data.data = this._data.serialize();
  228. if (this._day_data) save_data.day_data = this._day_data.serialize();
  229. if (this._week_data) save_data.week_data = this._week_data.serialize();
  230. if (this._month_data) save_data.month_data = this._month_data.serialize();
  231. save_data.uid = this._uid;
  232. save_data.gid = this._gid;
  233. this._save_time = (_crd && ch === void 0 ? (_reportPossibleCrUseOfch({
  234. error: Error()
  235. }), ch) : ch).date.now();
  236. save_data.save_time = this._save_time;
  237. if (this._ver) save_data.ver = this._ver;
  238. this.on_serialize(save_data);
  239. return save_data;
  240. }
  241. /**
  242. * 反序列化
  243. */
  244. unserialize(load_data) {
  245. var _this$_data, _this$_day_data2, _this$_week_data2, _this$_month_data2, _load_data$uid, _load_data$gid, _ref;
  246. if (!load_data) return;
  247. (_this$_data = this._data) == null || _this$_data.unserialize(load_data.data);
  248. (_this$_day_data2 = this._day_data) == null || _this$_day_data2.unserialize(load_data.day_data);
  249. (_this$_week_data2 = this._week_data) == null || _this$_week_data2.unserialize(load_data.week_data);
  250. (_this$_month_data2 = this._month_data) == null || _this$_month_data2.unserialize(load_data.month_data);
  251. this._uid = (_load_data$uid = load_data.uid) != null ? _load_data$uid : this._uid;
  252. this._gid = (_load_data$gid = load_data.gid) != null ? _load_data$gid : this._gid;
  253. this._save_time = (_ref = load_data.save_time) != null ? _ref : (_crd && ch === void 0 ? (_reportPossibleCrUseOfch({
  254. error: Error()
  255. }), ch) : ch).date.now();
  256. if (load_data.ver) this._ver = load_data.ver;
  257. this.on_unserialize(load_data);
  258. }
  259. /**重写此方法初始自定义数据*/
  260. on_init() {}
  261. /**重写此方法检测是否使用远程数据*/
  262. on_check(local, remote) {
  263. return true;
  264. }
  265. /**重写 版本检测数据处理*/
  266. on_ver(is_new, old_v, new_v) {}
  267. /**重写序列化加入自定义的数据*/
  268. on_serialize(data) {}
  269. /**重写反序列化*/
  270. on_unserialize(data) {}
  271. /**保存数据(0开始 1结束)*/
  272. on_save(step) {}
  273. /**加载数据(0开始 1结束)*/
  274. on_load(step) {}
  275. /**重写成其它加载方式*/
  276. load_data() {
  277. var _this5 = this;
  278. return _asyncToGenerator(function* () {
  279. var ret = yield (_crd && ch === void 0 ? (_reportPossibleCrUseOfch({
  280. error: Error()
  281. }), ch) : ch).sdk.loadGameData(_this5._key);
  282. if (ret.code === 0) {
  283. return ret.data;
  284. } else {
  285. (_crd && ch === void 0 ? (_reportPossibleCrUseOfch({
  286. error: Error()
  287. }), ch) : ch).log.warn("\u5C1D\u8BD5\u52A0\u8F7D\u6570\u636E\u5931\u8D25\uFF0C\u9519\u8BEF\u4EE3\u7801: " + ret.code + " " + ret.err);
  288. }
  289. return null;
  290. })();
  291. }
  292. /**重写成其它保存方式*/
  293. save_data(save_data) {
  294. var _this6 = this;
  295. return _asyncToGenerator(function* () {
  296. var ret = yield (_crd && ch === void 0 ? (_reportPossibleCrUseOfch({
  297. error: Error()
  298. }), ch) : ch).sdk.saveGameData(_this6._key, save_data);
  299. if (ret.code == 0) {
  300. return true;
  301. } else {
  302. (_crd && ch === void 0 ? (_reportPossibleCrUseOfch({
  303. error: Error()
  304. }), ch) : ch).log.warn("\u5C1D\u8BD5\u4FDD\u5B58\u6570\u636E\u5931\u8D25\uFF0C\u9519\u8BEF\u4EE3\u7801: " + ret.code + " " + ret.err);
  305. return false;
  306. }
  307. })();
  308. }
  309. });
  310. _cclegacy._RF.pop();
  311. _crd = false;
  312. }
  313. };
  314. });
  315. //# sourceMappingURL=c06beeae8a596cc2026e942e11b3062f57cc4dbb.js.map