Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
impaktor committed Dec 18, 2020
1 parent 054def9 commit 0b27a79
Showing 1 changed file with 28 additions and 30 deletions.
58 changes: 28 additions & 30 deletions data/modules/SoldOut/SoldOut.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,47 @@ local ads = {}
local onChat = function (form, ref, option)
local ad = ads[ref]

-- How much does the player have of this commodity?
-- Maximum amount the player can sell:
local max = Game.player:CountEquip(ad.commodity, "cargo")

-- also don't sell more than the buyer is requesting:
max = ad.amount < max and ad.amount or max

-- first half of the advert message (need to change this
-- dynamically as player sells goods to the buyer)
local msg = ad.message .. "\n\n"
print("max", max)

form:Clear()
form:SetTitle(ad.title)
form:SetFace(ad.client)

if option == 0 then
form:SetMessage(msg .. string.interp(l.AMOUNT, {amount = ad.amount}))
end
-- First half of the advert message (need to change this
-- dynamically as player sells off goods to the buyer)
local msg = ad.message .. "\n\n"

if option == -1 then
form:Close()
return
end

-- player clicks to sell everything they have, or everything the buyer wants
if option == "max" then
option = max
-- Buttons asking how much quantity to sell, store value as an int
-- wrapped in a table:
form:AddOption(l.SELL_1, {1}) -- separate for singluar form, for translation reasons
for i, val in pairs{10, 100, 1000} do
form:AddOption(string.interp(l.SELL_N, {N=val}), {val})
end
form:AddOption(l.SELL_ALL, {max})

-- if player clicks to sell 'option' amount of goods:
if option > 0 then
if max < option then
form:SetMessage(l.NOT_IN_STOCK)
-- if option is an integer, it's for chat-form, if a table with an
-- int, it holds the amout the player wants to sell
if option == 0 then
form:SetMessage(msg .. string.interp(l.AMOUNT, {amount = ad.amount}))
elseif option == -1 then
form:Close()
return
else
local amount = option[1]
print("amount, max:", amount, max)
if max < amount or max == 0 then
form:SetMessage(l.NOT_IN_STOCK .. " max: " .. max .. " amount: " .. amount)
else
Game.player:RemoveEquip(ad.commodity, option)
Game.player:AddMoney(ad.price * option)
ad.amount = ad.amount - option
Game.player:RemoveEquip(ad.commodity, amount)
Game.player:AddMoney(ad.price * amount)
ad.amount = ad.amount - amount
assert(ad.amount >= 0)
form:SetMessage(msg .. string.interp(l.AMOUNT, {amount = ad.amount}))
end
end
Expand All @@ -62,13 +67,6 @@ local onChat = function (form, ref, option)
form:RemoveAdvertOnClose()
end

-- Buttons of how much quantity to sell
form:AddOption(l.SELL_1, 1)
for i, val in pairs{10, 100, 1000} do
form:AddOption(string.interp(l.SELL_N, {N=val}), val)
end
form:AddOption(l.SELL_ALL, "max")

return
end

Expand All @@ -88,7 +86,7 @@ local makeAdvert = function(station, commodity)
-- Money is uniformly distirubted in log space
-- local money_to_spend = math.exp(Engine.rand:Number(0, 1e6))
local money_to_spend = Engine.rand:Number(0, 1e4)
ad.amount = math.ceil(money_to_spend / ad.price)
ad.amount = math.ceil(money_to_spend / math.abs(ad.price))

ad.title = string.interp(l.TITLE,
{commodity = ad.commodity:GetName(), price = Format.Money(ad.price)})
Expand Down

0 comments on commit 0b27a79

Please sign in to comment.