From 96d6e8747aa17ff43f0bd764dacd1cefa3c9827b Mon Sep 17 00:00:00 2001 From: Oliver Smith Date: Thu, 25 Dec 2014 19:44:10 -0800 Subject: [PATCH] Work towards on-the-fly station addition --- data/TradeDangerous.sql | 3 +- jsonprices.py | 38 ++++++++++---------- tradedb.py | 77 ++++++++++++++++++++++++++++++++--------- 3 files changed, 81 insertions(+), 37 deletions(-) diff --git a/data/TradeDangerous.sql b/data/TradeDangerous.sql index dfc04bfe..88219eae 100644 --- a/data/TradeDangerous.sql +++ b/data/TradeDangerous.sql @@ -62,7 +62,8 @@ CREATE TABLE Station station_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(40) COLLATE nocase, system_id INTEGER NOT NULL, - ls_from_star INTEGER NOT NULL, + ls_from_star INTEGER NOT NULL DEFAULT 0 + CHECK (ls_from_star > 0), blackmarket TEXT(1) NOT NULL DEFAULT '?' CHECK (blackmarket IN ('?', 'Y', 'N')), max_pad_size TEXT(1) NOT NULL DEFAULT '?' diff --git a/jsonprices.py b/jsonprices.py index 70f99476..0e86cf10 100644 --- a/jsonprices.py +++ b/jsonprices.py @@ -74,32 +74,30 @@ def lookup_system(tdb, tdenv, name, x, y, z): return None -def lookup_station(tdb, tdenv, system, name, lsFromStar, blackMarket): +def lookup_station( + tdb, tdenv, + system, name, + lsFromStar, blackMarket, maxPadSize + ): + station = None normalizedName = tradedb.TradeDB.normalizedStr(name) for stn in system.stations: stnNormalizedName = tradedb.TradeDB.normalizedStr(stn.dbname) if stnNormalizedName == normalizedName: - if lsFromStar and not stn.lsFromStar: - tdenv.DEBUG0( - "- Updating station {}: " - " dist from star = {}", - stn.name(), - lsFromStar - ) - db = tdb.getDB() - db.execute( - "UPDATE Station " - "SET dist_from_star = ? " - "WHERE station_id = ?", - [lsFromStar, stn.ID] - ) - db.commit() - return stn + station = stn + break - if tdenv.addUnknown: - return tdb.addLocalStation(system, name, lsFromStar, blackMarket) + if not station: + if not tdenv.addUnknown: + return None + station = tdb.addLocalStation(system, name) + + # Now set the parameters + tdb.updateLocalStation( + stn, lsFromStar, blackMarket, maxPadSize + ) + return station - return None def load_prices_json( tdb, diff --git a/tradedb.py b/tradedb.py index 503bf56f..52abc994 100644 --- a/tradedb.py +++ b/tradedb.py @@ -670,29 +670,77 @@ def addLocalStation( cur = db.cursor() cur.execute(""" INSERT INTO Station ( - name, - system_id, - ls_from_star, - blackMarket, - max_pad_size + name, system_id, ) VALUES ( - ?, ?, ?, ?, ? + ?, ? ) - """, [ - name, system.ID, lsFromStar, blackMarket, maxPadSize, - ]) + """, [ name, system.ID ]) ID = cur.lastrowid - station = Station(ID, system, name, lsFromStar, blackMarket, maxPadSize, 0) + station = Station(ID, system, name, 0, '?', '?', 0) self.stationByID[ID] = station db.commit() if not self.tdenv.quiet: print("- Added new station #{}:" - "{}/{} [ls:{}, bm:{}, mps:]".format( + "{}/{}".format( ID, system.name(), name, - lsFromStar, blackMarket, maxPadSize, )) return station + def updateLocalStation( + self, station, + lsFromStar=None, + blackMarket=None, + maxPadSize=None, + force=False, + ): + """ + Alter the properties of a station in-memory and in the DB. + """ + changes = False + if lsFromStar is not None: + assert lsFromStar >= 0 + if lsFromStar != station.lsFromStar: + if lsFromStar > 0 or force: + station.lsFromStar = lsFromStar + changes = True + if blackMarket is not None: + blackMarket = blackMarket.upper() + assert blackMarket in [ '?', 'Y', 'N' ] + if blackMarket != station.blackMarket: + if blackMarket != '?' or force: + station.blackMarket = blackMarket + changes = True + if maxPadSize is not None: + maxPadSize = maxPadSize.upper() + assert maxPadSize in [ '?', 'S', 'M', 'L' ] + if maxPadSize != station.maxPadSize: + if maxPadSize != '?' or force: + station.maxPadSize = maxPadSize + changes = True + if not changes: + return False + db = self.getDB() + db.execute(""" + UPDATE Station + SET ls_from_star={}, + blackmarket={}, + max_pad_size={} + WHERE station_id = {} + """, [ + station.lsFromStar, + station.blackMarket, + station.maxPadSize, + station.ID + ]) + db.commit() + if not self.tdenv.quiet: + print("- {}/{}: ls={}, bm={}, pad={}".format( + station.name(), + station.lsFromStar, + station.blackMarket, + station.maxPadSize, + )) + return True def lookupPlace(self, name): """ @@ -1236,10 +1284,7 @@ def load(self, maxSystemLinkLy=None): # Calculate the maximum distance anyone can jump so we can constrain # the maximum "link" between any two stars. - if not maxSystemLinkLy: - self.maxSystemLinkLy = 30 - else: - self.maxSystemLinkLy = maxSystemLinkLy + self.maxSystemLinkLy = maxSystemLinkLy or self.tdenv.maxSystemLinkLy or 30 ############################################################