Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Join the Playtest on Steam Now: SpiritVale

Module:GameInfo: Difference between revisions

From SpiritVale Wiki
No edit summary
No edit summary
Tags: Mobile edit Mobile web edit
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- Module:GameInfo
local p = {}
-- Base container + shared scaffolding for all GameInfo.* renderers (Phase 4.1).
--
-- Routing:
--  Skill pages (recommended):
--    {{#invoke:GameInfo|Skills|notes=...|data=...}}
--
--  Category pages (generic):
--    {{#invoke:GameInfo|Category|name=Skills|...}} -> Module:GameInfo/Skills
--    {{#invoke:GameInfo|Category|name=Monsters|...}} -> Module:GameInfo/Monsters (future)
--
-- Notes:
-- - This module is the stable entrypoint. Render logic lives in submodules.
-- - Submodules are expected to return a string of wikitext/HTML.


local p = {}
-- Sitewide color/system ownership now lives in Common.css + Citizen.css.
-- This module still loads its scoped TemplateStyles entrypoint, but that
-- stylesheet should defer to shared sitewide tokens/components whenever possible.
local DEFAULT_STYLE_SRC = "Module:GameInfo/styles.css"


-- Shared TemplateStyles page (Module space per Phase 4.1).
local ROUTES = {
local DEFAULT_STYLE_SRC = "Module:GameInfo/gameinfo.css"
Skills = "Module:GameInfo/Skills",
}
 
local function _trim(v)
if v == nil then return "" end
local s = tostring(v)
if mw.text and mw.text.trim then
return mw.text.trim(s)
end
return (s:gsub("^%s+", ""):gsub("%s+$", ""))
end


local function _to_int(v, fallback)
local function _to_int(v, fallback)
if v == nil then return fallback end
local n = tonumber(v)
local n = tonumber(v)
if not n then return fallback end
if not n then return fallback end
n = math.floor(n + 0.0)
return math.floor(n + 0.0)
return n
end
 
local function _trim(v)
if v == nil then return nil end
local s = tostring(v)
return mw.text and mw.text.trim(s) or s:match("^%s*(.-)%s*$")
end
end


local function _is_html_node(x)
local function _is_html_node(x)
-- mw.html nodes are Lua tables with common builder methods.
return type(x) == "table"
return type(x) == "table" and type(x.tag) == "function" and type(x.wikitext) == "function"
and type(x.tag) == "function"
and type(x.wikitext) == "function"
end
end


local function _add_content(container, content)
local function _append_content(node, value)
if content == nil then return end
if value == nil then return end
if _is_html_node(content) then
if _is_html_node(value) then
container:node(content)
node:node(value)
return
else
node:wikitext(tostring(value))
end
end
container:wikitext(tostring(content))
end
end


local function _error_box(msg)
local function _error_box(msg)
-- Keep this simple and obvious on-wiki while troubleshooting.
return tostring(
local div = mw.html.create("div")
mw.html.create("div")
:addClass("error")
:addClass("sv-card")
:wikitext(tostring(msg))
:addClass("sv-gi-error")
return tostring(div)
:wikitext(tostring(msg))
)
end
 
function p.arg(frame, key, fallback)
local args = frame and frame.args or {}
local v = _trim(args[key])
if v ~= "" then return v end
return fallback
end
 
function p.int(frame, key, fallback, minv, maxv)
local n = _to_int(p.arg(frame, key, nil), fallback)
if minv ~= nil and n < minv then n = minv end
if maxv ~= nil and n > maxv then n = maxv end
return n
end
 
function p.bool(frame, key, fallback)
local v = p.arg(frame, key, nil)
if v == nil then return fallback end
v = _trim(v):lower()
if v == "1" or v == "true" or v == "yes" or v == "y" then return true end
if v == "0" or v == "false" or v == "no" or v == "n" then return false end
return fallback
end
end


-- Emit TemplateStyles. Submodules should include this once at the top of output.
function p.styles(frame, src)
function p.styles(frame, src)
frame = frame or mw.getCurrentFrame()
frame = frame or mw.getCurrentFrame()
Line 62: Line 78:
end
end


-- Core builder: returns { root = <div>, top = <div>, bottom = <div> }
function p.box(opts)
-- Caller fills top/bottom and tostring(root) to return.
function p.new_box(opts)
opts = opts or {}
opts = opts or {}


Line 76: Line 90:


local root = mw.html.create("div")
local root = mw.html.create("div")
:addClass("sv-card")
:addClass("sv-gi-card")
:addClass("sv-gi-card")
:attr("data-gi", "1")
:attr("data-gi", "1")
:attr("data-sv-card", "1")
:attr("data-gi-phase", "4.1")
:attr("data-level", tostring(level))
:attr("data-level", tostring(level))
:attr("data-max-level", tostring(max_level))
:attr("data-max-level", tostring(max_level))


if root_id and root_id ~= "" then
if root_id and tostring(root_id) ~= "" then
root:attr("id", root_id)
root:attr("id", tostring(root_id))
end
end


-- Optional variant class for category modules (e.g. "sv-gi--skills").
if opts.variant and tostring(opts.variant) ~= "" then
if opts.variant and opts.variant ~= "" then
root:addClass("sv-gi--" .. tostring(opts.variant))
root:addClass("sv-gi--" .. tostring(opts.variant))
root:attr("data-sv-card-variant", tostring(opts.variant))
end
end


local top = root:tag("div"):addClass("sv-gi-top")
local top = root:tag("div"):addClass("sv-gi-top")
local bottom = root:tag("div"):addClass("sv-gi-bottom")
local bottom = root:tag("div")
:addClass("sv-gi-bottom")
:attr("data-sv-level-scope", "1")


return {
return { root = root, top = top, bottom = bottom }
root = root,
top = top,
bottom = bottom,
}
end
end


-- Convenience wrapper: build a full box in one call.
p.new_box = p.box
-- opts.top / opts.bottom can be strings or mw.html nodes.
 
function p.render_box(opts)
function p.render_box(opts)
local box = p.new_box(opts)
local box = p.box(opts)
_add_content(box.top, opts and opts.top)
if opts then
_add_content(box.bottom, opts and opts.bottom)
_append_content(box.top, opts.top)
_append_content(box.bottom, opts.bottom)
end
return tostring(box.root)
return tostring(box.root)
end
end


-- -----------------------------------------------------------------------------
local function _require_submodule(module_title)
-- Routing / Dispatch
local ok, mod = pcall(require, module_title)
-- -----------------------------------------------------------------------------
if not ok then
 
return nil, "GameInfo: failed to require " .. tostring(module_title)
local ROUTES = {
end
-- Canonical category -> submodule
if type(mod) ~= "table" then
["Skills"] = "Module:GameInfo/Skills",
return nil, "GameInfo: " .. tostring(module_title) .. " did not return a table"
-- Future:
end
-- ["Monsters"] = "Module:GameInfo/Monsters",
if type(mod.render) ~= "function" then
-- ["Equips"]  = "Module:GameInfo/Equips",
return nil, "GameInfo: " .. tostring(module_title) .. " must export render(frame)"
}
end
 
return mod, nil
local function _pick_entry(mod)
-- Support a few common entrypoint names.
if type(mod) ~= "table" then return nil end
if type(mod.main) == "function" then return mod.main end
if type(mod.render) == "function" then return mod.render end
if type(mod.Skills) == "function" then return mod.Skills end
return nil
end
end


local function _invoke_submodule(frame, module_title)
local function _invoke(frame, module_title)
local ok_mod, mod = pcall(require, module_title)
local mod, err = _require_submodule(module_title)
if not ok_mod then
if not mod then
return _error_box("GameInfo: failed to require " .. module_title)
return p.styles(frame) .. _error_box(err)
end
end


local fn = _pick_entry(mod)
local style_src = mod.STYLE_SRC or DEFAULT_STYLE_SRC
if type(fn) ~= "function" then
return _error_box("GameInfo: " .. module_title .. " has no entry function (main/render).")
end


local ok_call, out = pcall(fn, frame)
local ok, out = pcall(mod.render, frame)
if not ok_call then
if not ok then
return _error_box("GameInfo: error inside " .. module_title)
return p.styles(frame, style_src) .. _error_box("GameInfo: error inside " .. tostring(module_title))
end
end


return out
return p.styles(frame, style_src) .. tostring(out or "")
end
end


-- Explicit entrypoint for skill pages.
function p.Skills(frame)
function p.Skills(frame)
frame = frame or mw.getCurrentFrame()
frame = frame or mw.getCurrentFrame()
return _invoke_submodule(frame, "Module:GameInfo/Skills")
return _invoke(frame, ROUTES.Skills)
end
end


-- Generic entrypoint for category pages (not limited to Skills).
-- Usage:
--  {{#invoke:GameInfo|Category|name=Skills|...}}
-- If name matches ROUTES, we use that mapping.
-- Otherwise, we attempt Module:GameInfo/<name> (safe, limited characters).
function p.Category(frame)
function p.Category(frame)
frame = frame or mw.getCurrentFrame()
frame = frame or mw.getCurrentFrame()
local args = frame.args or {}
local name = p.arg(frame, "name", p.arg(frame, "category", nil))
 
local name = _trim(args.name or args.category or args[1])
if not name or name == "" then
if not name or name == "" then
return _error_box("GameInfo.Category: missing |name=CategoryName")
return p.styles(frame) .. _error_box("GameInfo.Category: missing |name=")
end
end


-- First: known route table.
local module_title = ROUTES[name]
local module_title = ROUTES[name]
-- Second: case-insensitive match on known routes.
if not module_title and mw.ustring then
local want = mw.ustring.lower(name)
for k, v in pairs(ROUTES) do
if mw.ustring.lower(k) == want then
module_title = v
break
end
end
end
-- Third: dynamic fallback to Module:GameInfo/<name>
if not module_title then
if not module_title then
-- Allow letters, numbers, spaces, underscores, hyphens, and slashes.
return p.styles(frame) .. _error_box("GameInfo.Category: unsupported name=" .. tostring(name))
-- (Spaces are fine in page titles; MediaWiki normalizes.)
if not name:match("^[%w %_%-/]+$") then
return _error_box("GameInfo.Category: invalid name=" .. tostring(name))
end
module_title = "Module:GameInfo/" .. name
end
end


return _invoke_submodule(frame, module_title)
return _invoke(frame, module_title)
end
end


-- Optional: #invoke test entrypoint to verify box + CSS wiring.
-- {{#invoke:GameInfo|skeleton|id=sv-gi-test-1|level=1|max=10}}
function p.skeleton(frame)
function p.skeleton(frame)
frame = frame or mw.getCurrentFrame()
frame = frame or mw.getCurrentFrame()
local args = frame.args or {}


local id = args.id or "sv-gi-skeleton-1"
local id = p.arg(frame, "id", "sv-gi-skeleton-1")
local level = args.level or args.data_level or 1
local level = p.int(frame, "level", 1, 1, 999)
local max_level = args.max or args.max_level or 10
local max_level = p.int(frame, "max", 10, 1, 999)
local variant = p.arg(frame, "variant", nil)


local top = mw.html.create("div"):wikitext("GameInfo Top (locked container)")
local top = mw.html.create("div"):wikitext("GameInfo Top (locked container)")
Line 210: Line 188:


return p.styles(frame) .. p.render_box({
return p.styles(frame) .. p.render_box({
root_id = id,
id = id,
level = level,
level = level,
max_level = max_level,
max_level = max_level,
variant = args.variant,
variant = variant,
top = tostring(top),
top = top,
bottom = tostring(bottom),
bottom = bottom,
})
})
end
end


return p
return p