cargo 1.16 KB
1 2
-- -*- lua -*-

3
local _env, args = ...
4

5 6 7
local prices = cargo_prices()

if(not prices) then
8 9 10 11
   print("This station does not sell cargo.")
   return
end

12
-- TODO: better message when amount is missing
13 14
if(args[1] == "list") then
   print("Prices: (buy/sell)")
15 16
   for good, price in pairs(prices) do
      print("  " .. good .. ": " .. price.buy .. "/" .. price.sell)
17
   end
18

19
   print("Stock:")
20
   for good, amount in pairs(cargo_amounts()) do
21 22
      print("  " .. good .. ": " .. amount)
   end
23

24
   print("Your hold:")
25
   for good, amount in pairs(cargo_hold()) do
26 27 28 29
      print("  " .. good .. ": " .. amount)
   end
elseif(args[1] == "buy" or args[1] == "sell") then
   local direction, good, amount = unpack(args)
30
   local success_price, msg = cargo_transfer(direction, good, tonumber(amount))
31 32

   if(success_price) then
33 34
      local verb = direction == "buy" and "Bought" or "Sold"
      print(verb .. " " .. amount .. " of " .. good .. " for " ..
35
               success_price .. ".")
36 37
   else
      print(msg)
38 39
   end
else
40
   print("Buy, sell, and list prices for cargo.\n")
41 42 43 44
   print("Usage:")
   print("  cargo list")
   print("  cargo buy GOOD AMOUNT")
   print("  cargo sell GOOD AMOUNT")
45
end