Beispiel #1
0
    def export_ship(self, filename=None):
        string = json.dumps(self.ship(False),
                            ensure_ascii=False,
                            indent=2,
                            separators=(',',
                                        ': ')).encode('utf-8')  # pretty print

        if filename:
            with open(filename, 'wt') as h:
                h.write(string)
            return

        ship = ship_file_name(self.state['ShipName'], self.state['ShipType'])
        regexp = re.compile(
            re.escape(ship) + '\.\d\d\d\d\-\d\d\-\d\dT\d\d\.\d\d\.\d\d\.txt')
        oldfiles = sorted(
            [x for x in listdir(config.get('outdir')) if regexp.match(x)])
        if oldfiles:
            with open(join(config.get('outdir'), oldfiles[-1]), 'rU') as h:
                if h.read() == string:
                    return  # same as last time - don't write

        # Write
        filename = join(
            config.get('outdir'), '%s.%s.txt' %
            (ship, strftime('%Y-%m-%dT%H.%M.%S', localtime(time()))))
        with open(filename, 'wt') as h:
            h.write(string)
Beispiel #2
0
def export(data, filename=None):

    string = json.dumps(companion.ship(data),
                        ensure_ascii=False,
                        indent=2,
                        sort_keys=True,
                        separators=(',', ': '))  # pretty print

    if filename:
        with open(filename, 'wt') as h:
            h.write(string)
        return

    # Look for last ship of this type
    ship = companion.ship_file_name(data['ship'].get('shipName'),
                                    data['ship']['name'])
    regexp = re.compile(
        re.escape(ship) + '\.\d\d\d\d\-\d\d\-\d\dT\d\d\.\d\d\.\d\d\.txt')
    oldfiles = sorted(
        [x for x in os.listdir(config.get('outdir')) if regexp.match(x)])
    if oldfiles:
        with open(join(config.get('outdir'), oldfiles[-1]), 'rU') as h:
            if h.read() == string:
                return  # same as last time - don't write

    querytime = config.getint('querytime') or int(time.time())

    # Write
    filename = join(
        config.get('outdir'), '%s.%s.txt' %
        (ship, time.strftime('%Y-%m-%dT%H.%M.%S', time.localtime(querytime))))
    with open(filename, 'wt') as h:
        h.write(string)
Beispiel #3
0
    def export_ship(self, filename=None):
        string = json.dumps(self.ship(False), ensure_ascii=False, indent=2, separators=(',', ': ')).encode('utf-8')	# pretty print

        if filename:
            with open(filename, 'wt') as h:
                h.write(string)
            return

        ship = ship_file_name(self.state['ShipName'], self.state['ShipType'])
        regexp = re.compile(re.escape(ship) + '\.\d\d\d\d\-\d\d\-\d\dT\d\d\.\d\d\.\d\d\.txt')
        oldfiles = sorted([x for x in listdir(config.get('outdir')) if regexp.match(x)])
        if oldfiles:
            with open(join(config.get('outdir'), oldfiles[-1]), 'rU') as h:
                if h.read() == string:
                    return	# same as last time - don't write

        # Write
        filename = join(config.get('outdir'), '%s.%s.txt' % (ship, strftime('%Y-%m-%dT%H.%M.%S', localtime(time()))))
        with open(filename, 'wt') as h:
            h.write(string)
Beispiel #4
0
    def export_ship(self, filename=None):
        # TODO(A_D): Some type checking has been disabled in here due to config.get getting weird outputs
        string = json.dumps(self.ship(False), ensure_ascii=False, indent=2, separators=(',', ': '))  # pretty print
        if filename:
            with open(filename, 'wt') as h:
                h.write(string)

            return

        ship = ship_file_name(self.state['ShipName'], self.state['ShipType'])
        regexp = re.compile(re.escape(ship) + r'\.\d{4}\-\d\d\-\d\dT\d\d\.\d\d\.\d\d\.txt')
        oldfiles = sorted((x for x in listdir(config.get('outdir')) if regexp.match(x)))  # type: ignore
        if oldfiles:
            with open(join(config.get('outdir'), oldfiles[-1]), 'rU') as h:  # type: ignore
                if h.read() == string:
                    return  # same as last time - don't write

        # Write
        filename = join(  # type: ignore
            config.get('outdir'), '{}.{}.txt'.format(ship, strftime('%Y-%m-%dT%H.%M.%S', localtime(time())))
        )

        with open(filename, 'wt') as h:
            h.write(string)
def export(data, filename=None):

    def class_rating(module):
        if 'guidance' in module:	# Missiles
            return module['class'] + module['rating'] + '/' + module.get('mount', 'F')[0] + module['guidance'][0] + ' '
        elif 'mount' in module:		# Hardpoints
            return module['class'] + module['rating'] + '/' + module['mount'][0] + ' '
        elif 'Cabin' in module['name']:	# Passenger cabins
            return module['class'] + module['rating'] + '/' + module['name'][0] + ' '
        else:
            return module['class'] + module['rating'] + ' '

    querytime = config.getint('querytime') or int(time.time())

    loadout = defaultdict(list)
    mass = 0.0
    fuel = 0
    cargo = 0
    fsd = None
    jumpboost = 0

    for slot in sorted(data['ship']['modules']):

        v = data['ship']['modules'][slot]
        try:
            if not v: continue

            module = outfitting.lookup(v['module'], ship_map)
            if not module: continue

            cr = class_rating(module)
            mods = v.get('modifications') or v.get('WorkInProgress_modifications') or {}
            if mods.get('OutfittingFieldType_Mass'):
                mass += (module.get('mass', 0) * mods['OutfittingFieldType_Mass']['value'])
            else:
                mass += module.get('mass', 0)

            # Specials
            if 'Fuel Tank'in module['name']:
                fuel += 2**int(module['class'])
                name = '%s (Capacity: %d)' % (module['name'], 2**int(module['class']))
            elif 'Cargo Rack' in module['name']:
                cargo += 2**int(module['class'])
                name = '%s (Capacity: %d)' % (module['name'], 2**int(module['class']))
            else:
                name = module['name']

            if name == 'Frame Shift Drive':
                fsd = module	# save for range calculation
                if mods.get('OutfittingFieldType_FSDOptimalMass'):
                    fsd['optmass'] *= mods['OutfittingFieldType_FSDOptimalMass']['value']
                if mods.get('OutfittingFieldType_MaxFuelPerJump'):
                    fsd['maxfuel'] *= mods['OutfittingFieldType_MaxFuelPerJump']['value']
            jumpboost += module.get('jumpboost', 0)

            for s in slot_map:
                if slot.lower().startswith(s):
                    loadout[slot_map[s]].append(cr + name)
                    break
            else:
                if slot.lower().startswith('slot'):
                    loadout[slot[-1]].append(cr + name)
                elif __debug__ and not slot.lower().startswith('planetaryapproachsuite'):
                    print 'EDShipyard: Unknown slot %s' % slot

        except AssertionError as e:
            if __debug__: print 'EDShipyard: %s' % e
            continue	# Silently skip unrecognized modules
        except:
            if __debug__: raise

    # Construct description
    ship = ship_map.get(data['ship']['name'].lower(), data['ship']['name'])
    string = '[%s]\n' % (data['ship'].get('shipName') and ', '.join([ship, data['ship']['shipName'].encode('utf-8')]) or ship)
    for slot in ['H', 'L', 'M', 'S', 'U', None, 'BH', 'RB', 'TM', 'FH', 'EC', 'PC', 'SS', 'FS', None, 'MC', None, '9', '8', '7', '6', '5', '4', '3', '2', '1']:
        if not slot:
            string += '\n'
        elif slot in loadout:
            for name in loadout[slot]:
                string += '%s: %s\n' % (slot, name)
    string += '---\nCargo : %d T\nFuel  : %d T\n' % (cargo, fuel)

    # Add mass and range
    assert data['ship']['name'].lower() in companion.ship_map, data['ship']['name']
    assert companion.ship_map[data['ship']['name'].lower()] in ships, companion.ship_map[data['ship']['name'].lower()]
    try:
        # https://github.com/cmmcleod/coriolis/blob/master/app/js/shipyard/module-shipyard.js#L184
        mass += ships[companion.ship_map[data['ship']['name'].lower()]]['hullMass']
        string += 'Mass  : %.2f T empty\n        %.2f T full\n' % (mass, mass + fuel + cargo)
        multiplier = pow(min(fuel, fsd['maxfuel']) / fsd['fuelmul'], 1.0 / fsd['fuelpower']) * fsd['optmass']
        string += 'Range : %.2f LY unladen\n        %.2f LY laden\n' % (
            multiplier / (mass + fuel) + jumpboost,
            multiplier / (mass + fuel + cargo) + jumpboost)
    except:
        if __debug__: raise

    if filename:
        with open(filename, 'wt') as h:
            h.write(string)
        return

    # Look for last ship of this type
    ship = companion.ship_file_name(data['ship'].get('shipName'), data['ship']['name'])
    regexp = re.compile(re.escape(ship) + '\.\d\d\d\d\-\d\d\-\d\dT\d\d\.\d\d\.\d\d\.txt')
    oldfiles = sorted([x for x in os.listdir(config.get('outdir')) if regexp.match(x)])
    if oldfiles:
        with open(join(config.get('outdir'), oldfiles[-1]), 'rU') as h:
            if h.read() == string:
                return	# same as last time - don't write

    # Write
    filename = join(config.get('outdir'), '%s.%s.txt' % (ship, time.strftime('%Y-%m-%dT%H.%M.%S', time.localtime(querytime))))
    with open(filename, 'wt') as h:
        h.write(string)
Beispiel #6
0
def export(data, filename=None):
    def class_rating(module):
        if 'guidance' in module:  # Missiles
            return module['class'] + module['rating'] + '/' + module.get(
                'mount', 'F')[0] + module['guidance'][0] + ' '
        elif 'mount' in module:  # Hardpoints
            return module['class'] + module['rating'] + '/' + module['mount'][
                0] + ' '
        elif 'Cabin' in module['name']:  # Passenger cabins
            return module['class'] + module['rating'] + '/' + module['name'][
                0] + ' '
        else:
            return module['class'] + module['rating'] + ' '

    querytime = config.getint('querytime') or int(time.time())

    loadout = defaultdict(list)
    mass = 0.0
    fuel = 0
    cargo = 0
    fsd = None
    jumpboost = 0

    for slot in sorted(data['ship']['modules']):

        v = data['ship']['modules'][slot]
        try:
            if not v: continue

            module = outfitting.lookup(v['module'], ship_map)
            if not module: continue

            cr = class_rating(module)
            mods = v.get('modifications') or v.get(
                'WorkInProgress_modifications') or {}
            if mods.get('OutfittingFieldType_Mass'):
                mass += (module.get('mass', 0) *
                         mods['OutfittingFieldType_Mass']['value'])
            else:
                mass += module.get('mass', 0)

            # Specials
            if 'Fuel Tank' in module['name']:
                fuel += 2**int(module['class'])
                name = '%s (Capacity: %d)' % (module['name'], 2**int(
                    module['class']))
            elif 'Cargo Rack' in module['name']:
                cargo += 2**int(module['class'])
                name = '%s (Capacity: %d)' % (module['name'], 2**int(
                    module['class']))
            else:
                name = module['name']

            if name == 'Frame Shift Drive':
                fsd = module  # save for range calculation
                if mods.get('OutfittingFieldType_FSDOptimalMass'):
                    fsd['optmass'] *= mods[
                        'OutfittingFieldType_FSDOptimalMass']['value']
                if mods.get('OutfittingFieldType_MaxFuelPerJump'):
                    fsd['maxfuel'] *= mods[
                        'OutfittingFieldType_MaxFuelPerJump']['value']
            jumpboost += module.get('jumpboost', 0)

            for s in slot_map:
                if slot.lower().startswith(s):
                    loadout[slot_map[s]].append(cr + name)
                    break
            else:
                if slot.lower().startswith('slot'):
                    loadout[slot[-1]].append(cr + name)
                elif __debug__ and not slot.lower().startswith(
                        'planetaryapproachsuite'):
                    print 'EDShipyard: Unknown slot %s' % slot

        except AssertionError as e:
            if __debug__: print 'EDShipyard: %s' % e
            continue  # Silently skip unrecognized modules
        except:
            if __debug__: raise

    # Construct description
    ship = ship_map.get(data['ship']['name'].lower(), data['ship']['name'])
    string = '[%s]\n' % (data['ship'].get('shipName') and ', '.join(
        [ship, data['ship']['shipName'].encode('utf-8')]) or ship)
    for slot in [
            'H', 'L', 'M', 'S', 'U', None, 'BH', 'RB', 'TM', 'FH', 'EC', 'PC',
            'SS', 'FS', None, 'MC', None, '9', '8', '7', '6', '5', '4', '3',
            '2', '1'
    ]:
        if not slot:
            string += '\n'
        elif slot in loadout:
            for name in loadout[slot]:
                string += '%s: %s\n' % (slot, name)
    string += '---\nCargo : %d T\nFuel  : %d T\n' % (cargo, fuel)

    # Add mass and range
    assert data['ship']['name'].lower(
    ) in companion.ship_map, data['ship']['name']
    assert companion.ship_map[data['ship']['name'].lower(
    )] in ships, companion.ship_map[data['ship']['name'].lower()]
    try:
        # https://github.com/cmmcleod/coriolis/blob/master/app/js/shipyard/module-shipyard.js#L184
        mass += ships[companion.ship_map[data['ship']
                                         ['name'].lower()]]['hullMass']
        string += 'Mass  : %.2f T empty\n        %.2f T full\n' % (
            mass, mass + fuel + cargo)
        multiplier = pow(
            min(fuel, fsd['maxfuel']) / fsd['fuelmul'],
            1.0 / fsd['fuelpower']) * fsd['optmass']
        string += 'Range : %.2f LY unladen\n        %.2f LY laden\n' % (
            multiplier / (mass + fuel) + jumpboost, multiplier /
            (mass + fuel + cargo) + jumpboost)
    except:
        if __debug__: raise

    if filename:
        with open(filename, 'wt') as h:
            h.write(string)
        return

    # Look for last ship of this type
    ship = companion.ship_file_name(data['ship'].get('shipName'),
                                    data['ship']['name'])
    regexp = re.compile(
        re.escape(ship) + '\.\d\d\d\d\-\d\d\-\d\dT\d\d\.\d\d\.\d\d\.txt')
    oldfiles = sorted(
        [x for x in os.listdir(config.get('outdir')) if regexp.match(x)])
    if oldfiles:
        with open(join(config.get('outdir'), oldfiles[-1]), 'rU') as h:
            if h.read() == string:
                return  # same as last time - don't write

    # Write
    filename = join(
        config.get('outdir'), '%s.%s.txt' %
        (ship, time.strftime('%Y-%m-%dT%H.%M.%S', time.localtime(querytime))))
    with open(filename, 'wt') as h:
        h.write(string)
Beispiel #7
0
def export(data, filename=None):
    def class_rating(module: __Module):
        mod_class = module['class']
        mod_rating = module['rating']
        mod_mount = module.get('mount')
        mod_guidance = module.get('guidance')

        ret = '{mod_class}{rating}'.format(mod_class=mod_class, rating=mod_rating)
        if 'guidance' in module:  # Missiles
            ret += "/{mount}{guidance}".format(
                mount=mod_mount[0] if mod_mount is not None else 'F',
                guidance=mod_guidance[0],
            )

        elif 'mount' in module:  # Hardpoints
            ret += "/{mount}".format(mount=mod_mount)

        elif 'Cabin' in module['name']:  # Passenger cabins
            ret += "/{name}".format(name=module['name'][0])

        return ret + ' '

    querytime = config.getint('querytime') or int(time.time())

    loadout = defaultdict(list)
    mass = 0.0
    fuel = 0
    cargo = 0
    fsd = None
    jumpboost = 0

    for slot in sorted(data['ship']['modules']):

        v = data['ship']['modules'][slot]
        try:
            if not v:
                continue

            module: __Module = outfitting.lookup(v['module'], ship_map)
            if not module:
                continue

            cr = class_rating(module)
            mods = v.get('modifications') or v.get('WorkInProgress_modifications') or {}
            if mods.get('OutfittingFieldType_Mass'):
                mass += (module.get('mass', 0) * mods['OutfittingFieldType_Mass']['value'])

            else:
                mass += module.get('mass', 0)

            # Specials
            if 'Fuel Tank' in module['name']:
                fuel += 2**int(module['class'])
                name = '{} (Capacity: {})'.format(module['name'], 2**int(module['class']))

            elif 'Cargo Rack' in module['name']:
                cargo += 2**int(module['class'])
                name = '{} (Capacity: {})'.format(module['name'], 2**int(module['class']))

            else:
                name = module['name']

            if name == 'Frame Shift Drive':
                fsd = module  # save for range calculation

                if mods.get('OutfittingFieldType_FSDOptimalMass'):
                    fsd['optmass'] *= mods['OutfittingFieldType_FSDOptimalMass']['value']

                if mods.get('OutfittingFieldType_MaxFuelPerJump'):
                    fsd['maxfuel'] *= mods['OutfittingFieldType_MaxFuelPerJump']['value']

            jumpboost += module.get('jumpboost', 0)

            for s in slot_map:
                if slot.lower().startswith(s):
                    loadout[slot_map[s]].append(cr + name)
                    break

            else:
                if slot.lower().startswith('slot'):
                    loadout[slot[-1]].append(cr + name)

                elif __debug__ and not slot.lower().startswith('planetaryapproachsuite'):
                    print('EDShipyard: Unknown slot {}'.format(slot))

        except AssertionError as e:
            if __debug__:
                print('EDShipyard: {}'.format(e))

            continue  # Silently skip unrecognized modules

        except Exception:
            if __debug__:
                raise

    # Construct description
    ship = ship_map.get(data['ship']['name'].lower(), data['ship']['name'])

    if data['ship'].get('shipName') is not None:
        _ships = '{}, {}'.format(ship, data['ship']['shipName'])
    else:
        _ships = ship

    string = '[{}]\n'.format(_ships)

    SLOT_TYPES = (
        'H', 'L', 'M', 'S', 'U', None, 'BH', 'RB', 'TM', 'FH', 'EC', 'PC', 'SS', 'FS', None, 'MC', None, '9', '8',
        '7', '6', '5', '4', '3', '2', '1'
    )
    for slot in SLOT_TYPES:
        if not slot:
            string += '\n'

        elif slot in loadout:
            for name in loadout[slot]:
                string += '{}: {}\n'.format(slot, name)

    string += '---\nCargo : {} T\nFuel  : {} T\n'.format(cargo, fuel)

    # Add mass and range
    assert data['ship']['name'].lower() in companion.ship_map, data['ship']['name']
    assert companion.ship_map[data['ship']['name'].lower()] in ships, companion.ship_map[data['ship']['name'].lower()]

    try:
        mass += ships[companion.ship_map[data['ship']['name'].lower()]]['hullMass']
        string += 'Mass  : {:.2f} T empty\n        {:.2f} T full\n'.format(mass, mass + fuel + cargo)

        multiplier = pow(min(fuel, fsd['maxfuel']) / fsd['fuelmul'], 1.0 / fsd['fuelpower']) * fsd['optmass']

        string += 'Range : {:.2f} LY unladen\n        {:.2f} LY laden\n'.format(
            multiplier / (mass + fuel) + jumpboost,
            multiplier / (mass + fuel + cargo) + jumpboost
        )

    except Exception:
        if __debug__:
            raise

    if filename:
        with open(filename, 'wt') as h:
            h.write(string)

        return

    # Look for last ship of this type
    ship = companion.ship_file_name(data['ship'].get('shipName'), data['ship']['name'])
    regexp = re.compile(re.escape(ship) + r'\.\d{4}-\d\d-\d\dT\d\d\.\d\d\.\d\d\.txt')
    oldfiles = sorted([x for x in os.listdir(config.get('outdir')) if regexp.match(x)])
    if oldfiles:
        with open(join(config.get('outdir'), oldfiles[-1]), 'rU') as h:
            if h.read() == string:
                return  # same as last time - don't write

    # Write
    filename = join(config.get('outdir'), '{}.{}.txt'.format(
        ship, time.strftime('%Y-%m-%dT%H.%M.%S', time.localtime(querytime)))
    )

    with open(filename, 'wt') as h:
        h.write(string)