Add ldoc documentation

master
rubenwardy 2019-01-17 18:56:34 +00:00
parent 4ea6b9a647
commit be42927648
18 changed files with 733 additions and 26 deletions

2
.gitignore vendored
View File

@ -1,5 +1,5 @@
*.mermaid.*
docs/ldoc
# Created by https://www.gitignore.io/api/linux,lua

2
.gitmodules vendored
View File

@ -3,7 +3,7 @@
url = https://github.com/rubenwardy/ChatCmdBuilder
[submodule "mods/areas"]
path = mods/areas
url = https://github.com/rubenwardy/areas
url = https://github.com/CapitalismGame/areas
[submodule "mods/content/oil"]
path = mods/content/oil
url = https://github.com/rubenwardy/oil

15
docs/config.ld Normal file
View File

@ -0,0 +1,15 @@
project = "Capitalism Game"
title = "Capitalism Game Reference"
format = "discount"
backtick_references = true
readme = "docs/dev_intro.md"
--boilerplate = true
file = {'mods', exclude = {'mods/mtg'}}
template = "docs/template/"
tparam_alias("pos", "table")
tparam_alias("owner", "string")
tparam_alias("player", "string")
tparam_alias("company", "company.Company")
tparam_alias("account", "banking.Account")
tparam_alias("shop", "shop.Shop")

3
docs/gen.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
ldoc mods/ -c docs/config.ld -d docs/ldoc -Mi

316
docs/template/ldoc.ltp vendored Normal file
View File

@ -0,0 +1,316 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=$(ldoc.doc_charset)"/>
<head>
<title>$(ldoc.title)</title>
<link rel="stylesheet" href="$(ldoc.css)" type="text/css" />
# if ldoc.custom_css then -- add custom CSS file if configured.
<link rel="stylesheet" href="$(ldoc.custom_css)" type="text/css" />
# end
</head>
<body>
<div id="container">
<div id="product">
<div id="product_logo"></div>
<div id="product_name"><big><b></b></big></div>
<div id="product_description"></div>
</div> <!-- id="product" -->
<div id="main">
# local no_spaces = ldoc.no_spaces
# local use_li = ldoc.use_li
# local display_name = ldoc.display_name
# local iter = ldoc.modules.iter
# local function M(txt,item) return ldoc.markup(txt,item,ldoc.plain) end
# local nowrap = ldoc.wrap and '' or 'nowrap'
<!-- Menu -->
<div id="navigation">
<br/>
<h1>$(ldoc.project)</h1>
# if not ldoc.single and module then -- reference back to project index
<ul>
<li><a href="../$(ldoc.output).html">Index</a></li>
</ul>
# else
<ul>
<li><strong>Index</strong></li>
</ul>
# end
# --------- contents of module -------------
# if ldoc.no_summary and module and not ldoc.one then -- bang out the functions on the side
# for kind, items in module.kinds() do
<h2>$(kind)</h2>
<ul class="nowrap">
# for item in items() do
<li><a href="#$(item.name)">$(display_name(item))</a></li>
# end
</ul>
# end
# end
# -------- contents of project ----------
# local this_mod = module and module.name
# for kind, mods, type in ldoc.kinds() do
# if ldoc.allowed_in_contents(type,module) then
# if kind == "Modules" then
<h2>Mods</h2>
# elseif kind == "Topics" then
<h2>Articles</h2>
# else
<h2>$(kind)</h2>
# end
<ul class="$(kind=='Topics' and '' or 'nowrap')">
# for mod in mods() do local name = display_name(mod)
# if mod.name == this_mod then
<li><strong>$(name)</strong></li>
# else
<li><a href="$(ldoc.ref_to_module(mod))">$(name)</a></li>
# end
# end
# end
</ul>
# end
</div>
<div id="content">
# if ldoc.body then -- verbatim HTML as contents; 'non-code' entries
$(ldoc.body)
# elseif module then -- module documentation
<h1>$(ldoc.module_typename(module)) <code>$(module.name)</code></h1>
<p>$(M(module.summary,module))</p>
<p>$(M(module.description,module))</p>
# if module.tags.include then
$(M(ldoc.include_file(module.tags.include)))
# end
# if module.see then
# local li,il = use_li(module.see)
<h3>See also:</h3>
<ul>
# for see in iter(module.see) do
$(li)<a href="$(ldoc.href(see))">$(see.label)</a>$(il)
# end -- for
</ul>
# end -- if see
# if module.usage then
# local li,il = use_li(module.usage)
<h3>Usage:</h3>
<ul>
# for usage in iter(module.usage) do
$(li)<pre class="example">$(ldoc.escape(usage))</pre>$(il)
# end -- for
</ul>
# end -- if usage
# if module.info then
<h3>Info:</h3>
<ul>
# for tag, value in module.info:iter() do
<li><strong>$(tag)</strong>: $(M(value,module))</li>
# end
</ul>
# end -- if module.info
# if not ldoc.no_summary then
# -- bang out the tables of item types for this module (e.g Functions, Tables, etc)
# for kind,items in module.kinds() do
<h2><a href="#$(no_spaces(kind))">$(kind)</a></h2>
<table class="function_list">
# for item in items() do
<tr>
<td class="name" $(nowrap)><a href="#$(item.name)">$(display_name(item))</a></td>
<td class="summary">$(M(item.summary,item))</td>
</tr>
# end -- for items
</table>
#end -- for kinds
<br/>
<br/>
#end -- if not no_summary
# --- currently works for both Functions and Tables. The params field either contains
# --- function parameters or table fields.
# local show_return = not ldoc.no_return_or_parms
# local show_parms = show_return
# for kind, items in module.kinds() do
# local kitem = module.kinds:get_item(kind)
# local has_description = kitem and ldoc.descript(kitem) ~= ""
<h2 class="section-header $(has_description and 'has-description')"><a name="$(no_spaces(kind))"></a>$(kind)</h2>
$(M(module.kinds:get_section_description(kind),nil))
# if kitem then
# if has_description then
<div class="section-description">
$(M(ldoc.descript(kitem),kitem))
</div>
# end
# if kitem.usage then
<h3>Usage:</h3>
<pre class="example">$(ldoc.prettify(kitem.usage[1]))</pre>
# end
# end
<dl class="function">
# for item in items() do
<dt>
<a name = "$(item.name)"></a>
<strong>$(display_name(item))</strong>
# if ldoc.prettify_files and ldoc.is_file_prettified[item.module.file.filename] then
<a style="float:right;" href="$(ldoc.source_ref(item))">line $(item.lineno)</a>
# end
</dt>
<dd>
$(M(ldoc.descript(item),item))
# if ldoc.custom_tags then
# for custom in iter(ldoc.custom_tags) do
# local tag = item.tags[custom[1]]
# if tag and not custom.hidden then
# local li,il = use_li(tag)
<h3>$(custom.title or custom[1]):</h3>
<ul>
# for value in iter(tag) do
$(li)$(custom.format and custom.format(value) or M(value))$(il)
# end -- for
# end -- if tag
</ul>
# end -- iter tags
# end
# if show_parms and item.params and #item.params > 0 then
# local subnames = module.kinds:type_of(item).subnames
# if subnames then
<h3>$(subnames):</h3>
# end
<ul>
# for parm in iter(item.params) do
# local param,sublist = item:subparam(parm)
# if sublist then
<li><span class="parameter">$(sublist)</span>$(M(item.params.map[sublist],item))
<ul>
# end
# for p in iter(param) do
# local name,tp,def = item:display_name_of(p), ldoc.typename(item:type_of_param(p)), item:default_of_param(p)
<li><span class="parameter">$(name)</span>
# if tp ~= '' then
<span class="types">$(tp)</span>
# end
$(M(item.params.map[p],item))
# if def == true then
(<em>optional</em>)
# elseif def then
(<em>default</em> $(def))
# end
# if item:readonly(p) then
<em>readonly</em>
# end
</li>
# end
# if sublist then
</li></ul>
# end
# end -- for
</ul>
# end -- if params
# if show_return and item.retgroups then local groups = item.retgroups
<h3>Returns:</h3>
# for i,group in ldoc.ipairs(groups) do local li,il = use_li(group)
<ol>
# for r in group:iter() do local type, ctypes = item:return_type(r); local rt = ldoc.typename(type)
$(li)
# if rt ~= '' then
<span class="types">$(rt)</span>
# end
$(M(r.text,item))$(il)
# if ctypes then
<ul>
# for c in ctypes:iter() do
<li><span class="parameter">$(c.name)</span>
<span class="types">$(ldoc.typename(c.type))</span>
$(M(c.comment,item))</li>
# end
</ul>
# end -- if ctypes
# end -- for r
</ol>
# if i < #groups then
<h3>Or</h3>
# end
# end -- for group
# end -- if returns
# if show_return and item.raise then
<h3>Raises:</h3>
$(M(item.raise,item))
# end
# if item.see then
# local li,il = use_li(item.see)
<h3>See also:</h3>
<ul>
# for see in iter(item.see) do
$(li)<a href="$(ldoc.href(see))">$(see.label)</a>$(il)
# end -- for
</ul>
# end -- if see
# if item.usage then
# local li,il = use_li(item.usage)
<h3>Usage:</h3>
<ul>
# for usage in iter(item.usage) do
$(li)<pre class="example">$(ldoc.prettify(usage))</pre>$(il)
# end -- for
</ul>
# end -- if usage
</dd>
# end -- for items
</dl>
# end -- for kinds
# else -- if module; project-level contents
# if ldoc.description then
<h2>$(M(ldoc.description,nil))</h2>
# end
# if ldoc.full_description then
<p>$(M(ldoc.full_description,nil))</p>
# end
# for kind, mods in ldoc.kinds() do
<h2>$(kind)</h2>
# kind = kind:lower()
<table class="module_list">
# for m in mods() do
<tr>
<td class="name" $(nowrap)><a href="$(no_spaces(kind))/$(m.name).html">$(m.name)</a></td>
<td class="summary">$(M(ldoc.strip_header(m.summary),m))</td>
</tr>
# end -- for modules
</table>
# end -- for kinds
# end -- if module
</div> <!-- id="content" -->
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/stevedonovan/LDoc">LDoc $(ldoc.version)</a></i>
<i style="float:right;">Last updated $(ldoc.updatetime) </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
</html>

View File

@ -1,8 +1,26 @@
--- Allows actions to be audited by companies
--
-- @module audit
--- audit table
audit = {}
--- Creates an `Auditor` object.
--
-- @function __call
-- @string area Area name
-- @usage local adt = audit("company")
--adt:pos(username, comp, message)
function audit.make(_, area)
local ret = {}
--- @type Auditor
--- Log a message
-- @function Auditor:post
-- @string username
-- @tparam company.Company|string comp
-- @string message
function ret:post(username, comp, message)
local cname = comp
if type(cname) ~= "string" then

View File

@ -1,6 +1,18 @@
--- Introduces bank accounts and transactions.
-- @module banking
--- Account
--
-- Class which represents a bank account
-- @type Account
local Account = {}
banking.Account = Account
--- Constructor
--
-- @param obj A table to construct an object on top of
-- @treturn banking.Account
function Account:new(obj)
obj = obj or {}
setmetatable(obj, self)
@ -11,6 +23,10 @@ function Account:new(obj)
return obj
end
--- Export to Lua table
--
-- @treturn table
function Account:to_table()
return {
owner = self.owner,
@ -19,6 +35,11 @@ function Account:to_table()
}
end
--- Import from Lua table
--
-- @tparam table t
-- @treturn bool true for success
function Account:from_table(t)
self.owner = t.owner
self.balance = t.balance
@ -29,6 +50,13 @@ function Account:from_table(t)
type(self.ledger) == "table"
end
--- Withdraw from account
--
-- @int amount
-- @string to target account
-- @string reason Transaction message
-- @treturn bool Returns false on failure
function Account:withdraw(amount, to, reason)
assert(amount > 0)
@ -40,7 +68,13 @@ function Account:withdraw(amount, to, reason)
return true
end
function Account:deposit(amount, to, reason)
--- Deposit into account
--
-- @int amount
-- @string from from account
-- @string reason Transaction message
function Account:deposit(amount, from, reason)
assert(amount > 0)
self.balance = self.balance + amount

View File

@ -1,8 +1,16 @@
---
-- @module banking
banking._accounts = {}
banking._account_by_owner = {}
local adt = audit("banking")
--- Gets the balance for a particular account
--
-- @string owner Account owner
-- @treturn int
function banking.get_balance(owner)
if type(owner) == "table" then
owner = owner.name
@ -14,6 +22,9 @@ function banking.get_balance(owner)
return acc.balance
end
--- Adds an account to the local cache
-- @account acc
function banking.add_account(acc)
assert(not banking._account_by_owner[acc.owner])
@ -24,12 +35,27 @@ function banking.add_account(acc)
return acc
end
--- Gets account by owner name
--
-- @owner owner
-- @treturn banking.Account
function banking.get_by_owner(owner)
assert(type(owner) == "string")
return banking._account_by_owner[owner]
end
--- Transfers money between accounts, with permission checking
--
-- @string actor User requesting this transfer
-- @string from From account
-- @string to Target account
-- @int amount
-- @string reason Transaction reason
-- @treturn true
-- @error Error message
function banking.transfer(actor, from, to, amount, reason)
assert(type(actor) == "string")
assert(type(from) == "string")

View File

@ -1,3 +1,7 @@
---
-- @module banking
--- banking table
banking = {}
print("[banking] loading...")

View File

@ -1,3 +1,8 @@
--- Introduces the concept of companies and the relationship between
-- companies and players. It adds permissions, but does not add stocks or shares.
--
-- @module company
_.extend(company, {
_companies = {},
_companies_by_name = {},
@ -8,16 +13,28 @@ local adt = audit("company")
local player_exists = minetest and minetest.player_exists or
function() return true end
--- Get a company by its name.
-- Company names are always in the form "c:[a-z-_]+"
-- @treturn company.Company
function company.get_by_name(name)
assert(type(name) == "string")
return company._companies_by_name[name:lower()]
end
--- Verifies where a given string is a valid company name
-- @treturn bool
function company.check_name(name)
return ("^(c:[a-z]+)$"):match(name)
end
--- Creates a new company from a company reference
--
-- @tparam company.Company obj
-- @treturn bool
function company.create(obj)
assert(type(obj) == "table")
assert(obj.get_ownership)
@ -35,6 +52,13 @@ function company.create(obj)
return true
end
--- Adds a company to in-memory store.
--
-- Called by load and create.
--
-- @tparam company.Company obj
-- @treturn bool
function company.add(obj)
assert(type(obj) == "table")
assert(obj.get_ownership)
@ -56,6 +80,14 @@ function company.add(obj)
return true
end
--- Set the active company for a player
--
-- Performs permission checks.
--
-- @string pname
-- @tparam company.Company|string comp
-- @treturn bool
function company.set_active(pname, comp)
assert(type(pname) == "string")
assert(player_exists(pname))
@ -76,6 +108,10 @@ function company.set_active(pname, comp)
end
end
--- Get active company for player name
--
-- @string pname
-- @treturn ?company.Company
function company.get_active(pname)
assert(type(pname) == "string")
assert(player_exists(pname))
@ -84,6 +120,18 @@ function company.get_active(pname)
return name and company.get_by_name(name) or nil
end
--- Check whether a player has a certain permission for a company
--
-- Calling with a nil or nonexistent company will always result in
-- a return value of false
--
-- @see company.permissions
-- @string pname the username
-- @tparam ?string cname
-- @string permission permission name, string
-- @tparam ?table meta Metadata about this request
-- @treturn bool
function company.check_perm(pname, cname, permission, meta)
assert(type(pname) == "string")
assert(player_exists(pname))
@ -104,6 +152,12 @@ function company.check_perm(pname, cname, permission, meta)
return comp:check_perm(pname, permission, meta)
end
--- Get active company for player name,
-- or send chat message if no active companies.
--
-- @string pname
-- @treturn ?company.Company
function company.get_active_or_msg(pname)
assert(type(pname) == "string")
assert(player_exists(pname))
@ -118,6 +172,10 @@ function company.get_active_or_msg(pname)
end
end
--- List companies player is a member or CEO of
-- @string pname
-- @treturn [company.Company]
function company.get_companies_for_player(pname)
assert(type(pname) == "string")
assert(player_exists(pname))
@ -131,6 +189,16 @@ function company.get_companies_for_player(pname)
return comps
end
--- Set the permissions for a particular member on a company
--
-- @tparam company.Company comp
-- @string actor The player which is performing this action
-- @string target The member which is having their perms set
-- @string permission Permission name
-- @bool is_grant Whether the permission should be added or taken away
-- @treturn true
-- @error string Error message
function company.set_perms(comp, actor, target, permission, is_grant)
if not comp:check_perm(actor, "MANAGE_MEMBERS",
{ action = "add", name = "username" }) then
@ -182,14 +250,27 @@ function company.register_on_create(func)
table.insert(company.registered_on_creates, func)
end
--- @see company.register_panel
company.registered_panels = {}
--- Register a panel, shown as colored boxes on the Company GUI
-- @tparam table def
function company.register_panel(def)
assert(type(def) == "table")
table.insert(company.registered_panels, def)
end
---@see company.register_snippet
company.registered_snippets = {}
--- Register a snippet to go on company switchers
-- @string name
-- @tparam function func
function company.register_snippet(name, func)
assert(type(name) == "string")
assert(type(func) == "function")
@ -198,6 +279,7 @@ function company.register_snippet(name, func)
company.registered_snippets[name] = func
end
-- Minetest won't be available in tests
if minetest then
local storage = minetest.get_mod_storage()

View File

@ -1,6 +1,19 @@
---
-- @module company
--- Company
--
-- Class which represents a company or government entity.
-- @type Company
local Company = {}
company.Company = Company
--- Constructor
--
-- @param obj A table to construct an object on top of
-- @treturn company.Company
function Company:new(obj)
obj = obj or {}
setmetatable(obj, self)
@ -10,6 +23,10 @@ function Company:new(obj)
return obj
end
--- Export to Lua table
--
-- @treturn table
function Company:to_table()
assert(self.name:sub(1, 2) == "c:")
return {
@ -20,6 +37,11 @@ function Company:to_table()
}
end
--- Import from Lua table
--
-- @tparam table t
-- @treturn bool true on success, false on failure
function Company:from_table(t)
self.title = t.title
self.name = t.name
@ -31,6 +53,10 @@ function Company:from_table(t)
return self.name ~= nil and self.ceo ~= nil and type(self.members) == "table"
end
--- Calculate an appropriate name from a given title
--
-- @string title
function Company:set_title_calc_name(title)
assert(type(title) == "string")
@ -38,10 +64,19 @@ function Company:set_title_calc_name(title)
self.name = "c:" .. title:lower():gsub("%W", "_")
end
--- Get the name of the current CEO or president
--
-- @treturn string
function Company:get_ceo_name()
return self.ceo
end
--- How must does a particular username own of this company?
--
-- @string username
-- @treturn number Proportion of ownership, as a number out of 1.
function Company:get_ownership(username)
-- TODO: ownership
if self.ceo == username then
@ -51,10 +86,23 @@ function Company:get_ownership(username)
end
end
--- Whether a particular player can act as this company
--
-- @string username
-- @treturn bool
function Company:can_become_active(username)
return self:check_perm(username, "SWITCH_TO")
end
--- Check whether a player has a certain permission
--
-- @see company.permissions
-- @string username the username
-- @string permission permission name, string
-- @tparam ?table meta Metadata about this request
-- @treturn bool
function Company:check_perm(username, permission, meta)
assert(type(username) == "string")
assert(type(permission) == "string")
@ -69,6 +117,14 @@ function Company:check_perm(username, permission, meta)
return member and member.perms[permission] and true or false
end
--- Adds a member to the company
--
-- Members are players other than the CEO that can have permissions
-- granted to them by the company
--
-- @string username
-- @treturn Member table
function Company:add_member(username)
assert(not self.members[username])
@ -83,6 +139,15 @@ function Company:add_member(username)
return mem
end
--- Check whether this company is a governmental entity.
--
-- Note: does NOT check whether the company is public or government-owned.
-- This condition imposes certain requirements on the government entity,
-- such as elections and voting, which do not make sense with government-owned
-- companies.
--
-- @treturn bool
function Company:is_government()
return self.name == "c:government"
end

View File

@ -1,3 +1,9 @@
---
-- @module company
--- Shows dialog to select company
-- @string name Player name to show to
-- @function company.show_company_select_dialog
company.show_company_select_dialog = lib_quickfs.register("company:set_company", {
get = function(context, player)
local pname = player:get_player_name()

View File

@ -1,3 +1,7 @@
---
-- @module company
--- company table
company = {}
print("[company] loading...")

View File

@ -1,3 +1,7 @@
---
-- @module company
--- Permissions table
company.permissions = {
SWITCH_TO = "Can act on behalf of the company - needed to do anything else",
EDIT_DETAILS = "Can edit the company's details, including name and branding",

View File

@ -1,7 +1,24 @@
--- Introduces land plots, and modifies areas to allow companies to own land.
--
-- This mod current uses areas as a backend, which means that a plot = an area.
--
-- @module land
--- Dictionary of valid types
land.valid_types = { commercial = true, residential = true, industrial = true }
local adt = audit("land")
--- Generates a tree representing land ownership hierarchy for a particular owner.
--
-- Each element returned will have a children property, which will be a table
-- of child elements.
--
-- @tparam [table] list A list of owned areas
-- @owner owner Player name or company name
-- @treturn (table,table) root, area by id
function land.get_area_tree(list, owner)
assert(list == nil or type(list) == "table")
assert(owner == nil or type(owner) == "string")
@ -56,6 +73,11 @@ function land.get_area_tree(list, owner)
return root, item_by_id
end
--- Gets all plots owned by a particular owner
--
-- @owner owner Player name or company name
-- @treturn [table] List of plots
function land.get_all(owner)
local lands = {}
for id, area in pairs(areas.areas) do
@ -68,6 +90,17 @@ function land.get_all(owner)
return lands
end
--- Gets the owning plot of a particular area
--
-- There is at most one owning area of a particular position; because
-- plots must not overlap, and any child of a plot most be fully contained.
--
-- This function essential returns the lowest plot in the tree at a particular
-- point
--
-- @pos pos
-- @treturn [table]
function land.get_by_pos(pos)
local areas = areas:getAreasAtPos(pos)
@ -90,6 +123,11 @@ function land.get_by_pos(pos)
return first
end
--- Gets a plot by its area ID (as in the mod area)
--
-- @int id
-- @treturn table plot
function land.get_by_area_id(id)
assert(type(id) == "number")
@ -97,6 +135,14 @@ function land.get_by_area_id(id)
return area and area.land_type and area
end
--- Transfers a plot between two owners, after checking relevant permissions.
--
-- @int id
-- @owner newowner
-- @player pname
-- @treturn true
-- @error Error message
function land.transfer(id, newowner, pname)
assert(type(id) == "number")
assert(type(newowner) == "string")
@ -145,6 +191,14 @@ function land.transfer(id, newowner, pname)
return true, "Transfered area id=" .. id .. " to " .. newowner
end
--- Whether a user can put a plot up for sale, or change the price when already
-- for sale
--
-- @tparam table area
-- @player pname
-- @treturn true
-- @error Error message
function land.can_set_price(area, pname)
if not area or not area.land_type then
return false, "Unable to sell unowned or unclassified (ie: c/i/r) area"
@ -172,6 +226,14 @@ function land.can_set_price(area, pname)
return true
end
--- Puts a plot up for sale, or changes the price when already for sale
--
-- @tparam table area
-- @player pname
-- @number price
-- @treturn true
-- @error Error message
function land.set_price(area, pname, price)
assert(type(area) == "table")
assert(pname == nil or type(pname) == "string")
@ -196,6 +258,14 @@ function land.set_price(area, pname, price)
return true
end
--- Whether a user can buy a plot
--
-- @tparam table area
-- @player pname
-- @company comp
-- @treturn true
-- @error Error message
function land.can_buy(area, pname, comp)
assert(type(area) == "table")
assert(type(pname) == "string")
@ -224,6 +294,13 @@ function land.can_buy(area, pname, comp)
return true
end
--- Buy a plot
--
-- @tparam table area
-- @player pname
-- @treturn true
-- @error Error message
function land.buy(area, pname)
assert(type(area) == "table")
assert(type(pname) == "string")
@ -261,6 +338,13 @@ function land.buy(area, pname)
return true
end
--- Whether a user can teleport to a plot
--
-- @tparam table area
-- @player pname
-- @treturn true
-- @error Error message
function land.can_teleport_to(area, pname)
if type(pname) == "userdata" then
pname = pname:get_player_name()
@ -283,6 +367,13 @@ function land.can_teleport_to(area, pname)
return true
end
--- Teleport a user to a plot
--
-- @tparam table area
-- @param player Player userdata
-- @treturn true
-- @error Error message
function land.teleport_to(area, player)
assert(type(area) == "table")
assert(type(player) == "userdata")
@ -297,6 +388,13 @@ function land.teleport_to(area, player)
return suc, msg
end
--- Whether a user can set the spawn of a plot
--
-- @tparam table area
-- @player pname
-- @treturn true
-- @error Error message
function land.can_set_spawn(area, pname)
if type(pname) == "userdata" then
pname = pname:get_player_name()

View File

@ -1,3 +1,7 @@
---
-- @module land
--- land table
land = {}
print("[land] loading...")

View File

@ -1,20 +1,32 @@
--- Adds shops
--
-- @module shop
_.extend(shop, {
_shops = {},
_shops_by_area = {},
_context = {}
})
--- Get a shop by its `land` area ID
--
-- @int aid
function shop.get_by_area(aid)
assert(type(aid) == "number")
return shop._shops_by_area[aid]
end
--- @pos pos
function shop.get_by_pos(pos)
assert(type(pos) == "table")
local area = land.get_by_pos(pos)
return area and shop.get_by_area(area.id)
end
--- @tparam table s
function shop.add_shop(s)
assert(not shop._shops_by_area[s.a_id])
@ -23,25 +35,10 @@ function shop.add_shop(s)
return true
end
function shop.init_inventory(name)
return minetest.create_detached_inventory("shop_inv_" .. name, {
allow_put = function(inv, listname, index, stack, player)
return 0
end,
allow_take = function(inv, listname, index, stack, player)
return 0
end
})
end
function shop.get_inventory_or_create(name)
return minetest.get_inventory({
type = "detached",
name = "shop_inv_" .. name
}) or shop.init_inventory(name)
end
--- Checks whether a user can use the admin interface for the shop
--
-- @player pname
-- @pos pos
function shop.can_admin(pname, pos)
local area = land.get_by_pos(pos)
local comp = company.get_by_name(area.owner)
@ -50,6 +47,13 @@ function shop.can_admin(pname, pos)
company.check_perm(pname, comp.name, "SHOP_ADMIN", { pos = pos })
end
--- Creates shops, checks permissions
--
-- @player pname
-- @pos pos
-- @treturn true
-- @error Error message
function shop.create_shop(pname, pos)
local area = land.get_by_pos(pos)
local comp = company.get_by_name(area.owner)
@ -71,6 +75,12 @@ function shop.create_shop(pname, pos)
return true
end
--- Unassigns item allocation of chest. Will return any items.
--
-- @shop s
-- @pos pos
-- @param inv Inventory userdata
function shop.unassign_chest(s, pos, inv)
local chest = s:get_chest(pos)
if chest.itemname then
@ -92,6 +102,16 @@ function shop.unassign_chest(s, pos, inv)
chest.count = 0
end
--- Can user buy from shop, checks permissions
--
-- @pos pos
-- @player pname
-- @string itemname
-- @int count
-- @int price
-- @treturn true
-- @error Error message
function shop.can_buy(pos, pname, itemname, count, price)
assert(type(pos) == "table")
assert(type(pname) == "string")
@ -117,6 +137,16 @@ function shop.can_buy(pos, pname, itemname, count, price)
return true
end
--- Buy from shop, checks permissions
--
-- @pos pos
-- @player pname
-- @tparam table item
-- @int count
--
-- @treturn true
-- @error Error message
function shop.buy(pos, pname, item, count)
assert(type(pos) == "table")
assert(type(pname) == "string")
@ -193,6 +223,7 @@ function shop.buy(pos, pname, item, count)
return true
end
-- Minetest won't be available in tests
if minetest then
local storage = minetest.get_mod_storage()

View File

@ -21,9 +21,6 @@
-- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-- OTHER DEALINGS IN THE SOFTWARE.
--- Underscore is a set of utility functions for dealing with
-- iterators, arrays, tables, and functions.
local Underscore = { funcs = {} }
Underscore.__index = Underscore
@ -59,7 +56,7 @@ function Underscore.range(start_i, end_i, step)
return Underscore:new(range_iter)
end
--- Identity function. This function looks useless, but is used throughout Underscore as a default.
-- Identity function. This function looks useless, but is used throughout Underscore as a default.
-- @name _.identity
-- @param value any object
-- @return value