def victory(request): hero = request.hero combat = HeroM(hero).get_combat(Combat.IS_ACTIVE_FIGHT) if not combat: return HttpResponseRedirect(reverse(settings.URL_REVERSE_404)) team = combat.combatwarrior_set.get(hero=hero).team combatm = CombatM(combat, hero) is_timeout = combatm.is_timeout(team) if not is_timeout: return HttpResponseRedirect(reverse('combat')) dead_warriors = [] for combatwarrior in combat.combatwarrior_set.filter(is_dead=False). \ exclude(team=team): HeroM(combatwarrior.hero).set_hp(0) combatwarrior.is_dead = True combatwarrior.save() dead_warriors.append({ 'warrior': combatwarrior.hero, 'team': combatwarrior.team }) combatm.after_death(dead_warriors) combat.is_active = Combat.IS_ACTIVE_AFTER_FIGHT combat.save() return HttpResponseRedirect(reverse('combat')) # End combat inside
def victory(request): hero = request.hero combat = HeroM(hero).get_combat(Combat.IS_ACTIVE_FIGHT) if not combat: return HttpResponseRedirect(reverse(settings.URL_REVERSE_404)) team = combat.combatwarrior_set.get(hero=hero).team combatm = CombatM(combat, hero) is_timeout = combatm.is_timeout(team) if not is_timeout: return HttpResponseRedirect(reverse('combat')) dead_warriors = [] for combatwarrior in combat.combatwarrior_set.filter(is_dead=False). \ exclude(team=team): HeroM(combatwarrior.hero).set_hp(0) combatwarrior.is_dead = True combatwarrior.save() dead_warriors.append({'warrior': combatwarrior.hero, 'team': combatwarrior.team}) combatm.after_death(dead_warriors) combat.is_active = Combat.IS_ACTIVE_AFTER_FIGHT combat.save() return HttpResponseRedirect(reverse('combat')) # End combat inside
def fight(request): hero = request.hero combat = HeroM(hero).get_combat(Combat.IS_ACTIVE_WAIT) if not combat: return HttpResponseRedirect(reverse(settings.URL_REVERSE_404)) is_fight = CombatM(combat, hero).is_fight() if is_fight: combat.is_active = Combat.IS_ACTIVE_FIGHT combat.save() CombatM(combat, hero).write_log_message(True) return HttpResponseRedirect(reverse('combat')) return HttpResponseRedirect(reverse('combat_duel'))
def combat(request, template_name='combat/combat.html'): hero = request.hero combat = HeroM(hero).get_combat() combatm = CombatM(combat, hero) if not combat or not combatm.is_active(): return HttpResponseRedirect(reverse(settings.URL_REVERSE_404)) team = combat.combatwarrior_set.get(hero=hero).team is_dead = combatm.is_dead(hero) is_draw = combatm.is_draw() is_win = combatm.is_win(team, is_draw) is_lose = combatm.is_lose(team, is_draw) enemy = form = all_experience = None is_timeout = is_next = is_enemy_hero = False if is_dead == False and is_win == False and is_lose == False and \ is_draw == False: cur_enemy_id_fn = None if request.method == 'POST': form = CombatForm(hero.feature.strike_count, hero.feature.block_count, None, None, request.POST) if form.is_valid(): if form.cleaned_data['next']: cur_enemy_id_fn = form.cleaned_data['hero_two_id'] else: hero_two = bot = None if form.cleaned_data['hero_two_id']: try: hero_two = Hero.objects. \ get(id=form.cleaned_data['hero_two_id']) except Hero.DoesNotExist: return HttpResponseRedirect( reverse(settings.URL_REVERSE_404)) else: try: bot = Bot.objects. \ get(id=form.cleaned_data['bot_id']) except Bot.DoesNotExist: return HttpResponseRedirect( reverse(settings.URL_REVERSE_404)) if not combatm.is_warrior_in_combat(hero_two, bot): return HttpResponseRedirect(reverse('combat')) if not combatm.is_dead(hero_two, bot): strikes = \ [ str(form.cleaned_data['strike'+str(strike)]) \ for strike in range(int(hero.feature.strike_count)) ] blocks = [] if form.cleaned_data['block_head']: blocks.append('0') if form.cleaned_data['block_breast']: blocks.append('1') if form.cleaned_data['block_zone']: blocks.append('2') if form.cleaned_data['block_legs']: blocks.append('3') combatm.write_log_strikes(team, hero_two, bot, strikes, blocks) return HttpResponseRedirect(reverse('combat')) is_bot_make_timeout = combatm.update_bots_timeout() if is_bot_make_timeout: combat.is_active = Combat.IS_ACTIVE_AFTER_FIGHT combat.save() return HttpResponseRedirect(reverse('combat')) enemies = combatm.get_enemies(team) enemy = combatm.get_enemy(enemies, cur_enemy_id_fn) is_enemy_hero = type(enemy) == Hero if len(enemies) > 1: is_next = True try: past_enemy_id = int(form.data['hero_two_id']) \ if is_enemy_hero else int(form.data['bot_id']) except: past_enemy_id = None if not past_enemy_id or past_enemy_id != enemy.id: form = CombatForm( hero.feature.strike_count, hero.feature.block_count, enemy.id if enemy and is_enemy_hero else None, enemy.id if enemy and not is_enemy_hero else None) if enemy is None: is_timeout = combatm.is_timeout(team) else: if is_draw or is_win or is_lose: win_team = None if is_win: win_team = team elif is_lose: win_team = int(not team) combatm.write_log_message(is_finish=True, win_team=win_team) if is_win: if team == Combat.TEAM_FIRST: all_experience = combat.combatlog_set. \ filter(hero_one=hero) \ .aggregate(Sum('warrior_one_experience'))['warrior_one_experience__sum'] else: all_experience = combat.combatlog_set. \ filter(hero_two=hero) \ .aggregate(Sum('warrior_two_experience'))['warrior_two_experience__sum'] if all_experience == None: all_experience = 0 combatm.free_bots() if combat.is_active == Combat.IS_ACTIVE_FIGHT: combat.is_active = Combat.IS_ACTIVE_AFTER_FIGHT combat.save() if team == Combat.TEAM_FIRST: all_damage = combat.combatlog_set.filter(hero_one=hero). \ aggregate(Sum('warrior_one_damage'))['warrior_one_damage__sum'] else: all_damage = combat.combatlog_set.filter(hero_two=hero). \ aggregate(Sum('warrior_two_damage'))['warrior_two_damage__sum'] if all_damage == None: all_damage = 0 variables = RequestContext(request, {'hero_two': enemy if is_enemy_hero \ else None, 'bot': enemy if not is_enemy_hero \ else None, 'form': form, 'combat': combat, 'combatlogs': combat.combatlog_set. \ all(), 'is_draw': is_draw, 'is_win': is_win, 'is_lose': is_lose, 'is_dead': is_dead, 'is_timeout': is_timeout, 'is_next': is_next, 'all_damage': all_damage, 'all_experiance': all_experience}) return render_to_response(template_name, variables)
def combat(request, template_name='combat/combat.html'): hero = request.hero combat = HeroM(hero).get_combat() combatm = CombatM(combat, hero) if not combat or not combatm.is_active(): return HttpResponseRedirect(reverse(settings.URL_REVERSE_404)) team = combat.combatwarrior_set.get(hero=hero).team is_dead = combatm.is_dead(hero) is_draw = combatm.is_draw() is_win = combatm.is_win(team, is_draw) is_lose = combatm.is_lose(team, is_draw) enemy = form = all_experience = None is_timeout = is_next = is_enemy_hero = False if is_dead == False and is_win == False and is_lose == False and \ is_draw == False: cur_enemy_id_fn = None if request.method == 'POST': form = CombatForm(hero.feature.strike_count, hero.feature.block_count, None, None, request.POST) if form.is_valid(): if form.cleaned_data['next']: cur_enemy_id_fn = form.cleaned_data['hero_two_id'] else: hero_two = bot = None if form.cleaned_data['hero_two_id']: try: hero_two = Hero.objects. \ get(id=form.cleaned_data['hero_two_id']) except Hero.DoesNotExist: return HttpResponseRedirect(reverse( settings.URL_REVERSE_404)) else: try: bot = Bot.objects. \ get(id=form.cleaned_data['bot_id']) except Bot.DoesNotExist: return HttpResponseRedirect(reverse( settings.URL_REVERSE_404)) if not combatm.is_warrior_in_combat(hero_two, bot): return HttpResponseRedirect(reverse('combat')) if not combatm.is_dead(hero_two, bot): strikes = \ [ str(form.cleaned_data['strike'+str(strike)]) \ for strike in range(int(hero.feature.strike_count)) ] blocks = [] if form.cleaned_data['block_head']: blocks.append('0') if form.cleaned_data['block_breast']: blocks.append('1') if form.cleaned_data['block_zone']: blocks.append('2') if form.cleaned_data['block_legs']: blocks.append('3') combatm.write_log_strikes(team, hero_two, bot, strikes, blocks) return HttpResponseRedirect(reverse('combat')) is_bot_make_timeout = combatm.update_bots_timeout() if is_bot_make_timeout: combat.is_active = Combat.IS_ACTIVE_AFTER_FIGHT combat.save() return HttpResponseRedirect(reverse('combat')) enemies = combatm.get_enemies(team) enemy = combatm.get_enemy(enemies, cur_enemy_id_fn) is_enemy_hero = type(enemy) == Hero if len(enemies) > 1: is_next = True try: past_enemy_id = int(form.data['hero_two_id']) \ if is_enemy_hero else int(form.data['bot_id']) except: past_enemy_id = None if not past_enemy_id or past_enemy_id != enemy.id: form = CombatForm(hero.feature.strike_count, hero.feature.block_count, enemy.id if enemy and is_enemy_hero else None, enemy.id if enemy and not is_enemy_hero else None) if enemy is None: is_timeout = combatm.is_timeout(team) else: if is_draw or is_win or is_lose: win_team = None if is_win: win_team = team elif is_lose: win_team = int(not team) combatm.write_log_message(is_finish=True, win_team=win_team) if is_win: if team == Combat.TEAM_FIRST: all_experience = combat.combatlog_set. \ filter(hero_one=hero) \ .aggregate(Sum('warrior_one_experience'))['warrior_one_experience__sum'] else: all_experience = combat.combatlog_set. \ filter(hero_two=hero) \ .aggregate(Sum('warrior_two_experience'))['warrior_two_experience__sum'] if all_experience == None: all_experience = 0 combatm.free_bots() if combat.is_active == Combat.IS_ACTIVE_FIGHT: combat.is_active = Combat.IS_ACTIVE_AFTER_FIGHT combat.save() if team == Combat.TEAM_FIRST: all_damage = combat.combatlog_set.filter(hero_one=hero). \ aggregate(Sum('warrior_one_damage'))['warrior_one_damage__sum'] else: all_damage = combat.combatlog_set.filter(hero_two=hero). \ aggregate(Sum('warrior_two_damage'))['warrior_two_damage__sum'] if all_damage == None: all_damage = 0 variables = RequestContext(request, {'hero_two': enemy if is_enemy_hero \ else None, 'bot': enemy if not is_enemy_hero \ else None, 'form': form, 'combat': combat, 'combatlogs': combat.combatlog_set. \ all(), 'is_draw': is_draw, 'is_win': is_win, 'is_lose': is_lose, 'is_dead': is_dead, 'is_timeout': is_timeout, 'is_next': is_next, 'all_damage': all_damage, 'all_experiance': all_experience}) return render_to_response(template_name, variables)