Module:Role: Difference between revisions

From Space Station 14 Wiki
Aliser (talk | contribs)
No edit summary
Aliser (talk | contribs)
initial fixes
Line 28: Line 28:
-- abbreviation. lookup actual definition.
-- abbreviation. lookup actual definition.
return roles_json[result]
return roles_json[result]
else
return result
end
end
end
end
Line 40: Line 42:
local role_obj = lookup_role(role)
local role_obj = lookup_role(role)
assert_not_nil(role, "failed to generate role label: role not found. Make sure the role is defined in the roles JSON in Module:Role")
assert_not_nil(role_obj, "failed to generate role label: role '" .. role .."' not found. Make sure the role is defined in the roles JSON in Module:Role")
local role_display = role_obj["display"]
local role_display = role_obj["display"]
assert_not_nil(role_display, "failed to generate role label: no 'display' name is defined for the role label '" .. role .."'")
assert_not_nil(role_display, "failed to generate role label: no 'display' name is defined for the role label '" .. role .."'")
Line 47: Line 50:


local label_wikitext = role_link and "[[" .. role_link .. "|" .. role_display .. "]]" or role_display
local label_wikitext = role_link and "[[" .. role_link .. "|" .. role_display .. "]]" or role_display
local icon_wikitext = "[[File:" .. role_icon .. "|" .. icon_size .. "]]"
local icon_wikitext = "[[" .. role_icon .. "|" .. icon_size .. "]]"


mw.html.create('span')
return mw.html.create('span')
:addClass('nowrap')
:addClass('nowrap')
:wikitext(label_wikitext, icon_wikitext)
:wikitext(icon_wikitext, " ", label_wikitext)
:allDone()
:allDone()
end
end

Revision as of 03:24, 15 March 2025

Module documentation
View or edit this documentation (about module documentation)
Uses JSON data
This module uses JSON data pages:

Implements {{Role}}.

How to add/edit/remove a role

Go to the roles JSON file (linked at the top).

Each key is a role name that can be used in the template. These must all be lowercase.

Each value is an object with properties:

{
	"display": "[required] display name aka the label that would be displayed, for example: Atmospheric Technician",
	"icon": "[optional] icon file name, for example: File:JobIconStationEngineer.png",
	"link": "[optional] page name to link to, for example: Station Engineer"
}

Value can also be a string. This is used to make abbreviations. Note that abbreviations are only used to lookup roles, they cannot be displayed.

To make an abbreviation, put the key of another role as a value. For example, to add a an atmosian abbreviation for an Atmospheric Technician:

{
	"atmosian": "atmospheric technician",
	"atmospheric technician": {
		"display": "Atmospheric Technician",
		"icon": "File:JobIconAtmosphericTechnician.png",
		"link": "Atmospheric Technician"
	}
}

local p = {}
local getArgs = require('Module:Arguments').getArgs

-- =======================

local roles_json = mw.loadJsonData("Module:Role/roles.json")
local default_icon = "File:JobIconUnknown.png"
local default_icon_size = "24px"

-- =======================

local function assert_not_nil(value, error_message)
    if value == nil then
        if error_message == nil then
            error("value is nil")
        else
            error(error_message)
        end
    end
end

-- =========================

-- Lookups role from the json. If a role is defined, returns the object defining it, or `nil` otherwise.
local function lookup_role(role)
	local result = roles_json[string.lower(role)]
	if type(result) == "string" then
		-- abbreviation. lookup actual definition.
		return roles_json[result]
	else
		return result
	end
end

-- Generates a role element.
function p.main(frame)
	local args = getArgs(frame)
	local role = args[1]
	assert_not_nil(role, "failed to generate role label: role not provided")

	local icon_size = args[2] or default_icon_size
	
	local role_obj = lookup_role(role)
	assert_not_nil(role_obj, "failed to generate role label: role '" .. role .."' not found. Make sure the role is defined in the roles JSON in Module:Role")
	
	local role_display = role_obj["display"]
	assert_not_nil(role_display, "failed to generate role label: no 'display' name is defined for the role label '" .. role .."'")
	local role_icon = role_obj["icon"] or default_icon
	local role_link = role_obj["link"]

	local label_wikitext = role_link and "[[" .. role_link .. "|" .. role_display .. "]]" or role_display
	local icon_wikitext = "[[" .. role_icon .. "|" .. icon_size .. "]]"

	return mw.html.create('span')
		:addClass('nowrap')
		:wikitext(icon_wikitext, " ", label_wikitext)
		:allDone()
end

-- Generates a list of roles.
-- 
-- @param `columns` - amount of columns to generate for all roles. Default is `4`.
function p.list_all_roles(frame)
	local columns = frame.columns or 4

	local role_els = {}
	for role, _ in pairs(roles_json) do
		frame.args[1] = role

		local wrapper_el = mw.html.create('div')
			:node(p.main(frame))

		table.insert(role_els, tostring(wrapper_el))
	end

	return frame:expandTemplate{
		title = 'Cols',
		args = {
			columns,
			table.concat(role_els, '')
		}
	}
end

return p