1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-04-17 01:22:19 +03:00

Rationalise handling of different ship names in-game, in EDDN messages, in E:D Shipyard loadouts and in Coriolis loadouts.

This commit is contained in:
Jonathan Harris 2015-12-05 02:48:20 +00:00
parent b6a588d4fb
commit face96eca0
8 changed files with 79 additions and 61 deletions

@ -11,7 +11,6 @@ import time
if __debug__:
from traceback import print_exc
from shipyard import ship_map
from config import config
holdoff = 60 # be nice
@ -22,12 +21,13 @@ URL_CONFIRM = 'https://companion.orerve.net/user/confirm'
URL_QUERY = 'https://companion.orerve.net/profile'
# Map values reported by the Companion interface to names displayed in-game and recognized by trade tools
# Map values reported by the Companion interface to names displayed in-game
category_map = {
'Narcotics' : 'Legal Drugs',
'Slaves' : 'Slavery',
'NonMarketable' : False,
'Waste ' : 'Waste',
'NonMarketable' : False, # Don't report these
}
commodity_map= {
@ -45,6 +45,34 @@ commodity_map= {
'Terrain Enrichment Systems' : 'Land Enrichment Systems',
}
ship_map = {
'adder' : 'Adder',
'anaconda' : 'Anaconda',
'asp' : 'Asp Explorer',
'cobramkiii' : 'Cobra MkIII',
'diamondback' : 'Diamondback Scout',
'diamondbackxl' : 'Diamondback Explorer',
'eagle' : 'Eagle',
'empire_courier' : 'Imperial Courier',
'empire_eagle' : 'Imperial Eagle',
'empire_fighter' : 'Imperial Fighter',
'empire_trader' : 'Imperial Clipper',
'federation_dropship' : 'Federal Dropship',
'federation_dropship_mkii' : 'Federal Assault Ship',
'federation_gunship' : 'Federal Gunship',
'federation_fighter' : 'F63 Condor',
'ferdelance' : 'Fer-de-Lance',
'hauler' : 'Hauler',
'orca' : 'Orca',
'python' : 'Python',
'sidewinder' : 'Sidewinder',
'type6' : 'Type-6 Transporter',
'type7' : 'Type-7 Transporter',
'type9' : 'Type-9 Heavy',
'viper' : 'Viper MkIII',
'vulture' : 'Vulture',
}
# Companion API sometimes returns an array as a json array, sometimes as a json object indexed by "int".
# This seems to depend on whether the there are 'gaps' in the Cmdr's data - i.e. whether the array is sparse.
@ -200,6 +228,7 @@ class Session:
# Check all required numeric fields are present and are numeric
# Catches "demandBracket": "" for some phantom commodites in ED 1.3 - https://github.com/Marginal/EDMarketConnector/issues/2
# But also see https://github.com/Marginal/EDMarketConnector/issues/32
for thing in ['buyPrice', 'sellPrice', 'demand', 'demandBracket', 'stock', 'stockBracket']:
if not isinstance(commodity.get(thing), numbers.Number):
if __debug__: print 'Invalid "%s":"%s" (%s) for "%s"' % (thing, commodity.get(thing), type(commodity.get(thing)), commodity.get('name', ''))
@ -207,9 +236,9 @@ class Session:
else:
if not category_map.get(commodity['categoryname'], True): # Check marketable
pass
elif not commodity.get('categoryname', '').strip():
elif not commodity.get('categoryname'):
if __debug__: print 'Missing "categoryname" for "%s"' % commodity.get('name', '')
elif not commodity.get('name', '').strip():
elif not commodity.get('name'):
if __debug__: print 'Missing "name" for a commodity in "%s"' % commodity.get('categoryname', '')
elif not commodity['demandBracket'] in range(4):
if __debug__: print 'Invalid "demandBracket":"%s" for "%s"' % (commodity['demandBracket'], commodity['name'])
@ -217,10 +246,8 @@ class Session:
if __debug__: print 'Invalid "stockBracket":"%s" for "%s"' % (commodity['stockBracket'], commodity['name'])
else:
# Rewrite text fields
commodity['categoryname'] = category_map.get(commodity['categoryname'].strip(),
commodity['categoryname'].strip())
commodity['name'] = commodity_map.get(commodity['name'].strip(),
commodity['name'].strip())
commodity['categoryname'] = category_map.get(commodity['categoryname'], commodity['categoryname'])
commodity['name'] = commodity_map.get(commodity['name'], commodity['name'])
# Force demand and stock to zero if their corresponding bracket is zero
# Fixes spurious "demand": 1 in ED 1.3

@ -9,9 +9,12 @@ import time
from config import config
import outfitting
import shipyard
import companion
# Map API slot names to Coriolis categories
# http://cdn.coriolis.io/schemas/ship-loadout/2.json
slot_map = {
'hugehardpoint' : 'hardpoints',
'largehardpoint' : 'hardpoints',
@ -29,12 +32,15 @@ slot_map = {
'slot' : 'internal',
}
# Map draft E:D Shipyard & EDDN outfitting to Coriolis
# https://raw.githubusercontent.com/jamesremuscat/EDDN/master/schemas/outfitting-v1.0-draft.json
# http://cdn.coriolis.io/schemas/ship-loadout/2.json
ship_map = dict(shipyard.ship_map)
ship_map['asp'] = 'Asp Explorer'
# Map API ship names to Coriolis names
ship_map = dict(companion.ship_map)
ship_map['cobramkiii'] = 'Cobra Mk III'
ship_map['viper'] = 'Viper'
# Map EDDN outfitting schema / in-game names to Coriolis names
# https://raw.githubusercontent.com/jamesremuscat/EDDN/master/schemas/outfitting-v1.0.json
standard_map = OrderedDict([ # in output order
('Armour', 'bulkheads'),
@ -78,8 +84,6 @@ def export(data, filename=None):
querytime = config.getint('querytime') or int(time.time())
ship = shipyard.ship_map.get(data['ship']['name'].lower(), data['ship']['name'])
loadout = OrderedDict([ # Mimic Coriolis export ordering
('$schema', 'http://cdn.coriolis.io/schemas/ship-loadout/2.json#'),
('name', ship_map.get(data['ship']['name'].lower(), data['ship']['name'])),
@ -114,7 +118,7 @@ def export(data, filename=None):
loadout['components'][category].append(None)
continue
module = outfitting.lookup(v['module'])
module = outfitting.lookup(v['module'], ship_map)
if not module:
raise AssertionError('Unknown module %s' % v) # Shouldn't happen
@ -176,6 +180,7 @@ def export(data, filename=None):
return
# Look for last ship of this type
ship = companion.ship_map.get(data['ship']['name'].lower(), data['ship']['name']) # Use in-game name
regexp = re.compile(re.escape(ship) + '\.\d\d\d\d\-\d\d\-\d\dT\d\d\.\d\d\.\d\d\.json')
oldfiles = sorted([x for x in os.listdir(config.get('outdir')) if regexp.match(x)])
if oldfiles:

11
eddn.py

@ -9,13 +9,20 @@ from sys import platform
import time
from config import applongname, appversion, config
from shipyard import ship_map
import companion
import outfitting
upload = 'http://eddn-gateway.elite-markets.net:8080/upload/'
timeout= 10 # requests timeout
# Map API ship names to EDDN schema names
# https://raw.githubusercontent.com/jamesremuscat/EDDN/master/schemas/shipyard-v1.0.json
ship_map = dict(companion.ship_map)
ship_map['asp'] = 'Asp' # Pre E:D 1.5 name for backwards compatibility
ship_map['cobramkiii'] = 'Cobra Mk III' # ditto
ship_map['viper'] = 'Viper' # ditto
bracketmap = { 1: 'Low',
2: 'Med',
3: 'High', }
@ -68,7 +75,7 @@ def export_outfitting(data):
modules = []
for v in data['lastStarport'].get('modules', {}).itervalues():
try:
module = outfitting.lookup(v)
module = outfitting.lookup(v, ship_map)
if module:
modules.append(module)
except AssertionError as e:

@ -8,8 +8,7 @@ from sys import platform
import time
from config import config
from companion import commodity_map
from shipyard import ship_map
from companion import commodity_map, ship_map
logfile = None

@ -8,10 +8,16 @@ import time
from config import config
import outfitting
from shipyard import ship_map
import companion
# API slot names to E:D Shipyard slot names
# Map API ship names to E:D Shipyard ship names
ship_map = dict(companion.ship_map)
ship_map['cobramkiii'] = 'Cobra Mk III'
ship_map['viper'] = 'Viper'
# Map API slot names to E:D Shipyard slot names
slot_map = {
'hugehardpoint' : 'H',
'largehardpoint' : 'L',
@ -40,8 +46,6 @@ def export(data, filename=None):
querytime = config.getint('querytime') or int(time.time())
ship = ship_map.get(data['ship']['name'].lower(), data['ship']['name'])
loadout = defaultdict(list)
for slot in sorted(data['ship']['modules']):
@ -50,7 +54,7 @@ def export(data, filename=None):
try:
if not v: continue
module = outfitting.lookup(v['module'])
module = outfitting.lookup(v['module'], ship_map)
if not module: continue
cr = class_rating(module)
@ -77,7 +81,7 @@ def export(data, filename=None):
if __debug__: raise
# Construct description
string = '[%s]\n' % ship
string = '[%s]\n' % ship_map.get(data['ship']['name'].lower(), data['ship']['name'])
for slot in ['H', 'L', 'M', 'S', 'U', None, 'BH', 'RB', 'TM', 'FH', 'EC', 'PC', 'SS', 'FS', None, '9', '8', '7', '6', '5', '4', '3', '2', '1']:
if not slot:
string += '\n'
@ -92,6 +96,7 @@ def export(data, filename=None):
return
# Look for last ship of this type
ship = companion.ship_map.get(data['ship']['name'].lower(), data['ship']['name']) # Use in-game 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:

@ -10,7 +10,7 @@ from os.path import exists, isfile
import sys
import time
from shipyard import ship_map
import companion
from config import config
@ -215,9 +215,11 @@ internal_map = {
# Given a module description from the Companion API returns a description of the module in the form of a
# dict { category, name, [mount], [guidance], [ship], rating, class } using the same terms found in the
# English langauge game. For fitted modules, dict also includes { enabled, priority }.
# Or returns None if the module is user-specific (i.e. decal, paintjob).
# ship_map tells us what ship names to use for Armour - i.e. EDDN schema names or in-game names.
#
# Returns None if the module is user-specific (i.e. decal, paintjob) or PP-specific in station outfitting.
# (Given the ad-hocery in this implementation a big lookup table might have been simpler and clearer).
def lookup(module):
def lookup(module, ship_map):
# if not module.get('category'): raise AssertionError('%s: Missing category' % module['id']) # only present post 1.3, and not present in ship loadout
if not module.get('name'): raise AssertionError('%s: Missing name' % module['id'])
@ -339,7 +341,7 @@ def export(data, filename):
h.write(header)
for v in data['lastStarport'].get('modules', {}).itervalues():
try:
m = lookup(v)
m = lookup(v, companion.ship_map)
if m:
h.write('%s,%s,%s,%s,%s,%s,%s,%s,%s\n' % (rowheader, m['category'], m['name'], m.get('mount',''), m.get('guidance',''), m.get('ship',''), m['class'], m['rating'], timestamp))
except AssertionError as e:

@ -2,37 +2,10 @@
import time
from companion import ship_map
from config import config
ship_map = {
'adder' : 'Adder',
'anaconda' : 'Anaconda',
'asp' : 'Asp',
'cobramkiii' : 'Cobra Mk III',
'diamondback' : 'Diamondback Scout',
'diamondbackxl' : 'Diamondback Explorer',
'eagle' : 'Eagle',
'empire_courier' : 'Imperial Courier',
'empire_eagle' : 'Imperial Eagle',
'empire_fighter' : 'Imperial Fighter',
'empire_trader' : 'Imperial Clipper',
'federation_dropship' : 'Federal Dropship',
'federation_dropship_mkii' : 'Federal Assault Ship',
'federation_gunship' : 'Federal Gunship',
'federation_fighter' : 'F63 Condor',
'ferdelance' : 'Fer-de-Lance',
'hauler' : 'Hauler',
'orca' : 'Orca',
'python' : 'Python',
'sidewinder' : 'Sidewinder',
'type6' : 'Type-6 Transporter',
'type7' : 'Type-7 Transporter',
'type9' : 'Type-9 Heavy',
'viper' : 'Viper',
'vulture' : 'Vulture',
}
def export(data, filename):
querytime = config.getint('querytime') or int(time.time())

@ -8,8 +8,8 @@ import Tkinter as tk
import ttk
import companion
from companion import ship_map
import prefs
from shipyard import ship_map
# Hack to fix notebook page background. Doesn't seem possible to do this with styles.