RedpacketModel.ts
5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import BaseModel from "./BaseModel";
import AdManager from "../../sdk/AdManager";
import TeaSdk, { DotKey } from "../../sdk/TeaSdk";
import TrackingSdk, { TrackingKey } from "../../sdk/TrackingSdk";
import DateUtils from "../../uitl/DateUtils";
import Util, { DataKey } from "../../uitl/Util";
import ObjectInstance from "../../uitl/ObjectInstance";
import { EventCenter } from "../../event/EventCenter";
import { EventEnum } from "../../event/EventEnum";
import AppSdk from "../AppSdk";
export default class RedpacketModel extends BaseModel {
private MAX = 20;
/**当天领取红包的最大次数 */
private DAY_MAX_CNT = 50;
private conf: any;
public curRedpacket: number = 0;
public confLen: number = 0;
/**初始化红包配置 */
async init() {
let arr = await this.getRedConfig();
console.log('---------------RedpacketModel-------' + JSON.stringify(arr))
let cash = 0;
this.conf = {};
arr.forEach(confData => {
this.conf[confData.id] = confData;
});
this.update(this.initData());
this.confLen = this.conf.length;
}
initData() {
let data = new RedpacketData;
let lastTime = Util.I.getItem(DataKey.lastTime) || '0';
data.lastTime = parseInt(lastTime);
let last = DateUtils.timeDay(data.lastTime);
let today = DateUtils.today;
if (last != today) {
data.todayCnt = 0;
lastTime = DateUtils.nowTime.toString();
Util.I.setItem(DataKey.lastTime, lastTime)
Util.I.setItem(DataKey.todayCnt, 0);
} else {
let todayCnt = Util.I.getItem(DataKey.todayCnt) || '0';
data.todayCnt = parseInt(todayCnt);
}
data.lastTime = parseInt(lastTime);
let dataCnt = Util.I.getItem(DataKey.dataCNT) || '0';
data.cnt = parseInt(dataCnt);
data.list = [];
return data;
}
async getRedConfig() {
let result = AppSdk.I.gameConfig;
console.log('--------------- 获取配置静态表-----' + JSON.stringify(result))
if (result) {
let configJson = await this.loadGoldCfg(result['coin_config'][0])
console.log('-------------- 获取红包静态表-----' + JSON.stringify(result))
return configJson;
} else {
AppSdk.I.initGameConfig();
}
}
async loadGoldCfg(res: string) {
let configJson = await this.load(res);
return configJson;
}
load(res, onProgress?: Laya.Handler): Promise<any> {
return new Promise((suc, fail) => {
Laya.loader.load(res, Laya.Handler.create(this, suc), onProgress);
})
}
async checkRedpacket() {
if (this.data.todayCnt >= this.DAY_MAX_CNT) return false;
if (AdManager.I.checkVideoInCd()) return false;
/**红包获取后没有看广告领取,再次获得红包的机会,不会获取红包, 自己也可以处理成 对应的红包提醒或不需要这里的判断 */
if (this.curRedpacket) {
return false;
}
if (undefined === this.data.cnt) this.data.cnt = 0;
let cnt = this.data.cnt;
if (cnt >= this.confLen) cnt = this.confLen - 1;
let data = this.conf[cnt + 1];
let ran2 = Math.random();
if (data && ran2 < data.per) {
let cash = 0.000001;
this.curRedpacket = cash;
//**弹出红包的界面, 暂时处理成 直接看广告获取金币 */
await AdManager.I.showRewardAd()
console.log('--------------- 触发了红包-----')
this.getRepacket()
/**更新基本显示 */
EventCenter.emit(EventEnum.UPDATE_COIN);
/*** */
return true;
}
return false;
}
getRepacket() {
if (!this.curRedpacket) return;
this.data.cnt += 1;
this.data.list.push(this.curRedpacket);
this.data.lastTime = (new Date).getTime();
this.data.todayCnt += 1;
if (this.data.cnt == 1)
TrackingSdk.I.dot(TrackingKey.RedPack1);
if (this.data.cnt == 5)
TrackingSdk.I.dot(TrackingKey.RedPack5);
this.curRedpacket = 0;
Util.I.setItem(DataKey.dataCNT, this.data.cnt);
Util.I.setItem(DataKey.todayCnt, this.data.todayCnt);
this.update(this.data);
}
/** */
async getCurRedpacket() {
return this.curRedpacket;
}
get data(): RedpacketData {
return this._data;
}
/**获取总共的金币数 */
async getTotalCoin() {
let totalCoin = await AppSdk.I.myCoin();
return totalCoin;
}
/**获取本次奖励的金币数 */
async addCoin(key: number = 1) {
let coinData = await AppSdk.I.addGold(key);
return coinData['coin'];
}
/**获取最低提档 */
async minWithDrawal() {
let minCoin = await AppSdk.I.minWithDrawal();
return minCoin;
}
static get I(): RedpacketModel {
return ObjectInstance.get(RedpacketModel) as RedpacketModel;
}
}
class RedpacketData {
cnt: number;
list: Array<number>;
lastTime: number;
todayCnt: number;
}
class RedpacketConfigData {
id: number;
per: number;
cash: number;
}