7fe3975b5b0a9baadc008a3c988c3961b05c76c3.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. System.register(["cc"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, PeriodData, _crd, PeriodDataUpdate;
  4. _export("default", void 0);
  5. return {
  6. setters: [function (_cc) {
  7. _cclegacy = _cc.cclegacy;
  8. }],
  9. execute: function () {
  10. _crd = true;
  11. _cclegacy._RF.push({}, "6e040Hgcc1C2r4oXdDQnwvy", "PeriodData", undefined);
  12. _export("PeriodDataUpdate", PeriodDataUpdate = /*#__PURE__*/function (PeriodDataUpdate) {
  13. PeriodDataUpdate[PeriodDataUpdate["none"] = 0] = "none";
  14. PeriodDataUpdate[PeriodDataUpdate["day"] = 1] = "day";
  15. PeriodDataUpdate[PeriodDataUpdate["week"] = 2] = "week";
  16. PeriodDataUpdate[PeriodDataUpdate["month"] = 3] = "month";
  17. return PeriodDataUpdate;
  18. }({}));
  19. /**周期数据
  20. * @param T 为一个枚举类型
  21. * @param update PeriodDataUpdate 更新周期
  22. * @param limt 限制值最大,最小
  23. * 调用check检测更新
  24. * @exampl
  25. 定义数据枚举 字段名和字段key需要一致
  26. export enum day_type{
  27. score ="score",
  28. level ="level",
  29. relive="relive",
  30. }
  31. 初始所有定义数据
  32. day_data = new PeriodData<day_type>(PeriodDataUpdate.day,
  33. Object.keys(day_type) as day_type[],
  34. new Map([
  35. [day_type.relive,{min:0,max:3}],//限制每日复活3次
  36. ])
  37. );
  38. day_data.get(day_type.score)...
  39. */
  40. _export("default", PeriodData = class PeriodData {
  41. constructor(update, limt) {
  42. this._data = new Map();
  43. this._data_Array = new Map();
  44. this._last = 0;
  45. this._update = void 0;
  46. this._update = update;
  47. if (!limt) return; //遍历所有键进行初始化
  48. limt.forEach((v, key) => {
  49. var _v$min;
  50. this._data.set(key, {
  51. v: (_v$min = v.min) != null ? _v$min : 0,
  52. min: v.min,
  53. max: v.max
  54. });
  55. }, this);
  56. }
  57. /**
  58. * 序列化(数据库存储)
  59. */
  60. serialize() {
  61. const obj = {};
  62. this._data.forEach((value, key) => {
  63. obj[String(key)] = value.v;
  64. });
  65. const obj1 = {};
  66. this._data_Array.forEach((value, key) => {
  67. obj1[String(key)] = value;
  68. });
  69. const data = {
  70. "last": this._last,
  71. "data": obj,
  72. "data_Array": obj1
  73. };
  74. return data;
  75. }
  76. /**
  77. * 反序列化
  78. */
  79. unserialize(data) {
  80. var _data$last;
  81. if (!data) return;
  82. this._last = (_data$last = data.last) != null ? _data$last : 0;
  83. const obj = data.data;
  84. if (obj) {
  85. for (const key in obj) {
  86. const value = obj[key];
  87. this._get(key).v = value;
  88. }
  89. }
  90. const obj1 = data.data_Array;
  91. if (obj1) {
  92. for (const key in obj1) {
  93. const value = obj1[key];
  94. this.set_Array(key, value);
  95. }
  96. }
  97. }
  98. _get(key) {
  99. let data = this._data.get(key);
  100. if (!data) {
  101. data = {
  102. v: 0,
  103. min: 0,
  104. max: null
  105. };
  106. this._data.set(key, data);
  107. }
  108. return data;
  109. }
  110. _get_Array(key) {
  111. let data = this._data_Array.get(key);
  112. console.log(`_get_Array called with key: ` + String(key) + `data:`, data); // 输出存储的数据
  113. if (!data) {
  114. data = new Array(5).fill(false);
  115. this._data_Array.set(key, data);
  116. }
  117. return data;
  118. }
  119. /**如果有必要,重新设置数据的min,max*/
  120. set(key, value, min, max) {
  121. const d = this._get(key);
  122. d.v = value;
  123. d.min = min;
  124. d.max = max;
  125. }
  126. /**获取数据*/
  127. get(key) {
  128. return this._get(key).v;
  129. }
  130. set_Array(key, value) {
  131. this._data_Array.set(key, value);
  132. }
  133. /**获取数据*/
  134. get_Array(key) {
  135. return this._get_Array(key);
  136. }
  137. /**有大最值的话,获取数据为剩余可用次数*/
  138. get_available(key) {
  139. const d = this._get(key);
  140. if (d.max) return d.max - d.v;
  141. return d.v;
  142. }
  143. isEnble(key) {
  144. const d = this._get(key);
  145. return d.v > 0;
  146. }
  147. enble(key) {
  148. this._get(key).v = 1;
  149. }
  150. disEnble(key) {
  151. this._get(key).v = 0;
  152. }
  153. /**增加数据的值*/
  154. add(key, n = 1) {
  155. return this.change(key, n);
  156. }
  157. /**减小数据的值*/
  158. sub(key, n = 1) {
  159. return this.change(key, -n);
  160. }
  161. /**改变数据 n>0增加 小于0减少*/
  162. change(key, n) {
  163. const d = this._get(key);
  164. if (!d) return null;
  165. d.v += n;
  166. if (d.min && d.v < d.min) d.v = d.min;
  167. if (d.max && d.v > d.max) d.v = d.max;
  168. return d.v;
  169. }
  170. /**改变数据,比原本值大直接替换,没有改变返回null*/
  171. change_big(key, n) {
  172. const d = this._get(key);
  173. if (!d) return null;
  174. if (n > d.v) {
  175. d.v = n;
  176. if (d.min && d.v < d.min) d.v = d.min;
  177. if (d.max && d.v > d.max) d.v = d.max;
  178. return d.v;
  179. }
  180. return null;
  181. }
  182. /**改变数据,比原本值小直接替换,没有改变返回null*/
  183. change_sm(key, n) {
  184. const d = this._get(key);
  185. if (!d) return null;
  186. if (n < d.v) {
  187. d.v = n;
  188. if (d.min && d.v < d.min) d.v = d.min;
  189. if (d.max && d.v > d.max) d.v = d.max;
  190. return d.v;
  191. }
  192. return null;
  193. }
  194. /**检测更新now当前时间戳(ms)*/
  195. check(now) {
  196. if (this._update == PeriodDataUpdate.none) return false;
  197. if (this._update == PeriodDataUpdate.day) {
  198. if (!this.isSameDate(now, this._last)) {
  199. this._last = now;
  200. this.reset();
  201. return true;
  202. }
  203. }
  204. if (this._update == PeriodDataUpdate.week) {
  205. if (!this.areDatesInSameWeek(now, this._last)) {
  206. this._last = now;
  207. this.reset();
  208. this._data_Array = new Map();
  209. return true;
  210. }
  211. }
  212. if (this._update == PeriodDataUpdate.month) {
  213. if (!this.areDatesInSameMonth(now, this._last)) {
  214. this._last = now;
  215. this.reset();
  216. return true;
  217. }
  218. }
  219. return false;
  220. }
  221. reset() {
  222. for (const key of this._data.keys()) {
  223. var _data$min;
  224. const data = this._get(key);
  225. data.v = (_data$min = data.min) != null ? _data$min : 0;
  226. }
  227. for (const key of this._data_Array.keys()) {
  228. this._data_Array.set(key, new Array(5).fill(false));
  229. }
  230. }
  231. /**判定两个时间是否是同一天*/
  232. isSameDate(timestamp1, timestamp2) {
  233. let date1 = new Date(timestamp1);
  234. let date2 = new Date(timestamp2);
  235. return date1.getFullYear() == date2.getFullYear() && date1.getMonth() == date2.getMonth() && date1.getDate() == date2.getDate();
  236. }
  237. /**判定两个时间是否是同一周*/
  238. areDatesInSameWeek(date1, date2) {
  239. const startOfWeek1 = this.getStartOfWeek(new Date(date1));
  240. const startOfWeek2 = this.getStartOfWeek(new Date(date2));
  241. return startOfWeek1.getTime() === startOfWeek2.getTime();
  242. }
  243. getStartOfWeek(date) {
  244. const startOfWeek = new Date(date);
  245. const day = startOfWeek.getDay(); // 星期日是 0,星期一是 1,依此类推
  246. const diff = startOfWeek.getDate() - day; // 计算当前日期所在周的第一天
  247. startOfWeek.setDate(diff); // 设置为该周的第一天
  248. startOfWeek.setHours(0, 0, 0, 0); // 设置时间为 00:00:00
  249. return startOfWeek;
  250. }
  251. areDatesInSameMonth(date1, date2) {
  252. const d1 = new Date(date1);
  253. const d2 = new Date(date2);
  254. return d1.getFullYear() === d2.getFullYear() && d1.getMonth() === d2.getMonth();
  255. }
  256. });
  257. _cclegacy._RF.pop();
  258. _crd = false;
  259. }
  260. };
  261. });
  262. //# sourceMappingURL=7fe3975b5b0a9baadc008a3c988c3961b05c76c3.js.map