ShakeModel.ts
7.84 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import BaseModel from "./BaseModel";
import { AppSdk } from "../../sdk/AppSdk";
import DateUtils from "../../uitl/DateUtils";
import Util, { DataKey } from "../../uitl/Util";
import ObjectInstance from "../../uitl/ObjectInstance";
/**
*
*/
export default class ShakeModel extends BaseModel {
private conf: any;
private eleProbabilityArr: number[] = [];
private totalWeight: number = 0;
private proArrLen: number = 0;
/**红包出现的总次数每日 */
private red_day_limit: number = 0;
private cur_red_day_limit: number = 0;
/**摇一摇出现的总次数 */
private shake_day_limit: number = 0;
private cur_shake_day_limit: number = 0;
/**
* red_day_limit 红包每日极限产出
* shake_day_limit 摇一摇每日极限产出
* interstitial_cnt 插屏计数(每出现x次通用获得物品界面后,出现插屏)
* interstitial_delay 插屏出现延时(毫秒)
*/
/**
* // let result = {
// data: {
// gameConfig: {
// "config": {
// "red_day_limit": 10,
// "shake_day_limit": 50,
// "interstitial_cnt": 2,
// "interstitial_delay": 500
// },
// "shake": [
// {
// "id": 1,
// "type": 1,
// "weight": 1000,
// "num": 0
// },
// {
// "id": 2,
// "type": 2,
// "weight": 400,
// "num": 0
// },
// {
// "id": 3,
// "type": 3,
// "weight": 300,
// "num": 1
// },
// {
// "id": 4,
// "type": 3,
// "weight": 100,
// "num": 2
// },
// {
// "id": 5,
// "type": 4,
// "weight": 300,
// "num": 1
// },
// {
// "id": 6,
// "type": 4,
// "weight": 100,
// "num": 2
// },
// {
// "id": 7,
// "type": 5,
// "weight": 300,
// "num": 1
// },
// {
// "id": 8,
// "type": 5,
// "weight": 100,
// "num": 2
// },
// {
// "id": 9,
// "type": 6,
// "weight": 300,
// "num": 1
// },
// {
// "id": 10,
// "type": 6,
// "weight": 100,
// "num": 2
// },
// {
// "id": 11,
// "type": 7,
// "weight": 300,
// "num": 1
// },
// {
// "id": 12,
// "type": 7,
// "weight": 100,
// "num": 2
// },
// {
// "id": 13,
// "type": 8,
// "weight": 500,
// "num": 0
// }
// ],
// 'coin_config': []
// }
// }
// }
*/
async init() {
let result = await AppSdk.I.getGameConfig();
console.log('---------------ShakeModel-------'+JSON.stringify(result.data))
let data = result.data;
if (data['gameConfig']) {
let gameData = data['gameConfig'];
this.update(this.initData(gameData));
}
}
initData(gameData) {
let data = new ShakeData;
let lastTime = Util.I.getItem(DataKey.shakeLastTime) || '0';
let lt = parseInt(lastTime);
let last = DateUtils.timeDay(lt);
let today = DateUtils.today;
if (last != today) {
this.cur_shake_day_limit = 0;
this.cur_red_day_limit = 0;
lastTime = DateUtils.nowTime.toString();
Util.I.setItem(DataKey.cur_shake_day_limit, 0);
Util.I.setItem(DataKey.cur_red_day_limit, 0);
Util.I.setItem(DataKey.shakeLastTime, lastTime);
} else {
let cur_red_day_limit = Util.I.getItem(DataKey.cur_red_day_limit) || '0';
this.cur_red_day_limit = parseInt(cur_red_day_limit);
let cur_shake_day_limit = Util.I.getItem(DataKey.cur_shake_day_limit) || '0';
this.cur_shake_day_limit = parseInt(cur_shake_day_limit);
}
data.config = gameData['config'];
gameData['shake'].forEach(ele => {
data.shakeWeightList.push(ele);
});
this.conf = [];
this.conf = data.shakeWeightList;
this.initProbability();
this.shake_day_limit = data.config.shake_day_limit;
this.red_day_limit = data.config.red_day_limit;
return data;
}
initProbability() {
this.totalWeight = 0;
this.conf.forEach((ele, index) => {
this.totalWeight += ele.weight;
let itemW = ele.weight;
if (index > 0)
itemW += this.eleProbabilityArr[index - 1];
this.eleProbabilityArr = [...this.eleProbabilityArr, itemW];
})
this.proArrLen = this.eleProbabilityArr.length;
}
getShakeType() {
++this.cur_shake_day_limit;
Util.I.setItem(DataKey.cur_shake_day_limit, this.cur_shake_day_limit);
if (this.cur_shake_day_limit > this.shake_day_limit) {
/**'次数达到上限,请明天再来*/
/**在此处可以弹框等处理 */
return false;
}
let ran = Math.floor(Math.random() * this.totalWeight);
for (let index = 0; index < this.proArrLen; index++) {
const ele = this.eleProbabilityArr[index];
if (ele >= ran) {
if (0 === index || 1 === index) {
++this.cur_red_day_limit;
Util.I.setItem(DataKey.cur_red_day_limit, this.cur_red_day_limit);
if (this.cur_red_day_limit > this.red_day_limit) {
return this.conf[this.proArrLen - 1];
}
}
return this.conf[index];
}
}
return this.conf[this.proArrLen - 1];
}
get data(): ShakeData {
return this._data;
}
static get I(): ShakeModel {
return ObjectInstance.get(ShakeModel) as ShakeModel;
}
}
class ShakeData {
config: ShakeConfig;
shakeWeightList: ShakeWeightData[] = [];
}
interface ShakeConfig {
red_day_limit: number,
shake_day_limit: number,
interstitial_cnt: number,
interstitial_delay: number
}
interface ShakeWeightData {
id: number;
type: number;
weight: number;
num: number;
}