-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
464 lines (346 loc) · 13.2 KB
/
server.js
File metadata and controls
464 lines (346 loc) · 13.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
const steem = require("steem");
const fetch = require("node-fetch");
const md5 = require("md5");
const KEYS = require("./keys.json");
const whiteRules = require("./whiteRules.json");
class Battle {
constructor(callback, appName = "steemmonsters/0.6.2", matchType = "Ranked") {
this.callback = callback;
this.status = {};
this.submittedTeam = false;
//broadcast sm_find_match
steem.broadcast.customJson(KEYS.posting, [], [KEYS.username], "sm_find_match", JSON.stringify({
match_type: matchType,
app: appName
}), (err, result) => {
if (err) throw err;
console.log("Broadcasted sm_find_match");
this.findMatchId = result.id;
});
//start /battle/status check loop
this._checkInterval = setInterval(() => {
this._checkBattleStatus();
}, 2500);
}
end() {
this.ended = true;
clearInterval(this._checkInterval);
}
setTeam(team) {
this.team = team;
}
//surrender if isn't your splinter
surrender(){
let dataSurrender= {
battle_queue_id: this.findMatchId,
app: this.appName
}
steem.broadcast.customJson(KEYS.posting, [], [KEYS.username], "sm_surrender",JSON.stringify(dataSurrender))
}
broadcastTeam(summoner, monsters, skipReveal = false) {
const secret = Battle.generatePassword();
const teamHash = md5(summoner + "," + monsters.join() + "," + secret)
const team = {
summoner,
monsters,
secret
};
this.submittedTeam = true;
var data = {
trx_id: this.findMatchId,
team_hash: teamHash,
app: this.appName
};
if (skipReveal) {
data.summoner = summoner;
data.monsters = monsters;
data.secret = secret;
}
//submit team into blockchain
steem.broadcast.customJson(KEYS.posting, [], [KEYS.username], "sm_submit_team", JSON.stringify(data), async (err, result) => {
if (err) throw err;
console.log("Broadcasted sm_submit_team");
this.findMatchId = result.id;
if (!skipReveal) {
await new Promise(resolve => setTimeout(resolve, 3300));
console.log("Revealing team...");
steem.broadcast.customJson(KEYS.posting, [], [KEYS.username], "sm_team_reveal", JSON.stringify({
...data,
summoner: summoner,
monsters: monsters,
secret: secret
}), (err, result) => {
console.log("Revealed team!");
});
}
});
}
_revealTeam() {
}
async _checkBattleStatus() {
if (!this.findMatchId) return;
const rawResponse = await fetch("https://api.steemmonsters.io/battle/status?id=" + this.findMatchId);
const json = await rawResponse.json();
this.status.data = json;
this.mana=json.mana_cap;
this.ruleset= json.ruleset;
this.inactive= json.inactive;
if ((typeof json) === "string") {
console.log(json);
this.status.statusName = "battleTxProcessing";
this.callback(this.status);
return;
}
if (json.error) {
this.status.statusName = "error";
} else if (json.status === 0) {
this.status.statusName = "searchingForEnemy";
} else if (json.status === 1) {
this.status.statusName = "enemyFound";
} else if (json.status === 3) {
this.status.statusName = "noEnemyFound";
}
this.callback(this.status);
}
_checkBattleTrxStatus() {
}
static generatePassword(length = 10) {
var charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
retVal = "";
for (var i = 0, n = charset.length; i < length; ++i) {
retVal += charset.charAt(Math.floor(Math.random() * n));
}
return retVal;
}
}
// create an infinite loop of battles
async function repeat(){
setInterval(async()=>{
start();
}, 180000)
}
async function start() {
let submittedTeam = false;
const battle = new Battle(async status => {
console.log(status.statusName);
let battleArray;
let manaCap= await battle.mana;
let currentRuleset= await battle.ruleset;
let inactive= await battle.inactive;
let regEx= /\bWhite/g;
switch (currentRuleset) {
//++++++++++++++++++++++++++++++++++INDIVIDUALS and doubles equivalent++++++++++++++++++++++++++++++++++
//----------------------------------------Primaries-----------------------------------
case "Standard":
battleArray = whiteRules.Standard[manaCap].white;
break;
case "Back to Basics|Lost Legendaries":
case "Back to Basics":
battleArray = whiteRules.backToBasics[manaCap].white;
break;
case "Silenced Summoners|Lost Legendaries":
case "Silenced Summoners":
battleArray = whiteRules.silencedSummoners[manaCap].white;
break;
case "Aim True|Lost Legendaries":
case "Aim True":
battleArray = whiteRules.aimTrue[manaCap].white;
break;
case "Super Sneak|Lost Legendaries":
case "Super Sneak":
battleArray = whiteRules.superSneak[manaCap].white;
break;
case "Weak Magic|Lost Legendaries":
case "Weak Magic":
battleArray = whiteRules.weakMagic[manaCap].white;
break;
case "Unprotected|Lost Legendaries":
case "Unprotected":
battleArray = whiteRules.unprotected[manaCap].white;
break;
case "Target Practice|Lost Legendaries":
case "Target Practice":
battleArray = whiteRules.targetPractice[manaCap].white;
break;
case "Fog of War|Lost Legendaries":
case "Fog of War":
battleArray = whiteRules.fogOfWar[manaCap].white;
break;
case "Armored Up|Lost Legendaries":
case "Armored Up":
battleArray = whiteRules.armoredUp[manaCap].white;
break;
//----------------------------------------any-----------------------------------
case "Reverse Speed|Lost Legendaries":
case "Earthquake|Reverse Speed":
case "Healed Out|Reverse Speed":
case "Armored Up|Reverse Speed":
case "Fog of War|Reverse Speed":
case "Target Practice|Reverse Speed":
case "Unprotected|Reverse Speed":
case "Weak Magic|Reverse Speed":
case "Super Sneak|Reverse Speed":
case "Aim True|Reverse Speed":
case "Back to Basics|Reverse Speed":
case "Silenced Summoners|Reverse Speed":
case "Reverse Speed":
battleArray = whiteRules.reverseSpeed[manaCap].white;
break;
case "Earthquake|Lost Legendaries":
case "Reverse Speed|Earthquake":
case "Healed Out|Earthquake":
case "Armored Up|Earthquake":
case "Fog of War|Earthquake":
case "Target Practice|Earthquake":
case "Unprotected|Earthquake":
case "Weak Magic|Earthquake":
case "Super Sneak|Earthquake":
case "Aim True|Earthquake":
case "Back to Basics|Earthquake":
case "Silenced Summoners|Earthquake":
case "Earthquake":
battleArray = whiteRules.earthquake[manaCap].white;
break;
case "Earthquake|Healed Out":
case "Reverse Speed|Healed Out":
case "Armored Up|Healed Out":
case "Fog of War|Healed Out":
case "Target Practice|Healed Out":
case "Unprotected|Healed Out":
case "Weak Magic|Healed Out":
case "Super Sneak|Healed Out":
case "Aim True|Healed Out":
case "Back to Basics|Healed Out":
case "Silenced Summoners|Healed Out":
case "Healed Out|Lost Legendaries":
case "Healed Out":
battleArray = whiteRules.healedOut[manaCap].white;
break;
//----------------------------------------secondaries-----------------------------------
case "Lost Legendaries":
battleArray = whiteRules.noLegendaries[manaCap].white;
break;
case "Earthquake|Melee Mayhem":
case "Reverse Speed|Melee Mayhem":
case "Healed Out|Melee Mayhem":
case "Armored Up|Melee Mayhem":
case "Fog of War|Melee Mayhem":
case "Target Practice|Melee Mayhem":
case "Unprotected|Melee Mayhem":
case "Weak Magic|Melee Mayhem":
case "Super Sneak|Melee Mayhem":
case "Aim True|Melee Mayhem":
case "Back to Basics|Melee Mayhem":
case "Silenced Summoners|Melee Mayhem":
case "Melee Mayhem":
battleArray = whiteRules.meleeMayhem[manaCap].white;
break;
case "Healed Out|Taking Sides":
case "Reverse Speed|Taking Sides":
case "Earthquake|Taking Sides":
case "Armored Up|Taking Sides":
case "Fog of War|Taking Sides":
case "Target Practice|Taking Sides":
case "Unprotected|Taking Sides":
case "Weak Magic|Taking Sides":
case "Super Sneak|Taking Sides":
case "Aim True|Taking Sides":
case "Back to Basics|Taking Sides":
case "Silenced Summoners|Taking Sides":
case "Taking Sides":
battleArray = whiteRules.takinSides[manaCap].white;
break;
case "Healed Out|Rise of the Commons":
case "Reverse Speed|Rise of the Commons":
case "Earthquake|Rise of the Commons":
case "Armored Up|Rise of the Commons":
case "Fog of War|Rise of the Commons":
case "Target Practice|Rise of the Commons":
case "Unprotected|Rise of the Commons":
case "Weak Magic|Rise of the Commons":
case "Super Sneak|Rise of the Commons":
case "Aim True|Rise of the Commons":
case "Back to Basics|Rise of the Commons":
case "Silenced Summoners|Rise of the Commons":
case "Rise of the Commons":
battleArray = whiteRules.riseOfTheCommons[manaCap].white;
break;
case "Healed Out|Up Close & Personal":
case "Reverse Speed|Up Close & Personal":
case "Earthquake|Up Close & Personal":
case "Armored Up|Up Close & Personal":
case "Fog of War|Up Close & Personal":
case "Target Practice|Up Close & Personal":
case "Unprotected|Up Close & Personal":
case "Weak Magic|Up Close & Personal":
case "Super Sneak|Up Close & Personal":
case "Aim True|Up Close & Personal":
case "Back to Basics|Up Close & Personal":
case "Silenced Summoners|Up Close & Personal":
case "Up Close & Personal":
battleArray = whiteRules.upClose[manaCap].white;
break;
case "Healed Out|Broken Arrows":
case "Reverse Speed|Broken Arrows":
case "Earthquake|Broken Arrows":
case "Armored Up|Broken Arrows":
case "Fog of War|Broken Arrows":
case "Target Practice|Broken Arrows":
case "Unprotected|Broken Arrows":
case "Weak Magic|Broken Arrows":
case "Super Sneak|Broken Arrows":
case "Aim True|Broken Arrows":
case "Back to Basics|Broken Arrows":
case "Silenced Summoners|Broken Arrows":
case "Broken Arrows":
battleArray = whiteRules.brokenArrows[manaCap].white;
break;
case "Healed Out|Little League":
case "Reverse Speed|Little League":
case "Earthquake|Little League":
case "Armored Up|Little League":
case "Fog of War|Little League":
case "Target Practice|Little League":
case "Unprotected|Little League":
case "Weak Magic|Little League":
case "Super Sneak|Little League":
case "Aim True|Little League":
case "Back to Basics|Little League":
case "Silenced Summoners|Little League":
case "Little League":
battleArray = whiteRules.littleLeague[manaCap].white;
break;
case "Healed Out|Keep Your Distance":
case "Reverse Speed|Keep Your Distance":
case "Earthquake|Keep Your Distance":
case "Armored Up|Keep Your Distance":
case "Fog of War|Keep Your Distance":
case "Target Practice|Keep Your Distance":
case "Unprotected|Keep Your Distance":
case "Weak Magic|Keep Your Distance":
case "Super Sneak|Keep Your Distance":
case "Aim True|Keep Your Distance":
case "Back to Basics|Keep Your Distance":
case "Silenced Summoners|Keep Your Distance":
case "Keep Your Distance":
battleArray = whiteRules.keepYourDistance[manaCap].white;
break;
}
//Check if your splinter is available and chose the right strategy
try{
if (inactive.match(regEx)) {
battle.surrender();
battle.end();
console.log("Sorry boss, I'm surrendered :(")
} else if (!submittedTeam && (status.statusName === "enemyFound") ){
battle.broadcastTeam("C1-38-1PQQEY75TC", battleArray);
submittedTeam = true;
battle.end();
}
} catch(error){
console.log(error)
}
});
}
//Start the loop
repeat();