ac8f28c70764b8ffa1e14212685b9c61f3354b97.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. console.log("无数据所以初始化");
  179. } else {
  180. _this3.unserialize(load_data);
  181. console.log("有数据初始化");
  182. }
  183. _this3.checkVer(ver);
  184. _this3.checkNewDay();
  185. _this3.on_load(1);
  186. })();
  187. }
  188. /**远程调用保存,如果不是强制远程保存,必并先设置脏数据*/
  189. save(force) {
  190. var _this4 = this;
  191. return _asyncToGenerator(function* () {
  192. if (force === void 0) {
  193. force = false;
  194. }
  195. _this4.on_save(0);
  196. if (force) {
  197. _this4.setDirty();
  198. } else if (!_this4._dirty) {
  199. _this4.on_save(1);
  200. return false;
  201. }
  202. var ret = yield _this4.saveGameDataWithRetry(_this4.serialize());
  203. _this4.on_save(1);
  204. if (ret) _this4._dirty = false;
  205. return ret;
  206. })();
  207. }
  208. /**设置脏数据后保存到本地*/
  209. setDirty() {
  210. this._dirty = true;
  211. (_crd && ch === void 0 ? (_reportPossibleCrUseOfch({
  212. error: Error()
  213. }), ch) : ch).storage.set(this._key, this.serialize(), this._gid);
  214. }
  215. checkVer(new_v) {
  216. if (!new_v) return;
  217. if (!this._ver) {
  218. this.on_ver(true, this._ver, new_v);
  219. }
  220. var k = this.compareVersion(this._ver, new_v);
  221. this.on_ver(k < 0, this._ver, new_v);
  222. this._ver = new_v;
  223. }
  224. /**
  225. * 序列化(数据库存储)
  226. */
  227. serialize() {
  228. var save_data = {};
  229. if (this._data) save_data.data = this._data.serialize();
  230. if (this._day_data) save_data.day_data = this._day_data.serialize();
  231. if (this._week_data) save_data.week_data = this._week_data.serialize();
  232. if (this._month_data) save_data.month_data = this._month_data.serialize();
  233. save_data.uid = this._uid;
  234. save_data.gid = this._gid;
  235. this._save_time = (_crd && ch === void 0 ? (_reportPossibleCrUseOfch({
  236. error: Error()
  237. }), ch) : ch).date.now();
  238. save_data.save_time = this._save_time;
  239. if (this._ver) save_data.ver = this._ver;
  240. this.on_serialize(save_data);
  241. return save_data;
  242. }
  243. /**
  244. * 反序列化
  245. */
  246. unserialize(load_data) {
  247. var _this$_data, _this$_day_data2, _this$_week_data2, _this$_month_data2, _load_data$uid, _load_data$gid, _ref;
  248. if (!load_data) return;
  249. (_this$_data = this._data) == null || _this$_data.unserialize(load_data.data);
  250. (_this$_day_data2 = this._day_data) == null || _this$_day_data2.unserialize(load_data.day_data);
  251. (_this$_week_data2 = this._week_data) == null || _this$_week_data2.unserialize(load_data.week_data);
  252. (_this$_month_data2 = this._month_data) == null || _this$_month_data2.unserialize(load_data.month_data);
  253. this._uid = (_load_data$uid = load_data.uid) != null ? _load_data$uid : this._uid;
  254. this._gid = (_load_data$gid = load_data.gid) != null ? _load_data$gid : this._gid;
  255. this._save_time = (_ref = load_data.save_time) != null ? _ref : (_crd && ch === void 0 ? (_reportPossibleCrUseOfch({
  256. error: Error()
  257. }), ch) : ch).date.now();
  258. if (load_data.ver) this._ver = load_data.ver;
  259. this.on_unserialize(load_data);
  260. }
  261. /**重写此方法初始自定义数据*/
  262. on_init() {}
  263. /**重写此方法检测是否使用远程数据*/
  264. on_check(local, remote) {
  265. return true;
  266. }
  267. /**重写 版本检测数据处理*/
  268. on_ver(is_new, old_v, new_v) {}
  269. /**重写序列化加入自定义的数据*/
  270. on_serialize(data) {}
  271. /**重写反序列化*/
  272. on_unserialize(data) {}
  273. /**保存数据(0开始 1结束)*/
  274. on_save(step) {}
  275. /**加载数据(0开始 1结束)*/
  276. on_load(step) {
  277. if (step == 0) {
  278. console.log("开始加载数据");
  279. } else if (step == 1) {
  280. console.log("加载数据结束");
  281. }
  282. }
  283. /**重写成其它加载方式*/
  284. load_data() {
  285. var _this5 = this;
  286. return _asyncToGenerator(function* () {
  287. var ret = yield (_crd && ch === void 0 ? (_reportPossibleCrUseOfch({
  288. error: Error()
  289. }), ch) : ch).sdk.loadGameData(_this5._key);
  290. if (ret.code === 0) {
  291. return ret.data;
  292. } else {
  293. (_crd && ch === void 0 ? (_reportPossibleCrUseOfch({
  294. error: Error()
  295. }), ch) : ch).log.warn("\u5C1D\u8BD5\u52A0\u8F7D\u6570\u636E\u5931\u8D25\uFF0C\u9519\u8BEF\u4EE3\u7801: " + ret.code + " " + ret.err);
  296. }
  297. return null;
  298. })();
  299. }
  300. /**重写成其它保存方式*/
  301. save_data(save_data) {
  302. var _this6 = this;
  303. return _asyncToGenerator(function* () {
  304. var ret = yield (_crd && ch === void 0 ? (_reportPossibleCrUseOfch({
  305. error: Error()
  306. }), ch) : ch).sdk.saveGameData(_this6._key, save_data);
  307. if (ret.code == 0) {
  308. return true;
  309. } else {
  310. (_crd && ch === void 0 ? (_reportPossibleCrUseOfch({
  311. error: Error()
  312. }), ch) : ch).log.warn("\u5C1D\u8BD5\u4FDD\u5B58\u6570\u636E\u5931\u8D25\uFF0C\u9519\u8BEF\u4EE3\u7801: " + ret.code + " " + ret.err);
  313. return false;
  314. }
  315. })();
  316. }
  317. });
  318. _cclegacy._RF.pop();
  319. _crd = false;
  320. }
  321. };
  322. });
  323. //# sourceMappingURL=ac8f28c70764b8ffa1e14212685b9c61f3354b97.js.map