-
Notifications
You must be signed in to change notification settings - Fork 23
/
level_bot_manager.py
executable file
·328 lines (309 loc) · 12.1 KB
/
level_bot_manager.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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# vim: set et sw=4 ts=4 sts=4 ff=unix fenc=utf8:
# Author: Binux<[email protected]>
# http://binux.me
# Created on 2013-08-31 19:36:06
import ma
import time
import random
import datetime
import gevent
import gevent.pywsgi
import regist
from webob import Request
from db import accountdb, battledb
from levelup_bot import LevelBot
from lxml.etree import XMLSyntaxError
import gevent.monkey; gevent.monkey.patch_all()
TARGET_LV = 25
class WebLevelBot(LevelBot):
master_cards = {} # shared master cards
def login(self, *args, **kwargs):
super(WebLevelBot, self).login(*args, **kwargs)
if not self.__class__.master_cards:
self.__class__.master_cards = self.ma.master_cards
else:
self.ma.master_cards = self.__class__.master_cards
def _print(self, *args):
message = ' '.join([unicode(x) for x in args])
print self.login_id, message
with open('/tmp/libma.%s.log' % self.login_id, 'a') as fp:
fp.write('%s ' % datetime.datetime.now())
fp.write(message.encode('utf8') if isinstance(message, unicode) else message)
fp.write('\n')
pass
stop_set = set()
running_set = set()
def _run_task(bot, account, battle_set):
bot.login(account['id'], account['pwd'])
account['name'] = bot.ma.name
account['uid'] = int(bot.ma.user_id)
account['lv'] = bot.ma.level
account['friends'] = bot.ma.friends
account['friend_max'] = bot.ma.friend_max
accountdb.update(**account)
bot.task_check()
bot.report()
# add friend
cur_friends = [int(x) for x in bot.ma.friendlist().xpath('//user/id/text()')]
if bot.ma.friend_max > bot.ma.friends:
friends = list(accountdb.find_friends())
random.shuffle(friends)
else:
friends = []
for cur in friends:
if not int(cur['uid']):
continue
if int(cur['uid']) == int(bot.ma.user_id):
continue
if int(cur['uid']) in cur_friends:
continue
try:
bot._print('add friend: %(name)s(%(uid)s)' % cur)
bot.ma.add_friend(cur['uid'])
except ma.HeaderError:
bot._print('add friend error: %(name)s(%(uid)s)' % cur)
break
except XMLSyntaxError:
bot._print('add friend error: %(name)s(%(uid)s)' % cur)
break
# battle
if not bot.build_roundtable('battle'):
bot._print('build battle roundtalbe failed!')
return True
battle_list = list(battledb.scan(where="(cast(%d/atk/1.1 as int)+1)*%d>hp" % (
bot.ma.roundtable[0].hp, bot.ma.roundtable[0].power)))
battle_list = [x for x in battle_list if int(x['uid']) not in battle_set]
random.shuffle(battle_list)
bot._print('battle: %s palyers found' % len(battle_list))
for cur in battle_list:
if bot.ma.level >= account['target_lv']:
break
if int(account['id']) in stop_set:
stop_set.remove(int(account['id']))
bot._print('stoped!')
break
# lvup!
if bot.ma.free_ap_bc_point:
account['lv'] = bot.ma.level
account['status'] = 'RUNNING'
accountdb.update(**account)
if '|ap' in account.get('invite', ''):
bot.free_point('ap')
else:
bot.free_point('bc')
if '|ap' in account.get('invite', '') and bot.ma.ap > 10:
bot.explore()
if not bot.build_roundtable('battle'):
break
# battle
try:
hp, atk = bot.battle(cur['uid'])
battle_set.add(cur['uid'])
except ma.HeaderError, e:
if e.code == 8000:
battle_set.add(cur['uid'])
bot._print(e.message)
time.sleep(2)
continue
raise
except XMLSyntaxError, e:
bot._print('xml error')
time.sleep(2)
continue
if hp != cur['hp'] or atk != cur['atk']:
battledb.update(cur['uid'], hp, atk)
# low bc
if bot.ma.bc < bot.ma.cost:
bot.explore()
cost = bot.ma.cost
while bot.ma.bc < cost and bot.story():
continue
bot.task_check()
bot.report()
account['lv'] = bot.ma.level
account['friends'] = bot.ma.friends
account['friend_max'] = bot.ma.friend_max
if bot.ma.level >= account['target_lv']:
account['status'] = 'DONE'
accountdb.update(**account)
return True
def run_task(account):
account = dict(account)
if int(account['id']) in running_set:
return
print 'running account:', account['id']
account['rounds'] += 1
account['status'] = 'RUNNING'
accountdb.update(**account)
running_set.add(int(account['id']))
try:
bot = WebLevelBot()
battle_set = set(map(int, account['battle'].split(','))) if account['battle'] else set()
_run_task(bot, account, battle_set)
account['nextime'] = time.time() + random.randint(50*60, 60*60)
except ma.HeaderError, e:
bot._print(account['id'], 'FAILED', e)
if e.code == 1000:
account['status'] = 'FAILED'
accountdb.update(**account)
return False
elif e.code == 8000:
import traceback
bot._print(traceback.format_exc())
account['nextime'] = time.time() + random.randint(10*60, 15*60)
except Exception, e:
import traceback
bot._print(traceback.format_exc())
account['nextime'] = time.time() + random.randint(10*60, 15*60)
except XMLSyntaxError, e:
account['nextime'] = time.time() + random.randint(6*60, 15*60)
finally:
if account['status'] not in ('FAILED', 'DONE'):
account['status'] = 'PENDING'
account['battle'] = ','.join(map(str, battle_set))
accountdb.update(**account)
running_set.remove(int(account['id']))
bot._print('finished account:', account['id'])
_quit = False
def auto_start():
while not _quit:
now = time.time()
cnt = 0
for each in accountdb.scan('PENDING'):
if each['nextime'] <= now:
cnt += 1
if cnt > 5:
break
gevent.spawn(run_task, each)
time.sleep(60)
def web_app(environ, start_response):
request = Request(environ)
if request.path == '/':
template = ('<html><body>'
'<form method=POST action="/invite">invite <input name=id /> '
'count<input name=count value=10 /> name<input name=name /><input type=submit /></form>'
'<form method=POST action="/add">add <input name="id" /><input name="pwd" /> '
'group:<input name=group /><input type=submit /></form>'
'<pre>%s</pre></body></html>')
content = []
content.append('<h1>RUNNING</h1><hr />')
now = time.time()
for cur in accountdb.scan('RUNNING'):
cur['mintime'] = datetime.datetime.fromtimestamp(cur['intime'])
cur['mnextime'] = cur['nextime'] - time.time()
content.append('%(mintime)s <a href="/log?id=%(id)s">%(id)s</a>:%(invite)s %(name)s lv:%(lv)s '
'rounds:%(rounds)s <a href="/stop?id=%(id)s">stop</a>' % cur)
content.append('<h1>PENDING</h1><hr />')
for cur in accountdb.scan('PENDING'):
cur['mintime'] = datetime.datetime.fromtimestamp(cur['intime'])
cur['mnextime'] = cur['nextime'] - time.time()
content.append('%(mintime)s <a href="/log?id=%(id)s">%(id)s</a>:%(invite)s %(name)s lv:%(lv)s '
'rounds:%(rounds)s %(mnextime)s <a href="/run?id=%(id)s&done=0">run</a> '
'<a href="/rm?id=%(id)s">del</a>' % cur)
content.append('<h1>DONE</h1><hr />')
done = []
for cur in accountdb.scan('DONE'):
cur['mintime'] = datetime.datetime.fromtimestamp(cur['intime'])
cur['mnextime'] = cur['nextime'] - time.time()
content.append('%(mintime)s <a href="/log?id=%(id)s">%(id)s</a>:%(invite)s %(name)s lv:%(lv)s '
'rounds:%(rounds)s' % cur)
content.append(' '.join(done))
content.append('<h1>FAILED</h1><hr />')
for cur in accountdb.scan('FAILED'):
cur['mintime'] = datetime.datetime.fromtimestamp(cur['intime'])
cur['mnextime'] = cur['nextime'] - time.time()
content.append('%(mintime)s <a href="/log?id=%(id)s">%(id)s</a>:%(invite)s %(name)s lv:%(lv)s '
'rounds:%(rounds)s <a href="/run?id=%(id)s&done=0">run</a>' % cur)
# return
start_response("200 OK", [("Content-Type", "text/html")])
return template % '\n'.join([x.encode('utf8') for x in content])
elif request.path == '/add':
_id = request.POST['id']
pwd = request.POST['pwd']
group = request.POST['group']
accountdb.add(_id, pwd, group=group)
start_response("302 FOUND", [("Location", "/")])
return 'redirect'
elif request.path == '/invite':
invitation_id = request.POST['id']
count = request.POST['count']
name_prefix = request.POST.get('name')
def do(invitation_id, count):
for _ in range(count):
try:
ret = regist.all_in_one(invitation_id, name_prefix)
except Exception, e:
ret = [e, ]
finally:
message = ' '.join([str(x) for x in ret])
print message
with open('/tmp/libma.%s.log' % invitation_id, 'a') as fp:
fp.write(message)
fp.write('\n')
gevent.spawn(do, invitation_id, int(count))
start_response("302 FOUND", [("Location", "/log?id=%s" % invitation_id)])
return 'redirect'
elif request.path == '/stop':
_id = int(request.GET['id'])
if _id in running_set:
stop_set.add(_id)
start_response("302 FOUND", [("Location", "/")])
return 'redirect'
else:
start_response("200 OK", [("Content-Type", "text/html")])
return 'failed!'
elif request.path == '/rm':
_id = int(request.GET['id'])
data = accountdb.get(_id)
if data:
data['status'] = 'FAILED'
accountdb.update(**data)
start_response("302 FOUND", [("Location", "/")])
return 'redirect'
else:
start_response("200 OK", [("Content-Type", "text/html")])
return 'failed!'
elif request.path == '/run':
_id = int(request.GET['id'])
done = int(request.GET.get('done', 1))
if done == 0:
data = accountdb.get(_id)
if not data:
start_response("302 FOUND", [("Location", "/run?id=%s&done=2" % _id)])
return 'redirect'
gevent.spawn(run_task, data)
start_response("302 FOUND", [("Location", "/run?id=%s&done=1" % _id)])
return 'redirect'
elif done == 2:
start_response("200 OK", [("Content-Type", "text/html")])
return 'id:%s failed!' % _id
else:
start_response("302 FOUND", [("Location", "/")])
return 'redirect'
elif request.path == '/log':
_id = request.GET['id']
try:
with open('/tmp/libma.%s.log' % _id, 'r') as fp:
start_response("200 OK", [("Content-Type", "text/plain")])
return fp.readlines()
except IOError, e:
start_response("404 NOT FOUND", [])
return '404'
elif request.path == '/quit':
global _quit
_quit = True
for each in running_set:
stop_set.add(each)
start_response("302 FOUND", [("Location", "/")])
return 'redirect'
else:
start_response("404 NOT FOUND", [])
return '404'
if __name__ == '__main__':
for each in accountdb.scan('RUNNING'):
accountdb.update(each['id'], status='PENDING')
server = gevent.pywsgi.WSGIServer(("", 8888), web_app)
gevent.spawn(auto_start)
server.serve_forever()