def fromJSON(cls, json_data, idolized=False): card_id, card_name, member_name = json_data['card_id'], json_data['card_name'], json_data['member_name'] rarity, main_attr, stats_list = json_data['rarity'], json_data['main_attr'], json_data['stats_list'] skill_data, cskill_data = json_data['skill'], json_data['cskill'] if skill_data is not None: skill = Skill(skill_data['name'], skill_data['trigger_type'], skill_data['trigger_count'], skill_data['effect_type'], skill_data['odds_list'], skill_data['rewards_list']) cskill = CenterSkill(cskill_data['name'], cskill_data['main_attr'], cskill_data['base_attr'], cskill_data['main_ratio'], cskill_data['bonus_range'], cskill_data['bonus_ratio']) else: skill, cskill = None, None idolized, promo = idolized or json_data['promo'], json_data['promo'] return cls(card_id, card_name, member_name, rarity, main_attr, stats_list, idolized, skill, cskill, promo)
def calculate(request): strings = { 'CN': { 'DEFAULT': '默认谱面', 'ERR_LIVE': '载入谱面信息失败...', 'ERR_PROFILE': '载入用户卡组信息失败...', 'ERR_TEAM': '构造组队失败...', 'ERR_EXCOND': '应用附加条件失败...', 'ERR_PREPARE': '仿真预处理失败, 可能是缺少技能槽信息,请尝试进入手动编辑界面点击保存再返回仿真', 'ERR_EXPORT': '导出文件失败...', 'SUCCESS': '队伍强度计算成功,共耗时{0:.2f}秒', 'SUCCESS_AUTO': '#{0} 宝石分配成功,共耗时{1:.2f}秒', 'ERR_NONAJAX': '服务器接收请求不是AJAX' }, 'EN': { 'DEFAULT': 'Default', 'ERR_LIVE': 'Failed to load live notes...', 'ERR_PROFILE': 'Failed to read user profile...', 'ERR_TEAM': 'Failed to construct team...', 'ERR_EXCOND': 'Failed to apply extra condition...', 'ERR_PREPARE': 'Failed to finish preprocessing of advanced simulation, please go to manual edit page to save and return again', 'ERR_EXPORT': 'Failed to export result into other formats...', 'SUCCESS': 'Team strength calculated, used {0:.2f} secs', 'SUCCESS_AUTO': '#{0} optimal SIS allocated, used {1:.2f} secs', 'ERR_NONAJAX': 'The request is not AJAX' } } lang = request.POST.get('lang', 'EN') if request.is_ajax(): start_time = time.time() song_list, PR = eval(request.POST['song_list']), float( request.POST['perfect_rate']) group, attr, diff = [ request.POST[x] for x in ['group', 'attribute', 'difficulty'] ] score_up, skill_up = float(request.POST.get('score_up', 0)), float( request.POST.get('skill_up', 0)) extra_cond, json_str = request.POST['extra_cond'], request.POST[ 'user_profile'] user_info = 'User Information: {0} from {1} page\n'.format( str(get_client_ip(request)), lang) user_info += 'Live Info: {0} {1} {2} {3}, P Rate={4}\n'.format( song_list, group, attr, diff, PR) user_info += 'ScoreUp={0}, SkillUp={1}, ExtraCond={2}'.format( score_up, skill_up, extra_cond) print(user_info) # Load live try: live = MFLive(song_list, diff, local_dir=settings.BASE_DIR + '/static/live_json/', perfect_rate=PR) note_list = json.dumps(live.web_note_list) print('Successfully loaded live.') except: print('Failed to load live.') message = {'complete': False, 'msg': strings[lang]['ERR_LIVE']} return JsonResponse(message) # Load user profile try: if 'detail' in json_str: user_profile = GameData(json_str, file_type='pll', string_input=True) else: user_profile = GameData(json_str, file_type='ieb', string_input=True) print('Successfully loaded user profile.') except: print('Failed to load user profile.') message = {'complete': False, 'msg': strings[lang]['ERR_PROFILE']} return JsonResponse(message) # Solve for optimal SIS allocation or compute the team strength try: guest_center = request.POST.get('guest_center', 'None') guest_cskill = None if guest_center == 'None' else CenterSkill.fromStr( guest_center) if guest_cskill != None: print('Guest skill specified to be', str(guest_cskill)) opt = { 'score_up_bonus': score_up, 'skill_up_bonus': skill_up, 'guest_cskill': guest_cskill } tb = TeamBuilder(live, user_profile, opt=opt, extra_cond=extra_cond) try: adv_card_list = sorted(tb.cards, key=lambda x: x.index) for card, info in zip(adv_card_list, json.loads(json_str)['unit_info']): gems = [gem_skill_id_dict[x] for x in info['removable']] card.equip_gem(gems) print('Successfully equipped team.') except: print('Failed to equip team.') message = {'complete': False, 'msg': strings[lang]['ERR_TEAM']} return JsonResponse(message) input_team = Team(adv_card_list) print('Successfully finished preparation for advanced simulation.') except: print( 'Failed to compute team strength details. Perhaps lack of slot number information, please try manual edit team first.' ) message = {'complete': False, 'msg': strings[lang]['ERR_PREPARE']} return JsonResponse(message) # Covert result to LL Helper and SIFStats try: sd_file, ieb_file = input_team.to_LLHelper( None), input_team.to_ieb(None) simul_base_info = json.dumps(input_team.prepare_simulation(opt)) except: print('Failed to export file.') message = {'complete': False, 'msg': strings[lang]['ERR_EXPORT']} return JsonResponse(message) elapsed_time = time.time() - start_time message = { 'complete': True, 'sd_file': sd_file, 'ieb_file': ieb_file, 'note_list': note_list, 'simul_base_info': simul_base_info, 'msg': strings[lang]['SUCCESS'].format(elapsed_time) } else: message = {'complete': False, 'msg': strings[lang]['ERR_NONAJAX']} return JsonResponse(message)
def calculate(request): strings = { 'CN': { 'DEFAULT': '默认谱面', 'ERR_LIVE': '载入谱面信息失败...', 'ERR_PROFILE': '载入用户卡组信息失败...', 'ERR_EXCOND': '应用附加条件失败...', 'ERR_SOLVE': '求解最佳卡组失败,请检查该色是否有13张卡', 'ERR_EXPORT': '导出文件失败...', 'IMCOMPLETE': '由于服务器响应时间限制,返回算法中断前最优结果,算法完成度 {0}/{1},请尝试<button id="recalculate" class="w3-green w3-hover-yellow roundBtn" style="height:25px" onclick="calculate(true)"><b>继续未完成计算</b></button>。<br>可能是你的宝石太少了,您也可以尝试开启无限宝石看你需要哪些并抓紧时间收集。', 'SUCCESS': '#{0} 组队成功,共耗时{1:.2f}秒', 'ERR_NONAJAX': '服务器接收请求不是AJAX' }, 'EN': { 'DEFAULT': 'Default', 'ERR_LIVE': 'Failed to load live notes...', 'ERR_PROFILE': 'Failed to read user profile...', 'ERR_EXCOND': 'Failed to apply extra condition...', 'ERR_SOLVE': 'Failed to solve optimal team, please check whether there are at least 13 cards have current attribute', 'ERR_EXPORT': 'Failed to export result into other formats...', 'IMCOMPLETE': 'Due to server response time limit, algorithm terminated with progress {0}/{1}, please try to <button id="recalculate" class="w3-green w3-hover-yellow roundBtn" style="height:25px" onclick="calculate(true)"><b>continue</b></button> the computation.', 'SUCCESS': '#{0} Team builded, used {1:.2f} secs', 'ERR_NONAJAX': 'The request is not AJAX' } } lang = request.POST.get('lang', 'EN') if request.is_ajax(): start_time = time.time() song_list, PR = eval(request.POST['song_list']), float(request.POST['perfect_rate']) group, attr, diff = [request.POST[x] for x in ['group', 'attribute', 'difficulty']] score_up, skill_up = float(request.POST.get('score_up',0)), float(request.POST.get('skill_up',0)) unlimit_gem, extra_cond, json_str = bool(request.POST['unlimit_gem']=='true'), request.POST['extra_cond'], request.POST['user_profile'] is_sm, is_random = bool(request.POST.get('is_sm', 'false')=='true'), bool(request.POST.get('is_random', 'false')=='true') pin_index = [int(x) for x in json.loads(request.POST.get('pin_index', '[]'))] exclude_index = [int(x) for x in json.loads(request.POST.get('exclude_index', '[]'))] next_cskill_index, prev_max_cskill_index = int(request.POST.get('next_cskill_index', 0)), int(request.POST.get('prev_max_cskill_index', 0)) user_info = 'User Information: {0} from {1} team building page\n'.format(str(get_client_ip(request)), lang) if not is_sm: user_info += 'Live Info: {0} {1} {2} {3}, P Rate={4}, {5}/{6} pinned cards\n'.format(song_list, group, attr, diff, PR, len(pin_index), len(exclude_index)) else: user_info += 'Live Info: {5} {0} {1} {2} {3}, P Rate={4}, {6}/{7} pinned cards\n'.format(song_list, group, attr, diff, PR, 'SM'+' random'*is_random, len(pin_index), len(exclude_index)) user_info += 'ScoreUp={0}, SkillUp={1}, GemUnlimited={2}, ExtraCond={3}, start from case {4}, previous best case is at {5}'.format(score_up, skill_up, unlimit_gem, extra_cond, next_cskill_index, prev_max_cskill_index) print(user_info) # Load live try: if strings[lang]['DEFAULT'] not in song_list[0] and not is_sm: live = MFLive(song_list, diff, local_dir=settings.BASE_DIR+'/static/live_json/', perfect_rate=PR) live_stats, note_list = html_view(live, lang=lang).data, json.dumps(live.web_note_list) elif is_sm: live = SMLive(song_list, diff, local_dir=settings.BASE_DIR+'/static/live_json/', perfect_rate=PR, is_random=is_random) live_stats, note_list = 'NA', '{}' else: song_name = song_list[0].replace(strings[lang]['DEFAULT'], 'Default') live = DefaultLive(song_name, diff, perfect_rate=PR) live_stats, note_list = 'NA', '{}' print('Successfully loaded live.') except: print('Failed to load live.') message = {'complete':False, 'msg':strings[lang]['ERR_LIVE']} return JsonResponse(message) # Load user profile try: if 'detail' in json_str: user_profile = GameData(json_str, file_type='pll', string_input=True) else: user_profile = GameData(json_str, file_type='ieb', string_input=True) print('Successfully loaded user profile.') except: print('Failed to load user profile.') message = {'complete':False, 'msg':strings[lang]['ERR_PROFILE']} return JsonResponse(message) # Solve for optimal team try: guest_center = request.POST.get('guest_center', 'None') guest_cskill = None if guest_center == 'None' else CenterSkill.fromStr(guest_center) if guest_cskill != None: print('Guest skill specified to be', str(guest_cskill)) opt = {'score_up_bonus':score_up, 'skill_up_bonus':skill_up, 'guest_cskill':guest_cskill} tb = TeamBuilder(live, user_profile, opt=opt, unlimited_SIS=unlimit_gem, extra_cond=extra_cond) # Choose alloc method wisely _, (num_calc, num_total, prev_max_cskill_index) = tb.build_team(K=12, method='1-suboptimal', alloc_method='auto', time_limit=24, pin_index=pin_index, exclude_index=exclude_index, next_cskill_index=next_cskill_index, prev_max_cskill_index=prev_max_cskill_index) result = '' if num_calc < num_total: result += '<p style="text-align:center; color:red"><b>{0}</b></p>'.format(strings[lang]['IMCOMPLETE'].format(num_calc, num_total)) result += tb.view_result(show_cost=True, lang=lang).data.replace('http:','').replace('https:','') except: print('Failed to compute optimal team.') message = {'complete':False, 'msg':strings[lang]['ERR_SOLVE']} return JsonResponse(message) # Covert result to LL Helper and SIFStats try: sd_file, ieb_file = tb.best_team.to_LLHelper(None), tb.best_team.to_ieb(None) simul_base_info = json.dumps(tb.best_team.prepare_simulation(opt)) except: print('Failed to export file.') message = {'complete':False, 'msg':strings[lang]['ERR_EXPORT']} return JsonResponse(message) elapsed_time = time.time() - start_time counter, _ = Counter.objects.get_or_create() counter.TeamCount += 1 counter.save() print('This is the {0}-th built team, computation takes {1:.2f} seconds'.format(counter.TeamCount, elapsed_time)) message = { 'complete': True, 'counter': counter.TeamCount, 'sd_file': sd_file, 'ieb_file': ieb_file, 'live_stats': live_stats, 'result': result, 'note_list': note_list, 'simul_base_info': simul_base_info, 'next_cskill_index': num_calc+1, 'prev_max_cskill_index': prev_max_cskill_index, 'msg': strings[lang]['SUCCESS'].format(counter.TeamCount, elapsed_time) } else: message = {'complete':False, 'msg':strings[lang]['ERR_NONAJAX']} return JsonResponse(message)
def calculate(request): strings = { 'CN': { 'DEFAULT': '默认谱面', 'ERR_LIVE': '载入谱面信息失败...', 'ERR_PROFILE': '载入用户卡组信息失败...', 'ERR_AUTOSIS': '自动配宝石失败,请检查卡组是否有与谱面同色的卡牌...', 'ERR_TEAM': '构造组队失败...', 'ERR_EXCOND': '应用附加条件失败...', 'ERR_SOLVE': '求解最佳卡组失败,请检查该色是否有13张卡', 'ERR_EXPORT': '导出文件失败...', 'SUCCESS': '队伍强度计算成功,共耗时{0:.2f}秒', 'SUCCESS_AUTO': '#{0} 宝石分配成功,共耗时{1:.2f}秒', 'ERR_NONAJAX': '服务器接收请求不是AJAX' }, 'EN': { 'DEFAULT': 'Default', 'ERR_LIVE': 'Failed to load live notes...', 'ERR_PROFILE': 'Failed to read user profile...', 'ERR_AUTOSIS': 'Fail to allocate SIS, team must have at least one card that matches live attribute.', 'ERR_TEAM': 'Failed to construct team...', 'ERR_EXCOND': 'Failed to apply extra condition...', 'ERR_SOLVE': 'Failed to compute team strength details. Perhaps lack of slot number information, please try manual edit team first.', 'ERR_EXPORT': 'Failed to export result into other formats...', 'SUCCESS': 'Team strength calculated, used {0:.2f} secs', 'SUCCESS_AUTO': '#{0} optimal SIS allocated, used {1:.2f} secs', 'ERR_NONAJAX': 'The request is not AJAX' } } lang = request.POST.get('lang', 'EN') if request.is_ajax(): start_time = time.time() song_list, PR = eval(request.POST['song_list']), float( request.POST['perfect_rate']) group, attr, diff = [ request.POST[x] for x in ['group', 'attribute', 'difficulty'] ] score_up, skill_up = float(request.POST.get('score_up', 0)), float( request.POST.get('skill_up', 0)) unlimit_gem, extra_cond = bool( request.POST['unlimit_gem'] == 'true'), request.POST['extra_cond'] auto_mode, json_str = request.POST[ 'auto_mode'] == 'true', request.POST['user_profile'] is_sm, is_random = bool(request.POST['is_sm'] == 'true'), bool( request.POST['is_random'] == 'true') user_info = 'User Information: {0} from {1} page\n'.format( str(get_client_ip(request)), lang) if not is_sm: user_info += 'Live Info: {0} {1} {2} {3}, P Rate={4}\n'.format( song_list, group, attr, diff, PR) else: user_info += 'Live Info: {5} {0} {1} {2} {3}, P Rate={4}\n'.format( song_list, group, attr, diff, PR, 'SM' + ' random' * is_random) user_info += 'ScoreUp={0}, SkillUp={1}, GemUnlimited={2}, ExtraCond={3}, AutoMode={4}'.format( score_up, skill_up, unlimit_gem, extra_cond, auto_mode) print(user_info) # Load live try: if strings[lang]['DEFAULT'] not in song_list[0] and not is_sm: live = MFLive(song_list, diff, local_dir=settings.BASE_DIR + '/static/live_json/', perfect_rate=PR) note_list = json.dumps(live.web_note_list) elif is_sm: live = SMLive(song_list, diff, local_dir=settings.BASE_DIR + '/static/live_json/', perfect_rate=PR, is_random=is_random) note_list = '{}' else: song_name = song_list[0].replace(strings[lang]['DEFAULT'], 'Default') live = DefaultLive(song_name, diff, perfect_rate=float(PR)) note_list = '{}' print('Successfully loaded live.') except: print('Failed to load live.') message = {'complete': False, 'msg': strings[lang]['ERR_LIVE']} return JsonResponse(message) # Load user profile try: if 'detail' in json_str: user_profile = GameData(json_str, file_type='pll', string_input=True) else: user_profile = GameData(json_str, file_type='ieb', string_input=True) print('Successfully loaded user profile.') except: print('Failed to load user profile.') message = {'complete': False, 'msg': strings[lang]['ERR_PROFILE']} return JsonResponse(message) # Solve for optimal SIS allocation or compute the team strength try: guest_center = request.POST.get('guest_center', 'None') guest_cskill = None if guest_center == 'None' else CenterSkill.fromStr( guest_center) if guest_cskill != None: print('Guest skill specified to be', str(guest_cskill)) opt = { 'score_up_bonus': score_up, 'skill_up_bonus': skill_up, 'guest_cskill': guest_cskill } tb = TeamBuilder(live, user_profile, opt=opt, unlimited_SIS=unlimit_gem, extra_cond=extra_cond) if auto_mode: try: tb.build_team(K=8, method='1-suboptimal', alloc_method='auto') result = tb.view_result(show_cost=True, lang=lang).data.replace( 'http:', '').replace('https:', '') except: print('Failed to achieve auto SIS allocation.') message = { 'complete': False, 'msg': strings[lang]['ERR_AUTOSIS'] } return JsonResponse(message) else: # Construct team by placing the given SIS try: adv_card_list = sorted(tb.cards, key=lambda x: x.index) for card, info in zip(adv_card_list, json.loads(json_str)['unit_info']): gems = [ gem_skill_id_dict[x] for x in info['removable'] ] card.equip_gem(gems) print('Successfully equipped team.') except: print('Failed to equip team.') message = { 'complete': False, 'msg': strings[lang]['ERR_TEAM'] } return JsonResponse(message) input_team = Team(adv_card_list) result = tb.team_strength_detail(input_team, show_cost=True).data.replace( 'http:', '').replace('https:', '') print('Successfully computed team strength details.') except: print( 'Failed to compute team strength details. Perhaps lack of slot number information, please try manual edit team first.' ) message = {'complete': False, 'msg': strings[lang]['ERR_SOLVE']} return JsonResponse(message) # Covert result to LL Helper and SIFStats try: if auto_mode: sd_file, ieb_file = tb.best_team.to_LLHelper( None), tb.best_team.to_ieb(None) simul_base_info = json.dumps( tb.best_team.prepare_simulation(opt)) else: sd_file, ieb_file = input_team.to_LLHelper( None), input_team.to_ieb(None) simul_base_info = json.dumps( input_team.prepare_simulation(opt)) except: print('Failed to export file.') message = {'complete': False, 'msg': strings[lang]['ERR_EXPORT']} return JsonResponse(message) elapsed_time = time.time() - start_time if auto_mode: counter, _ = Counter.objects.get_or_create() counter.TeamCount += 1 counter.save() success_msg = strings[lang]['SUCCESS_AUTO'].format( counter.TeamCount, elapsed_time) print( 'This is the {0}-th built team, computation takes {1:.2f} seconds' .format(counter.TeamCount, elapsed_time)) else: success_msg = strings[lang]['SUCCESS'].format(elapsed_time) message = { 'complete': True, 'sd_file': sd_file, 'ieb_file': ieb_file, 'result': result, 'note_list': note_list, 'simul_base_info': simul_base_info, 'msg': success_msg } else: message = {'complete': False, 'msg': strings[lang]['ERR_NONAJAX']} return JsonResponse(message)
def filter_cards(request): strings = { 'CN': { 'DEFAULT': '默认谱面', 'ERR_LIVE': '载入谱面信息失败...', 'ERR_PROFILE': '载入用户卡组信息失败...', 'ERR_EXCOND': '应用附加条件失败...', 'ERR_TIER': '计算卡牌强度天梯失败...', 'SUCCESS': '成功筛选卡牌', 'ERR_NONAJAX': '服务器接收请求不是AJAX' }, 'EN': { 'DEFAULT': 'Default', 'ERR_LIVE': 'Failed to load live notes...', 'ERR_PROFILE': 'Failed to read user profile...', 'ERR_EXCOND': 'Failed to apply extra condition...', 'ERR_TIER': 'Failed to compute card strength tier...', 'SUCCESS': 'Successfully filter the cards', 'ERR_NONAJAX': 'The request is not AJAX' } } lang = request.POST.get('lang', 'EN') if request.is_ajax(): start_time = time.time() song_list, PR = eval(request.POST['song_list']), float(request.POST['perfect_rate']) group, attr, diff = [request.POST[x] for x in ['group', 'attribute', 'difficulty']] score_up, skill_up = float(request.POST.get('score_up',0)), float(request.POST.get('skill_up',0)) extra_cond, json_str = request.POST['extra_cond'], request.POST['user_profile'] is_sm, is_random = bool(request.POST.get('is_sm', 'false')=='true'), bool(request.POST.get('is_random', 'false')=='true') user_info = 'User Information: {0} from {1} card filtering page\n'.format(str(get_client_ip(request)), lang) if not is_sm: user_info += 'Live Info: {0} {1} {2} {3}, P Rate={4}\n'.format(song_list, group, attr, diff, PR) else: user_info += 'Live Info: {5} {0} {1} {2} {3}, P Rate={4}\n'.format(song_list, group, attr, diff, PR, 'SM'+' random'*is_random) user_info += 'ScoreUp={0}, SkillUp={1}, ExtraCond={2}'.format(score_up, skill_up, extra_cond) print(user_info) # Load live try: if strings[lang]['DEFAULT'] not in song_list[0] and not is_sm: live = MFLive(song_list, diff, local_dir=settings.BASE_DIR+'/static/live_json/', perfect_rate=PR) live_stats, note_list = html_view(live, lang=lang).data, json.dumps(live.web_note_list) elif is_sm: live = SMLive(song_list, diff, local_dir=settings.BASE_DIR+'/static/live_json/', perfect_rate=PR, is_random=is_random) live_stats, note_list = 'NA', '{}' else: song_name = song_list[0].replace(strings[lang]['DEFAULT'], 'Default') live = DefaultLive(song_name, diff, perfect_rate=PR) live_stats, note_list = 'NA', '{}' print('Successfully loaded live.') except: print('Failed to load live.') message = {'complete':False, 'msg':strings[lang]['ERR_LIVE']} return JsonResponse(message) # Load user profile try: if 'detail' in json_str: user_profile = GameData(json_str, file_type='pll', string_input=True) else: user_profile = GameData(json_str, file_type='ieb', string_input=True) print('Successfully loaded user profile.') except: print('Failed to load user profile.') message = {'complete':False, 'msg':strings[lang]['ERR_PROFILE']} return JsonResponse(message) # Modify user profile try: guest_center = request.POST.get('guest_center', 'None') guest_cskill = None if guest_center == 'None' else CenterSkill.fromStr(guest_center) if guest_cskill != None: print('Guest skill specified to be', str(guest_cskill)) opt = {'score_up_bonus':score_up, 'skill_up_bonus':skill_up, 'guest_cskill':guest_cskill} tb = TeamBuilder(live, user_profile, opt=opt, extra_cond=extra_cond) result = {k:v.data.replace('http:','').replace('https:','') for k, v in tb.show_rough_strength().items()} except: print('Failed to filter cards.') message = {'complete':False, 'msg':strings[lang]['ERR_TIER']} return JsonResponse(message) message = { 'complete': True, 'result': result, 'msg': strings[lang]['SUCCESS'] } else: message = {'complete':False, 'msg':strings[lang]['ERR_NONAJAX']} return JsonResponse(message)