Module:Territories: Difference between revisions

From NPOWiki
Jump to navigation Jump to search
create
 
use error module
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}
local territories = mw.loadData('Module:Territories/data')
local territories = mw.loadData('Module:Territories/data')
local err = require('Module:Error').call


--Create keyed index of territories from the data (which is an array)
--Create keyed index of territories from the data (which is an array)
Line 37: Line 37:
return table.concat(getNeighbors(data), ', ')
return table.concat(getNeighbors(data), ', ')
elseif not data[prop] then
elseif not data[prop] then
return '<span class="error">invalid property: ' .. prop .. '</span>'
return err('invalid property: ' .. prop)
else
else
return data[prop]
return data[prop]
Line 63: Line 63:
return table.concat(out, '\n')
return table.concat(out, '\n')
elseif stat == 'neighbors' then
elseif stat == 'neighbors' then
--Return formatted list of neighbors of the given territory
--Return the number of neighbors of the given territory
return #getNeighbors(p.index[tt])
return #getNeighbors(p.index[tt])
elseif stat == 'connected' then
elseif stat == 'connected' then
Line 69: Line 69:
prop = mw.ustring.upper(prop or '')
prop = mw.ustring.upper(prop or '')
if prop == '' then
if prop == '' then
return '<span class="error">two territories required for stat=connected</span>'
return err('two territories required for stat=connected')
end
end
for _, n in ipairs(p.index[tt].neighbors) do
for _, n in ipairs(p.index[tt].neighbors) do
Line 78: Line 78:
return false
return false
else
else
return '<span class="error">invalid stat parameter</span>'
return err('invalid stat parameter')
end
end
end
end
Line 89: Line 89:
if tt ~= '' and not p.index[tt] then
if tt ~= '' and not p.index[tt] then
return '<span class="error">territory "' .. tt .. '" not found</span>'
return err('territory "' .. tt .. '" not found')
end
end
if stat and stat ~= '' then
if stat and stat ~= '' then
return stats(tt, prop, stat)
return stats(tt, prop, stat)
elseif tt ~= '' then
return lookup(tt, prop)
else
else
return lookup(tt, prop)
return err('at least one parameter is required')
end
end
end
end

Latest revision as of 11:55, 21 October 2025

Documentation [edit]

This page is a stub - it only covers the very basics of its subject. More information should be added to make the page informative and useful. If no more information is available, the page should be considered for a merge, redirect, or deletion.

Details: documentation to be written

If you can correct the issue, please edit the page to do so, then remove this notice.
Please see Template:Territory for usage information. Use the template rather than invoking the module. Data for this module is imported from Module:Territories/data.

The above documentation is transcluded from Module:Territories/doc.

local p = {}
local territories = mw.loadData('Module:Territories/data')
local err = require('Module:Error').call

--Create keyed index of territories from the data (which is an array)
local function makeIndex()
	local index = {}
	for i, tt in ipairs(territories) do
		if index[tt.id] then
			mw.log('Duplicate territory found: ' .. tt.id .. ' at index ' .. i)
			tt.id = tt.id .. "_"
		end
		index[tt.id] = tt
	end
	return index
end
p.index = makeIndex()

--Table functions do not work with mw.loadData, so this function returns a new table of the given territory's neighbors.
--Expects a territory object as input.
local function getNeighbors(tt)
	local neighbors = {}
	for _, n in ipairs(tt.neighbors) do
		table.insert(neighbors, n)
	end
	return neighbors
end

local function lookup(tt, prop)
	local data = p.index[tt]
	
	if prop == 'coordinates' then
		return '(' .. data.coordinates.x .. ', ' .. data.coordinates.y .. ')'
	elseif prop == 'x' or prop == 'y' then
		return data.coordinates[prop]
	elseif prop == 'neighbors' then
		return table.concat(getNeighbors(data), ', ')
	elseif not data[prop] then
		return err('invalid property: ' .. prop)
	else
		return data[prop]
	end
end

local function stats(tt, prop, stat)
	if stat == 'overall' then
		--Overall territories stats
		local total = 0
		local possible = 26 * 26 * 26
		local sectors = {0, 0, 0, 0, 0, 0, 0}
		for _, terr in ipairs(territories) do
			total = total + 1
			sectors[terr.sector] = sectors[terr.sector] + 1
		end
		local out = {
			'*Total territories: ' .. total,
			'*Possible three-letter territories: ' .. possible .. ' (' .. mw.ustring.format('%.2f', total/possible*100) .. '% used)',
			'*By sector:',
		}
		for i, sCount in ipairs(sectors) do
			table.insert(out, '**Sector ' .. i .. ': ' .. sCount)
		end
		return table.concat(out, '\n')
	elseif stat == 'neighbors' then
		--Return the number of neighbors of the given territory
		return #getNeighbors(p.index[tt])
	elseif stat == 'connected' then
		--Return boolean indicating whether the two given territories are connected
		prop = mw.ustring.upper(prop or '')
		if prop == '' then
			return err('two territories required for stat=connected')
		end
		for _, n in ipairs(p.index[tt].neighbors) do
			if n == prop then
				return true
			end
		end
		return false
	else
		return err('invalid stat parameter')
	end
end

--To allow for console testing and requiring this module
function p.call(args)
	local tt = mw.ustring.upper(args[1] or '')
	local prop = args[2]
	local stat = args.stat
	
	if tt ~= '' and not p.index[tt] then
		return err('territory "' .. tt .. '" not found')
	end
	
	if stat and stat ~= '' then
		return stats(tt, prop, stat)
	elseif tt ~= '' then
		return lookup(tt, prop)
	else
		return err('at least one parameter is required')
	end
end

function p.main(frame)
	return p.call(frame:getParent().args)
end

return p