Skip to content

Commit

Permalink
Work towards on-the-fly station addition
Browse files Browse the repository at this point in the history
  • Loading branch information
kfsone committed Dec 26, 2014
1 parent 56693bd commit 96d6e87
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 37 deletions.
3 changes: 2 additions & 1 deletion data/TradeDangerous.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 '?'
Expand Down
38 changes: 18 additions & 20 deletions jsonprices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
77 changes: 61 additions & 16 deletions tradedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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


############################################################
Expand Down

0 comments on commit 96d6e87

Please sign in to comment.