69708f163aeb7f1d3d93c5d9454dca6576ef2581.js 12 KB

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