Module:RankedWars: Difference between revisions

From NPOWiki
Jump to navigation Jump to search
m oops
add history mode, redo args, refactor
Line 1: Line 1:
--todo: remember to handle ongoing wars (end = 0). Display war.note if present.
--todo: handle ongoing wars (end = 0). Display war.note if present. War lookup without faction (special case for internal).
--local data = mw.loadData('Module:RankedWars/data')
--local data = mw.loadData('Module:RankedWars/data')
local jsonData = mw.loadJsonData('Module:RankedWars/data.json')
local jsonData = mw.loadJsonData('Module:RankedWars/data.json')
Line 5: Line 5:
local p = {}
local p = {}


function p.main(frame)
local function factionName(facKey)
--local args=frame.args
return mw.ustring.upper(mw.ustring.sub(facKey, 1, 1)) .. mw.ustring.sub(facKey, 2)
local args = frame:getParent().args
end
local faction = mw.ustring.lower(args[1])
 
local war = args[2]
local function displayFactionStats(faction)
local capName
local capName = factionName(faction)
local overall = jsonData.wars.overall[faction]
return table.concat({
'NPO - ' .. capName,
'has participated in',
overall.wars,
'ranked wars, winning',
overall.wins,
'(' .. overall.termedWins .. ' termed)',
'and losing',
overall.losses,
'(' .. overall.termedLosses .. ' termed).',
}, ' ')
end
 
local function displayWarData(faction, warId)
local capName = factionName(faction)
local warData = jsonData.wars[faction][warId]
local outcome = 'fought'
if warData.win then
outcome = 'won against'
else
outcome = 'lost to'
end
local terms = 'war'
if warData.termed then
terms = 'termed war'
end
return table.concat({
'NPO - ' .. capName,
outcome,
warData.oppName,
'in a',
terms,
'that started at',
os.date('!%F %T TCT', warData.start),
'and lasted',
string.format('%.0f', (warData['end'] - warData['start']) / 3600),
'hours. With a lead target of',
warData.target .. ',',
capName,
'scored',
warData.ourScore,
'while the opponent scored',
warData.oppScore .. '.',
}, ' ')
end
 
local function displayWarHistory(faction, args)
local warIDs = {}
local repeats = {}
local wars = jsonData.wars[faction]
if not jsonData.factions[faction] then
--The JSON sorts the wars numerically, we want newest first
return '<span class="error">Incorrect faction name</span>'
for warID, warData in pairs(wars) do
table.insert(warIDs, 1, warID)
if repeats[warData.oppName] then
repeats[warData.oppName] = repeats[warData.oppName] + 1
else
repeats[warData.oppName] = 1
end
end
end
capName = mw.ustring.upper(mw.ustring.sub(faction, 1, 1)) .. mw.ustring.sub(faction, 2)
if war and war ~= '' then
for _, id in ipairs(warIDs) do
local warData = jsonData.wars[faction][tonumber(war)]
local warData = wars[id]
local out = ''
local outcome = 'fought'
local heading = warData.oppName
if warData.win then
local disambig
outcome = 'won against'
if repeats[warData.oppName] > 1 then
else
disambig = os.time('!%b %Y', warData.start)
outcome = 'lost to'
heading = heading .. ' (' .. disambig .. ')'
end
out = out .. '===' .. heading .. '===\n'
local warpage
if mw.ustring.find(args['has page'], ',' .. warData.id .. ',', 1, true) then
warpage = warData.oppName .. ' Ranked War'
if disambig then
warpage = warpage .. ' (' .. disambig .. ')'
end
end
if warpage then
out = out .. '{' .. '{' .. 'main|' .. warpage .. '}}\n'
end
end
local terms = 'war'
out = out .. displayWarData(faction, warData.id) .. '\n'
if warData.termed then
local blurb = args[tostring(warData.id)]
terms = 'termed war'
if blurb then
out = out .. '\n' .. blurb
end
end
return table.concat({
return out
'NPO - ' .. capName,
outcome,
warData.oppName,
'in a',
terms,
'that started at',
os.date('!%F %T TCT', warData.start),
'and lasted',
string.format('%.0f', (warData['end'] - warData['start']) / 3600),
'hours. With a lead target of',
warData.target .. ',',
capName,
'scored',
warData.ourScore,
'while the opponent scored',
warData.oppScore .. '.',
}, ' ')
end
end
end
function p.main(frame)
p.call(frame:getParent().args)
end
function p.call(args)
local faction, war, mode
if faction and faction ~= '' then
if args[1] then
local overall = jsonData.wars.overall[faction]
faction = mw.ustring.lower(args[1])
end
return table.concat({
if args[2] then
'NPO - ' .. capName,
war = tonumber(args[2])
'has participated in',
end
overall.wars,
mode = args.mode
'ranked wars, winning',
args['has page'] = mw.ustring.gsub(
overall.wins,
',' .. (args['has page'] or '') .. ',',
'(' .. overall.termedWins .. ' termed)',
' ',
'and losing',
''
overall.losses,
)
'(' .. overall.termedLosses .. ' termed).',
}, ' ')
if not faction then
return '<span class="error">faction is currently required</span>'
end
if faction and not jsonData.factions[faction] then
return '<span class="error">incorrect faction name</span>'
end
if faction and war and not jsonData.wars[faction][war] then
return '<span class="error">specified war not found for the given faction</span>'
end
end
return '<span class="error">at least one parameter is required</span>'
if mode == 'history' then
return displayWarHistory(faction,  args)
elseif mode == 'overall' then
return displayFactionStats(faction)
elseif mode == 'war' then
if not war then
return '<span class="error">war id must be specified for war lookup</span>'
end
return displayWarData(faction, war)
else
return '<span class="error">mode is currently required</span>'
end
end
end


return p
return p

Revision as of 12:47, 16 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:Ranked war for usage information. Use the template rather than invoking the module. Data for this module is imported from Module:RankedWars/data and Module:RankedWars/data.json.

For how to update data, please see Module:RankedWars/data/doc.

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

--todo: handle ongoing wars (end = 0). Display war.note if present. War lookup without faction (special case for internal).
--local data = mw.loadData('Module:RankedWars/data')
local jsonData = mw.loadJsonData('Module:RankedWars/data.json')

local p = {}

local function factionName(facKey)
	return mw.ustring.upper(mw.ustring.sub(facKey, 1, 1)) .. mw.ustring.sub(facKey, 2)
end

local function displayFactionStats(faction)
	local capName = factionName(faction)
	local overall = jsonData.wars.overall[faction]
		
	return table.concat({
		'NPO - ' .. capName,
		'has participated in',
		overall.wars,
		'ranked wars, winning',
		overall.wins,
		'(' .. overall.termedWins .. ' termed)',
		'and losing',
		overall.losses,
		'(' .. overall.termedLosses .. ' termed).',
	}, ' ')
end

local function displayWarData(faction, warId)
	local capName = factionName(faction)
	local warData = jsonData.wars[faction][warId]
		
	local outcome = 'fought'
	if warData.win then
		outcome = 'won against'
	else
		outcome = 'lost to'
	end
	local terms = 'war'
	if warData.termed then
		terms = 'termed war'
	end
	
	return table.concat({
		'NPO - ' .. capName,
		outcome,
		warData.oppName,
		'in a',
		terms,
		'that started at',
		os.date('!%F %T TCT', warData.start),
		'and lasted',
		string.format('%.0f', (warData['end'] - warData['start']) / 3600),
		'hours. With a lead target of',
		warData.target .. ',',
		capName,
		'scored',
		warData.ourScore,
		'while the opponent scored',
		warData.oppScore .. '.',
	}, ' ')
end

local function displayWarHistory(faction, args)
	local warIDs = {}
	local repeats = {}
	local wars = jsonData.wars[faction]
	
	--The JSON sorts the wars numerically, we want newest first
	for warID, warData in pairs(wars) do
		table.insert(warIDs, 1, warID)
		if repeats[warData.oppName] then
			repeats[warData.oppName] = repeats[warData.oppName] + 1
		else
			repeats[warData.oppName] = 1
		end
	end
	
	for _, id in ipairs(warIDs) do
		local warData = wars[id]
		local out = ''
		
		local heading = warData.oppName
		local disambig
		if repeats[warData.oppName] > 1 then
			disambig = os.time('!%b %Y', warData.start)
			heading = heading .. ' (' .. disambig .. ')'
		end
		out = out .. '===' .. heading .. '===\n'
		local warpage
		if mw.ustring.find(args['has page'], ',' .. warData.id .. ',', 1, true) then
			warpage = warData.oppName .. ' Ranked War'
			if disambig then
				warpage = warpage .. ' (' .. disambig .. ')'
			end
		end
		if warpage then
			out = out .. '{' .. '{' .. 'main|' .. warpage .. '}}\n'
		end
		out = out .. displayWarData(faction, warData.id) .. '\n'
		local blurb = args[tostring(warData.id)]
		if blurb then
			out = out .. '\n' .. blurb
		end
		
		return out
	end
end

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

function p.call(args)
	local faction, war, mode
	
	if args[1] then
		faction = mw.ustring.lower(args[1])
	end
	if args[2] then
		war = tonumber(args[2])
	end
	mode = args.mode
	args['has page'] = mw.ustring.gsub(
		',' .. (args['has page'] or '') .. ',',
		' ',
		''
	)
	
	if not faction then
		return '<span class="error">faction is currently required</span>'
	end
	
	if faction and not jsonData.factions[faction] then
		return '<span class="error">incorrect faction name</span>'
	end
	if faction and war and not jsonData.wars[faction][war] then
		return '<span class="error">specified war not found for the given faction</span>'
	end
	
	if mode == 'history' then
		return displayWarHistory(faction,  args)
	elseif mode == 'overall' then
		return displayFactionStats(faction)
	elseif mode == 'war' then
		if not war then
			return '<span class="error">war id must be specified for war lookup</span>'
		end
		return displayWarData(faction, war)
	else
		return '<span class="error">mode is currently required</span>'
	end
end

return p