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)
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
# 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)