Skip to content

Commit

Permalink
v3.1 Multiple Command mode and Update mode.
Browse files Browse the repository at this point in the history
TradeDangerous now supports multiple 'commands', currently 'run' and 'update'. Run does what pre-3.1 trade.py used to do. Update provides a way for you to edit data.

  ./trade.py update aulin

will walk you through the prices at aulin (not implemented yet)

  ./trade.py --subl update aulin

will launch Sublime Text 3 (if installed) to edit a .prices list of aulin

  ./trade.py --notepad update aulin

will launch notepad to edit a .prices list for aulin

  ./trade.py --editor foo.exe update aulin

will use 'foo.exe' as the editor.
  • Loading branch information
kfsone committed Sep 1, 2014
1 parent 65ecb86 commit 6a2f0e2
Show file tree
Hide file tree
Showing 6 changed files with 425 additions and 278 deletions.
40 changes: 1 addition & 39 deletions data/TradeDangerous.prices
Original file line number Diff line number Diff line change
@@ -1,42 +1,4 @@
# SOURCE List of item prices for TradeDangerous.
#
# This file is used by TradeDangerous to populate the
# SQLite databsae with prices, whenever this file is
# updated or you delete the TradeDangerous.db file.

# FORMAT:
#
# # ...
# A comment
#
# @ SYSTEM NAME/Station Name
# Sets the current station
# e.g. @ CHANGO/Chango Dock
#
# + Product Category
# Sets the current product category
# e.g. + Consumer Tech
#
# Item Name Value Cost
# Item value line.
# Item Name is the name of the item, e.g. Fish
# Value is what the STATION pays for the item,
# Cost is how much the item costs FROM the station
#
# Example:
# @ CHANGO/Chango Dock
# + Chemicals
# Mineral Oil 150 0
# Hydrogen Fuels 63 0
# Explosives 150 160
#
# This gives prives for items under the "Chemicals"
# heading. Mineral oil was listed first and was selling
# at the station for 150cr.
# Hydrogen Fuels was listed 3rd and sells for 63 cr.
# Explosives was listed 2nd and sells for 150 cr AND
# can be bought here for 160cr.
#
# Source for TradeDangerous' price database.

@ ACIHAUT/Cuffey Plant
+ Chemicals
Expand Down
34 changes: 22 additions & 12 deletions data/buildcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
class PriceEntry(namedtuple('PriceEntry', [ 'stationID', 'itemID', 'asking', 'paying', 'uiOrder' ])):
pass

def priceLineNegotiator(priceFile, db, debug):
def priceLineNegotiator(priceFile, db, debug=0):
"""
Yields SQL for populating the database with prices
by reading the file handle for price lines.
Expand Down Expand Up @@ -96,6 +96,26 @@ def priceLineNegotiator(priceFile, db, debug):
continue


def processPricesFile(db, pricesPath, stationID=None, debug=0):
if debug: print("* Processing Prices file '{}'".format(str(pricesPath)))

if stationID:
if debug: print("* Deleting stale entries for {}".format(stationID))
db.execute("DELETE FROM Price WHERE station_id = {}".format(stationID))

with pricesPath.open() as pricesFile:
bindValues = []
for price in priceLineNegotiator(pricesFile, db, debug):
if debug > 2: print(price)
bindValues += [ price ]
stmt = """
INSERT INTO Price (station_id, item_id, sell_to, buy_from, ui_order)
VALUES (?, ?, ?, ?, ?)
"""
db.executemany(stmt, bindValues)
db.commit()


def buildCache(dbPath, sqlPath, pricesPath, debug=0):
"""
Rebuilds the SQlite database from source files.
Expand All @@ -122,17 +142,7 @@ def buildCache(dbPath, sqlPath, pricesPath, debug=0):
tempDB.executescript(sqlScript)

# Parse the prices file
if debug: print("* Processing Prices file '%s'" % pricesPath)
with pricesPath.open() as pricesFile:
bindValues = []
for price in priceLineNegotiator(pricesFile, tempDB, debug):
if debug > 2: print(price)
bindValues += [ price ]
stmt = """
INSERT INTO Price (station_id, item_id, sell_to, buy_from, ui_order)
VALUES (?, ?, ?, ?, ?)
"""
tempDB.executemany(stmt, bindValues)
processPricesFile(tempDB, pricesPath)

# Database is ready; copy it to a persistent store.
if debug: print("* Populating SQLite database file '%s'" % dbPath)
Expand Down
79 changes: 79 additions & 0 deletions data/prices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#! /usr/bin/env python
#---------------------------------------------------------------------
# Copyright (C) Oliver 'kfsone' Smith 2014 <[email protected]>:
# You are free to use, redistribute, or even print and eat a copy of
# this software so long as you include this copyright notice.
# I guarantee there is at least one bug neither of us knew about.
#---------------------------------------------------------------------
# TradeDangerous :: Modules :: Generate TradeDangerous.prices

import sys
import os
import re
import sqlite3

######################################################################
# Main

def dumpPrices(dbFilename, withModified=False, stationID=None, file=None, debug=0):
""" Generate a 'prices' list for the given list of stations using data from the DB. """
conn = sqlite3.connect(str(dbFilename)) # so we can handle a Path object too
cur = conn.cursor()

systems = { ID: name for (ID, name) in cur.execute("SELECT system_id, name FROM system") }
stations = { ID: name for (ID, name) in cur.execute("SELECT station_id, name FROM station") }
categories = { ID: name for (ID, name) in cur.execute("SELECT category_id, name FROM category") }
items = { ID: name for (ID, name) in cur.execute("SELECT item_id, name FROM item") }

# find longest item name
longestName = max(items.values(), key=lambda name: len(name))
longestNameLen = len(longestName)

stationClause = "1" if not stationID else "Station.station_id = {}".format(stationID)
cur.execute("""
SELECT Station.system_id
, Price.station_id
, Item.category_id
, Price.item_id
, Price.sell_to
, Price.buy_from
, Price.modified
FROM Station, Item, Category, Price
WHERE {} -- station clause
AND Station.station_id = Price.station_id
AND (Item.category_id = Category.category_id) AND Item.item_id = Price.item_id
ORDER BY Station.system_id, Station.station_id, Category.name, Price.ui_order, Price.item_id
""".format(stationClause))
lastSys, lastStn, lastCat = None, None, None

if not file: file = sys.stdout
file.write("# Source for TradeDangerous' price database.\n\n")

for (sysID, stnID, catID, itemID, fromStn, toStn, modified) in cur:
system = systems[sysID]
if system is not lastSys:
if lastStn: file.write("\n\n")
lastStn, lastCat = None, None
lastSys = system

station = stations[stnID]
if station is not lastStn:
if lastStn: file.write("\n")
lastCat = None
file.write("@ {}/{}\n".format(system.upper(), station))
lastStn = station

category = categories[catID]
if category is not lastCat:
file.write(" + {}\n".format(category))
lastCat = category

file.write(" {:<{width}} {:7d} {:6d}".format(items[itemID], fromStn, toStn, width=longestNameLen))
if withModified and modified:
file.write(" {}".format(modified))
file.write("\n")


if __name__ == "__main__":
from tradedb import TradeDB
dumpPrices(TradeDB.defaultDB, withModified=True)
108 changes: 0 additions & 108 deletions generate-prices-from-db.py

This file was deleted.

Loading

0 comments on commit 6a2f0e2

Please sign in to comment.