def do_army_creation(self):
     army_position = ArmyPosition.get_by_id(1)
     army = Army(army_position)
     army.name = self.request.POST['army_name']
     player_game = PlayerGame.get_by_name('Me')
     if player_game.armies == None:
         player_game.armies = [army]
     else:
         player_game.armies.append(army)
     army.save(explicit=True)
     url = self.request.route_url('army_edition', id=army.id)
     return HTTPFound(location=url)
 def do_army_selection(self, army=None, special_message=''):
     army_id = self.request.POST['chosen_army']
     army = Army.get_by_id(army_id)
     if army.current_action != None:
         url = self.request.route_url('save_action', id=army.current_action.id)
     else:
         url = self.request.route_url('army_edition', id=army.id)
     return HTTPFound(location=url)
 def initiate_save_action(self):
     army_id = self.request.POST['chosen_army']
     army = Army.get_by_id(army_id)
     from pprint import pprint
     pprint(army.current_action)
     if army.current_action != None:
         raise RuntimeError('There is already a current action')
     action = SaveAction(army.owner.game, army, self.request.POST['damage'])
     action.save(explicit=True)
     url = self.request.route_url('save_action', id=action.id)
     return HTTPFound(location=url)
    def do_army_edition(self, army=None, special_message=''):
        army = Army.get_by_id(self.request.matchdict.get('id'))

        if 'unit_to_remove[]' in self.request.POST:
            for dice_id in self.request.POST.getall('unit_to_remove[]'):
                Dice.get_by_id(int(dice_id)).delete()
        #Ugly, but how to do otherwise ?
        regexp = re.compile('unit_amount_([1-9][0-9]{0,2})')
        for element, amount in self.request.POST.items():
            m = regexp.search(element)
            if ((m != None) and (int(amount) > 0)):
                for i in range(int(amount)):
                    dice = Dice(DiceTemplate.get_by_id(m.group(1)), army)
                    dice.save()

        return self.army_edition(army, ('army %s succesfully modified !' % army.name))
    def army_edition(self, army=None, special_message=''):
        if (army == None):
            army_id = self.request.matchdict.get('id', 0)
            army = Army.get_by_id(army_id)
        
        dice_list = [{'id': dice.id, 'name': dice.name, 'picture': dice.template.picture} for dice in army.components]

        template_list = []
        for race in Race.get_all():
            if race.break_by_color:
                for color in Element.get_all():
                    sub_race = []
                    for dice in race.dices:
                        if color in dice.elements:
                            sub_race.append(dice)
                    if sub_race != []:
                        template_list.append((color.name+' '+race.name, color.element_short_name+' '+race.tag, race.color, [{'id': dice.id, 'name': dice.name, 'picture': dice.picture} for dice in sub_race]))
            else:
                template_list.append((race.name, race.tag, race.color, [{'id': dice_template.id, 'name': dice_template.name, 'picture': dice_template.picture} for dice_template in race.dices]))

        return {'special_message': special_message, 'races': template_list, 'dices': dice_list, 'army_id': army.id}
            {'name': 'Swallow', 'amount': 4},
            {'name': 'Swallow', 'amount': 4},
            {'name': 'Maneuver', 'amount': 4},
            {'name': 'Maneuver', 'amount': 4},
            {'name': 'Save', 'amount': 4},
            {'name': 'Save', 'amount': 4},
            {'name': 'Save', 'amount': 4},
            {'name': 'Melee', 'amount': 4},
            {'name': 'Melee', 'amount': 4}
        ]))

dice8 = dice_template.get_instance()
dice9 = dice_template.get_instance()
dice10 = dice_template.get_instance()

army1 = Army()
army1.add(dice1)
army1.add(dice2)
army1.add(dice3)
army1.add(dice4)

army2 = Army()
army1.add(dice5)
army1.add(dice6)
army1.add(dice7)

army1.add(dice8)
army1.add(dice9)
army1.add(dice10)

#march = March(army1, [army2])
    def save_missile_edition(self):
        army = Army.get_by_id(self.request.matchdict.get('id'))

        results, instants, specials = self.roll(army, SaveMissileRoll())

        return {'army_id': army.id, 'results': results, 'instants': instants, 'specials': specials, 'sums': [('save',army.get_save_result())]}
    def maneuver_roll(self):
        army = Army.get_by_id(self.request.matchdict.get('id'))

        results, instants, specials = self.roll(army, ManeuverRoll())

        return {'army_id': army.id, 'results': results, 'instants': instants, 'specials': specials, 'sums': [('maneuver',army.get_maneuver_result())]}
    def dragon_roll(self):
        army = Army.get_by_id(self.request.matchdict.get('id'))

        results, instants, specials = self.roll(army, DragonRoll())

        return {'army_id': army.id, 'results': results, 'instants': instants, 'specials': specials, 'sums': [('melee',army.get_melee_result()), ('missile',army.get_missile_result()), ('save', army.get_save_result())]}