import { _decorator, Component, Node } from 'cc'; const { ccclass, property } = _decorator; // 月签类 export class MonthlySign { // 签到的天数 private _sign_data: number[]; private _creat_date: number = 0; private _year: number private _month: number private _day: number // 可签到的总天数 private _max: number get year(){ return this._year } get month(){ return this._month } get day(){ return this._day } // 获取年月信息 public init(data: { _sign_data: number[], creat_date: number } = null): void { // 初始化年月信息 let now = chsdk.date.now() let date = new Date(now) this._year = date.getFullYear() this._month = date.getMonth() + 1 this._max = new Date(this._year, this._month, 0).getDate() this._day = date.getDate() if (data) { this._creat_date = data.creat_date ?? chsdk.date.now(); let d = new Date(this._creat_date) if (d.getFullYear() == this._year && d.getMonth() + 1 == this._month) { this._sign_data = data._sign_data ?? new Array(this._max).fill(0) } else { this._sign_data = new Array(this._max).fill(0) } // 判断是否转月 } else { this._creat_date = now; this._sign_data = new Array(this._max).fill(0); } } /**已签到的数据,用于存档*/ public getSignData(): { _sign_data: number[], creat_date: number } { return { _sign_data: this._sign_data, creat_date: this._creat_date } } /** 返回某一天的状态 * 0 等待签到 * 1 已经签到 * 2 失效等待补签 * */ public checkSigineState(day: number): number { if (day < 1 || day > this._max) return 0; if (this._sign_data[day]) return 1; const count = this.getCreatDayCount(); if (count <= 0) return 0; if (day < count) return 2; return 0; } private getCreatDayCount(): number { if (this._creat_date == 0) { chsdk.log.warn("签到模块没有初始化"); return 0; } return this._day; } /**补签 * 失败返回 0 * 成功返回补签那天 */ public reSignIn(): number { const index = this.checkReSigin(); if (index <= 0 || index > this._max) { return 0; } this._sign_data[index] = 1 return index; } /** * 是否能补签 * 不能返回0, * 可以补签返回可以补签的那一天*/ public checkReSigin(): number { let count = this.getCreatDayCount() - 1; if (count <= 0) return 0; for (let i = count; i >= 1; i--) { if (!this._sign_data[i]) return i; } return 0; } /**签到 * 失败返回 0 * 成功返回签到当天 */ public signIn(): number { if (!this.checkSigin()) { return 0; } const day = this.getCreatDayCount(); this._sign_data[day] = 1; return day; } /** * 计算漏签的天数 */ getReSignNumber(){ let res = 0 let count = this.getCreatDayCount() - 1; if (count <= 0) return res; for (let i = count; i >= 1; i--) { if (!this._sign_data[i]) res++; } return res } /**今天是否可以签到*/ public checkSigin(): boolean { const count = this.getCreatDayCount(); if (count <= 0 || count > this._max) return false; return !this._sign_data[count]; } getSiginMax(): number { return this._max; } getSignNumber(){ return this._sign_data.filter(it=>it).length } }