Fractured Online Wiki
Advertisement
Edit-copy purple Documentation

This module is responsible for the output of places.

It has an accompanying Lua data module at Module:Places/Data.
Templates: Template:Places, Template:Places/Continent


local Utils = require("Module:Utils")
local TableTools = require("Module:TableTools")
local List = require("Module:List")

local mw = mw
local sprintf = Utils.sprintf
local map = Utils.map
local tableSort = Utils.tableSort
local keysToList = TableTools.keysToList
local shallowClone = TableTools.shallowClone
local makeList = List.makeList

--[[------------------------------------------------]]
--[[--------------- MODULE FUNCTIONS ---------------]]
--[[------------------------------------------------]]
local p = require("Module:BaseModule"):newModule()
local module_data = p:getModuleData("Module:Test/data")

local main_args = {
    parentFirst = true,
    wrappers = {
        "Template:Places",
        "Template:Places/Continent"
    }
}

p.world = p:makeInvokeFunc('_world', main_args)
p.continent = p:makeInvokeFunc('_continent', main_args)
p.zone = p:makeInvokeFunc('_zone', main_args)
p.getListOfAllZones = p:makeInvokeFunc('_getListOfAllZones', main_args)


--[[------------------------------------------------]]
--[[---------------- PAGE FUNCTIONS ----------------]]
--[[------------------------------------------------]]

----------------------------
-- Continent
--
-- Get zone names for a given continent.
----------------------------
function p._continent(args, frame)  -- luacheck: ignore
    local continent = args[1]
    local html = mw.html.create("ul")
    local stop = false

    for _, continents in pairs(module_data) do
        for world_continent, zones in pairs(continents) do
            if world_continent == continent then
                html
                    :tag("li")
                        :wikitext(sprintf("[[%s]] → %s", world_continent, p._zone(zones)))
                    :done()
                stop = true
                break
            end
        end

        if stop == true then
            break
        end
    end

    return tostring(html:allDone())
end

----------------------------
-- World
--
-- Return world with all continents and continent zones as a
-- two-level <ul>
--
-- * World
--     * Continent -> Zone_List
----------------------------
function p._world(args, frame)  -- luacheck: ignore
    local world = args[1]
    local world_data = module_data[world]

    local html
    local list

    if world ~= "all" then
        -- In case args is not all, makes list with one desired world
        if not world_data then
            -- World doesn't exist
            list = {}
        else
            list = {world}
        end
    else
        -- In case args is all, makes list with all worlds
        list = keysToList(module_data)
    end

    -- If we have at least one world, start creating HTML list
    if #list > 0 then
        html = mw.html.create("ul")
    else
        html = mw.html.create("")
    end
    
    -- Goes through world list and prints world data
    for _, name in ipairs(list) do
        -- Output world name
        html
            :tag("li")
                :wikitext(sprintf("[[%s]]", name))
            :done()

        -- Output continent info in alphabetical order
        world_data = keysToList(module_data[name])

        for _, continent in ipairs(world_data) do
            html:wikitext(p._continent({continent}))
        end
    end

    return tostring(html:allDone())
end

----------------------------
-- Zone
--
-- Accepts list of zones in args
-- Returns formatted list of zones as a horizontal <ul>
----------------------------
function p._zone(args, frame)  -- luacheck: ignore
    -- Need to create a clone of args so it works if called with module data as that data read-only
    -- Sort zones in case they get entered out of order in the data module.
    args = map(function(zone) return sprintf("[[%s]]", zone) end, tableSort(shallowClone(args)))

    -- Formatting for makeList()
    args.style = "display: inline;"
    args.list_style = "display: inline; margin-left: 0"

    return makeList("horizontal", args)
end

----------------------------
-- Get List Of All Zones
----------------------------
function p._getListOfAllZones(args, frame)  -- luacheck: ignore
    local list = {}
    --local i = 1
    
    for _, continents in pairs(module_data) do
        for _, zones in pairs(continents) do
            for _, zone in ipairs(zones) do
                list[#list + 1] = zone
                --i = i + 1
            end
        end
    end

    return list
end

------------------------------------
return p
Advertisement