-
Notifications
You must be signed in to change notification settings - Fork 0
/
attacks.rb
453 lines (354 loc) · 14.3 KB
/
attacks.rb
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
require './commonFunctions.rb'
require 'active_support/core_ext/string'
# called every minute from app.rb
def attackInterval(bot, mongo)
mongo[:armies].find({:arriveAt => {'$lte' => Time.now}}).each do |army|
if army[:isAttacking]
doAttack(bot, mongo, army)
else
returnToRealm(bot, mongo, army)
end
mongo[:armies].delete_one(:_id => army[:_id])
end
end
def doAttack(bot, mongo, army)
user = mongo[:users].find(:_id => army[:userId]).first
attackingArmy = user.merge army
defendingArmy = mongo[:users].find(:_id => army[:otherUserId]).first
if !defendingArmy
sendArmyToRealm(mongo, attackingArmy, nil, armyTravelTime(attackingArmy))
return
end
# attack
getNumberOfSoldiers(attackingArmy)
getNumberOfSoldiers(defendingArmy)
getPower(attackingArmy, true)
getPower(defendingArmy, false)
getPercentage(attackingArmy)
getPercentage(defendingArmy)
getBonus(attackingArmy, defendingArmy)
getFinalPower(attackingArmy)
getFinalPower(defendingArmy)
getWinner(attackingArmy, defendingArmy)
getPowerToLose(attackingArmy, defendingArmy)
getLoses(attackingArmy)
getLoses(defendingArmy)
# save last lost battle
if attackingArmy[:isWinner]
mongo[:users].update_one({:_id => attackingArmy[:userId]}, {"$set" => {:lastWonBattle => Time.now}})
else
mongo[:users].update_one({:_id => attackingArmy[:userId]}, {"$set" => {:lastLostBattle => Time.now}})
end
# get winnings
winnings = getWinnings(attackingArmy, defendingArmy, mongo[:market].find())
set = {:gold => [defendingArmy[:gold] - winnings[:gold], 0.0].max}
$settings[:resourceTypes].each do |resourceType|
set[resourceType.to_sym] = [defendingArmy[resourceType.to_sym] - winnings[resourceType.to_sym], 0.0].max
end
# save defendingArmy
$settings[:soldierTypes].each do |soldierType|
set[soldierType.pluralize.to_sym] = [defendingArmy[soldierType.pluralize.to_sym].to_i - defendingArmy[:loses][soldierType.to_sym], 0].max
end
mongo[:users].update_one({_id: defendingArmy[:_id]}, {"$set": set})
updateNetworthFor(mongo, defendingArmy[:discordId])
sendAttackReport(bot, attackingArmy, defendingArmy, winnings)
# check if army is dead
if attackingArmy[:numLoses] == attackingArmy[:numSoldiers]
updateNetworthFor(mongo, attackingArmy[:discordId])
return
end
# take away loses
$settings[:soldierTypes].each do |soldierType|
attackingArmy[soldierType.pluralize.to_sym] = attackingArmy[soldierType.pluralize.to_sym].to_i - attackingArmy[:loses][soldierType.singularize.to_sym].to_i
end
# check if shrine being built needs to be destroyed
# if attacking army won and has a catapult destroy shrines being built
if attackingArmy[:catapults] > 0 && attackingArmy[:isWinner]
mongo[:orders].find({:discordId => defendingArmy[:discordId], :type => "buildShrine"}).delete_many
end
sendArmyToRealm(mongo, attackingArmy, winnings, armyTravelTime(attackingArmy))
updateNetworthFor(mongo, attackingArmy[:discordId])
end
def getWinnings(attackingArmy, defendingArmy, markets)
winnings = {:gold => 0.0} # for winner
canCarryInGold = (attackingArmy[:numSoldiers] - attackingArmy[:numLoses]) * $settings[:winningsSoldierCanCarry]
if attackingArmy[:isWinner] && attackingArmy[:numLoses] < attackingArmy[:numSoldiers]
winnings[:gold] = [defendingArmy[:gold] * $settings[:battleWinnings], canCarryInGold].min
canCarryInGold -= winnings[:gold]
end
$settings[:resourceTypes].shuffle.each do |resourceType|
if attackingArmy[:isWinner] && attackingArmy[:numLoses] < attackingArmy[:numSoldiers]
resourceWorth = resourceToGold(markets, resourceType, 1.0)
steal = defendingArmy[resourceType.to_sym].to_f * $settings[:battleWinnings]
steal = [canCarryInGold / resourceWorth, steal].min
canCarryInGold -= [steal * resourceWorth, 0.0].max
winnings[resourceType.to_sym] = steal
else
winnings[resourceType.to_sym] = 0.0
end
end
return winnings
end
# used when attack is over and in %cancelattack
def sendArmyToRealm(mongo, army, winnings, durationSeconds)
returningArmy = {
:discordId => army[:discordId],
:userId => army[:userId],
:createdAt => Time.now,
:arriveAt => Time.now + durationSeconds,
:isAttacking => false, # false if army is returning from battle
:winnings => winnings
}
$settings[:soldierTypes].each do |soldierType|
returningArmy[soldierType.pluralize.to_sym] = army[soldierType.pluralize.to_sym]
end
# create returning army
mongo[:armies].insert_one(returningArmy)
end
# army has arrived at realm. insert into realm
def returnToRealm(bot, mongo, army)
user = mongo[:users].find(:_id => army[:userId]).first
str = "**Your army returned from battle.**\n"
str += "__Soldiers:__ "
$settings[:soldierTypes].each do |soldierType|
if army[soldierType.pluralize.to_sym].to_i > 0
str += number_with_commas(army[soldierType.pluralize.to_sym].to_i)+ " "
if army[soldierType.pluralize.to_sym].to_i == 1
soldierType
else
soldierType.pluralize
end
str += " "
end
end
str += "\n"
if army[:winnings] # winnings can be nil
str += "__Stole:__ "
str += army[:winnings][:gold].round.to_s+" gold "
$settings[:resourceTypes].each do |resourceType|
if army[:winnings][resourceType.to_sym] > 0.0
str += number_with_commas(army[:winnings][resourceType.to_sym].round(1))+" "+resourceType+" "
end
end
str += "\n"
end
sendPM(bot, user[:pmChannelId], str)
# add army back to user
set = {}
$settings[:soldierTypes].each do |soldierType|
set[soldierType.pluralize.to_sym] = [user[soldierType.pluralize.to_sym] + army[soldierType.pluralize.to_sym].to_i, 0].max
end
if army[:winnings]
set[:gold] = [user[:gold] + army[:winnings][:gold].to_f, 0.0].max
$settings[:resourceTypes].each do |resourceType|
set[resourceType.to_sym] = [user[resourceType.to_sym] + army[:winnings][resourceType.to_sym].to_f, 0.0].max
end
end
mongo[:users].update_one({:_id => army[:userId]}, {'$set' => set})
updateNetworthFor(mongo, user[:discordId])
end
def getNumberOfSoldiers(army)
army[:numSoldiers] = 0
$settings[:soldierTypes].each do |soldierType|
army[:numSoldiers] += army[soldierType.pluralize.to_sym].to_i
end
end
def getPower(army, isAttacker)
army[:totalPower] = 0.0
army[:power] = {}
$settings[:soldierTypes].each do |soldierType|
army[:power][soldierType.to_sym] = 0.0
end
$settings[:soldierTypes].each do |soldierType|
if isAttacker
army[:power][soldierType.to_sym] = $settings[:soldiers][soldierType.to_sym][:attack] * army[soldierType.pluralize.to_sym].to_f
else
army[:power][soldierType.to_sym] = $settings[:soldiers][soldierType.to_sym][:defense] * army[soldierType.pluralize.to_sym].to_f
end
army[:totalPower] += army[:power][soldierType.to_sym]
end
end
def getPercentage(army)
army[:percentage] = {}
# count soldiers
numSoldiers = 0
$settings[:soldierTypes].each do |soldierType|
numSoldiers += army[soldierType.pluralize.to_sym].to_i
end
$settings[:soldierTypes].each do |soldierType|
#prevent divide by 0
if numSoldiers == 0
army[:percentage][soldierType.to_sym] = 0.0
else
army[:percentage][soldierType.to_sym] = army[soldierType.pluralize.to_sym].to_f / numSoldiers.to_f
end
end
end
def getBonus(attackingArmy, defendingArmy)
attackingArmy[:totalBonus] = 0.0
defendingArmy[:totalBonus] = 0.0
attackingArmy[:bonus] = {}
defendingArmy[:bonus] = {}
$settings[:soldierTypes].each do |soldierType|
attackingArmy[:bonus][soldierType.to_sym] = 0.0
defendingArmy[:bonus][soldierType.to_sym] = 0.0
end
$settings[:soldierTypes].each do |soldierType|
$settings[:soldiers][soldierType.to_sym][:bonusAgainst].each do |bonusAgainst|
if defendingArmy[bonusAgainst.pluralize.to_sym] == 0
attackBonus = 0.0
else
attackBonus = attackingArmy[:power][soldierType.to_sym].to_f * defendingArmy[:percentage][bonusAgainst.to_sym].to_f * $settings[:battleBonusMultiplier]
end
if attackingArmy[bonusAgainst.pluralize.to_sym] == 0
defendBonus = 0.0
else
defendBonus = defendingArmy[:power][soldierType.to_sym].to_f * attackingArmy[:percentage][bonusAgainst.to_sym].to_f * $settings[:battleBonusMultiplier]
end
attackingArmy[:bonus][soldierType.to_sym] += attackBonus
defendingArmy[:bonus][soldierType.to_sym] += defendBonus
attackingArmy[:totalBonus] += attackBonus
defendingArmy[:totalBonus] += defendBonus
end
end
end
def getFinalPower(army)
army[:finalPowerPerSoldier] = {}
# final power
army[:finalPower] = army[:totalPower] + army[:totalBonus]
# final power of each soldier type
$settings[:soldierTypes].each do |soldierType|
if army[soldierType.pluralize.to_sym].to_i == 0
army[:finalPowerPerSoldier][soldierType.to_sym] = 0.0
else
army[:finalPowerPerSoldier][soldierType.to_sym] = (army[:power][soldierType.to_sym] + army[:bonus][soldierType.to_sym]) / army[soldierType.pluralize.to_sym].to_f
end
end
end
def getWinner(attackingArmy, defendingArmy)
dif = attackingArmy[:finalPower] - defendingArmy[:finalPower]
attackingArmy[:isWinner] = dif > 0
defendingArmy[:isWinner] = dif <= 0
end
def getPowerToLose(attackingArmy, defendingArmy)
if attackingArmy[:isWinner]
attackingArmy[:powerToLose] = defendingArmy[:totalPower] * 0.05
defendingArmy[:powerToLose] = defendingArmy[:totalPower] * 0.2
else
attackingArmy[:powerToLose] = [attackingArmy[:totalPower] * 0.5, defendingArmy[:totalPower] * 0.20].max
defendingArmy[:powerToLose] = [attackingArmy[:totalPower] * 0.01, defendingArmy[:totalPower] * 0.01].min
end
end
def getLoses(army)
army[:numLoses] = 0
army[:loses] = {}
$settings[:soldierTypes].each do |soldierType|
army[:loses][soldierType.to_sym] = 0
end
if army[:numSoldiers] == 0
return
end
# find which soldier is worth the least
smallestSoldierPower = 999999.0
$settings[:soldierTypes].each do |soldierType|
if army[:finalPowerPerSoldier][soldierType.to_sym] > 0.0 && army[:finalPowerPerSoldier][soldierType.to_sym] < smallestSoldierPower
smallestSoldierPower = army[:finalPowerPerSoldier][soldierType.to_sym]
end
end
# take away until powerToLose is less than smallestSoldierPower
fails = 0
maxFails = $settings[:soldierTypes].length * 2
powerLeft = army[:powerToLose]
numSoldiers = army[:numSoldiers]
while powerLeft > 0 && numSoldiers > 0 && fails < maxFails do
$settings[:soldierTypes].each do |soldierType|
# if there is a unit of this type in the army
if army[soldierType.pluralize.to_sym].to_i - army[:loses][soldierType.to_sym].to_i > 0
#if there is enough power left to take this unit away
if army[:finalPowerPerSoldier][soldierType.to_sym] <= powerLeft
army[:loses][soldierType.to_sym] += 1
numSoldiers -= 1
powerLeft -= army[:finalPowerPerSoldier][soldierType.to_sym]
army[:numLoses] += 1
else
fails += 1
end
end
end
end
end
def sendAttackReport(bot, attackingArmy, defendingArmy, winnings)
str = "-] **ATTACK REPORT** [-\n\n"
str += "__ATTACKING ARMY__\n"
str += createReport(attackingArmy, true, winnings)
str += "\n"
str += "__DEFENDING REALM__\n"
str += createReport(defendingArmy, false, nil)
sendPM(bot, attackingArmy[:pmChannelId], str)
sendPM(bot, defendingArmy[:pmChannelId], str)
end
def createReport(army, isAttacker, winnings)
# sent
str = "**"+army[:display_name]+"**"
if isAttacker
str += " sent "
else
str += " defended with "
end
$settings[:soldierTypes].each do |soldierType|
if army[soldierType.pluralize.to_sym].to_i > 0
str += number_with_commas(army[soldierType.pluralize.to_sym].to_i)+ " "+soldierType.pluralize+" "
end
end
str += "\n"
str += "__Power:__ "
$settings[:soldierTypes].each do |soldierType|
if army[:power][soldierType.to_sym].to_i > 0
str += soldierType.pluralize+": "+number_with_commas(army[:power][soldierType.to_sym].round(1).to_f)+" "
end
end
str += "\n"
str += "__Bonus:__ "
if army[:totalBonus] > 0
$settings[:soldierTypes].each do |soldierType|
if army[:bonus][soldierType.to_sym].to_i > 0
str += soldierType.pluralize+": "+number_with_commas(army[:bonus][soldierType.to_sym].round(1).to_f).to_s+" "
end
end
else
str += "none"
end
str += "\n"
str += number_with_commas(army[:totalPower].round(1))+" power + "+number_with_commas(army[:totalBonus].round(1))+" bonus = "+number_with_commas(army[:finalPower].round(1))+" final power\n"
if army[:isWinner]
str += "**Won Battle**\n"
else
str += "**Lost Battle**\n"
end
if army[:numLoses] > 0
if army[:numLoses] == army[:numSoldiers]
str += "Lost all soldiers."
else
str += "Lost "
$settings[:soldierTypes].each do |soldierType|
if army[:loses][soldierType.to_sym].to_i > 0
str += number_with_commas(army[:loses][soldierType.to_sym].to_i)+ " "+soldierType.pluralize+" "
end
end
end
else
str += "Lost no soldiers."
end
str += "\n"
if isAttacker && army[:isWinner]
str += "__Stole:__ "
str += number_with_commas(winnings[:gold].round(1))+" gold "
$settings[:resourceTypes].each do |resourceType|
if winnings[resourceType.to_sym] > 0
str += number_with_commas(winnings[resourceType.to_sym].to_f.round(1))+" "+resourceType+" "
end
end
str += "\n"
end
return str
end