Add documentation to Shop class

master
rubenwardy 2019-01-18 21:15:36 +00:00
parent be42927648
commit 04ee7426b8
1 changed files with 80 additions and 0 deletions

View File

@ -1,6 +1,29 @@
--- @module shop
--- @type Shop
local Shop = {} local Shop = {}
shop.Shop = Shop shop.Shop = Shop
--- StockedItem
-- @table StockedItem
-- @tfield string Item name
-- @tfield int Count in stock, across all chests
-- @tfield int Set price, -1 for not for sale
-- @tfield int Count sold
--- Chest
-- @table Chest
-- @tfield table pos Position
-- @tfield string itemname Assigned item name, nil for unassigned
-- @tfield int count Stored item count
--- Constructor
--
-- @param obj A table to construct an object on top of
-- @treturn company.Company
function Shop:new(obj) function Shop:new(obj)
obj = obj or {} obj = obj or {}
setmetatable(obj, self) setmetatable(obj, self)
@ -14,6 +37,10 @@ function Shop:new(obj)
return obj return obj
end end
--- Export to Lua table
--
-- @treturn table
function Shop:to_table() function Shop:to_table()
return { return {
name = self.name, name = self.name,
@ -23,6 +50,11 @@ function Shop:to_table()
} }
end end
--- Import from Lua table
--
-- @tparam table tab
-- @treturn bool true on success, false on failure
function Shop:from_table(tab) function Shop:from_table(tab)
self.name = tab.name self.name = tab.name
self.a_id = tab.a_id self.a_id = tab.a_id
@ -32,14 +64,26 @@ function Shop:from_table(tab)
return self.a_id ~= nil return self.a_id ~= nil
end end
--- Get table of items sold by this shop
--
-- @treturn table Keys are resolved item names, values are `StockedItem`.
function Shop:get_items() function Shop:get_items()
return self.items return self.items
end end
--- Get `StockedItem` by item name
--
-- @treturn table
function Shop:get_item(name) function Shop:get_item(name)
return self.items[name] return self.items[name]
end end
--- Get stock item table by item name, or make it if it doesn't exist.
--
-- @treturn table
function Shop:get_item_or_make(name) function Shop:get_item_or_make(name)
local item = self.items[name] or { local item = self.items[name] or {
name = name, name = name,
@ -52,21 +96,40 @@ function Shop:get_item_or_make(name)
return item return item
end end
--- Add a chest to store stock in
--
-- @pos pos
-- @treturn table chest table
function Shop:add_chest(pos) function Shop:add_chest(pos)
local posstr = minetest.pos_to_string(vector.floor(pos)) local posstr = minetest.pos_to_string(vector.floor(pos))
assert(not self.chests[posstr]) assert(not self.chests[posstr])
self.chests[posstr] = { self.chests[posstr] = {
pos = vector.new(pos), pos = vector.new(pos),
itemname = nil, itemname = nil,
count = 0, count = 0,
} }
return self.chests[posstr]
end end
--- Get a chest by pos
--
-- @pos pos
-- @treturn table chest table
function Shop:get_chest(pos) function Shop:get_chest(pos)
local posstr = minetest.pos_to_string(vector.floor(pos)) local posstr = minetest.pos_to_string(vector.floor(pos))
return self.chests[posstr] return self.chests[posstr]
end end
--- Get a list of chests which contain enough itmes to
-- fulfill the requested name and count, or nil
--
-- @string name Item name
-- @int count Number of items, >0
-- @func filter func(chest_table)
-- @treturn [table]|nil
function Shop:get_chests_for_item(name, count, filter) function Shop:get_chests_for_item(name, count, filter)
local ret = {} local ret = {}
for _, chest in pairs(self.chests) do for _, chest in pairs(self.chests) do
@ -82,6 +145,13 @@ function Shop:get_chests_for_item(name, count, filter)
return nil return nil
end end
--- Reads chest inventory, and updates stock counts
--
-- This should be avoided, and the incremental methods used instead.
--
-- @pos pos
-- @tparam InvRef inv
function Shop:chest_poll(pos, inv) function Shop:chest_poll(pos, inv)
local chest = self:get_chest(pos) local chest = self:get_chest(pos)
assert(chest) assert(chest)
@ -115,6 +185,11 @@ function Shop:chest_poll(pos, inv)
end end
end end
--- An item has been added to a chest, make note of it
--
-- @pos pos
-- @tparam ItemStack stack
function Shop:chest_add_item(pos, stack) function Shop:chest_add_item(pos, stack)
local chest = self:get_chest(pos) local chest = self:get_chest(pos)
assert(chest) assert(chest)
@ -126,6 +201,11 @@ function Shop:chest_add_item(pos, stack)
new.stock = new.stock + stack:get_count() new.stock = new.stock + stack:get_count()
end end
--- An item has been removed from a chest, make note of it
--
-- @pos pos
-- @tparam ItemStack stack
function Shop:chest_remove_item(pos, stack) function Shop:chest_remove_item(pos, stack)
local chest = self:get_chest(pos) local chest = self:get_chest(pos)
assert(chest) assert(chest)