Skip to content

Commit

Permalink
Better support for blackmarket
Browse files Browse the repository at this point in the history
  • Loading branch information
kfsone committed Dec 22, 2014
1 parent a262e25 commit 19dae79
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
12 changes: 10 additions & 2 deletions jsonprices.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def lookup_system(tdb, tdenv, name, x, y, z):
return None


def lookup_station(tdb, tdenv, system, name, lsFromStar):
def lookup_station(tdb, tdenv, system, name, lsFromStar, blackMarket):
normalizedName = tradedb.TradeDB.normalizedStr(name)
for stn in system.stations:
stnNormalizedName = tradedb.TradeDB.normalizedStr(stn.dbname)
Expand All @@ -97,7 +97,7 @@ def lookup_station(tdb, tdenv, system, name, lsFromStar):
return stn

if tdenv.addUnknown:
return tdb.addLocalStation(system, name, lsFromStar)
return tdb.addLocalStation(system, name, lsFromStar, blackMarket)

return None

Expand All @@ -120,6 +120,13 @@ def load_prices_json(
stnName = stnData['name']
lsFromStar = stnData['ls']

try:
blackMarket = stnData['bm'].upper()
if not blackMarket in [ 'Y', 'N' ]:
blackMarket = '?'
except KeyError:
blackMarket = '?'

system = lookup_system(
tdb, tdenv,
sysName,
Expand All @@ -145,6 +152,7 @@ def load_prices_json(
system,
stnName,
lsFromStar,
blackMarket,
)
if not station:
if tdenv.ignoreUnknown:
Expand Down
6 changes: 3 additions & 3 deletions misc/add-station.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ def addStation(conn, sysName, stnName, distLs, blackMarket):
cur = conn.cursor()
cur.execute("""
INSERT INTO Station
(system_id, name, ls_from_star)
VALUES (?, ?, ?)
""", [systemID, stnName, distLs])
(system_id, name, ls_from_star, blackMarket)
VALUES (?, ?, ?, ?)
""", [systemID, stnName, distLs, blackMarket])
stationID = cur.lastrowid
conn.commit()

Expand Down
40 changes: 27 additions & 13 deletions tradedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,13 @@ class Station(object):
Describes a station within a given system along with what trade
opportunities it presents.
"""
__slots__ = ('ID', 'system', 'dbname', 'lsFromStar', 'tradingWith', 'itemCount')
__slots__ = ('ID', 'system', 'dbname', 'lsFromStar', 'blackMarket', 'tradingWith', 'itemCount')

def __init__(self, ID, system, dbname, lsFromStar, itemCount):
self.ID, self.system, self.dbname, self.lsFromStar, self.itemCount = ID, system, dbname, lsFromStar, itemCount
def __init__(self, ID, system, dbname, lsFromStar, blackMarket, itemCount):
self.ID, self.system, self.dbname = ID, system, dbname
self.lsFromStar = lsFromStar
self.blackMarket = blackMarket
self.itemCount = itemCount
self.tradingWith = None # dict[tradingPartnerStation] -> [ available trades ]
system.stations.append(self)

Expand All @@ -155,7 +158,12 @@ def str(self):


def __repr__(self):
return "Station(ID={}, system='{}', dbname='{}', lsFromStar={})".format(self.ID, re.escape(self.system.dbname), re.escape(self.dbname), self.lsFromStar)
return "Station(ID={}, system='{}', dbname='{}', lsFromStar={})".format(
self.ID,
re.escape(self.system.dbname),
re.escape(self.dbname),
self.lsFromStar
)


######################################################################
Expand Down Expand Up @@ -595,7 +603,7 @@ def _loadStations(self):
If you have previously loaded Stations, this will orphan the old objects.
"""
stmt = """
SELECT station_id, system_id, name, ls_from_star,
SELECT station_id, system_id, name, ls_from_star, blackMarket,
(SELECT COUNT(*)
FROM StationItem
WHERE station_id = Station.station_id) AS itemCount
Expand All @@ -604,15 +612,21 @@ def _loadStations(self):
self.cur.execute(stmt)
stationByID = {}
systemByID = self.systemByID
for (ID, systemID, name, lsFromStar, itemCount) in self.cur:
station = Station(ID, systemByID[systemID], name, lsFromStar, itemCount)
for (
ID, systemID, name, lsFromStar, blackMarket, itemCount
) in self.cur:
station = Station(
ID, systemByID[systemID], name,
lsFromStar, blackMarket,
itemCount
)
stationByID[ID] = station

self.stationByID = stationByID
self.tdenv.DEBUG1("Loaded {:n} Stations", len(stationByID))


def addLocalStation(self, system, name, lsFromStar):
def addLocalStation(self, system, name, lsFromStar, blackMarket):
"""
Add a station to the local cache and memory copy.
"""
Expand All @@ -621,20 +635,20 @@ def addLocalStation(self, system, name, lsFromStar):
cur = db.cursor()
cur.execute("""
INSERT INTO Station (
name, system_id, ls_from_star
name, system_id, ls_from_star, blackMarket
) VALUES (
?, ?, ?
?, ?, ?, ?
)
""", [
name, system.ID, lsFromStar
])
ID = cur.lastrowid
station = Station(ID, system, name, lsFromStar, 0)
station = Station(ID, system, name, lsFromStar, blackMarket, 0)
self.stationByID[ID] = station
db.commit()
if not self.tdenv.quiet:
print("- Added new station #{}: {}/{} [{}ls]".format(
ID, system.name(), name, lsFromStar
print("- Added new station #{}: {}/{} [ls:{}, bm:{}]".format(
ID, system.name(), name, lsFromStar, blackMarket
))
return station

Expand Down

0 comments on commit 19dae79

Please sign in to comment.