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 island(request, template_name='island/island.html'): hero = request.hero herom = HeroM(hero) island = herom.get_island() islandm = IslandM(island) if not islandm.is_near_island(hero.location): return HttpResponseRedirect(reverse(settings.URL_REVERSE_404)) BuildingM(None, hero).remove_from_location() hero_position = herom.get_position_on_island() hero_time_left = islandm.get_time_left_to_move(hero_position) x, y = hero_position[0], hero_position[1] buildings = Building.objects.filter(island=island, coordinate_x1__lte=x, coordinate_y1__lte=y, coordinate_x2__gte=x, coordinate_y2__gte=y) bots = Bot.objects.filter(island=island, current_coordinate_x=x, current_coordinate_y=y, in_combat=False) variables = RequestContext(request, {'island': island, 'buildings': buildings, 'bots': bots, 'hero_position_x': hero_position[0], 'hero_position_y': hero_position[1], 'hero_time_left': hero_time_left}) return render_to_response(template_name, variables)
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)
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)
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)
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)
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'))
def buy(request, slug, shopthing_id): hero = request.hero try: building = Building.objects.get(slug=slug) shopthing = building.buildingshopthing_set.get(id=shopthing_id) except (Building.DoesNotExist, BuildingShopThing.DoesNotExist): return HttpResponseRedirect(reverse(settings.URL_REVERSE_404)) if shopthing.price > hero.money: # messages.add_message(request, messages.ERROR, 'You have not enough money.') elif not shopthing.count: # messages.add_message(request, messages.ERROR, 'Thing are not available.') else: HeroThing.objects.create(hero=hero, thing=shopthing.thing, stability_all=shopthing.thing.stability, stability_left=shopthing.thing.stability) shopthing.count -= 1 shopthing.save() hero.money -= shopthing.price hero.save() HeroM(hero).update_capacity() # messages.add_message(request, messages.SUCCESS, 'You buy thing.') return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
def update_bots_in_combats(self, coordinate_x, coordinate_y): bots = Bot.objects.filter(coordinate_x1__lte=coordinate_x, coordinate_y1__lte=coordinate_y, coordinate_x2__gte=coordinate_x, coordinate_y2__gte=coordinate_y, in_combat=True) for bot in bots: self.combat = Combat.objects.filter(combatwarrior__bot=bot, combatwarrior__is_dead=False, is_active=Combat.IS_ACTIVE_FIGHT). \ get() team = self.combat.combatwarrior_set.get(bot=bot).team if self.is_timeout(team, True): dead_warriors = [] for combatwarrior in self.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 }) self.after_death(dead_warriors) self.free_bots() # End bots
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 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)
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)
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'))
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)
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)
def throw(request, herothing_id): hero = request.hero try: hero.herothing_set.get(id=herothing_id).delete() except HeroThing.DoesNotExist: return HttpResponseRedirect(reverse(settings.URL_REVERSE_404)) HeroM(hero).update_capacity() # messages.add_message(request, messages.SUCCESS, 'Your thing thrown.') return HttpResponseRedirect(reverse('hero_inventory'))
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'))
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 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'))
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'))
def island(request, template_name='island/island.html'): hero = request.hero herom = HeroM(hero) island = herom.get_island() islandm = IslandM(island) if not islandm.is_near_island(hero.location): return HttpResponseRedirect(reverse(settings.URL_REVERSE_404)) BuildingM(None, hero).remove_from_location() hero_position = herom.get_position_on_island() hero_time_left = islandm.get_time_left_to_move(hero_position) x, y = hero_position[0], hero_position[1] buildings = Building.objects.filter(island=island, coordinate_x1__lte=x, coordinate_y1__lte=y, coordinate_x2__gte=x, coordinate_y2__gte=y) bots = Bot.objects.filter(island=island, current_coordinate_x=x, current_coordinate_y=y, in_combat=False) variables = RequestContext( request, { 'island': island, 'buildings': buildings, 'bots': bots, 'hero_position_x': hero_position[0], 'hero_position_y': hero_position[1], 'hero_time_left': hero_time_left }) return render_to_response(template_name, variables)
def is_near_building(self, slug): slugs = [ i.split(':')[1] for i in self.hero.location.split('&')[1:] ] herom = HeroM(self.hero) x, y, time = herom.get_position_on_island() island = herom.get_island() if not len(slugs): if self.building.coordinate_x1 <= x and \ self.building.coordinate_x2 >= x and \ self.building.coordinate_y1 <= y and \ self.building.coordinate_y2 >= y and \ island == self.building.island: return True else: if (len(slugs) >= 2 and slug == slugs[-2]) or \ (self.building.parent and \ self.building.parent.slug == slugs[-1]) or \ slug == slugs[-1] or \ (self.building.parent and \ self.building.parent.default_child == True): return True return False
def is_near_building(self, slug): slugs = [i.split(':')[1] for i in self.hero.location.split('&')[1:]] herom = HeroM(self.hero) x, y, time = herom.get_position_on_island() island = herom.get_island() if not len(slugs): if self.building.coordinate_x1 <= x and \ self.building.coordinate_x2 >= x and \ self.building.coordinate_y1 <= y and \ self.building.coordinate_y2 >= y and \ island == self.building.island: return True else: if (len(slugs) >= 2 and slug == slugs[-2]) or \ (self.building.parent and \ self.building.parent.slug == slugs[-1]) or \ slug == slugs[-1] or \ (self.building.parent and \ self.building.parent.default_child == True): return True return False
def undress(request, herothing_id): hero = request.hero try: herothing = hero.herothing_set.get(id=herothing_id) except HeroThing.DoesNotExist: return HttpResponseRedirect(reverse(settings.URL_REVERSE_404)) herothing.dressed = False herothing.save() HeroM(hero).update_feature() # messages.add_message(request, messages.SUCCESS, 'Thing undressed.') return HttpResponseRedirect(reverse('hero_inventory'))
def accept(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_WAIT: # messages.add_message(request, messages.ERROR, 'Fight is begin.') return HttpResponseRedirect(request.META.get('HTTP_REFERER')) if int(team) == Combat.TEAM_FIRST: team_count = combat.one_team_count team_lvl_min = combat.one_team_lvl_min team_lvl_max = combat.one_team_lvl_max else: team_count = combat.two_team_count team_lvl_min = combat.two_team_lvl_min team_lvl_max = combat.two_team_lvl_max team_count_now = combat.combatwarrior_set.filter(team=team).count() if (hero.level >= team_lvl_min and hero.level <= team_lvl_max) or \ combat.type == Combat.TYPE_DUEL: if team_count_now < team_count: combat.combatwarrior_set.create(hero=hero, team=team) # messages.add_message(request, messages.SUCCESS, 'Demand accept.') return HttpResponseRedirect(request.META.get('HTTP_REFERER')) else: # messages.add_message(request, messages.SUCCESS, 'Demand accept until you.') return HttpResponseRedirect(request.META.get('HTTP_REFERER')) else: return HttpResponseRedirect(reverse(settings.URL_REVERSE_404))
def take_select(request, slug, commissionherothing_id): hero = request.hero try: building = Building.objects.get(slug=slug) commissionherothing = building.buildingcommissionherothing_set. \ get(id=commissionherothing_id) except (Building.DoesNotExist, BuildingCommissionHeroThing.DoesNotExist): return HttpResponseRedirect(reverse(settings.URL_REVERSE_404)) commissionherothing.herothing.away = False commissionherothing.herothing.save() commissionherothing.delete() HeroM(hero).update_capacity() # messages.add_message(request, messages.SUCCESS, 'You take thing.') return HttpResponseRedirect(reverse('commission_take', args=[slug])) # End take
def update_bots_timeout(self): try: combatwarrior = self.combat.combatwarrior_set. \ filter(is_dead=False).exclude(bot=None)[0:1].get() team = combatwarrior.team if self.is_timeout(team, True): dead_warriors = [] for combatwarrior in self.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 }) self.after_death(dead_warriors) return True except CombatWarrior.DoesNotExist: return False
def sell(request, slug, herothing_id): hero = request.hero try: building = Building.objects.get(slug=slug) herothing = hero.herothing_set.get(id=herothing_id, dressed=False, away=False) except (Building.DoesNotExist, HeroThing.DoesNotExist): return HttpResponseRedirect(reverse(settings.URL_REVERSE_404)) percent = building.buildingbuyingup_set.get(building=building).percent hero.money += herothing.thing.price * (percent / 100) hero.save() herothing.delete() HeroM(hero).update_capacity() # messages.add_message(request, messages.SUCCESS, 'You sell thing.') return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
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'))
def put_select(request, slug, herothing_id, template_name='building/module/commission/put_select.html'): hero = request.hero try: building = Building.objects.get(slug=slug) herothing = hero.herothing_set.get(id=herothing_id, dressed=False, away=False) except (Building.DoesNotExist, HeroThing.DoesNotExist): return HttpResponseRedirect(reverse(settings.URL_REVERSE_404)) if request.method == 'POST': form = PutForm(request.POST) if form.is_valid(): herothing.away = True herothing.save() commissionherothing = building.buildingcommissionherothing_set. \ create(herothing=herothing, price=form.cleaned_data['price']) commissionherothing.save() HeroM(hero).update_capacity() # messages.add_message(request, messages.SUCCESS, 'You put thing.') return HttpResponseRedirect(reverse('commission_put', args=[slug])) else: form = PutForm() variables = RequestContext(request, { 'building': building, 'herothing': herothing, 'form': form }) return render_to_response(template_name, variables)
def buy(request, slug, commissionherothing_id): hero = request.hero try: building = Building.objects.get(slug=slug) commissionherothing = building.buildingcommissionherothing_set. \ get(id=commissionherothing_id) except (Building.DoesNotExist, BuildingCommissionHeroThing.DoesNotExist): return HttpResponseRedirect(reverse(settings.URL_REVERSE_404)) price = commissionherothing.price if hero.money < price: # messages.add_message(request, messages.ERROR, 'You have not enough money.') else: percent = building.buildingcommission_set.get().percent hero.money -= price hero.save() commissionherothing.herothing.hero.money += price - \ round((price * (percent / 100))) commissionherothing.herothing.hero.save() commissionherothing.herothing.away = False commissionherothing.herothing.hero = hero commissionherothing.herothing.save() commissionherothing.delete() HeroM(hero).update_capacity() # messages.add_message(request, messages.SUCCESS, 'You buy thing.') return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
def increase(request, type, what): hero = request.hero if type == 'abilities' and hero.number_of_abilities > 0: hero.number_of_abilities -= 1 if what == 'swords': hero.swords += 1 elif what == 'axes': hero.axes += 1 elif what == 'knives': hero.knives += 1 elif what == 'clubs': hero.clubs += 1 elif what == 'shields': hero.shields += 1 if type == 'parameters' and hero.number_of_parameters > 0: hero.number_of_parameters -= 1 if what == 'strength': hero.strength += 1 elif what == 'dexterity': hero.dexterity += 1 elif what == 'intuition': hero.intuition += 1 elif what == 'health': hero.health += 1 if type == 'skills' and hero.number_of_skills > 0: try: heroskill = HeroSkill.objects.get(id=int(what)) except HeroSkill.DoesNotExist: return HttpResponseRedirect(reverse(settings.URL_REVERSE_404)) hero.number_of_skills -= 1 try: heroskill = hero.heroheroskill_set.get(skill=heroskill) heroskill.level += 1 heroskill.save() except HeroHeroSkill.DoesNotExist: HeroHeroSkill.objects.create(hero=hero, skill=heroskill, level=1) hero.save() HeroM(hero).update_feature() return HttpResponseRedirect(reverse('hero'))
def dress(request, herothing_id): hero = request.hero try: herothing = hero.herothing_set.get(id=herothing_id) except HeroThing.DoesNotExist: return HttpResponseRedirect(reverse(settings.URL_REVERSE_404)) if not ThingM(herothing.thing, hero).is_available_to_dress(): return HttpResponseRedirect(reverse(settings.URL_REVERSE_404)) herothing.dressed = True herothing.save() type = herothing.thing.type take_two_hands = herothing.thing.take_two_hands if type == Thing.TYPE_HELMET or type == Thing.TYPE_KOLCHUGA or \ type == Thing.TYPE_ARMOR or type == Thing.TYPE_BELT or \ type == Thing.TYPE_PANTS or type == Thing.TYPE_TREETOP or \ type == Thing.TYPE_GLOVE or type == Thing.TYPE_BOOT or \ type == Thing.TYPE_AMULET: try: herothing = hero.herothing_set.filter(dressed=True, thing__type=type). \ exclude(id=herothing_id).get() herothing.dressed = False herothing.save() except HeroThing.DoesNotExist: pass elif type == Thing.TYPE_RING: herothings = hero.herothing_set.filter(dressed=True, thing__type=type). \ exclude(id=herothing_id) if len(herothings) == settings.THINGS_COUNT_OF_RINGS: herothings[0].dressed = False herothings[0].save() elif type == Thing.TYPE_SWORD or type == Thing.TYPE_AXE or \ type == Thing.TYPE_KNIVE or type == Thing.TYPE_CLUBS or \ type == Thing.TYPE_SHIELD: herothings = hero.herothing_set.filter( Q(thing__type=Thing.TYPE_SWORD) | Q(thing__type=Thing.TYPE_AXE) | Q(thing__type=Thing.TYPE_KNIVE) | Q(thing__type=Thing.TYPE_CLUBS) | Q(thing__type=Thing.TYPE_SHIELD), dressed=True).\ exclude(id=herothing_id) if len(herothings): if len(herothings) == settings.THINGS_COUNT_OF_ARMS or \ herothings[0].thing.take_two_hands or take_two_hands: if take_two_hands: for herothing in herothings: herothing.dressed = False herothing.save() else: herothings[0].dressed = False herothings[0].save() HeroM(hero).update_feature() # messages.add_message(request, messages.SUCCESS, 'Thing dressed.') return HttpResponseRedirect(reverse('hero_inventory'))
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 write_log_strikes(self, team, hero_two, bot, strikes, blocks): combatlog = self.__get_log(team, hero_two, bot) if combatlog is None: strikes_s = '|'.join(strikes) blocks_s = '|'.join(blocks) if team == Combat.TEAM_FIRST: tcombatlog = self.combat.combatlog_set.create( hero_one=self.hero, hero_two=hero_two, bot_two=bot, is_past=False, warrior_one_wstrike=strikes_s, warrior_one_wblock=blocks_s) else: tcombatlog = self.combat.combatlog_set.create( hero_one=hero_two, bot_one=bot, hero_two=self.hero, is_past=False, warrior_two_wstrike=strikes_s, warrior_two_wblock=blocks_s) if bot: combatlog = tcombatlog if combatlog: if hero_two: if team == Combat.TEAM_FIRST: strikes_two = combatlog.warrior_two_wstrike.split('|') blocks_two = combatlog.warrior_two_wblock.split('|') else: strikes_two = combatlog.warrior_one_wstrike.split('|') blocks_two = combatlog.warrior_one_wblock.split('|') warrior_two = hero_two else: strikes_two = [ str(random.randint(0, 3)) for i in range(int(bot.feature.strike_count)) ] count_blocks = int(bot.feature.block_count) blocks_two = [] i = 0 while (True): block = str(random.randint(0, 3)) if block not in blocks_two: blocks_two.append(block) i += 1 if count_blocks == i: break warrior_two = bot warrior = self.hero accuracy_p = settings.COMBAT_RANGE_ACCURACY devastate_p = settings.COMBAT_RANGE_DEVASTATE block_break_p = settings.COMBAT_RANGE_BLOCK_BREAK armor_break_p = settings.COMBAT_RANGE_ARMOR_BREAK damage_p = settings.COMBAT_RANGE_DAMAGE dead_warriors = [] combatlog.text = '' for j in range(2): if j == 1: strikes = strikes_two blocks_two = blocks team = int(not team) combatlog.text = combatlog.text[0:-1] + ':' warrior, warrior_two = warrior_two, warrior coefficient = TableExperience.objects. \ get(level=warrior_two.level).coefficient damage_bamp = 0 i = 0 for strike in strikes: accuracy = devastate = block = block_break = \ armor_break = is_block = False if random.randint(0, accuracy_p) not in \ range(int(warrior.feature.accuracy) - int(warrior_two.feature.dodge)): accuracy = True if random.randint(0, devastate_p) in \ range(int(warrior.feature.devastate) - int(warrior_two.feature.durability)): devastate = True if strike in blocks_two: block = True is_block = True if random.randint(0, block_break_p) in \ range(int(warrior.feature.block_break)): block_break = True is_block = False if random.randint(0, armor_break_p) in \ range(int(warrior.feature.armor_break)): armor_break = True damage = random.randint(int(warrior.feature.damage_min), int(warrior.feature.damage_max)) if strike == 1: protection = int(warrior.feature.protection_breast) elif strike == 2: protection = int(warrior.feature.protection_zone) elif strike == 3: protection = int(warrior.feature.protection_legs) else: protection = int(warrior.feature.protection_head) if not armor_break: strike_damage = int(damage - damage * (protection / damage_p / 100.0)) else: strike_damage = damage if devastate: strike_damage *= 2 if is_block == True or not accuracy: strike_damage = 0 if strike_damage > 0: current_hp = int(float(warrior_two.feature.hp. \ split('|')[0])) if current_hp != 0: current_hp -= strike_damage if int(current_hp) <= 0: current_hp = 0 if type(warrior_two) == Hero: combatwarrior = self.combat. \ combatwarrior_set.get(hero=warrior_two) else: combatwarrior = self.combat. \ combatwarrior_set.get(bot=warrior_two) combatwarrior.is_dead = True combatwarrior.save() dead_warriors.append({ 'warrior': warrior_two, 'team': combatwarrior.team }) if type(warrior_two) == Hero: HeroM(warrior_two).set_hp(current_hp) else: warrior_two.feature.hp = '%s|%s' % ( current_hp, warrior_two.feature.hp.split('|')[1]) warrior_two.feature.save() strikes[i] = str(strike) + '_' + str(strike_damage) + \ '_' + str(int(block)) + '_' + \ str(int(block_break)) + '_' + \ str(int(not accuracy)) + '_' + \ str(int(devastate)) + '_' + \ str(int(armor_break)) damage_bamp += strike_damage combatlog.text += '[warrior_one]' + str(warrior) + \ '[/warrior_one][warrior_two]' + \ str(warrior_two) + \ '[/warrior_two][strikes]' + \ strikes[i] + '[/strikes][blocks]' + \ '|'.join(blocks_two) + '[/blocks]&' i += 1 strikes_s = '|'.join(strikes) blocks_s = '|'.join(blocks_two) if team == Combat.TEAM_FIRST: combatlog.warrior_one_wstrike = strikes_s combatlog.warrior_two_wblock = blocks_s combatlog.warrior_one_damage = damage_bamp combatlog.warrior_one_experience = \ int(damage_bamp * coefficient) else: combatlog.warrior_two_wstrike = strikes_s combatlog.warrior_one_wblock = blocks_s combatlog.warrior_two_damage = damage_bamp combatlog.warrior_two_experience = \ int(damage_bamp * coefficient) combatlog.is_past = True combatlog.text = combatlog.text[0:-1] combatlog.save() self.after_death(dead_warriors)
def update_feature(self): from hero.manipulation import HeroM herom = HeroM(self) herom.update_feature() herom.level_up()
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)