Exemplo n.º 1
0
 def test_as_json(self):
     '''Test planet.as_json() exporter'''
     planet = Planet()
     jdata = planet.as_json()
     print(jdata)
     p_data = json.loads(jdata)
     self.assertTrue(p_data['uwp'] == planet.uwp())
     self.assertTrue(p_data['trade_codes'] == planet.trade_codes)
     self.assertTrue(p_data['travel_code'] == planet.travel_code)
     self.assertTrue(p_data['bases'] == planet.bases)
     self.assertTrue(p_data['orbit'] == planet.orbit)
     self.assertTrue(p_data['is_mainworld'] == planet.is_mainworld)
Exemplo n.º 2
0
class System(object):
    '''Return a T5 basic system with the specified name and location hex'''

    naval_base_presence = Table()
    naval_base_presence.add_row('A', 6)
    naval_base_presence.add_row('B', 5)

    scout_base_presence = Table()
    scout_base_presence.add_row('A', 4)
    scout_base_presence.add_row('B', 5)
    scout_base_presence.add_row('C', 6)
    scout_base_presence.add_row('D', 7)

    mw_orbit_flux_table = Table()
    mw_orbit_flux_table.add_row(-6, -2)
    mw_orbit_flux_table.add_row((-5, -3), -1)
    mw_orbit_flux_table.add_row((-2, 2), 0)
    mw_orbit_flux_table.add_row((3, 5), 1)
    mw_orbit_flux_table.add_row(6, 2)

    def __init__(self, name='', location_hex='0000'):
        self.hex = location_hex
        self.name = name
        self.zone = ''
        self.stellar = Primary()
        self.mainworld = Planet()
        self.determine_mw_orbit()

        self.bases = self.determine_bases()
        self.pbg = Pbg(self.mainworld)
        self.allegiance = 'Na'
        self.determine_trade_codes()
        self.determine_x()
        self.nobility = self.determine_nobility()
        self.num_worlds = (self.pbg.belts + self.pbg.gasgiants + D6.roll(1) +
                           1)
        self.determine_travel_zone()

    def display(self):
        '''Display'''
        return '{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}'.format(
            self.hex, self.name, self.mainworld.uwp(),
            ' '.join(self.mainworld.trade_codes), str(self.importance_x),
            str(self.economic_x),
            str(self.cultural_x), self.nobility, self.bases, self.zone,
            str(self.pbg), self.num_worlds, self.allegiance,
            self.stellar.display())

    def __str__(self):
        oformat = '{0:4} {1:20} {2:9} {3:18} {4:4} {5:7} {6:6} ' +\
            '{7:7} {8:2} {9:1} {10:3} {11:2} {12:2} {13:14}'
        return oformat.format(self.hex, self.name, self.mainworld.uwp(),
                              ' '.join(self.mainworld.trade_codes),
                              str(self.importance_x), str(self.economic_x),
                              str(self.cultural_x.display()),
                              self.nobility, self.bases, self.zone,
                              str(self.pbg), self.num_worlds, self.allegiance,
                              self.stellar.display())

    def determine_nobility(self):
        '''Determine noble representation'''
        nobility = 'B'  # Every world gets a knight
        # Baronet
        if ('Pa' in self.mainworld.trade_codes
                or 'Pr' in self.mainworld.trade_codes):
            nobility += 'c'
        # Baron
        if ('Ag' in self.mainworld.trade_codes
                or 'Ri' in self.mainworld.trade_codes):
            nobility += 'C'
        if 'Pi' in self.mainworld.trade_codes:
            nobility += 'D'  # Marquis
        if 'Ph' in self.mainworld.trade_codes:
            nobility += 'e'  # Viscount
        # Count
        if ('In' in self.mainworld.trade_codes
                or 'Hi' in self.mainworld.trade_codes):
            nobility += 'E'
        if int(self.importance_x) >= 4:
            nobility += 'f'  # Duke
        return nobility

    def determine_bases(self):
        '''Determine bases'''
        bases = ''
        # Naval base
        target = self.naval_base_presence.lookup(self.mainworld.starport)
        if target is not None:
            if D6.roll(2) <= target:
                bases += 'N'
        # Scout base
        target = self.scout_base_presence.lookup(self.mainworld.starport)
        if target is not None:
            if D6.roll(2) <= target:
                bases += 'S'
        return bases

    def determine_trade_codes(self):
        '''Determine climate trade codes'''
        tcs = TradeCodes(self.mainworld, self)
        self.mainworld.trade_codes = tcs.generate()

    def determine_mw_orbit(self):
        '''Determine mainworld orbit'''
        orbit = self.stellar.habitable_zone +\
            self.mw_orbit_flux_table.lookup(FLUX.flux())
        orbit = max(orbit, 0)
        self.mainworld.orbit = orbit

    def determine_travel_zone(self, starport_x_is_red=True):
        '''Determine travel zone - A or R'''
        self.zone = ''
        if int(self.mainworld.government) + \
                int(self.mainworld.law_level) in [20, 21]:
            self.zone = 'A'
            self.mainworld.trade_codes.append('Da')
        elif int(self.mainworld.government) + \
                int(self.mainworld.law_level) > 22:
            self.zone = 'R'
        if starport_x_is_red:
            if self.mainworld.starport == 'X':
                self.zone = 'R'
                self.mainworld.trade_codes.append('Fo')

    def as_json(self):
        '''Return JSON representation of system'''
        system_dict = {
            'name': self.name,
            'hex': self.hex,
            'stellar': self.stellar.as_json(),
            'mainworld': self.mainworld.as_json(),
            'bases': self.bases,
            'pbg': str(self.pbg),
            'allegiance': self.allegiance,
            'Ix': str(self.importance_x),
            'Ex': str(self.economic_x),
            'Cx': str(self.cultural_x),
            'nobility': self.nobility,
            'worlds': self.num_worlds,
            'zone': self.zone
        }
        return json.dumps(system_dict)

    def json_import(self, jdata):
        '''Import from JSON'''
        system_dict = json.loads(jdata)
        self.name = system_dict['name']
        self.hex = system_dict['hex']
        self.bases = system_dict['bases']
        self.allegiance = system_dict['allegiance']
        self.nobility = system_dict['nobility']
        self.num_worlds = int(system_dict['worlds'])
        self.zone = system_dict['zone']
        self.stellar.json_import(system_dict['stellar'])
        self.mainworld.json_import(system_dict['mainworld'])
        self.pbg.json_import(system_dict['pbg'])
        self.importance_x.json_import(system_dict['Ix'])
        self.economic_x.json_import(system_dict['Ex'])
        self.cultural_x.json_import(system_dict['Cx'])

    def determine_x(self):
        '''Determine Ix Ex Cx values'''
        self.importance_x = ImportanceExtension(self.mainworld, self)
        self.economic_x = EconomicExtension(self.pbg,
                                            int(self.mainworld.population),
                                            int(self.mainworld.tech_level),
                                            self.mainworld.trade_codes,
                                            int(self.importance_x))
        self.cultural_x = CulturalExtension(int(self.mainworld.population),
                                            int(self.importance_x),
                                            int(self.mainworld.tech_level))