-
Notifications
You must be signed in to change notification settings - Fork 36
/
ResourcesLib.js
400 lines (306 loc) · 10.4 KB
/
ResourcesLib.js
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
let libPrefix = 'ResourcesLib_';
let growthResource = function(resource){
return {
resource: resource,
/// auto growthing or decreasing
propName: function(){ return this.resource.propName() + '_growth'},
info: function(){
return Bot.getProperty(this.propName()) || {}
},
title: function(){
if(!this.isEnabled){ return }
let growth = this.info();
let start_text = 'add ' + String(growth.increment);
let middle_text = ' once at ' + String(growth.interval) + ' secs';
if(growth.type=='simple'){
return start_text + middle_text
}
if(growth.type=='percent'){
return start_text + '%' + middle_text
}
if(growth.type=='compound_interest'){
return start_text + '%' + middle_text + ' with reinvesting'
}
},
have: function(){ return this.info() },
isEnabled: function(){
let growth = this.info();
if(growth){ return growth.enabled }
return false;
},
_toggle: function(status){
let growth = this.info();
if(!growth){ return }
growth.enabled = status;
return Bot.setProperty(this.propName(), growth, 'json');
},
stop: function(){
return this._toggle(false);
},
progress: function(){
let growth = this.info();
if(!growth){ return }
let total_iterations = this.totalIterations(growth);
let fraction = total_iterations % 1;
return fraction*100;
},
willCompletedAfter: function(){
return this.info().interval - this.progress()/100 * this.info().interval;
},
totalIterations: function(growth){
if(!growth){ growth = this.info() }
let now = (new Date().getTime());
let duration_in_seconds = ( now - growth.started_at ) / 1000;
return duration_in_seconds / growth.interval;
},
_calcMinMax(result, growth){
if((growth.min)&&(growth.min > result)){
return growth.min
}
if((growth.max)&&(growth.max < result)){
return growth.max
}
return result
},
_calcByTotalIterations(value, total_iterations, growth){
var result;
if(growth.type=='simple'){
result = value + total_iterations * growth.increment
}
if(growth.type=='percent'){
let percent = growth.increment / 100;
let all_percents = percent * growth.base_value * total_iterations
result = value + all_percents;
}
if(growth.type=='compound_interest'){
let percent = (1 + growth.increment / 100);
result = value * Math.pow(percent, total_iterations)
}
return result;
},
_getTotalIterationsWithLimit(growth){
let total_iterations = this.totalIterations(growth);
if(!growth.max_iterations_count){ return total_iterations }
let total = total_iterations + growth.completed_iterations_count;
if(total < growth.max_iterations_count){
return total_iterations
}
return growth.max_iterations_count - growth.completed_iterations_count;
},
_calcValue(value, growth){
let total_iterations = this._getTotalIterationsWithLimit(growth);
if(total_iterations<1){ return }
let fraction = total_iterations % 1;
total_iterations = total_iterations - fraction;
var result = this._calcByTotalIterations(value, total_iterations, growth)
growth.completed_iterations_count+= total_iterations;
result = this._calcMinMax(result, growth);
this._updateIteration(growth, fraction * 1000);
return result;
},
getValue: function(value){
let growth = this.info();
if(!growth){ return value }
if(!growth.enabled){ return value }
let new_value = this._calcValue(value, growth);
if(!new_value){ return value }
this.resource._set(new_value); /// update value
return new_value;
},
_updateIteration: function(growth, fraction){
if(!growth){ growth = this.info() }
if(!growth){ return }
let started_at = (new Date().getTime());
/// started same early
if(fraction){ started_at = started_at - fraction }
growth.started_at = started_at;
return Bot.setProperty(this.propName(), growth, 'json');
},
_updateBaseValue: function(base_value){
var growth = this.info();
if(!growth){ return }
growth.base_value = base_value;
return Bot.setProperty(this.propName(), growth, 'json');
},
_newGrowth: function(options){
return {
base_value: this.resource.baseValue(),
increment: options.increment,
interval: options.interval,
type: options.type,
min: options.min,
max: options.max,
max_iterations_count: options.max_iterations_count,
enabled: true,
completed_iterations_count: 0
}
},
_addAs: function(options){
let growth = this._newGrowth(options);
return this._updateIteration(growth);
},
add: function(options){
/// absolute growth value
options.type = 'simple';
options.increment = options.value;
return this._addAs(options);
},
addPercent: function(options){
/// percent
options.type = 'percent';
options.increment = options.percent;
return this._addAs(options);
},
addCompoundInterest: function(options){
/// compound percent
options.type = 'compound_interest';
options.increment = options.percent;
return this._addAs(options);
}
}
}
let commonResource = function(objName, objID, resName){
return {
objName: objName,
objID: objID,
name: resName,
growth: null,
_setGrowth: function(growth){
this.growth = growth;
},
propName: function(){
return libPrefix + this.objName + this.objID + '_' + this.name
},
isNumber: function(value){ return typeof(value)=='number' },
verifyNumber: function(value){
if(!this.isNumber(value)){
let evalue = '';
if(typeof(value)!='undefined'){ evalue = JSON.stringify(value) }
throw 'ResLib: value must be number only. It is not number: ' + typeof(value) + ' ' + evalue;
}
},
removeRes: function(res_amount){
this.set(this.value() - res_amount);
return true;
},
baseValue: function(){
let cur_value = Bot.getProperty(this.propName());
if(typeof(cur_value)=='undefined'){ return 0 }
return cur_value;
},
value: function(){
let cur_value = this.baseValue();
if(this._withEnabledGrowth()){
return this.growth.getValue(cur_value);
}
return cur_value;
},
add: function(res_amount){
this.verifyNumber(res_amount);
this.set(this.value() + res_amount)
return true;
},
have: function(res_amount){
this.verifyNumber(res_amount);
// can not have negative or null amount
if(res_amount < 0){ return false }
if(res_amount == 0){ return false }
return this.value() >= res_amount;
},
remove: function(res_amount){
if(!this.have(res_amount)){
throw 'ResLib: not enough resources'
}
return this.removeRes(res_amount);
},
removeAnyway: function(res_amount){
this.verifyNumber(res_amount);
return this.removeRes(res_amount)
},
_withEnabledGrowth: function(){
return (this.growth && this.growth.isEnabled())
},
_set: function(res_amount){
Bot.setProperty(this.propName(), res_amount, 'float');
},
set: function(res_amount){
this.verifyNumber(res_amount);
if( this._withEnabledGrowth() ){
this.growth._updateBaseValue(res_amount)
}
return this._set(res_amount);
},
anywayTakeFromAndTransferTo: function(fromResource, toResource, res_amount){
if(fromResource.name!=toResource.name){
throw 'ResLib: can not transfer different resources'
}
if(fromResource.removeAnyway(res_amount)){
return toResource.add(res_amount)
}
return false
},
anywayTakeFromAndTransferToDifferent: function(fromResource, toResource, remove_amount, add_amount){
if(fromResource.removeAnyway(remove_amount)){
return toResource.add(add_amount)
}
return false
},
takeFromAndTransferTo: function(fromResource, toResource, res_amount){
if(!fromResource.have(res_amount)){
throw 'ResLib: not enough resources for transfer'
}
return this.anywayTakeFromAndTransferTo(fromResource, toResource, res_amount)
},
takeFromAndTransferToDifferent: function(fromResource, toResource, remove_amount, add_amount){
if(!fromResource.have(remove_amount)){
throw 'ResLib: not enough resources for transfer'
}
return this.anywayTakeFromAndTransferToDifferent(fromResource, toResource, remove_amount, add_amount)
},
takeFromAnother: function(anotherResource, res_amount){
return this.takeFromAndTransferTo(anotherResource, this, res_amount);
},
transferTo: function(anotherResource, res_amount){
return this.takeFromAndTransferTo(this, anotherResource, res_amount);
},
exchangeTo: function(anotherResource, options){
return this.takeFromAndTransferToDifferent(this,
anotherResource, options.remove_amount, options.add_amount );
},
takeFromAnotherAnyway: function(anotherResource, res_amount){
return this.anywayTakeFromAndTransferTo(anotherResource, this, res_amount);
},
transferToAnyway: function(anotherResource, res_amount){
return this.anywayTakeFromAndTransferTo(this, anotherResource, res_amount);
}
}
}
let growthFor = function(resource){
let growth = growthResource(resource);
resource._setGrowth(growth);
return growth;
}
let getResourceFor = function(object, object_id, resName){
let res = commonResource(object, object_id, resName);
growthFor(res);
return res;
}
let userResource = function(resName){
return getResourceFor('user', user.telegramid, resName);
}
let chatResource = function(resName){
return getResourceFor('chat', chat.chatid, resName);
}
let anotherUserResource = function(resName, telegramid){
return getResourceFor('user', telegramid, resName);
}
let anotherChatResource = function(resName, chatid){
return getResourceFor('chat', chatid, resName);
}
publish({
userRes: userResource,
chatRes: chatResource,
anotherUserRes: anotherUserResource,
anotherChatRes: anotherChatResource,
growthFor: growthFor
})