Пример #1
0
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
Пример #2
0
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
Пример #3
0
def quit(request):

    hero = request.hero
    herom = HeroM(hero)

    combat = herom.get_combat(Combat.IS_ACTIVE_AFTER_FIGHT)

    if not combat:
        return HttpResponseRedirect(reverse(settings.URL_REVERSE_404))

    combatm = CombatM(combat, hero)

    combatwarrior = combat.combatwarrior_set.get(hero=hero)
    combatwarrior.is_quit = True
    herom.set_hp()
    combatwarrior.save()

    team = combat.combatwarrior_set.get(hero=hero).team

    is_draw = combatm.is_draw()
    is_win = combatm.is_win(team, is_draw)

    if is_draw:
        hero.number_of_draws += 1
    elif 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

        hero.experience += all_experience
        hero.number_of_wins += 1

        herom.level_up()
    else:
        hero.number_of_losses += 1
    hero.save()

    if not combatm.is_anybody_not_quit():
        if is_win:
            combat.win_team = team
        elif is_draw:
            combat.win_team = None
        else:
            combat.win_team = Combat.TEAM_FIRST if team else Combat.TEAM_SECOND
        combat.end_date_time = datetime.datetime.now()
        combat.is_active = Combat.IS_ACTIVE_PAST
        combat.save()

    return HttpResponseRedirect(reverse('hero'))
Пример #4
0
def quit(request):
    
    hero = request.hero
    herom = HeroM(hero)

    combat = herom.get_combat(Combat.IS_ACTIVE_AFTER_FIGHT)

    if not combat:
        return HttpResponseRedirect(reverse(settings.URL_REVERSE_404))

    combatm = CombatM(combat, hero)
    
    combatwarrior = combat.combatwarrior_set.get(hero=hero)
    combatwarrior.is_quit = True
    herom.set_hp()
    combatwarrior.save()
    
    team = combat.combatwarrior_set.get(hero=hero).team
    
    is_draw = combatm.is_draw()
    is_win = combatm.is_win(team, is_draw)
    
    if is_draw:
        hero.number_of_draws += 1
    elif 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
                
        hero.experience += all_experience
        hero.number_of_wins += 1
        
        herom.level_up()
    else:
        hero.number_of_losses += 1
    hero.save()
    
    if not combatm.is_anybody_not_quit():
        if is_win:
            combat.win_team = team
        elif is_draw:
            combat.win_team = None
        else:
            combat.win_team = Combat.TEAM_FIRST if team else Combat.TEAM_SECOND
        combat.end_date_time = datetime.datetime.now()
        combat.is_active = Combat.IS_ACTIVE_PAST
        combat.save()
        
    return HttpResponseRedirect(reverse('hero'))
Пример #5
0
def group(request, template_name='combat/group.html'):

    hero = request.hero
    herom = HeroM(hero)
    combat = herom.get_combat(Combat.IS_ACTIVE_WAIT)

    combatm = CombatM(combat, hero)

    combatm.update_combats(Combat.TYPE_GROUP)
    if combat and combatm.is_active():
        return HttpResponseRedirect(reverse('combat'))

    is_cancel = False
    if combat:
        is_cancel = combatm.is_cancel()
          
    if request.method == 'POST' and not combat:
        form = GroupForm(request.POST)
        if form.is_valid():
            combat = Combat(type=Combat.TYPE_GROUP,
                            time_out=form.cleaned_data['time_out'],
                            injury=form.cleaned_data['injury'],
                            with_things=form.cleaned_data['with_things'],
                            time_wait=form.cleaned_data['time_wait'],
                            location=herom.get_location(),
                            one_team_count=form.cleaned_data['one_team_count'],
                            two_team_count=form.cleaned_data['two_team_count'],
                        one_team_lvl_min=form.cleaned_data['one_team_lvl_min'],
                        one_team_lvl_max=form.cleaned_data['one_team_lvl_max'],
                        two_team_lvl_min=form.cleaned_data['two_team_lvl_min'],
                        two_team_lvl_max=form.cleaned_data['two_team_lvl_max'])
            
            combat.save()
            combat.combatwarrior_set.create(hero=hero)
#
            messages.add_message(request, messages.SUCCESS, 
                                 'Your demand create.')
            return HttpResponseRedirect(reverse('combat_group'))
    else:
        form = GroupForm()
        
    combats = Combat.objects.filter(type=Combat.TYPE_GROUP, 
                                    is_active=Combat.IS_ACTIVE_WAIT, 
                                    location=herom.get_location())
    
    variables = RequestContext(request, {'form': form,
                                         'combats': combats,
                                         'in_combat': combat,
                                         'is_cancel': is_cancel})
    
    return render_to_response(template_name, variables)
Пример #6
0
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'))
Пример #7
0
    def inner_decorator(request, *args, **kwargs):
        if 'hero_id' not in request.session:
            #
            messages.add_message(request, messages.ERROR,
                                 'You have to log in.')
            return HttpResponseRedirect(reverse('main'))
        try:
            hero = Hero.objects.get(id=request.session['hero_id'])
        except Hero.DoesNotExist:
            return HttpResponseRedirect(reverse('main'))

        request.hero = hero

        from combat.manipulation import CombatM

        herom = HeroM(hero)
        combat = herom.get_combat()

        if combat and CombatM(combat, hero).is_active():
            if request.path != reverse('combat') and \
               request.path != reverse('combat_quit') and \
               request.path != reverse('combat_victory'):
                return HttpResponseRedirect(reverse('combat'))
        else:
            herom.update_hp()

        return origin_func(request, *args, **kwargs)
Пример #8
0
def move(request, coordinate_x, coordinate_y):

    hero = request.hero
    herom = HeroM(hero)
    island = herom.get_island()
    hero_position = herom.get_position_on_island()

    islandm = IslandM(island)
    if not islandm.is_can_make_step(coordinate_x, coordinate_y, hero_position):
        return HttpResponseRedirect(reverse(settings.URL_REVERSE_404))

    if not herom.get_combat():
        if not islandm.get_time_left_to_move(hero_position):
            try:
                island.islandpart_set.get(coordinate_x=coordinate_x,
                                          coordinate_y=coordinate_y,
                                          is_move=False)
            except IslandPart.DoesNotExist:
                herom.update_position_on_island(coordinate_x, coordinate_y)
                islandm.update_bots_position(coordinate_x, coordinate_y)
                CombatM(None,
                        hero).update_bots_in_combats(coordinate_x,
                                                     coordinate_y)
    else:
        #
        messages.add_message(request, messages.ERROR, 'Take away your demand.')

    return HttpResponseRedirect(reverse('island'))
Пример #9
0
def past(request, template_name='combat/past.html'):

    hero = request.hero
    herom = HeroM(hero)

    combat = herom.get_combat(Combat.IS_ACTIVE_WAIT)

    is_cancel = False
    if combat:
        is_cancel = CombatM(combat, hero).is_cancel()

    combats = None
    if request.method == 'POST':
        form = PastForm(request.POST)
        if form.is_valid():
            login = form.cleaned_data['login']
            date_begin = form.cleaned_data['date_begin']
            date_end = form.cleaned_data['date_end']

            search_hero = Hero.objects.get(login=login)

            combats = Combat.objects.filter(is_active=Combat.IS_ACTIVE_PAST,
                                            combatwarrior__hero=search_hero,
                                            start_date_time__gte=date_begin,
                                            start_date_time__lte=date_end)
    else:
        form = PastForm()

    variables = RequestContext(request, {
        'form': form,
        'combats': combats,
        'is_cancel': is_cancel
    })

    return render_to_response(template_name, variables)
Пример #10
0
def duel(request, template_name='combat/duel.html'):

    hero = request.hero
    herom = HeroM(hero)
    combat = herom.get_combat(Combat.IS_ACTIVE_WAIT)

    is_cancel = is_fight = is_refuse = False
    if combat:
        combatm = CombatM(combat, hero)
        is_cancel = combatm.is_cancel()
        is_refuse = combatm.is_refuse()
        is_fight = combatm.is_fight()

    if request.method == 'POST' and not combat:
        form = DuelForm(request.POST)
        if form.is_valid():
            combat = Combat(type=Combat.TYPE_DUEL,
                            time_out=form.cleaned_data['time_out'],
                            injury=form.cleaned_data['injury'],
                            with_things=form.cleaned_data['with_things'],
                            location=herom.get_location(),
                            one_team_count=1,
                            two_team_count=1)
            combat.save()
            combat.combatwarrior_set.create(hero=hero)
            #
            messages.add_message(request, messages.SUCCESS,
                                 'Your demand accept.')
            return HttpResponseRedirect(reverse('combat_duel'))
    else:
        form = DuelForm()

    combats = Combat.objects.filter(type=Combat.TYPE_DUEL,
                                    is_active=Combat.IS_ACTIVE_WAIT,
                                    location=herom.get_location())

    variables = RequestContext(
        request, {
            'form': form,
            'combats': combats,
            'in_combat': combat,
            'is_cancel': is_cancel,
            'is_fight': is_fight,
            'is_refuse': is_refuse
        })

    return render_to_response(template_name, variables)
Пример #11
0
def duel(request, template_name='combat/duel.html'):

    hero = request.hero
    herom = HeroM(hero)
    combat = herom.get_combat(Combat.IS_ACTIVE_WAIT)

    is_cancel = is_fight = is_refuse = False
    if combat:
        combatm = CombatM(combat, hero)
        is_cancel = combatm.is_cancel()
        is_refuse = combatm.is_refuse()
        is_fight = combatm.is_fight()

    if request.method == 'POST' and not combat:
        form = DuelForm(request.POST)
        if form.is_valid():
            combat = Combat(type=Combat.TYPE_DUEL,
                            time_out=form.cleaned_data['time_out'],
                            injury=form.cleaned_data['injury'],
                            with_things=form.cleaned_data['with_things'],
                            location=herom.get_location(),
                            one_team_count=1, two_team_count=1)
            combat.save()
            combat.combatwarrior_set.create(hero=hero)
#
            messages.add_message(request, messages.SUCCESS,
                                 'Your demand accept.')
            return HttpResponseRedirect(reverse('combat_duel'))
    else:
        form = DuelForm()

    combats = Combat.objects.filter(type=Combat.TYPE_DUEL,
                                    is_active=Combat.IS_ACTIVE_WAIT,
                                    location=herom.get_location())

    variables = RequestContext(request, {'form': form,
                                         'combats': combats,
                                         'in_combat': combat,
                                         'is_cancel': is_cancel,
                                         'is_fight': is_fight,
                                         'is_refuse': is_refuse})

    return render_to_response(template_name, variables)
Пример #12
0
def refuse(request):

    hero = request.hero
    combat = HeroM(hero).get_combat(Combat.IS_ACTIVE_WAIT)
    if not combat:
        return HttpResponseRedirect(reverse(settings.URL_REVERSE_404))

    is_refuse = CombatM(combat, hero).is_refuse()

    if is_refuse:
        combat.combatwarrior_set.get(team=Combat.TEAM_SECOND).delete()
        #
        messages.add_message(request, messages.SUCCESS, 'Demand refuse.')
    return HttpResponseRedirect(reverse('combat_duel'))
Пример #13
0
def cancel(request):

    hero = request.hero
    combat = HeroM(hero).get_combat(Combat.IS_ACTIVE_WAIT)
    if not combat:
        return HttpResponseRedirect(reverse(settings.URL_REVERSE_404))

    is_cancel = CombatM(combat, hero).is_cancel()

    if is_cancel:
        combat.delete()
        #
        messages.add_message(request, messages.SUCCESS, 'Your demand cancel.')

    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
Пример #14
0
def group(request, template_name='combat/group.html'):

    hero = request.hero
    herom = HeroM(hero)
    combat = herom.get_combat(Combat.IS_ACTIVE_WAIT)

    combatm = CombatM(combat, hero)

    combatm.update_combats(Combat.TYPE_GROUP)
    if combat and combatm.is_active():
        return HttpResponseRedirect(reverse('combat'))

    is_cancel = False
    if combat:
        is_cancel = combatm.is_cancel()

    if request.method == 'POST' and not combat:
        form = GroupForm(request.POST)
        if form.is_valid():
            combat = Combat(
                type=Combat.TYPE_GROUP,
                time_out=form.cleaned_data['time_out'],
                injury=form.cleaned_data['injury'],
                with_things=form.cleaned_data['with_things'],
                time_wait=form.cleaned_data['time_wait'],
                location=herom.get_location(),
                one_team_count=form.cleaned_data['one_team_count'],
                two_team_count=form.cleaned_data['two_team_count'],
                one_team_lvl_min=form.cleaned_data['one_team_lvl_min'],
                one_team_lvl_max=form.cleaned_data['one_team_lvl_max'],
                two_team_lvl_min=form.cleaned_data['two_team_lvl_min'],
                two_team_lvl_max=form.cleaned_data['two_team_lvl_max'])

            combat.save()
            combat.combatwarrior_set.create(hero=hero)
            #
            messages.add_message(request, messages.SUCCESS,
                                 'Your demand create.')
            return HttpResponseRedirect(reverse('combat_group'))
    else:
        form = GroupForm()

    combats = Combat.objects.filter(type=Combat.TYPE_GROUP,
                                    is_active=Combat.IS_ACTIVE_WAIT,
                                    location=herom.get_location())

    variables = RequestContext(
        request, {
            'form': form,
            'combats': combats,
            'in_combat': combat,
            'is_cancel': is_cancel
        })

    return render_to_response(template_name, variables)
Пример #15
0
def current(request, template_name='combat/current.html'):

    hero = request.hero
    herom = HeroM(hero)

    combat = herom.get_combat(Combat.IS_ACTIVE_WAIT)
    is_cancel = False
    if combat:
        is_cancel = CombatM(combat, hero).is_cancel()

    combats = Combat.objects.filter(is_active=Combat.IS_ACTIVE_FIGHT,
                                    location=herom.get_location())

    variables = RequestContext(request, {
        'combats': combats,
        'is_cancel': is_cancel
    })

    return render_to_response(template_name, variables)
Пример #16
0
def enter(request, combat_id, team):
    hero = request.hero

    combat = HeroM(hero).get_combat(Combat.IS_ACTIVE_WAIT)

    if combat:
        return HttpResponseRedirect(reverse(settings.URL_REVERSE_404))

    try:
        combat = Combat.objects.filter(id=combat_id).get()
    except Combat.DoesNotExist:
        return HttpResponseRedirect(reverse(settings.URL_REVERSE_404))

    if combat.is_active != Combat.IS_ACTIVE_FIGHT:
        #
        messages.add_message(request, messages.ERROR, 'Fight is end.')
        return HttpResponseRedirect(reverse('combat_territorial'))

    combat.combatwarrior_set.create(hero=hero, team=team, is_join=True)
    CombatM(combat, hero).write_log_message(is_join=True, hero=hero)

    return HttpResponseRedirect(reverse('combat'))
Пример #17
0
def bot_attack(request, bot_id):
    hero = request.hero
    herom = HeroM(hero)
    hero_position = herom.get_position_on_island()

    try:
        bot = Bot.objects.get(id=bot_id)
    except Bot.DoesNotExist:
        return HttpResponseRedirect(reverse(settings.URL_REVERSE_404))

    if bot.current_coordinate_x != hero_position[0] or \
        bot.current_coordinate_y != hero_position[1]:
        #
        messages.add_message(request, messages.ERROR, 'Bot go away.')
        return HttpResponseRedirect(reverse('island'))

    if herom.get_combat():
        #
        messages.add_message(request, messages.ERROR, 'Take away your demand.')
        return HttpResponseRedirect(reverse('island'))

    if not bot.in_combat:
        combat = Combat(type=Combat.TYPE_TERRITORIAL,
                        is_active=Combat.IS_ACTIVE_FIGHT,
                        location=herom.get_location())
        combat.save()
        combat.combatwarrior_set.create(hero=hero, team=Combat.TEAM_FIRST)
        combat.combatwarrior_set.create(bot=bot, team=Combat.TEAM_SECOND)

        bot.in_combat = True
        bot.save()

        CombatM(combat, hero).write_log_message(combat, True)
        return HttpResponseRedirect(reverse('combat'))
    else:
        #
        messages.add_message(request, messages.ERROR, 'Bot in combat.')
        return HttpResponseRedirect(reverse('island'))
Пример #18
0
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)
Пример #19
0
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)