Module:Cenrail: Difference between revisions

From Nguhcraft Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 7: Line 7:
for i, v in pairs(list) do
for i, v in pairs(list) do
if predicate(i, v) then
if predicate(i, v) then
return i, v
return v
end
end
end
end
Line 14: Line 14:
_m.line_table = function (frame)
_m.line_table = function (frame)
local linecode = frame.args[1] or frame.args.code or "CE"
local linecode = frame.args[1] or frame.args.code or "CE"
local style = frame.args[2] or frame.args.style or "cenrail"
local nether = linecode:sub(1,2) == "N-"  
local nether = linecode:sub(1,2) == "N-"  
local dim = nether and "the_nether" or "overworld"
local dim = nether and "the_nether" or "overworld"
local _, line = find_when(function(_, v) return v.code == linecode end, data.lines[dim])
local line = find_when(function(_, v) return v.code == linecode end, data.lines[dim])
local out = "{| class=\"wikitable\"\n"
if style == "cenrail" then
out = out .. "|+ " .. line.name .. " [" .. line.code .. "]\n"
local out = "{| class=\"wikitable\" id=\"line_table-" .. linecode .. "\"\n"  
out = out .. "|-\n"
out = out .. "|+ " .. line.name .. " [" .. line.code .. "]\n"
out = out .. "! rowspan = " .. #line.stops .. " style=\"background-color:" .. ( line.color or "#000" ) .. "\" | \n"
out = out .. "|-\n"
out = out .. "! Stop \n! Code \n! Date opened\n! Usage notes\n"
out = out .. "! rowspan = " .. #line.stops .. " style=\"background-color:" .. ( line.color or "#000" ) .. "\" | \n"
out = out .. "|-\n"
out = out .. "! Stop \n! Code \n! Date opened\n! Usage notes\n"
for _, stop in ipairs(line.stops) do
out = out .. "|-\n"
if type(stop) == "string" then
for _, stop in ipairs(line.stops) do
out = out .. "| colspan = 4 | '''" .. stop .. "'''\n|-\n"
if type(stop) == "string" then
else
out = out .. "| colspan = 4 | '''" .. stop .. "'''\n|-\n"
local code = ((nether and "N-" or "") .. stop.code)
else
local _, name = find_when(function(i, v)
local code = ((nether and "N-" or "") .. stop.code)
return i == code
local name = find_when(function(i, v)
end, data.stations) or (function () return nil, "<ERROR station `" .. code .. "` has no assigned name>" end)()
return i == code
local country = ""
end, data.stations) or ("<ERROR station `" .. code .. "` has no assigned name>")
if stop.code:sub(1,1) ~= "X" then
local country = ""
country = ", [[" .. (
if stop.code:sub(1,1) ~= "X" then
countries.get(stop.code:sub(1,2)) or {common_name = "??"}
country = ", [[" .. (
).common_name .. "]]"
countries.get(stop.code:sub(1,2)) or {common_name = "??"}
).common_name .. "]]"
end
local usage_notes = stop.usage_notes or ""
for _, s in ipairs(data.connections) do
local self = nether and 2 or 1
local other = nether and 1 or 2
if s[self].code == stop.code then
usage_notes = usage_notes .. (nether and "Cen" or "Lesh") .."rail connection with " .. s[other].code .. "."
end
end
if type(name) == "table" then name = name[1] end
out = out .. "| " .. name .. country .. "\n"
out = out .. "| " .. code .. "\n"
out = out .. "| " .. (stop.date_opened or "") .. "\n"
out = out .. "| " .. usage_notes  .. "\n"
out = out .. "|-\n"
end
end
local usage_notes = stop.usage_notes or ""
end
for _, s in ipairs(data.connections) do
if line.loop then out = out .. "| colspan = 4 | '''LOOPS'''\n|-\n" end
local self = nether and 2 or 1
out = out .. "|}"
local other = nether and 1 or 2
return out
if s[self].code == stop.code then
elseif style == "leshrail" then
usage_notes = usage_notes .. (nether and "Cen" or "Lesh") .."rail connection with " .. s[other].code .. "."
local out = [[<table class="wikitable" id = "line_table-]] .. linecode .. [[">]]
out = out .. "<caption>" .. line.name .. " [" .. line.code .. "]<caption>"
out = out .. "<tr>"
  .. "<td rowspan=" .. #line.stops .. " style=\"background-color:" .. ( line.color or "#000" ) .. "\"></td>"
  .. "<th>Stop</th>"
  .. "<th>Code</th>"
  .. "<th>Date opened</th>"
  .. "<th>Notes</th>"
  .. "</tr>"
 
for _, stop in ipairs(line.stops) do
out = out .. "<tr>"
if type(stop) == "string" then
out = out .. "<td colspan=4/>'''" .. stop .. "'''</td>"
else
local code = ((nether and "N-" or "") .. stop.code)
local name = find_when(function(i, v)
return i == code
end, data.stations) or ("<ERROR station `" .. code .. "` has no assigned name>")
local country = ""
if stop.code:sub(1,1) ~= "X" then
country = ", [[" .. (
countries.get(stop.code:sub(1,2)) or {common_name = "??"}
).common_name .. "]]"
end
local usage_notes = stop.usage_notes or ""
for _, s in ipairs(data.connections) do
local self = nether and 2 or 1
local other = nether and 1 or 2
if s["N-" .. self].code == stop.code then
usage_notes = usage_notes .. "Dimensional connection with " .. s[other].code .. ". "
-- TODO find what lines are at this stop
end
end
end
end
end
if type(name) == "table" then name = name[1] end
out = out .. "</tr>"
out = out .. "| " .. name .. country .. "\n"
out = out .. "| " .. code .. "\n"
out = out .. "| " .. (stop.date_opened or "") .. "\n"
out = out .. "| " .. usage_notes  .. "\n"
out = out .. "|-\n"
end
end
out = out .. "</table>"
return out
end
end
if line.loop then out = out .. "| colspan = 4 | '''LOOPS'''\n|-\n" end
out = out .. "|}"
return out
end
end


return _m
return _m

Latest revision as of 20:41, 1 December 2025

Documentation for this module may be created at Module:Cenrail/doc

local _m = {}

local data = mw.loadJsonData "Data:NguhRoutes/network.json"
local countries = require "Module:Places"

local find_when = function (predicate, list)
	for i, v in pairs(list) do
		if predicate(i, v) then
			return v
		end
	end
end

_m.line_table = function (frame)
	local linecode = frame.args[1] or frame.args.code or "CE"
	local style = frame.args[2] or frame.args.style or "cenrail"
	local nether = linecode:sub(1,2) == "N-" 
	local dim = nether and "the_nether" or "overworld"
	local line = find_when(function(_, v) return v.code == linecode end, data.lines[dim])
	if style == "cenrail" then
		local out = "{| class=\"wikitable\" id=\"line_table-" .. linecode .. "\"\n" 
		out = out .. "|+ " .. line.name .. " [" .. line.code .. "]\n"
		out = out .. "|-\n"
		out = out .. "! rowspan = " .. #line.stops .. " style=\"background-color:" .. ( line.color or "#000" ) .. "\" | \n"
		out = out .. "! Stop \n! Code \n! Date opened\n! Usage notes\n"
		out = out .. "|-\n"
		for _, stop in ipairs(line.stops) do
			if type(stop) == "string" then
				out = out .. "| colspan = 4 | '''" .. stop .. "'''\n|-\n"
			else
				local code = ((nether and "N-" or "") .. stop.code)
				local name = find_when(function(i, v)
					return i == code
				end, data.stations) or ("<ERROR station `" .. code .. "` has no assigned name>")
				local country = ""
				if stop.code:sub(1,1) ~= "X" then
					country = ", [[" .. (
						countries.get(stop.code:sub(1,2)) or {common_name = "??"}
					).common_name .. "]]"
				end
				local usage_notes = stop.usage_notes or ""
				for _, s in ipairs(data.connections) do
					local self = nether and 2 or 1
					local other = nether and 1 or 2
					if s[self].code == stop.code then
						usage_notes = usage_notes .. (nether and "Cen" or "Lesh") .."rail connection with " .. s[other].code .. "."
					end
				end
				if type(name) == "table" then name = name[1] end
				out = out .. "| " .. name .. country .. "\n"
				out = out .. "| " .. code .. "\n"
				out = out .. "| " .. (stop.date_opened or "") .. "\n"
				out = out .. "| " .. usage_notes  .. "\n"
				out = out .. "|-\n"
			end
		end
		if line.loop then out = out .. "| colspan = 4 | '''LOOPS'''\n|-\n" end
		out = out .. "|}"
		return out
	elseif style == "leshrail" then
		local out = [[<table class="wikitable" id = "line_table-]] .. linecode .. [[">]]
		out = out .. "<caption>" .. line.name .. " [" .. line.code .. "]<caption>"
		out = out .. "<tr>"
			  .. "<td rowspan=" .. #line.stops .. " style=\"background-color:" .. ( line.color or "#000" ) .. "\"></td>"
			  .. "<th>Stop</th>"
			  .. "<th>Code</th>"
			  .. "<th>Date opened</th>"
			  .. "<th>Notes</th>"
			  .. "</tr>"
			  
		for _, stop in ipairs(line.stops) do
			out = out .. "<tr>"
			if type(stop) == "string" then
				out = out .. "<td colspan=4/>'''" .. stop .. "'''</td>"
			else
				local code = ((nether and "N-" or "") .. stop.code)
				local name = find_when(function(i, v)
					return i == code
				end, data.stations) or ("<ERROR station `" .. code .. "` has no assigned name>")
				local country = ""
				if stop.code:sub(1,1) ~= "X" then
					country = ", [[" .. (
						countries.get(stop.code:sub(1,2)) or {common_name = "??"}
					).common_name .. "]]"
				end
				local usage_notes = stop.usage_notes or ""
				for _, s in ipairs(data.connections) do
					local self = nether and 2 or 1
					local other = nether and 1 or 2
					if s["N-" .. self].code == stop.code then
						usage_notes = usage_notes .. "Dimensional connection with " .. s[other].code .. ". "
						-- TODO find what lines are at this stop
					end
					
				end
			end
			out = out .. "</tr>"
		end
		out = out .. "</table>"
		return out
	end
end

return _m