Skip to content

Commit

Permalink
Merge pull request #52 from lmst2/master
Browse files Browse the repository at this point in the history
adding options to select qualiy when buying items
  • Loading branch information
djmango authored Oct 29, 2024
2 parents ab29584 + 4cbf0f1 commit 2876ce1
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 30 deletions.
6 changes: 6 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
---------------------------------------------------------------------------------------------------
Version: 2.2.5
Date: 2024-10-28
Changes:
- Adds the ability to set the quality of items when purchasing
---------------------------------------------------------------------------------------------------
Version: 2.2.0
Date: 2024-10-24
Changes:
Expand Down
119 changes: 90 additions & 29 deletions control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ local gui = require('__flib__.gui')

local trader_type = { item=1, fluid=2, energy=3, "item", "fluid", "energy" }
local energy_name = "market-energy" -- name of fake energy item
local quality_names = {"normal", "uncommon", "rare", "epic", "legendary"}
local quality_multipliers = {
normal = 1,
uncommon = 2.1,
rare = 4.8,
epic = 10.5,
legendary = 35
}

local trader_signals =
{
Expand Down Expand Up @@ -436,7 +444,9 @@ local function update_menu_trader( player, player_mem, update_orders )
if trader.sell_or_buy then
local sold_name = trader.sold_name
if sold_name then
local multiplier = 1
if trader.type == trader_type.item then
multiplier = quality_multipliers[trader.sold_quality]
player_mem.but_blkmkt_trader_sold.sprite = "item/" .. sold_name
player_mem.but_blkmkt_trader_sold.tooltip = prototypes.get_item_filtered({})[sold_name].localised_name
elseif trader.type == trader_type.fluid then
Expand All @@ -448,7 +458,7 @@ local function update_menu_trader( player, player_mem, update_orders )
end
local price = storage.prices[sold_name]
if price then
player_mem.lbl_blkmkt_trader_sold.caption = " " .. format_money(price.current) .. " " .. format_evolution(price.evolution)
player_mem.lbl_blkmkt_trader_sold.caption = " " .. format_money(price.current * multiplier) .. " " .. format_evolution(price.evolution)
else
player_mem.lbl_blkmkt_trader_sold.caption = " -"
end
Expand All @@ -461,17 +471,28 @@ local function update_menu_trader( player, player_mem, update_orders )

if update_orders then
clean_gui(player_mem.scr_blkmkt_trader_orders)
local gui2 = player_mem.scr_blkmkt_trader_orders.add({type = "table", name = "tab_blkmkt_trader_orders", column_count = 4, style = "table_blkmkt_style"})
local gui2
if trader.type == trader_type.item then
gui2 = player_mem.scr_blkmkt_trader_orders.add({type = "table", name = "tab_blkmkt_trader_orders", column_count = 5, style = "table_blkmkt_style"})
else
gui2 = player_mem.scr_blkmkt_trader_orders.add({type = "table", name = "tab_blkmkt_trader_orders", column_count = 4, style = "table_blkmkt_style"})
end

gui2.add({type = "label"})
gui2.add({type = "label", caption = "name", style = "label_blkmkt_style"})
gui2.add({type = "label", caption = "count", style = "label_blkmkt_style"})
gui2.add({type = "label", caption = "price", style = "label_blkmkt_style"})

function add_order(n,prefix,name,count,del_but)
if trader.type == trader_type.item then
gui2.add({type = "label", caption = "quality", style = "label_blkmkt_style"})
end

function add_order(n,prefix,name,count,quality,del_but)
local price = storage.prices[name]
local current, evol
if price then
if price and quality then
current = price.current * quality_multipliers[quality_names[quality]]
evol = price.evolution
elseif price then
current = price.current
evol = price.evolution
else
Expand All @@ -490,25 +511,35 @@ local function update_menu_trader( player, player_mem, update_orders )
else
gui2.add({type = "sprite-button", name = "but_blkmkt_ori_" .. string.format("%4d",n) .. name, sprite = prefix .. name, style = "sprite_obj_blkmkt_style"})
end

gui2.add({type = "textfield", name = "but_blkmkt_orc_" .. string.format("%4d",n) .. name, text = count, style = "textfield_blkmkt_style"})
gui2.add({type = "label", caption = format_money(current) .. " " .. format_evolution(evol), style = "label_blkmkt_style"})

if quality then
gui3 = gui2.add({type = "drop-down", name = "dpn_blkmkt_qlt_" .. string.format("%4d",n) .. name})
gui3.add_item("normal")
gui3.add_item("uncommon")
gui3.add_item("rare")
gui3.add_item("epic")
gui3.add_item("legendary")
gui3.selected_index = quality
end
end

if trader.type == trader_type.item then
for n, order in pairs(trader.orders) do
if n > 99 then break end
add_order(n,"item/",order.name,order.count,true)
add_order(n,"item/",order.name,order.count,order.quality,true)
end
elseif trader.type == trader_type.fluid then
local order = trader.orders[1]
if order then
add_order(1,"fluid/",order.name,order.count,false)
add_order(1,"fluid/",order.name,order.count,order.quality,false)
end
elseif trader.type == trader_type.energy then
local order = trader.orders[1]
if order then
add_order(1,nil,energy_name,order.count,false)
add_order(1,nil,energy_name,order.count,order.quality,false)
end
end
end
Expand Down Expand Up @@ -591,7 +622,8 @@ local function build_menu_objects(player, open_or_close, ask_sel)
end

main_window.add({type = "button", name = "but_blkmkt_itml_refresh", caption = {"blkmkt-gui-refresh"}, style = "button_blkmkt_style"})



if ask_sel then
main_window.add({type = "button", name = "but_blkmkt_itml_cancel", caption = {"blkmkt-gui-cancel"}, style = "button_blkmkt_style"})
else
Expand Down Expand Up @@ -1233,6 +1265,7 @@ local function init_trader( trader, level )
trader.orders_tot = 0 -- total of the purchase list, without taxes
trader.orders = {} -- purchase orders, list of {name, count}
trader.sold_name = nil -- name of the last sold item
trader.sold_quality = 1 -- quality of the last sold item
-- trader.price = nil -- price of the main object sold (1 item, fluid or energy)

trader.editer = nil -- player who is currently editing
Expand Down Expand Up @@ -1369,17 +1402,8 @@ local function sell_trader(trader,force_mem,tax_rate)
for i, item in pairs(inv.get_contents()) do
price = storage.prices[item.name]
local quality_multiplier = 1

if item.quality == "normal" then
quality_multiplier = 1
elseif item.quality == "uncommon" then
quality_multiplier = 2.1
elseif item.quality == "rare" then
quality_multiplier = 4.8
elseif item.quality == "epic" then
quality_multiplier = 10.5
elseif item.quality == "legendary" then
quality_multiplier = 35
if item.quality ~= nil then
quality_multiplier = quality_multipliers[item.quality]
end

if price ~= nil then
Expand All @@ -1398,6 +1422,7 @@ local function sell_trader(trader,force_mem,tax_rate)
update_transaction(force_mem,"item",item.name,price,-item.count)
if item.count ~= 0 then
trader.sold_name = item.name
trader.sold_quality = item.quality
end
end
end
Expand All @@ -1419,6 +1444,7 @@ local function sell_trader(trader,force_mem,tax_rate)
update_transaction(force_mem,"fluid",name,price,-amount)
if amount ~= 0 then
trader.sold_name = name
trader.sold_quality = 1
end
end
end
Expand All @@ -1439,6 +1465,7 @@ local function sell_trader(trader,force_mem,tax_rate)

update_transaction(force_mem,"energy",name,price,-count)
trader.sold_name = name
trader.sold_quality = 1
end
end

Expand Down Expand Up @@ -1474,10 +1501,11 @@ local function buy_trader(trader,force_mem,tax_rate)
local order = trader.orders[i]
local name = order.name
local count = order.count
local quality = quality_names[order.quality]
price = storage.prices[name]

if price and count > 0 then
money1 = count * price.current
money1 = count * price.current * quality_multipliers[quality]

if name == "ucoin" then
tax1 = 0
Expand All @@ -1487,9 +1515,9 @@ local function buy_trader(trader,force_mem,tax_rate)

if money1+tax1 <= force_mem.credits then
-- can buy !
local purchased = inv.insert({name=name,count=count})
local purchased = inv.insert({name=name,count=count,quality=quality})
if purchased < count then
money1 = purchased * price.current
money1 = purchased * price.current * quality_multipliers[quality]
if name == "ucoin" then
tax1 = 0
else
Expand Down Expand Up @@ -1702,7 +1730,9 @@ local function compute_trader_data(trader,update_orders)
tot = 0
for _, order in pairs(trader.orders) do
local price = storage.prices[order.name]
if price then
if price and order.quality then
tot = tot + order.count * price.current * quality_multipliers[quality_names[order.quality]]
elseif price then
tot = tot + order.count * price.current
end
end
Expand Down Expand Up @@ -2080,15 +2110,15 @@ local function on_creation( event )
else
if type == trader_type.item then
trader.orders = {
{name = "ucoin", count = 0, price = storage.prices["ucoin"].current },
{name = "ucoin", count = 0, price = storage.prices["ucoin"].current, quality = 1 },
}
elseif type == trader_type.fluid then
trader.orders = {
{name = "crude-oil", count = 0, price = storage.prices["crude-oil"].current },
{name = "crude-oil", count = 0, price = storage.prices["crude-oil"].current, quality = nil },
}
elseif type == trader_type.energy then
trader.orders = {
{name = energy_name, count = 0, price = storage.prices[energy_name].current },
{name = energy_name, count = 0, price = storage.prices[energy_name].current, quality = nil },
}
end
table.insert(force_mem.traders_buy,trader)
Expand Down Expand Up @@ -2704,7 +2734,7 @@ local function on_gui_click(event)

if trader.type == trader_type.item then
if #trader.orders < 99 then
table.insert(trader.orders,1,{name="coal", count=0, price=storage.prices.coal.current})
table.insert(trader.orders,1,{name="coal", count=0, price=storage.prices.coal.current, quality=1})
update_menu_trader(player,player_mem,true)
end
end
Expand Down Expand Up @@ -2751,6 +2781,37 @@ end
script.on_event(defines.events.on_gui_click, on_gui_click)
script.on_event(defines.events.on_gui_checked_state_changed, on_gui_click)

--------------------------------------------------------------------------------------
local function on_gui_selection_state_changed(event)
local player = game.players[event.player_index]
-- local force = player.force
local player_mem = storage.player_mem[player.index]
local event_name = event.element.name
local prefix = string.sub(event_name,1,15)
local nix = tonumber(string.sub(event_name,16,19))
-- local suffix = string.sub(event_name,20)

local trader = player_mem.opened_trader

if prefix == "dpn_blkmkt_qlt_" then

if trader and nix ~= nil then


local order = trader.orders[nix]

if order then
order.quality = event.element.selected_index
end

compute_trader_data(trader,true)
-- update_menu_trader(player,player_mem,true)
end
end
end

script.on_event(defines.events.on_gui_selection_state_changed, on_gui_selection_state_changed)

--------------------------------------------------------------------------------------
local function on_gui_text_changed(event)
local player = game.players[event.player_index]
Expand Down
2 changes: 1 addition & 1 deletion info.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "BlackMarket2",
"version": "2.2.4",
"version": "2.2.5",
"title": "Black Market 2",
"author": "djmango",
"description": "Sell and buy items/fluids/energy on the universal black market using trading chests/tanks/accumulators, choosing the frequency of exchanges and related fees. You can now sell your overproduction and buy things that you don't want to craft by yourself. You can also use these trading units as a paying shipment system.",
Expand Down
6 changes: 6 additions & 0 deletions locale/cn/cn.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ blkmkt-gui-trader-orders=订单:
blkmkt-gui-trader-orders-tt=无税订单合计

blkmkt-gui-refresh=刷新价格
blkmkt-gui-quality=选择质量
blkmkt-gui-quality-normal=普通
blkmkt-gui-quality-uncommon=不常见
blkmkt-gui-quality-rare=稀有
blkmkt-gui-quality-epic=史诗
blkmkt-gui-quality-legendary=传奇
blkmkt-gui-close=关闭
blkmkt-gui-cancel=取消
blkmkt-gui-ok=OK
Expand Down
6 changes: 6 additions & 0 deletions locale/en/en.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ blkmkt-gui-trader-orders=Orders:
blkmkt-gui-trader-orders-tt=Total of the orders list without taxes

blkmkt-gui-refresh=Refresh prices
blkmkt-gui-quality=Select quality
blkmkt-gui-quality-normal=Normal
blkmkt-gui-quality-uncommon=Uncommon
blkmkt-gui-quality-rare=Rare
blkmkt-gui-quality-epic=Epic
blkmkt-gui-quality-legendary=Legendary
blkmkt-gui-close=Close
blkmkt-gui-cancel=Cancel
blkmkt-gui-ok=OK
Expand Down
21 changes: 21 additions & 0 deletions prototypes/styles.lua
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,27 @@ default_gui.button_blkmkt_style =
},
}

default_gui.dropdown_blkmkt_style =
{
type="dropdown_style",
parent="dropdown",
font="font_bold_blkmkt",
align = "center",
default_font_color={r=1, g=1, b=1},
hovered_font_color={r=1, g=1, b=1},
top_padding = 0,
right_padding = 0,
bottom_padding = 0,
left_padding = 0,
left_click_sound =
{
{
filename = "__core__/sound/gui-click.ogg",
volume = 1
}
},
}

default_gui.button_blkmkt_credits_style =
{
type="button_style",
Expand Down

0 comments on commit 2876ce1

Please sign in to comment.