Module:Game functions

From IFWiki

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

local p = {}
local cargo = mw.ext.cargo

function p.dump(o)
	function dumper(o)
	   if type(o) == 'table' then
	      local s = '{ '
	      for k,v in pairs(o) do
	         if type(k) ~= 'number' then k = '"'..k..'"' end
	         s = s .. '['..k..'] = ' .. dumper(v) .. ','
	      end
	      return s .. '} '
	   else
	      return tostring(o)
	   end
	end
	--echo("<pre>")
	--echo(dumper(o))
	--echo("</pre>")
	return (dumper(o))
end


function p.numeric(str)
    if type(str) ~= "string" then 
      return nil 
    end
    str = mw.text.trim(str)
    if string.match(str, "^%d+$") then
        return tonumber(str)
    end
    local num, suffix = string.match(str, "^(%d+)(%a+)$")
    if num and suffix then
        suffix = suffix:lower()
        if suffix == "st" or suffix == "nd" or suffix == "rd" or suffix == "th" then
            return tonumber(num)
        end
    end
    return nil
end

function p.ordinalNumber(num)
	if num:match("^[0-9]+$") ~= nil then
	    local last_digit = num % 10
	    if last_digit == 1 and num ~= '11'
	        then ending = 'st'
	    elseif last_digit == 2 and num ~= '12'
	        then ending = 'nd'
	    elseif last_digit == 3 and num ~= '13'
	        then ending = 'rd'
	    else 
	        ending = 'th'
	    end
		return num .. ending
	else
		return num
	end
end

function p.displayEntities(tableName, entityType, context)
	-- Couldn't think of a better word than entities for people + companies
	-- tableName is the actual table, entityType is 'Person' or 'Company'
	-- context is 'Citation' or just nil
	-- There will be a better way to deal with pseudonyms, but this works

	function hasPseudonym(t)
		for _, r in ipairs(t) do
	        if r["Pseudonym"] ~= nil then
	            return true
	        end
	    end
	    return false		
	end
 
	if hasPseudonym(tableName) then
		-- Sort table so rows with same pseudonyms are grouped together
		table.sort(tableName, function(a, b)
		    local aHasP = a.Pseudonym ~= nil
		    local bHasP = b.Pseudonym ~= nil
		    if aHasP and not bHasP then
		        return true 
		    elseif not aHasP and bHasP then
		        return false
		    elseif not aHasP and not bHasP then
		        return false 
		    else
		        return a.Pseudonym < b.Pseudonym
		    end
		end)		
	end

	if tableName then 
		m = #tableName 
	end
	numP = 0; -- counter for pseudonyms
	for i, e in pairs (tableName) do
		if (i<=5) or (i==6 and m==6) or numP~=0 or context~='Citation' then
			-- Display 1-6 as normal, but display 7 or more showing 5 "and others"
			-- Continue if we're in the middle of a pseudonym group
			
			-- "and" between exactly two people
			if i == 2 and m == 2 and entityType == 'Person' then
				echo ' and ' 
			elseif i > 1 then 
				echo ', ' 
			end
			
			-- Name or "Anonymous"
			if e.Name then
				if mw.title.getCurrentTitle().text ~= e.Name then
					if context == 'Citation' then
						echo ( '[[' .. e.Name .. '|' .. string.gsub(e.Name, " %(.*%)", "") .. ']]' )
					else
						echo ( '[[' .. e.Name .. ']]' )
					end
				else
					if context == 'Citation' then
						echo ( string.gsub(e.Name, " %(.*%)", "") )
					else
						echo ( e.Name )
					end
				end
			else
				echo "Anonymous"
			end
			
			if e.Pseudonym then
				numP = numP + 1
				if tableName[i+1] and tableName[i+1].Pseudonym then
					nextPseudonym = tableName[i+1].Pseudonym 
				else
					nextPseudonym = nil
				end
				
				if ( nextPseudonym and nextPseudonym ~= e.Pseudonym ) -- there's another person but different pseudonym
				or ( nextPseudonym == nil ) then -- either no other person, or another person but with no pseudonym
					if numP == 1 then
						echo ' as '
					elseif numP == 2 then
						echo ', both as '
					else
						echo ', all as '
					end
					numP = 0;
					if mw.title.getCurrentTitle().text ~= e.Pseudonym then
						echo ( '"[[' .. e.Pseudonym .. ']]"' )
					else
						echo ( '"' .. e.Pseudonym .. '"' )
					end
				end
			end
		elseif i==6 or (i>6 and numP==0) then
			echo ", and others";
			break;
		end
	end
end



return p