1
0

add bundle requesting

This commit is contained in:
Rokas Puzonas 2022-09-03 17:18:49 +00:00
parent 3fe33524fb
commit 7aa0ab57ac

View File

@ -96,10 +96,6 @@ local function is_bundle_name(name)
return name:find("#") == 1
end
local function get_number_width(num)
return math.floor(math.log(num, 10) + 1)
end
-- TODO: improve this function
local function resolve_bundle_items(bundles, bundle_name, seen_bundles)
seen_bundles = seen_bundles or {}
@ -145,7 +141,7 @@ local function derive_available_bundles(available_items, bundle_details)
bundle_count = math.min(bundle_count, math.max(0, available_count / item_count))
end
if bundle_count ~= math.huge and bundle_count > 0 then
available_bundles[bundle_name] = math.max(bundle_count, 1)
available_bundles[bundle_name] = bundle_count
end
end
return available_bundles
@ -300,13 +296,15 @@ function main_view:process_movement_keys(store, area)
for item, count in pairs(bundle) do
lstore.items[item] = lstore.items[item] - count
end
rstore.bundles[bundle_name] = (rstore.bundles[bundle_name] or 0) + 1
local transferred = math.min(lstore.bundles[bundle_name], 1)
rstore.bundles[bundle_name] = (rstore.bundles[bundle_name] or 0) + transferred
elseif self.left_pressed and rstore.bundles[bundle_name] > 0 then
for item, count in pairs(bundle) do
lstore.items[item] = lstore.items[item] + count
end
rstore.bundles[bundle_name] = rstore.bundles[bundle_name] - 1
local transferred = math.min(rstore.bundles[bundle_name], 1)
rstore.bundles[bundle_name] = rstore.bundles[bundle_name] - transferred
end
else
local item = selected_option
@ -336,6 +334,13 @@ function main_view:display_list(store, area, selected_idx)
local filtered_count = #store.filtered_names
local function get_number_width(num)
return math.floor(math.log(num, 10) + 1)
end
local function is_integer(num)
return num % 1 == 0
end
local count_collumn_width
do -- Figure out how wide does the count column need to be
local max_count = 0
@ -349,7 +354,11 @@ function main_view:display_list(store, area, selected_idx)
end
end
end
count_collumn_width = get_number_width(max_count)
if not is_integer(max_count) then
count_collumn_width = count_collumn_width + 1
end
end
do -- Display the options
@ -371,8 +380,16 @@ function main_view:display_list(store, area, selected_idx)
else
term.setTextColor(colors.white)
end
term.setCursorPos((count_collumn_width+1 - count_width), row)
term.write(count)
if is_integer(count) then
term.setCursorPos((count_collumn_width+1 - count_width), row)
term.write(count)
else
term.setCursorPos((count_collumn_width+1 - count_width - 1), row)
if count >= 1 then
term.write(math.floor(count))
end
term.write("*")
end
term.setCursorPos(count_collumn_width+2, row)
term.write(ensure_width(display_name, area.w-count_collumn_width-1))
end
@ -450,8 +467,10 @@ function main_view:refresh_items()
self.item_registry = list_items(self.inventories)
populate_item_details(self.item_details, self.item_registry)
self.left_store.all_items = get_item_counts(self.item_registry)
self.right_store.all_items = {}
self.right_store.items = {}
self.right_store.bundles = {}
self.left_store.items = get_item_counts(self.item_registry)
self.left_store.bundles = derive_available_bundles(self.left_store.items, self.bundle_details)
self:refresh_filtered_names(self.left_store, self.left_area)
self:refresh_filtered_names(self.right_store, self.right_area)
end
@ -503,6 +522,49 @@ function main_view:display_bundle_popup(area)
end
end
function main_view:request_items()
local lstore = self.left_store
local rstore = self.right_store
if self:has_selected_items() then
for name, count in pairs(rstore.items) do
local transferred = self:move_item(name, count, self.result_inventory)
lstore.items[name] = lstore.items[name] + (count - transferred)
end
for bundle_name, bundle_count in pairs(rstore.bundles) do
bundle_count = math.max(bundle_count, 1)
for item, count in pairs(self.bundle_details[bundle_name]) do
local transferred = self:move_item(item, count*bundle_count, self.result_inventory)
lstore.items[item] = lstore.items[item] + (count*bundle_count - transferred)
end
end
rstore.items = {}
rstore.bundles = {}
lstore.bundles = derive_available_bundles(lstore.items, self.bundle_details)
self:refresh_filtered_names(lstore, self.left_area)
self:refresh_filtered_names(rstore, self.right_area)
else
local selected_option = lstore.filtered_names[lstore.selected_idx]
if is_bundle_name(selected_option) then
local bundle_name = selected_option
for item, count in pairs(self.bundle_details[bundle_name]) do
local transferred = self:move_item(item, count, self.result_inventory)
lstore.items[item] = lstore.items[item] - transferred
end
else
local item = selected_option
local stack_size = self.item_details[item].stack_size
local transferred_count = self:move_item(item, stack_size, self.result_inventory)
lstore.items[item] = lstore.items[item] - transferred_count
end
lstore.bundles = derive_available_bundles(lstore.items, self.bundle_details)
self:refresh_filtered_names(lstore, self.left_area)
self:select_option(lstore, self.left_area, selected_option)
end
end
function main_view:run()
term.setBackgroundColor(colors.black)
term.clear()
@ -564,9 +626,7 @@ function main_view:on_key(key)
end
if not self.bundle_popup then
local has_selected_items = self:has_selected_items()
if key == keys.n and has_selected_items then
if key == keys.n and self:has_selected_items() then
self.bundle_popup = true
elseif key == keys.tab then
@ -576,25 +636,7 @@ function main_view:on_key(key)
self:refresh_items()
elseif self.submit_pressed then
if has_selected_items then
for name, count in pairs(self.right_store.all_items) do
local transferred = self:move_item(name, count, self.result_inventory)
self.left_store.all_items[name] = self.left_store.all_items[name] - transferred
end
self.right_store.all_items = {}
self:refresh_filtered_names(self.left_store, self.left_area)
self:refresh_filtered_names(self.right_store, self.right_area)
else
local selected_item = self.left_store.filtered_names[self.left_store.selected_idx]
if selected_item then
local item = selected_item
local stack_size = self.item_details[item].stack_size
local transferred_count = self:move_item(item, stack_size, self.result_inventory)
self.left_store.all_items[item] = self.left_store.all_items[item] - transferred_count
self:refresh_filtered_names(self.left_store, self.left_area)
self:select_option(self.left_store, self.left_area, item)
end
end
self:request_items()
elseif self.ctrl_down and key == keys.d then
self:deposit_items()