| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- 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
- }
- }
|