-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommonFunctions.rb
More file actions
579 lines (441 loc) · 16.2 KB
/
Copy pathcommonFunctions.rb
File metadata and controls
579 lines (441 loc) · 16.2 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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
require './orders/buildFarm.rb'
require './orders/buildShrine.rb'
def armyTravelTime(army)
slowest = 99999
$settings[:soldierTypes].each do |soldierType|
if army[soldierType.pluralize.to_sym].to_i > 0
s = $settings[:soldiers][soldierType.to_sym][:speed]
if s < slowest
slowest = s
end
end
end
return $settings[:armyTravelDistance] / slowest.to_f * 60.0
end
# called when someone wins the game
def resetGame(mongo, winningUser)
# save winner
winner = {
:startedAt => winningUser[:createdAt],
:endedAt => Time.now,
:userId => winningUser[:_id],
:discordId => winningUser[:discordId],
:display_name => winningUser[:display_name],
:avatar_url => winningUser[:avatar_url],
:mention => winningUser[:mention],
:distinct => winningUser[:distinct],
:discriminator => winningUser[:discriminator],
:pmChannelId => winningUser[:pmChannelId],
:serverId => winningUser[:serverId],
:serverName => winningUser[:serverName],
:networth => winningUser[:networth],
:population => winningUser[:population],
:numShrinesBuilt => winningUser[:numShrinesBuilt]
}
mongo[:winners].insert_one(winner);
# drop buildings
$settings[:buildingTypes].each do |buildingType|
mongo[buildingType.pluralize.to_sym].drop()
end
mongo[:market].drop()
mongo[:armies].drop()
mongo[:orders].drop()
mongo[:users].drop()
validateMarket(mongo)
end
def isUserPlaying(mongo, discordId)
if mongo[:users].find(:discordId => discordId).count == 0
return false
else
return true
end
end
# update networth for all players
def updateNetworth(mongo)
bulk = []
markets = mongo[:market].find()
mongo[:users].find().each do |user|
bulk << {:update_one => {
:filter => {:_id => user[:_id]},
:update => {'$set' => {:networth => calculateNetworthForUser(mongo, markets, user)}}
}}
end
mongo[:users].bulk_write bulk
end
def updateNetworthFor(mongo, discordId)
user = mongo[:users].find(:discordId => discordId).first
markets = mongo[:market].find()
if user
mongo[:users].update_one({:_id => user[:_id]}, {'$set' => {:networth => calculateNetworthForUser(mongo, markets, user)}})
end
end
def calculateNetworthForUser(mongo, markets, user)
net = user[:gold]
# resources
$settings[:resourceTypes].each do |resourceType|
gold = resourceToGold(markets, resourceType, user[resourceType.to_sym].to_f)
if gold != nil
net += gold
end
end
# soldiers
$settings[:soldierTypes].each do |soldierType|
$settings[:soldiers][soldierType.to_sym][:cost].each do |cost|
gold = resourceToGold(markets, cost[:type], cost[:num])
if gold != nil
net += user[soldierType.pluralize.to_sym].to_f * gold
end
end
end
# armies
mongo[:armies].find({:userId => user[:_id]}).each do |army|
$settings[:soldierTypes].each do |soldierType|
$settings[:soldiers][soldierType.to_sym][:cost].each do |cost|
gold = resourceToGold(markets, cost[:type], cost[:num])
if gold != nil
net += army[soldierType.pluralize.to_sym].to_f * gold
end
end
end
end
# shrines
mongo[:shrines].find({:discordId => user[:discordId]}).each do |shrine|
$settings[:buildings][:shrine][:cost].each do |cost|
gold = resourceToGold(markets, cost[:type], cost[:num])
if gold != nil
net += gold
end
end
end
# shrines being built
mongo[:orders].find({:discordId => user[:discordId], :type => "buildShrine"}).each do |shrine|
$settings[:buildings][:shrine][:cost].each do |cost|
gold = resourceToGold(markets, cost[:type], cost[:num])
if gold != nil
net += gold
end
end
end
net
end
def resourceToGold(markets, resourceType, quantity)
market = nil
markets.each do |m|
if m[:type] == resourceType
market = m
end
end
if !market
return nil
end
totalOfSell(market[:value], quantity)
end
# resourceObject is market data for resource from mongo
def updateMarketPrice(mongo, resourceObject, type, quantity, isBuy)
value = resourceObject[:value]
if !isBuy
quantity *= -1.0
end
value = value * (($settings[:marketIncrement] + 1.0) ** quantity)
mongo[:market].update_one({:_id => resourceObject[:_id]}, {"$set" => {value:value}})
# redo networth for everyone
updateNetworth(mongo)
end
# returns a string with commas
def number_with_commas(number)
if number == nil
return 0
end
parts = number.to_s.split('.')
parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")
parts.join('.')
end
def totalOfSell(marketValue, quantity)
marketValue * (1.0 - ((1.0 - $settings[:marketIncrement]) ** quantity.to_f)) / $settings[:marketIncrement]
end
def totalOfBuy(marketValue, quantity)
marketValue * (1.0 + $settings[:marketTax]) / $settings[:marketIncrement] * ((($settings[:marketIncrement] + 1.0) ** quantity.to_f) - 1.0)
end
def maxBuy(gold, price)
base = Math.log($settings[:marketIncrement] + 1)
log = Math.log(gold * $settings[:marketIncrement] / (price * (1 + $settings[:marketTax])) + 1)
return log / base
end
def sendPM(bot, channelId, message)
begin
channel = bot.channel(channelId)
channel.send_message(message)
rescue Exception => error
puts "Bot received an error 403 from Discord"
puts error
end
end
def giveResources(mongo)
mongo[:farms].find().each do |farm|
set = {}
user = mongo[:users].find(:discordId => farm[:discordId]).first
$settings[:resourceTypes].each do |resourceType|
set[resourceType.to_sym] = [user[resourceType.to_sym] + farm[resourceType.to_sym], 0.0].max
end
mongo[:users].update_one({:discordId => farm[:discordId]}, {"$set" => set})
end
end
def feedArmies(bot, mongo)
mongo[:users].find().each do |user|
set = {}
cost = {}
# zero out
$settings[:resourceTypes].each do |resourceType|
cost[resourceType.to_sym] = 0.0
end
# get cost
$settings[:soldierTypes].each do |soldierType|
$settings[:soldiers][soldierType.to_sym][:consumes].each do |consume|
cost[consume[:type].to_sym] += consume[:num] * user[soldierType.pluralize.to_sym].to_f
end
end
# have enough?
enough = true
$settings[:resourceTypes].each do |resourceType|
if cost[resourceType.to_sym] > user[resourceType.to_sym]
enough = false
end
end
# remove from user
$settings[:resourceTypes].each do |resourceType|
set[resourceType.to_sym] = [user[resourceType.to_sym] - cost[resourceType.to_sym], 0.0].max
end
# destroy some soldiers
if !enough
# get lowest percentage
percentage = 1.0
$settings[:resourceTypes].each do |resourceType|
if cost[resourceType.to_sym] > 0.0
p = user[resourceType.to_sym] / cost[resourceType.to_sym]
if p < percentage
percentage = p
end
end
end
# clamp, 0.95 - 1.0, max 5% of soldiers
killPercentage = 1.0 - [[percentage, 1.0 - $settings[:starvingSoldierDeathRate]].max, 1.0].min
# get total number of soldiers
numSoldiers = 0
toKill = {} # for later
$settings[:soldierTypes].each do |soldierType|
numSoldiers += user[soldierType.pluralize.to_sym]
toKill[soldierType.pluralize.to_sym] = 0 # set to 0 for later
end
# how many should we fire
numToKill = (numSoldiers.to_f * killPercentage).ceil.to_i
fails = 20 # just in case
while numSoldiers > 0 && numToKill > 0 && fails > 0 do
$settings[:soldierTypes].shuffle.each do |soldierType|
numUserHas = user[soldierType.pluralize.to_sym] - toKill[soldierType.pluralize.to_sym]
if numUserHas > 0
toKill[soldierType.pluralize.to_sym] += 1
numSoldiers -= 1
numToKill -= 1
else
fails -= 1
end
end
end
# save to set
$settings[:soldierTypes].each do |soldierType|
set[soldierType.pluralize.to_sym] = [user[soldierType.pluralize.to_sym] - toKill[soldierType.pluralize.to_sym], 0].max
end
# send message
str = "Some of your soldiers died from starvation. "
$settings[:soldierTypes].each do |soldierType|
if toKill[soldierType.pluralize.to_sym] > 0
str += toKill[soldierType.pluralize.to_sym].to_s+" "+soldierType.pluralize+" "
end
end
sendPM(bot, user[:pmChannelId], str)
end
mongo[:users].update_one({:_id => user[:_id]}, {"$set" => set})
updateNetworthFor(mongo, user[:discordId])
end
end
# make sure market exists and create if it doesn't
# runs when bot starts
def validateMarket(mongo)
market = mongo[:market].find()
isMarketValid = true
$settings[:resourceTypes].each do |resourceType|
exists = false
market.each do |m|
if m[:type] == resourceType
exists = true
end
end
if !exists
isMarketValid = false
end
end
# create market if not valid
if !isMarketValid
mongo[:market].drop
$settings[:resourceTypes].each do |resourceType|
mongo[:market].insert_one({
:type => resourceType,
:value => 10.0
})
end
end
end
# get orders that need to run
# things like farms that need to be built
# called from app.rb every minute
def ordersInterval(bot, mongo)
mongo[:orders].find({:finishedAt => {'$lte' => Time.now}}).each do |order|
# call function if it exists
if respond_to?("order_"+order[:type].to_s, :include_private)
send("order_"+order[:type].to_s, bot, order, mongo)
end
# delete order
mongo[:orders].delete_one(:_id => order[:_id])
end
end
def getNewPopulation(previousPopulation, happiness, reputation, numShrines)
# happiness
population = previousPopulation + slopeInterpolate(happiness, 0.0, 1.0, $settings[:populationMaxGrowth].to_f * -1.0, $settings[:populationMaxGrowth].to_f, 0.5).round.to_i
# make it go down faster if low happiness
multiplier = (1.0 - [happiness * 2.0, 1.0].min) * 0.05
population -= (population * multiplier)
# increase from reputation
population = population + slopeInterpolate([[reputation * 2.0 - 1.0, 0.0].max, 1.0].min, 0.0, 1.0, 0.0, $settings[:populationMaxGrowthFromReputation].to_f, 0.5).round.to_i
# increase from shrines
growth = population - previousPopulation
if growth > 0
population += growth * $settings[:popGrowthMultiplierPerShrine] * numShrines
end
[population, 0].max.round.to_i
end
def collectTaxes(mongo)
mongo[:users].find().each do |user|
# find how many resources are collected by farms
# zero out
res = {}
$settings[:resourceTypes].each do |resourceType|
res[resourceType.to_sym] = 0.0
end
# get numbers from farms
sum = 0.0
hasFarm = false
mongo[:farms].find({:discordId => user[:discordId]}).each do |farm|
hasFarm = true
$settings[:resourceTypes].each do |resourceType|
res[resourceType.to_sym] += farm[resourceType.to_sym]
sum += farm[resourceType.to_sym]
end
end
# get percentages
percentages = {}
$settings[:resourceTypes].each do |resourceType|
if sum == 0.0
percentages[resourceType.to_sym] = 0.0
else
percentages[resourceType.to_sym] = res[resourceType.to_sym] / sum
end
end
# what if no farms
if !hasFarm
$settings[:resourceTypes].each do |resourceType|
percentages[resourceType.to_sym] = 1.0 / $settings[:resourceTypes].length
end
end
set = {}
taxCollected = {}
$settings[:resourceTypes].each do |resourceType|
collected = user[:population].to_f * $settings[:spendingPerPerson] * user[:tax] * percentages[resourceType.to_sym]
taxCollected[resourceType.to_sym] = collected
set[resourceType.to_sym] = [user[resourceType.to_sym] + collected, 0.0].max
end
set[:taxCollected] = taxCollected
mongo[:users].update_one({:_id => user[:_id]}, {"$set" => set})
end
end
def getNewHappiness(happiness, tax, lastLostBattle, reputation)
# find target happiness from tax
targetHappiness = 1.0 - slopeInterpolate(tax, 0.0, 1.0, 0.0, 1.0, 0.9) # 0.33 is about even tax with 0.9 slope
# find target happiness form losing a battle
if lastLostBattle != nil
value = [[Time.now - lastLostBattle, 0].max, $settings[:losingBattleAffectsHappinessFor]].min # 0 - max, 0 means recent
target = slopeInterpolate(value.to_f, 0.0, $settings[:losingBattleAffectsHappinessFor].to_f, 0.0, targetHappiness, 0.5)
targetHappiness = (target + targetHappiness) / 2.0
end
# reputation
if reputation < 0.5
targetHappiness = slopeInterpolate(reputation, 0.0, 0.5, 0.0, targetHappiness, 0.5)
end
# slowly adjust towards targetHappiness
lerp(happiness, targetHappiness, 0.1)
end
# called at 10 min interval
def getNewReputation(reputation, lastWonBattle)
if reputation < 0.5
reputation = reputation + 0.02
elsif reputation > 0.5
reputation = reputation - 0.02
end
if lastWonBattle != nil
if Time.now - lastWonBattle < $settings[:winningBattleAffectsReputationFor]
reputation = reputation + 0.05
end
end
[[reputation, 0.0].max, 1.0].min
end
# called with someone attacks
def getReputationFromAttack(attackerNetworth, defenderNetworth, attackerReputation)
percentSmaller = [defenderNetworth / (attackerNetworth * $settings[:maxReputationPercentage]), 1.0].min
rep = slopeInterpolate(percentSmaller, 0.0, 1.0, 0.0, attackerReputation, 0.5)
[rep, 0.0].max
end
# linear interpolate by amount
# amount is 0.0 - 1.0
def lerp(from, to, amount)
from + (to - from) * amount
end
# maps a linear range to a curved range
# used for tax
# value - value to be interpolated
# s1 - source range min
# s2 - source range max
# t1 - target range min
# t2 - target range max
# slope - Weight of the curve (0.5 = linear, 0.1 = weighted near target start, 0.9 = weighted near target end)
def slopeInterpolate(value, s1, s2, t1, t2, slope)
# Reverse the value, to make it correspond to the target range (this is a side-effect of the bezier calculation)
value = s2 - value
# Find out how far the value is on the curve
percent = value / (s2 - s1)
c2y = t1 + slope.abs * (t2 - t1)
b3 = (1 - percent) * (1 - percent)
return t1 * (percent * percent) + c2y * (2 * percent * (1 - percent)) + t2 * b3
end
def is_number? string
true if Float(string) rescue false
end
def adjust_market mongo
market = mongo[:market].find()
sum = 0.0
target = 0.0
market.each do |res|
sum += res[:value]
target += 10.0
end
if sum < target - 2.0
market.each do |res|
updateMarketPrice(mongo, res, res[:type], (target-sum) * 20.0, true)
end
elsif sum > target + 2.0
market.each do |res|
updateMarketPrice(mongo, res, res[:type], (sum-target) * 20.0, false)
end
end
end
def getGoldInterest(gold)
[gold * (1.0 + $settings[:goldInterestRate]), 0.0].max
end