Ejemplo n.º 1
0
    def setUp(self):

        self.state = State()
        self.paris = InlandTerritory(1, 'Paris', 'France', [7])
        self.london = CoastalTerritory(2, 'London', 'England', [3, 6], [3])
        self.wales = CoastalTerritory(3, 'Wales', 'England', [2, 6], [2])
        self.english_channel = SeaTerritory(6, 'English Channel', [2, 3])
        self.brest = CoastalTerritory(7, 'Brest', 'France', [1, 6], [])
        self.rome = CoastalTerritory(8, 'Rome', 'Italy', [9], [])
        self.apulia = CoastalTerritory(9, 'Apulia', 'Italy', [8], [])

        self.spain = CoastalTerritory(10, 'Spain', None, [11, 12, 13, 14],
                                      [11, 12])
        self.gascony = CoastalTerritory(11, 'Gascony', 'France', [10], [])
        self.marseilles = CoastalTerritory(12, 'Marseilles', 'France', [10],
                                           [10])
        self.mid_atlantic = SeaTerritory(13, 'Mid Atlantic', [10])
        self.gulf_of_lyon = SeaTerritory(14, 'Gulf of Lyon', [10])
        self.spain_north_coast = NamedCoast(1, 'North Coast', self.spain,
                                            [self.gascony, self.mid_atlantic])
        self.spain_south_coast = NamedCoast(
            2, 'South Coast', self.spain,
            [self.marseilles, self.gulf_of_lyon, self.marseilles])

        to_register = [
            self.paris, self.london, self.wales, self.english_channel,
            self.brest, self.rome, self.apulia, self.spain, self.gascony,
            self.marseilles, self.mid_atlantic, self.gulf_of_lyon,
            self.spain_north_coast, self.spain_south_coast
        ]
        [self.state.register(o) for o in to_register]
Ejemplo n.º 2
0
 def create(self, validated_data):
     state = self.context['state']
     territory_id = validated_data.get('parent')
     if territory_id:
         validated_data['parent'] = state.get_territory_by_id(territory_id)
     instance = NamedCoast(state, **validated_data)
     return instance
Ejemplo n.º 3
0
    def test_is_complex(self):
        london = CoastalTerritory(self.state, 1, 'London', 'England', [], [])
        spain = CoastalTerritory(self.state, 2, 'Spain', None, [], [])
        NamedCoast(self.state, 1, 'North Coast', spain, [])

        self.assertTrue(spain.is_complex)
        self.assertFalse(london.is_complex)
Ejemplo n.º 4
0
    def test_named_coast(self):
        london = CoastalTerritory(self.state, 1, 'London', 'England', [], [])
        spain = CoastalTerritory(self.state, 2, 'Spain', None, [], [])
        spain_north_coast = NamedCoast(self.state, 1, 'North Coast', spain, [])

        self.assertEqual(spain.named_coasts, [spain_north_coast])
        self.assertEqual(london.named_coasts, [])
Ejemplo n.º 5
0
    def test_is_complex(self):
        london = CoastalTerritory(1, 'London', 'England', [], [])
        spain = CoastalTerritory(2, 'Spain', None, [], [])
        spain_north_coast = NamedCoast(1, 'North Coast', spain, [])
        self.state.register(london, spain, spain_north_coast)

        self.assertTrue(spain.is_complex)
        self.assertFalse(london.is_complex)
Ejemplo n.º 6
0
    def test_named_coast(self):
        london = CoastalTerritory(1, 'London', 'England', [], [])
        spain = CoastalTerritory(2, 'Spain', None, [], [])
        spain_north_coast = NamedCoast(1, 'North Coast', spain, [])
        self.state.register(london, spain, spain_north_coast)

        self.assertEqual(spain.named_coasts, {spain_north_coast})
        self.assertEqual(london.named_coasts, set())
Ejemplo n.º 7
0
    def setUp(self):
        self.state = State()
        self.portugal = CoastalTerritory(11, 'Portugal', None, [], [])
        self.spain = CoastalTerritory(10, 'Spain', None, [], [])
        self.spain_south_coast = NamedCoast(2, 'South Coast', self.spain, [])

        to_register = [self.spain, self.spain_south_coast]
        [self.state.register(o) for o in to_register]
Ejemplo n.º 8
0
    def test_register_named_coast_updates_territory(self):
        state = State()
        spain = CoastalTerritory(1, 'spain', None, [], [])
        spain_nc = NamedCoast(1, 'spain sc', spain, [])

        state.register(spain)
        state.register(spain_nc)

        self.assertTrue(spain_nc in spain.named_coasts)
Ejemplo n.º 9
0
 def __init__(self, state, territories):
     self.SPAIN_SC = NamedCoast(state, 1, 'spain sc', territories.SPAIN, [
         territories.MARSEILLES.id, territories.PORTUGAL.id,
         territories.MID_ATLANTIC.id, territories.WESTERN_MEDITERRANEAN.id,
         territories.GULF_OF_LYON.id
     ])
     self.SPAIN_NC = NamedCoast(state, 2, 'spain nc', territories.SPAIN, [
         territories.PORTUGAL.id, territories.MID_ATLANTIC.id,
         territories.GASCONY.id
     ])
     self.BULGARIA_EC = NamedCoast(state, 3, 'bulgaria ec',
                                   territories.BULGARIA, [
                                       territories.BLACK_SEA.id,
                                       territories.RUMANIA.id,
                                       territories.CONSTANTINOPLE.id,
                                   ])
     self.BULGARIA_SC = NamedCoast(
         state, 4, 'bulgaria sc', territories.BULGARIA, [
             territories.CONSTANTINOPLE.id, territories.AEGEAN_SEA.id,
             territories.GREECE.id
         ])
     self.ST_PETERSBURG_NC = NamedCoast(
         state, 5, 'st petersburg nc', territories.ST_PETERSBURG,
         [territories.BARRENTS_SEA.id, territories.NORWAY.id])
     self.ST_PETERSBURG_SC = NamedCoast(
         state, 6, 'st petersburg nc', territories.ST_PETERSBURG, [
             territories.FINLAND.id, territories.LIVONIA.id,
             territories.GULF_OF_BOTHNIA.id
         ])
Ejemplo n.º 10
0
 def __init__(self, territories):
     self.SPAIN_SC = NamedCoast(1, 'spain sc', territories.SPAIN, [
         territories.MARSEILLES, territories.PORTUGAL,
         territories.MID_ATLANTIC, territories.WESTERN_MEDITERRANEAN,
         territories.GULF_OF_LYON
     ])
     self.SPAIN_NC = NamedCoast(2, 'spain nc', territories.SPAIN, [
         territories.PORTUGAL, territories.MID_ATLANTIC, territories.GASCONY
     ])
     self.BULGARIA_EC = NamedCoast(3, 'bulgaria ec', territories.BULGARIA, [
         territories.BLACK_SEA,
         territories.RUMANIA,
         territories.CONSTANTINOPLE,
     ])
     self.BULGARIA_SC = NamedCoast(4, 'bulgaria sc', territories.BULGARIA, [
         territories.CONSTANTINOPLE, territories.AEGEAN_SEA,
         territories.GREECE
     ])
     self.ST_PETERSBURG_NC = NamedCoast(
         5, 'st petersburg nc', territories.ST_PETERSBURG,
         [territories.BARRENTS_SEA, territories.NORWAY])
     self.ST_PETERSBURG_SC = NamedCoast(
         6, 'st petersburg nc', territories.ST_PETERSBURG, [
             territories.FINLAND, territories.LIVONIA,
             territories.GULF_OF_BOTHNIA
         ])
Ejemplo n.º 11
0
def data_to_state(data):
    state = State()
    data = data
    # instantiate and register territories
    for territory_data in data['territories']:
        type = territory_data.pop('type')
        territory_class = terrtitory_type_dict[type]
        if not type == 'coastal':
            territory_data.pop('shared_coast_ids')
        if type == 'sea':
            territory_data.pop('supply_center', None)
            territory_data.pop('nationality', None)
            territory_data.pop('controlled_by', None)
        territory = territory_class(**territory_data)
        state.register(territory)
    # instantiate and register named coasts
    for named_coast_data in data['named_coasts']:
        t_id = named_coast_data.pop('territory_id')
        named_coast_data['parent'] = [
            t for t in state.territories if t.id == t_id
        ][0]
        n_ids = named_coast_data.pop('neighbour_ids')
        named_coast_data['neighbours'] = [
            t for t in state.territories if t.id in n_ids
        ]
        named_coast = NamedCoast(**named_coast_data)
        state.register(named_coast)
    # instantiate and register pieces
    for piece_data in data['pieces']:
        t_id = piece_data.pop('territory_id')
        n_id = piece_data.pop('named_coast_id', None)
        type = piece_data.pop('type')
        piece_data['territory'] = [
            t for t in state.territories if t.id == t_id
        ][0]
        if type == 'fleet':
            named_coasts = [n for n in state.named_coasts if n.id == n_id]
            if named_coasts:
                piece_data['named_coast'] = named_coasts[0]
        piece_class = piece_type_dict[type]
        piece = piece_class(**piece_data)
        state.register(piece)
    # instantiate and register orders
    for order_data in data['orders']:
        type = order_data.pop('type')
        source_id = order_data.pop('source_id')
        order_data['source'] = [
            t for t in state.territories if t.id == source_id
        ][0]
        if order_data.get('target_id'):
            target_id = order_data.pop('target_id')
            order_data['target'] = [
                t for t in state.territories if t.id == target_id
            ][0]
        if order_data.get('target_coast_id'):
            target_coast_id = order_data.pop('target_coast_id')
            s = 'target_coast'
            if type == 'build':
                s = 'named_coast'
            order_data[s] = [
                n for n in state.named_coasts if n.id == target_coast_id
            ][0]
        if order_data.get('aux_id'):
            aux_id = order_data.pop('aux_id')
            order_data['aux'] = [
                t for t in state.territories if t.id == aux_id
            ][0]
        if not type == 'build':
            order_data.pop('piece_type')
        if not type == 'move':
            order_data.pop('via_convoy', None)
        order_class = order_type_dict[type]
        order = order_class(**order_data)
        state.register(order)
    # create an order for all pieces without orders
    for piece in state.pieces:
        if not piece.order:
            order_data = {
                '_id': 0,
                'nation': piece.nation,
                'source': piece.territory
            }
            hold = Hold(**order_data)
            state.register(hold)
    return state
Ejemplo n.º 12
0
def process_game_state(data):
    territory_map = {}
    named_coast_map = {}

    # Marshall data into expected format and validate
    validated_data = TurnSchema().load(data)

    season = validated_data['season']
    phase = validated_data['phase']
    year = validated_data['year']

    # Instantiate `State` for this turn
    state = State(season, phase, year)

    # Initialise territory instances and register each to state. Add to
    # territory map
    for territory_data in validated_data['territories']:
        territory_type = territory_data.pop('type')
        territory_class = territory_type_dict[territory_type]
        territory = territory_class(state, **territory_data)
        territory_map[territory_data['id']] = territory

    # Initialise named coasts - grab parent from territory map
    for named_coast_data in validated_data['named_coasts']:
        named_coast_data['parent'] = territory_map[named_coast_data['parent']]
        named_coast = NamedCoast(state, **named_coast_data)
        named_coast_map[named_coast.id] = named_coast

    # Initialise orders - grab source, target, aux, target_coast from maps
    for order_data in validated_data['orders']:
        order_type = order_data.pop('type')
        order_class = order_type_dict[order_type]
        for arg_name in ['source', 'target', 'aux']:
            territory_id = order_data[arg_name]
            if territory_id:
                order_data[arg_name] = territory_map[territory_id]
        target_coast_id = order_data['target_coast']
        if target_coast_id:
            order_data['target_coast'] = named_coast_map[target_coast_id]
        order_class(state, **order_data)

    # Initialise pieces - grab source, target, aux, target_coast from maps
    for piece_data in validated_data['pieces']:
        piece_type = piece_data.pop('type')
        piece_class = piece_type_dict[piece_type]
        for arg_name in ['territory', 'attacker_territory']:
            territory_id = piece_data[arg_name]
            if territory_id:
                piece_data[arg_name] = territory_map[territory_id]
        named_coast_id = piece_data['named_coast']
        if named_coast_id:
            piece_data['named_coast'] = named_coast_map[named_coast_id]
        piece_class(state, **piece_data)

    # Initialise nation instances and register each to state
    for nation_data in validated_data['nations']:
        Nation(state, **nation_data)

    # Process game state
    process(state)

    # Serialize processed game state and return
    processed_data = TurnSchema().dump(state)
    return processed_data