c0e9a0408b2c77521760095bef7e3d1dc8b428fc.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. if (!data) {
  113. data = new Array(7).fill(false);
  114. this._data_Array.set(key, data);
  115. }
  116. return data;
  117. }
  118. /**如果有必要,重新设置数据的min,max*/
  119. set(key, value, min, max) {
  120. const d = this._get(key);
  121. d.v = value;
  122. d.min = min;
  123. d.max = max;
  124. }
  125. /**获取数据*/
  126. get(key) {
  127. return this._get(key).v;
  128. }
  129. set_Array(key, value) {
  130. this._data_Array.set(key, value);
  131. }
  132. /**获取数据*/
  133. get_Array(key) {
  134. return this._get_Array(key);
  135. }
  136. /**有大最值的话,获取数据为剩余可用次数*/
  137. get_available(key) {
  138. const d = this._get(key);
  139. if (d.max) return d.max - d.v;
  140. return d.v;
  141. }
  142. isEnble(key) {
  143. const d = this._get(key);
  144. return d.v > 0;
  145. }
  146. enble(key) {
  147. this._get(key).v = 1;
  148. }
  149. disEnble(key) {
  150. this._get(key).v = 0;
  151. }
  152. /**增加数据的值*/
  153. add(key, n = 1) {
  154. return this.change(key, n);
  155. }
  156. /**减小数据的值*/
  157. sub(key, n = 1) {
  158. return this.change(key, -n);
  159. }
  160. /**改变数据 n>0增加 小于0减少*/
  161. change(key, n) {
  162. const d = this._get(key);
  163. if (!d) return null;
  164. d.v += n;
  165. if (d.min && d.v < d.min) d.v = d.min;
  166. if (d.max && d.v > d.max) d.v = d.max;
  167. return d.v;
  168. }
  169. /**改变数据,比原本值大直接替换,没有改变返回null*/
  170. change_big(key, n) {
  171. const d = this._get(key);
  172. if (!d) return null;
  173. if (n > d.v) {
  174. d.v = n;
  175. if (d.min && d.v < d.min) d.v = d.min;
  176. if (d.max && d.v > d.max) d.v = d.max;
  177. return d.v;
  178. }
  179. return null;
  180. }
  181. /**改变数据,比原本值小直接替换,没有改变返回null*/
  182. change_sm(key, n) {
  183. const d = this._get(key);
  184. if (!d) return null;
  185. if (n < d.v) {
  186. d.v = n;
  187. if (d.min && d.v < d.min) d.v = d.min;
  188. if (d.max && d.v > d.max) d.v = d.max;
  189. return d.v;
  190. }
  191. return null;
  192. }
  193. /**检测更新now当前时间戳(ms)*/
  194. check(now) {
  195. if (this._update == PeriodDataUpdate.none) return false;
  196. if (this._update == PeriodDataUpdate.day) {
  197. if (!this.isSameDate(now, this._last)) {
  198. this._last = now;
  199. this.reset();
  200. return true;
  201. }
  202. }
  203. if (this._update == PeriodDataUpdate.week) {
  204. if (!this.areDatesInSameWeek(now, this._last)) {
  205. this._last = now;
  206. this.reset();
  207. this._data_Array = new Map();
  208. return true;
  209. }
  210. }
  211. if (this._update == PeriodDataUpdate.month) {
  212. if (!this.areDatesInSameMonth(now, this._last)) {
  213. this._last = now;
  214. this.reset();
  215. return true;
  216. }
  217. }
  218. return false;
  219. }
  220. reset() {
  221. for (const key of this._data.keys()) {
  222. var _data$min;
  223. const data = this._get(key);
  224. data.v = (_data$min = data.min) != null ? _data$min : 0;
  225. }
  226. }
  227. /**判定两个时间是否是同一天*/
  228. isSameDate(timestamp1, timestamp2) {
  229. let date1 = new Date(timestamp1);
  230. let date2 = new Date(timestamp2);
  231. return date1.getFullYear() == date2.getFullYear() && date1.getMonth() == date2.getMonth() && date1.getDate() == date2.getDate();
  232. }
  233. /**判定两个时间是否是同一周*/
  234. areDatesInSameWeek(date1, date2) {
  235. const startOfWeek1 = this.getStartOfWeek(new Date(date1));
  236. const startOfWeek2 = this.getStartOfWeek(new Date(date2));
  237. return startOfWeek1.getTime() === startOfWeek2.getTime();
  238. }
  239. getStartOfWeek(date) {
  240. const startOfWeek = new Date(date);
  241. const day = startOfWeek.getDay(); // 星期日是 0,星期一是 1,依此类推
  242. const diff = startOfWeek.getDate() - day; // 计算当前日期所在周的第一天
  243. startOfWeek.setDate(diff); // 设置为该周的第一天
  244. startOfWeek.setHours(0, 0, 0, 0); // 设置时间为 00:00:00
  245. return startOfWeek;
  246. }
  247. areDatesInSameMonth(date1, date2) {
  248. const d1 = new Date(date1);
  249. const d2 = new Date(date2);
  250. return d1.getFullYear() === d2.getFullYear() && d1.getMonth() === d2.getMonth();
  251. }
  252. });
  253. _cclegacy._RF.pop();
  254. _crd = false;
  255. }
  256. };
  257. });
  258. //# sourceMappingURL=c0e9a0408b2c77521760095bef7e3d1dc8b428fc.js.map