-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsimc.py
555 lines (521 loc) · 26.3 KB
/
simc.py
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
import os
import sys
import subprocess
import discord
import aiohttp
import asyncio
import json
import logging
import time
import threading
from datetime import datetime
from urllib.parse import quote
from flask import Flask, app, render_template, request, redirect
os.chdir(os.path.dirname(os.path.abspath(__file__)))
with open('user_data.json') as data_file:
user_opt = json.load(data_file)
simc_opts = user_opt['simcraft_opt'][0]
server_opts = user_opt['server_opt'][0]
logger = logging.getLogger('discord')
level = logging.getLevelName(server_opts['loglevel'])
logger.setLevel(level)
handler = logging.FileHandler(filename=server_opts['logfile'], encoding='utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
def webservice():
app.run(host=server_opts['listen_ip'], port=server_opts['listen_port'], threaded=True)
app = Flask(__name__)
thread = threading.Thread(target=webservice, args=())
thread.daemon = True
thread.start()
bot = discord.Client()
server = bot.get_server(server_opts['serverid'])
threads = os.cpu_count()
if 'threads' in simc_opts:
threads = simc_opts['threads']
process_priority = 'below_normal'
if 'process_priority' in simc_opts:
process_priority = simc_opts['process_priority']
htmldir = simc_opts['htmldir']
website = simc_opts['website']
os.makedirs(os.path.dirname(os.path.join(htmldir + 'debug', 'test.file')), exist_ok=True)
waiting = False
wait_data = False
busy = False
addon_data = None
user = ''
timeout = simc_opts['timeout']
api_key = simc_opts['api_key']
sims = {}
def check_simc():
null = open(os.devnull, 'w')
stdout = open(os.path.join(htmldir, 'debug', 'simc.ver'), "w")
try:
subprocess.Popen(simc_opts['executable'], universal_newlines=True, stderr=null, stdout=stdout)
except FileNotFoundError as e:
logger.critical('Simulationcraft program could not be run. (ERR: %s)' % e)
time.sleep(1)
with open(os.path.join(htmldir, 'debug', 'simc.stout'), errors='replace') as v:
version = v.readline().rstrip('\n')
return version
async def set_status():
if len(sims) == server_opts['queue_limit']:
try:
await bot.change_presence(status=discord.Status.dnd,
game=discord.Game(name='Sim: %s/%s' % (len(sims), server_opts['queue_limit'])))
except:
logger.warning('Failed to set presence for full queue.')
pass
else:
try:
await bot.change_presence(status=discord.Status.online,
game=discord.Game(name='Sim: %s/%s' % (len(sims), server_opts['queue_limit'])))
except:
logger.warning('Failed to set presence for queue.')
pass
async def check_spec(region, realm, char):
global api_key
url = "https://%s.api.battle.net/wow/character/%s/%s?fields=talents&locale=en_GB&apikey=%s" % (region, realm,
quote(char), api_key)
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
try:
data = await response.json()
if 'reason' in data:
return data['reason']
else:
spec = 0
for i in range(len(data['talents'])):
for line in data['talents']:
if 'selected' in line:
role = data['talents'][spec]['spec']['role']
return role
else:
spec += +1
except:
logger.critical('Error in aiohttp request: %s', url)
return 'Failed to look up class spec from armory.'
@app.route('/')
def default():
return redirect('https://github.com/stokbaek/simc-discord', code=302)
@app.route('/<addon_url>')
def my_form(addon_url):
if wait_data:
return render_template("data_receieve.html")
else:
return render_template("403.html")
@app.route('/submit', methods=['POST'])
def submit_textarea():
global addon_data
global wait_data
text = request.form['text']
addon_data = text
with open(sims[user]['addon'], "w") as fo:
fo.write(text)
wait_data = False
return 'Data received\nThis page can now be closed'
async def data_sim():
global api_key
global waiting
global wait_data
failed = False
m_temp = ''
while not waiting:
waiting = True
timer = 0
if sims[user]['data'] == 'addon':
sims[user]['addon'] = '%ssims/%s/%s-%s.simc' % (
htmldir, sims[user]['char'], sims[user]['char'], sims[user]['timestr'])
addon_url = '%s-%s' % (sims[user]['char'], sims[user]['timestr'])
await set_status()
msg = 'You can add your addon data here: %s:%s/%s' % (website, server_opts['listen_port'], addon_url)
await bot.send_message(sims[user]['message'].author, msg)
wait_data = True
while wait_data:
timer += 1
await asyncio.sleep(1)
if timer > simc_opts['data_timeout']:
wait_data = False
if not os.path.isfile(sims[user]['addon']):
await bot.send_message(sims[user]['message'].author, 'No data given. Resetting session.')
del sims[user]
waiting = False
await set_status()
logger.info('No data was given to bot. Aborting sim.')
failed = True
else:
healing_roles = ['restoration', 'holy', 'discipline', 'mistweaver']
for crole in healing_roles:
crole = 'spec=' + crole
if crole in addon_data:
await bot.send_message(sims[user]['message'].channel,
'SimulationCraft does not support healing.')
del sims[user]
waiting = False
await set_status()
logger.info('Character is a healer. Aborting sim.')
failed = True
if sims[user]['data'] != 'addon':
api = await check_spec(sims[user]['region'], sims[user]['realm'].replace('_', '-'), sims[user]['char'])
if api == 'HEALING':
await bot.send_message(sims[user]['message'].channel, 'SimulationCraft does not support healing.')
waiting = False
del sims[user]
logger.info('Character is a healer. Aborting sim.')
failed = True
elif not api == 'DPS' and not api == 'TANK':
msg = 'Something went wrong: %s' % api
await bot.send_message(sims[user]['message'].channel, msg)
waiting = False
del sims[user]
logger.warning('Simulation could not start: %s' % api)
failed = True
for item in simc_opts['fightstyles']:
if item.lower() == sims[user]['fightstyle'].lower():
m_temp = m_temp + '**__' + item + '__**, '
else:
m_temp = m_temp + item + ', '
for key in sims[user]:
if key == 'movements':
sims[user]['movements'] = m_temp
if busy:
position = len(sims) - 1
if position > 0:
await bot.send_message(sims[user]['message'].channel,
'Simulation added to queue. Queue position: %s' % position)
await set_status()
logger.info('A new simulation has been added to queue')
if not failed:
bot.loop.create_task(sim())
else:
return
async def sim():
global sims
global busy
global waiting
running = 0
waiting = False
while not busy:
busy = True
ptr = 'No'
sim_user = list(sorted(sims))[0]
filename = '%s-%s' % (sims[sim_user]['char'], sims[sim_user]['timestr'])
link = 'Simulation: %s/sims/%s/%s.html' % (website, sims[sim_user]['char'], filename)
message = sims[sim_user]['message']
loop = True
scale_stats = 'agility,strength,intellect,crit_rating,haste_rating,mastery_rating,versatility_rating'
options = 'calculate_scale_factors=%s scale_only=%s html=%ssims/%s/%s.html threads=%s iterations=%s ' \
'fight_style=%s enemy=%s apikey=%s process_priority=%s max_time=%s' % (sims[sim_user]['scale'],
scale_stats, htmldir,
sims[sim_user]['char'],
filename,
threads, sims[sim_user][
'iterations'],
sims[sim_user]['fightstyle'],
sims[sim_user]['enemy'],
api_key,
process_priority,
sims[sim_user]['length'])
if sims[sim_user]['data'] == 'addon':
options += ' input=%s' % sims[sim_user]['addon']
else:
options += ' armory=%s,%s,%s' % (
sims[sim_user]['region'], sims[sim_user]['realm'].replace('_', '-'),
sims[sim_user]['char'])
if sims[sim_user]['l_fixed'] == 1:
options += ' vary_combat_length=0.0 fixed_time=1'
if sims[sim_user]['ptr'] == 1:
options += ' ptr=1'
ptr = 'Yes'
await set_status()
command = "%s %s" % (simc_opts['executable'], options)
stout = open(os.path.join(htmldir, 'debug', 'simc.stout'), "w")
sterr = open(os.path.join(htmldir, 'debug', 'simc.sterr'), "w")
try:
process = subprocess.Popen(command.split(" "), universal_newlines=True, stdout=stout, stderr=sterr)
logger.info('----------------------------------')
logger.info('%s started a simulation:' % sims[sim_user]['message'].author)
logger.info('Character: ' + sims[sim_user]['char'].capitalize())
logger.info('Realm: ' + sims[sim_user]['realm'].title().replace('_', ' '))
logger.info('Fightstyle: ' + sims[sim_user]['movements'][
sims[sim_user]['movements'].find("**__") + 4:sims[sim_user]['movements'].find(
"__**")])
logger.info('Fight Length: ' + str(sims[sim_user]['length']))
logger.info('AOE: ' + sims[sim_user]['aoe'])
logger.info('Iterations: ' + sims[sim_user]['iterations'])
logger.info('Scaling: ' + sims[sim_user]['scaling'].capitalize())
logger.info('Data: ' + sims[sim_user]['data'].capitalize())
logger.info('PTR: ' + ptr)
logger.info('----------------------------------')
except FileNotFoundError as e:
await bot.send_message(sims[sim_user]['message'].channel, 'ERR: Simulation could not start.')
logger.critical('Bot could not start simulationcraft program. (ERR: %s)' % e)
del sims[sim_user]
await set_status()
busy = False
return
msg = 'Realm: %s\nCharacter: %s\nFightstyle: %s\nFight Length: %s\nAoE: %s\n' \
'Iterations: %s\nScaling: %s\nData: %s\nPTR: %s' % (
sims[sim_user]['realm'].title().replace('_', ' '), sims[sim_user]['char'].capitalize(),
sims[sim_user]['movements'],
sims[sim_user]['length'], sims[sim_user]['aoe'].capitalize(), sims[sim_user]['iterations'],
sims[sim_user]['scaling'].capitalize(), sims[sim_user]['data'].capitalize(), ptr)
await bot.send_message(sims[sim_user]['message'].channel, '\nSimulationCraft:\n' + msg)
load = await bot.send_message(sims[sim_user]['message'].channel, 'Simulating: Starting...')
await asyncio.sleep(1)
while loop:
running += 2
if running > timeout * 60:
await bot.edit_message(load, 'Simulation timeout')
process.terminate()
del sims[sim_user]
await set_status()
logger.warning('Simulation timeout')
loop = False
busy = False
while wait_data:
logger.info('Wont start next sim, still waiting on data')
await asyncio.sleep(1)
if len(sims) == 0:
return
else:
bot.loop.create_task(sim())
await asyncio.sleep(1)
with open(os.path.join(htmldir, 'debug', 'simc.stout'), errors='replace') as p:
process_check = p.readlines()
with open(os.path.join(htmldir, 'debug', 'simc.sterr'), errors='replace') as e:
err_check = e.readlines()
if len(err_check) > 0:
if 'ERROR' in err_check[-1]:
await bot.edit_message(load, 'Simulation failed: ' + '\n'.join(err_check))
process.terminate()
del sims[sim_user]
await set_status()
logger.warning('Simulation failed: ' + '\n'.join(err_check))
loop = False
busy = False
while wait_data:
logger.info('Wont start next sim, still waiting on data')
await asyncio.sleep(1)
if len(sims) == 0:
return
else:
bot.loop.create_task(sim())
if len(process_check) > 1:
if 'report took' in process_check[-2]:
loop = False
await bot.edit_message(load, 'Simulation done.')
await bot.send_message(sims[sim_user]['message'].channel,
link + ' {0.author.mention}'.format(message))
process.terminate()
busy = False
del sims[sim_user]
await set_status()
logger.info('Simulation completed.')
while wait_data:
logger.info('Wont start next sim, still waiting on data')
await asyncio.sleep(1)
if len(sims) != 0:
bot.loop.create_task(sim())
else:
return
else:
if 'Generating' in process_check[-1]:
done = '█' * (20 - process_check[-1].count('.'))
missing = '░' * (process_check[-1].count('.'))
progressbar = done + missing
percentage = 100 - process_check[-1].count('.') * 5
status = process_check[-1].split()[1]
if 'sec' in process_check[-1].split()[-1] or 'min' in process_check[-1].split()[-1]:
if 'min' in process_check[-1].split()[-2]:
timer = ' (' + process_check[-1].split()[-2] + ' ' + process_check[-1].split()[-1] + \
' left)'
else:
timer = ' (' + process_check[-1].split()[-1] + ' left)'
else:
timer = ''
try:
load = await bot.edit_message(load, status + ' ' + progressbar + ' ' +
str(percentage) + '%' + timer)
except:
logger.warning('Failed updating progress')
pass
def check(addon_data):
if addon_data.channel.is_private:
return addon_data.content.endswith('DONE')
@bot.event
async def on_message(message):
global busy
global user
global sims
a_temp = ''
channel = bot.get_channel(server_opts['channelid'])
timestr = datetime.utcnow().strftime('%Y%m%d.%H%m%S%f')[:-3]
args = message.content.lower()
if message.author == bot.user:
return
if message.channel.is_private:
logger.info('%s sent follow data to bot: %s' % (message.author, message.content))
elif args.startswith('!simc'):
args = args.split(' -')
if args:
try:
if args[1].startswith(('h', 'help')):
with open('help.file', errors='replace') as h:
msg = h.read()
await bot.send_message(message.author, msg)
return
elif args[1].startswith(('v', 'version')):
await bot.send_message(message.channel, check_simc())
return
else:
if message.channel != channel:
await bot.send_message(message.channel, 'Please use the correct channel.')
return
if args[1].startswith(('q', 'queue')):
if busy:
await bot.send_message(message.channel,
'Queue: %s/%s' % (len(sims), server_opts['queue_limit']))
else:
await bot.send_message(message.channel, 'Queue is empty')
return
if busy:
if len(sims) > server_opts['queue_limit'] - 1:
await bot.send_message(sims[user]['message'].channel,
'**Queue is full, please try again later.**')
logger.info('Sim could not be started because queue is full.')
return
if waiting:
await bot.send_message(message.channel,
'**Waiting for simc addon data from %s.**' %
sims[user]['message'].author.display_name)
logger.info('Failed starting sim. Still waiting on data from previous sim')
return
user = timestr + '-' + str(message.author)
user_sim = {user: {'realm': simc_opts['default_realm'],
'region': simc_opts['region'],
'iterations': simc_opts['default_iterations'],
'scale': 0,
'scaling': 'no',
'data': 'armory',
'char': '',
'aoe': 'no',
'enemy': '',
'addon': '',
'fightstyle': simc_opts['fightstyles'][0],
'movements': '',
'length': simc_opts['length'],
'l_fixed': 0,
'ptr': 0,
'timestr': datetime.utcnow().strftime('%Y%m%d.%H%m%S%f')[:-3],
'message': ''
}
}
sims.update(user_sim)
for key in sims[user]:
if key == 'message':
sims[user]['message'] = message
for i in range(len(args)):
if args[i] != '!simc':
if args[i].startswith(('r ', 'realm ')):
temp = args[i].split()
for key in sims[user]:
if key == 'realm':
sims[user]['realm'] = "_".join(temp[1:])
elif args[i].startswith(('c ', 'char ', 'character ')):
temp = args[i].split()
for key in sims[user]:
if key == 'char':
sims[user]['char'] = temp[1]
elif args[i].startswith(('s ', 'scaling ')):
temp = args[i].split()
for key in sims[user]:
if key == 'scaling':
sims[user]['scaling'] = temp[1]
elif args[i].startswith(('d ', 'data ')):
temp = args[i].split()
for key in sims[user]:
if key == 'data':
sims[user]['data'] = temp[1]
elif args[i].startswith(('i ', 'iterations ')):
if simc_opts['allow_iteration_parameter']:
temp = args[i].split()
for key in sims[user]:
if key == 'iterations':
sims[user]['iterations'] = temp[1]
else:
await bot.send_message(message.channel, 'Custom iterations is disabled')
logger.info('%s tried using custom iterations while the option is disabled')
return
elif args[i].startswith(('f ', 'fight ', 'fightstyle ')):
fstyle = False
temp = args[i].split()
for opt in range(len(simc_opts['fightstyles'])):
if temp[1] == simc_opts['fightstyles'][opt].lower():
for key in sims[user]:
if key == 'fightstyle':
sims[user]['fightstyle'] = temp[1]
fstyle = True
if fstyle is not True:
await bot.send_message(message.channel, 'Unknown fightstyle.\nSupported Styles: ' +
', '.join(simc_opts['fightstyles']))
logger.info(
'%s tried starting sim with unknown fightstyle: %s' % (message.author, temp[1]))
return
elif args[i].startswith(('a ', 'aoe ')):
temp = args[i].split()
for key in sims[user]:
if key == 'aoe':
sims[user]['aoe'] = temp[1]
elif args[i].startswith(('l ', 'length ')):
temp = args[i].split()
for key in sims[user]:
if key == 'length':
sims[user]['length'] = temp[1]
if len(temp) > 2:
if temp[2] == 'fixed':
for key in sims[user]:
if key == 'l_fixed':
sims[user]['l_fixed'] = 1
elif args[i] == 'ptr':
for key in sims[user]:
if key == 'ptr':
sims[user]['ptr'] = 1
else:
await bot.send_message(message.channel,
'Unknown command. Use !simc -h/help for commands')
del sims[user]
logger.info('Unknown command given to bot.')
return
if sims[user]['char'] == '':
await bot.send_message(message.channel, 'Character name is needed')
del sims[user]
logger.info('No character name given. Aborting sim.')
return
if sims[user]['scaling'] == 'yes':
for key in sims[user]:
if key == 'scale':
sims[user]['scale'] = 1
if sims[user]['aoe'] == 'yes':
for targets in range(0, simc_opts['aoe_targets']):
targets += + 1
a_temp += 'enemy=target%s ' % targets
for key in sims[user]:
if key == 'enemy':
sims[user]['enemy'] = a_temp
os.makedirs(os.path.dirname(os.path.join(htmldir + 'sims', sims[user]['char'], 'test.file')),
exist_ok=True)
bot.loop.create_task(data_sim())
except IndexError as e:
await bot.send_message(message.channel, 'Unknown command. Use !simc -h/help for commands')
logger.info('No command given to bot.(ERR: %s)' % e)
return
@bot.event
async def on_ready():
logger.info('Logged in as')
logger.info(bot.user.name)
logger.info(bot.user.id)
logger.info(check_simc())
logger.info('--------------')
await set_status()
bot.run(server_opts['token'])