MonthlySign.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { _decorator, Component, Node } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. // 月签类
  4. export class MonthlySign {
  5. // 签到的天数
  6. private _sign_data: number[];
  7. private _creat_date: number = 0;
  8. private _year: number
  9. private _month: number
  10. private _day: number
  11. // 可签到的总天数
  12. private _max: number
  13. get year(){
  14. return this._year
  15. }
  16. get month(){
  17. return this._month
  18. }
  19. get day(){
  20. return this._day
  21. }
  22. // 获取年月信息
  23. public init(data: { _sign_data: number[], creat_date: number } = null): void {
  24. // 初始化年月信息
  25. let now = chsdk.date.now()
  26. let date = new Date(now)
  27. this._year = date.getFullYear()
  28. this._month = date.getMonth() + 1
  29. this._max = new Date(this._year, this._month, 0).getDate()
  30. this._day = date.getDate()
  31. if (data) {
  32. this._creat_date = data.creat_date ?? chsdk.date.now();
  33. let d = new Date(this._creat_date)
  34. if (d.getFullYear() == this._year && d.getMonth() + 1 == this._month) {
  35. this._sign_data = data._sign_data ?? new Array(this._max).fill(0)
  36. } else {
  37. this._sign_data = new Array(this._max).fill(0)
  38. }
  39. // 判断是否转月
  40. } else {
  41. this._creat_date = now;
  42. this._sign_data = new Array(this._max).fill(0);
  43. }
  44. }
  45. /**已签到的数据,用于存档*/
  46. public getSignData(): { _sign_data: number[], creat_date: number } {
  47. return { _sign_data: this._sign_data, creat_date: this._creat_date }
  48. }
  49. /** 返回某一天的状态
  50. * 0 等待签到
  51. * 1 已经签到
  52. * 2 失效等待补签
  53. * */
  54. public checkSigineState(day: number): number {
  55. if (day < 1 || day > this._max) return 0;
  56. if (this._sign_data[day]) return 1;
  57. const count = this.getCreatDayCount();
  58. if (count <= 0) return 0;
  59. if (day < count) return 2;
  60. return 0;
  61. }
  62. private getCreatDayCount(): number {
  63. if (this._creat_date == 0) {
  64. chsdk.log.warn("签到模块没有初始化");
  65. return 0;
  66. }
  67. return this._day;
  68. }
  69. /**补签
  70. * 失败返回 0
  71. * 成功返回补签那天
  72. */
  73. public reSignIn(): number {
  74. const index = this.checkReSigin();
  75. if (index <= 0 || index > this._max) {
  76. return 0;
  77. }
  78. this._sign_data[index] = 1
  79. return index;
  80. }
  81. /**
  82. * 是否能补签
  83. * 不能返回0,
  84. * 可以补签返回可以补签的那一天*/
  85. public checkReSigin(): number {
  86. let count = this.getCreatDayCount() - 1;
  87. if (count <= 0) return 0;
  88. for (let i = count; i >= 1; i--) {
  89. if (!this._sign_data[i]) return i;
  90. }
  91. return 0;
  92. }
  93. /**签到
  94. * 失败返回 0
  95. * 成功返回签到当天
  96. */
  97. public signIn(): number {
  98. if (!this.checkSigin()) {
  99. return 0;
  100. }
  101. const day = this.getCreatDayCount();
  102. this._sign_data[day] = 1;
  103. return day;
  104. }
  105. /**
  106. * 计算漏签的天数
  107. */
  108. getReSignNumber(){
  109. let res = 0
  110. let count = this.getCreatDayCount() - 1;
  111. if (count <= 0) return res;
  112. for (let i = count; i >= 1; i--) {
  113. if (!this._sign_data[i]) res++;
  114. }
  115. return res
  116. }
  117. /**今天是否可以签到*/
  118. public checkSigin(): boolean {
  119. const count = this.getCreatDayCount();
  120. if (count <= 0 || count > this._max) return false;
  121. return !this._sign_data[count];
  122. }
  123. getSiginMax(): number {
  124. return this._max;
  125. }
  126. getSignNumber(){
  127. return this._sign_data.filter(it=>it).length
  128. }
  129. }