Module:ModuleData: Difference between revisions

From Official Gamemode 4 Wiki
Jump to navigation Jump to search
(<@188013945699696640> <-that's a test)
 
(This should fix images)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}
 
local trim = mw.text.trim
local trim = mw.text.trim
local split = mw.text.split
local split = mw.text.split
local tableParser = mw.text.listToText
local tableParser = mw.text.listToText
 
local data = {}
local data = {}
local translation = {}
local translation = {}
Line 11: Line 11:
local langCode = ""
local langCode = ""
local titlearg
local titlearg
 
function check(string)  
function check(string)
    return string and #tostring(string) > 0
return string and #tostring(string) > 0
end
end
 
function toTable(table)
function toTable(table)
    local bck = {}
local bck = {}
    for k,v in pairs(table) do
for k,v in pairs(table) do
        bck[k] = v
bck[k] = v
    end
end
    return bck
return bck
end
end
 
function transform(inp, toType)
function transform(inp, toType)
    if type(inp) == toType then
if type(inp) == toType then
        return inp
return inp
    end
end
    if toType == "string" then
if toType == "string" then
        return tostring(inp)
return tostring(inp)
    end
end
    if toType == "number" then
if toType == "number" then
        return tonumber(inp)
return tonumber(inp)
    end
end
end
end
 
function translate(string)  
function translate(string)
    if string == "pagename" then
if string == "pagename" then
        string = titlearg or string
string = titlearg or string
    end
end
    if string == "pagename" and data.translated then
if string == "pagename" and data.translated then
        return data.translated[langCode] or translation[string] or english[string] or string  
return data.translated[langCode] or translation[string] or english[string] or string
    end
end
    return translation[string] or english[string] or string  
return translation[string] or english[string] or string
end
end
 
function replace(string, fnd, repl)
function replace(string, fnd, repl)
    local splits = split(string, fnd,true)
local splits = split(string, fnd,true)
    local bck = splits[1]
local bck = splits[1]
    for i = 2, #splits do
for i = 2, #splits do
        bck = bck .. repl .. splits[i]
bck = bck .. repl .. splits[i]
    end
end
    return bck
return bck
end
end
 
function special(value, rules)
function special(value, rules)
    -- Only run if a special tag is in the "order"
-- Only run if a special tag is in the "order"
    local i = 1
local i = 1
    while rules[i] do
while rules[i] do
        -- Loop through all the rules until a good one is found
-- Loop through all the rules until a good one is found
        -- (while cause it's a fake table)
-- (while cause it's a fake table)
        cur = rules[i]
cur = rules[i]
 
        -- Use of "copy" cause the source needs to stay its type
-- Use of "copy" cause the source needs to stay its type
        local copy
local copy
        if check(cur.transform) then
if check(cur.transform) then
            copy = transform(value,cur.transform)
copy = transform(value,cur.transform)
        else
else
            copy = value
copy = value
        end
end
 
        -- Transformation of fake table to real table so the count parameter can be used
-- Transformation of fake table to real table so the count parameter can be used
        if type(copy) == "table" then
if type(copy) == "table" then
            copy = toTable(copy)
copy = toTable(copy)
        end
end
 
        local ok = true -- This rule is good until there is a problem
local ok = true -- This rule is good until there is a problem
        local req = cur.require
local req = cur.require
        if check(req) then
if check(req) then
            ok = ok and ( ( not check(req.count) ) or ( #copy == req.count) )
ok = ok and ( ( not check(req.count) ) or ( #copy == req.count) )
            ok = ok and ( ( not check(req.type) ) or ( type(copy) == req.type) )
ok = ok and ( ( not check(req.type) ) or ( type(copy) == req.type) )
            ok = ok and ( ( not check(req.regex) ) or not(mw.ustring.gmatch( copy, req.regex )() == nil) )
ok = ok and ( ( not check(req.regex) ) or not(mw.ustring.gmatch( copy, req.regex )() == nil) )
            --  http://dev.wikia.com/wiki/Lua_reference_manual/Standard_libraries#Patterns
--  http://dev.wikia.com/wiki/Lua_reference_manual/Standard_libraries#Patterns
            --  http://dev.wikia.com/wiki/Lua_reference_manual/Scribunto_libraries#Ustring_patterns
--  http://dev.wikia.com/wiki/Lua_reference_manual/Scribunto_libraries#Ustring_patterns
        end
end
 
        if ok then
if ok then
            local table = {}
local table = {}
            if type(cur.result) == "string" then
if type(cur.result) == "string" then
                return replace(cur.result,"$",transform(copy,"string"))
return replace(cur.result,"$",transform(copy,"string"))
            elseif type(cur.result) == "table" then
elseif type(cur.result) == "table" then
                for k,v in pairs(cur.result.format) do
for k,v in pairs(cur.result.format) do
                    if type(v) == "string" then
if type(v) == "string" then
                        table[k] = replace(v,"$",copy)
table[k] = replace(v,"$",copy)
                        if table[k] == v then
if table[k] == v then
                            table[k] = translate(v)
table[k] = translate(v)
                        end
end
                    elseif type(v) == "number" then
elseif type(v) == "number" then
                        table[k] = copy[v]
table[k] = copy[v]
                    end
end
                end
end
                return tableParser(table,
return tableParser(table,
                    translate(cur.result.separatorMid or "separatorMid"),
translate(cur.result.separatorMid or "separatorMid"),
                    translate(cur.result.separatorEnd or "separatorEnd")
translate(cur.result.separatorEnd or "separatorEnd")
                )
)
            end
end
        end
end
        i = i + 1
i = i + 1
    end
end
    return "Data was suplied in the wrong format."
return "Data was suplied in the wrong format."
end
end
 
function addData(value, info)
function addData(value, info)
    q = value or info.default
q = value or info.default
 
    if not(check(q)) then
if not(check(q)) then
        return ""
return {}
    end
end
    t = "<" .. info.tag .. ">"
 
    if check(info.label) then
ret = {}
        t = t .. "<label>" .. translate(info.label) .. "</label>"
ret.type = info.tag
    end
if check(info.label) then
    t = t .. "<default>"
ret.label = translate(info.label)
    if check(info.special) then
end
        t = t .. special(q, info.special)
 
    elseif type(q) == "string" then
 
        t = t .. translate(q)
if check(info.special) then
    elseif type(q) == "table" then
ret.value = special(q, info.special)
        local tabl = {}
elseif type(q) == "string" then
        for k,v in pairs(q) do
ret.value = translate(q)
            table[k] = v
elseif type(q) == "table" then
        end
local tabl = {}
        t = t ..  tableParser(table,translate("separatorMid"), translate("separatorEnd"))
for k,v in pairs(q) do
    end
table[k] = v
    t = t .. "</default>"
end
    return t .. "</" .. info.tag .. ">\n"
ret.value = tableParser(table,translate("separatorMid"), translate("separatorEnd"))
end
 
return ret
end
end
 
function loopTilNil(func,start)
function loopTilNil(func,start)
    return func(start) and (loopTilNil(func,func(start)) or start)
return func(start) and (loopTilNil(func,func(start)) or start)
end
end
   
 
function calcInfoBox(boxData)
local nextLabel = 1
local res = ""
for i = 1,#boxData do
current = boxData[i]
if current and current.type then
if current.type == "data" then
local x = tostring(nextLabel)
res = res .. "\n| label" .. x .. " = " .. current.label .. "\n|  data" .. x .. " = " .. current.value
nextLabel = nextLabel + 1
elseif current.type == "image" then
res = res .. "\n| " .. current.type .. " = [[File:" .. current.value .. "]]"
else
res = res .. "\n| " .. current.type .. " = " .. current.value
end
end
end
return "{{Infobox" .. res .. "\n}}"
end
 
function p.ModuleInfo(frame,bol)
function p.ModuleInfo(frame,bol)
    local page = ""
local page = ""
    local mainPage
local mainPage
    if(bol) then
if(bol) then
        page = "Sweethearts"
page = "Bat Grenades"
        langCode = "cs"
langCode = "nl"
    else  
else
mw.log("this is not supported in debuger mate")
mw.log("this is not supported in debuger mate")
mainPage = loopTilNil(
mainPage = loopTilNil(
            function(start)
function(start)
                return start:getParent()
return start:getParent()
            end
end
            ,frame)
,frame)
        titlearg = frame.args["Title"]
titlearg = frame.args["Title"]
    page = frame.args["Title"] or mainPage:preprocess("{{Template:English Parent}}")
page = frame.args["Title"] or mainPage:preprocess("{{Template:English Parent}}")
        langCode = frame.args["Language Code"] or mainPage:preprocess("{{Template:Language Code}}")
langCode = frame.args["Language Code"] or mainPage:preprocess("{{Template:Language Code}}")
    end
end
    if not pcall(function() data = mw.loadData("Module:ModuleData/" .. page) end) then  
if not pcall(function() data = mw.loadData("Module:ModuleData/" .. page) end) then
        return "Couldn't find module:" .. page
return "Couldn't find module:" .. page
    end
end
    if not pcall(function() order = mw.loadData("Module:ModuleData/Order") end) then  
if not pcall(function() order = mw.loadData("Module:ModuleData/Order") end) then
        return "An error has ocured, please contact staff\nErrorcode:ModuleData#orderNotFound"
return "An error has ocured, please contact staff\nErrorcode:ModuleData#orderNotFound"
    end
end
    if not pcall(function() translation = mw.loadData("Module:ModuleData/Translations/" .. langCode) end) then
if not pcall(function() translation = mw.loadData("Module:ModuleData/Translations/" .. langCode) end) then
        if not pcall(function() translation = mw.loadData("Module:ModuleData/Translations/en") end) then  
if not pcall(function() translation = mw.loadData("Module:ModuleData/Translations/en") end) then
            return "An error has ocured, please contact staff.\nErrorcode:ModuleData#translationNotFound"
return "An error has ocured, please contact staff.\nErrorcode:ModuleData#translationNotFound"
        else
else
            english = translation
english = translation
        end
end
    else
else
        if not pcall(function() english = mw.loadData("Module:ModuleData/Translations/en") end) then  
if not pcall(function() english = mw.loadData("Module:ModuleData/Translations/en") end) then
            return "An error has ocured, please contact staff.\nErrorcode:ModuleData#translationNotFound"
return "An error has ocured, please contact staff.\nErrorcode:ModuleData#translationNotFound"
        end
end
    end
end
    local t = "<div style=\"width:280px; float:right;\"><tabber>\n"
 
    local i = 1
local mode = "latest"
    while data[i] do
 
        if i > 1 then  
local boxData = {}
            t = t .. "\n|-|"
 
            end
if mode == "latest" then
        t = t .. data[i].version .. "=<infobox>\n"
local i = 1
        local j = 1
while order[i] do
        while order[j] do
boxData[i] = addData(data[1].moduleInfo[order[i].source], order[i])
            t = t .. addData(data[i].moduleInfo[order[j].source],order[j])
i = i + 1
            j = j + 1
end
        end
end
        t = t .. "</infobox>\n"
 
        i = i + 1
 
    end
--[[
local t = "<div style=\"width:280px; float:right;\"><tabber>\n"
local i = 1
while data[i] do
if i > 1 then
t = t .. "\n|-|"
end
t = t .. data[i].version .. "=<infobox>\n"
local j = 1
while order[j] do
t = t .. addData(data[i].moduleInfo[order[j].source],order[j])
j = j + 1
end
t = t .. "</infobox>\n"
i = i + 1
end
]]
local calc = calcInfoBox(boxData)
if bol or frame.args.raw then
if bol or frame.args.raw then
        return t .. "\n</tabber></div>"
return calc
    end
end
    return frame:preprocess(t .. "\n</tabber></div>")
return frame:preprocess(calc)
end
end
 
return p
return p

Latest revision as of 20:39, 26 October 2018

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

local p = {}

local trim = mw.text.trim
local split = mw.text.split
local tableParser = mw.text.listToText

local data = {}
local translation = {}
local english = {}
local order = {}
local langCode = ""
local titlearg

function check(string)
	return string and #tostring(string) > 0
end

function toTable(table)
	local bck = {}
	for k,v in pairs(table) do
		bck[k] = v
	end
	return bck
end

function transform(inp, toType)
	if type(inp) == toType then
		return inp
	end
	if toType == "string" then
		return tostring(inp)
	end
	if toType == "number" then
		return tonumber(inp)
	end
end

function translate(string)
	if string == "pagename" then
		string = titlearg or string
	end
	if string == "pagename" and data.translated then
		return data.translated[langCode] or translation[string] or english[string] or string
	end
	return translation[string] or english[string] or string
end

function replace(string, fnd, repl)
	local splits = split(string, fnd,true)
	local bck = splits[1]
	for i = 2, #splits do
		bck = bck .. repl .. splits[i]
	end
	return bck
end

function special(value, rules)
	-- Only run if a special tag is in the "order"
	local i = 1
	while rules[i] do
		-- Loop through all the rules until a good one is found
		-- (while cause it's a fake table)
		cur = rules[i]

		-- Use of "copy" cause the source needs to stay its type
		local copy
		if check(cur.transform) then
			copy = transform(value,cur.transform)
		else
			copy = value
		end

		-- Transformation of fake table to real table so the count parameter can be used
		if type(copy) == "table" then
			copy = toTable(copy)
		end

		local ok = true -- This rule is good until there is a problem
		local req = cur.require
		if check(req) then
			ok = ok and ( ( not check(req.count) ) or ( #copy == req.count) )
			ok = ok and ( ( not check(req.type) ) or ( type(copy) == req.type) )
			ok = ok and ( ( not check(req.regex) ) or not(mw.ustring.gmatch( copy, req.regex )() == nil) )
			--  http://dev.wikia.com/wiki/Lua_reference_manual/Standard_libraries#Patterns
			--  http://dev.wikia.com/wiki/Lua_reference_manual/Scribunto_libraries#Ustring_patterns
		end

		if ok then
			local table = {}
			if type(cur.result) == "string" then
				return replace(cur.result,"$",transform(copy,"string"))
			elseif type(cur.result) == "table" then
				for k,v in pairs(cur.result.format) do
					if type(v) == "string" then
						table[k] = replace(v,"$",copy)
						if table[k] == v then
							table[k] = translate(v)
						end
					elseif type(v) == "number" then
						table[k] = copy[v]
					end
				end
				return tableParser(table,
					translate(cur.result.separatorMid or "separatorMid"),
					translate(cur.result.separatorEnd or "separatorEnd")
				)
			end
		end
		i = i + 1
	end
	return "Data was suplied in the wrong format."
end

function addData(value, info)
	q = value or info.default

	if not(check(q)) then
		return {}
	end

	ret = {}
	ret.type = info.tag
	if check(info.label) then
		ret.label = translate(info.label)
	end


	if check(info.special) then
		ret.value = special(q, info.special)
	elseif type(q) == "string" then
		ret.value = translate(q)
	elseif type(q) == "table" then
		local tabl = {}
		for k,v in pairs(q) do
			table[k] = v
		end
		ret.value = tableParser(table,translate("separatorMid"), translate("separatorEnd"))
	end

	return ret
end

function loopTilNil(func,start)
	return func(start) and (loopTilNil(func,func(start)) or start)
end

function calcInfoBox(boxData)
	local nextLabel = 1
	local res = ""
	for i = 1,#boxData do
		current = boxData[i]
		if current and current.type then
			if current.type == "data" then
				local x = tostring(nextLabel)
				res = res .. "\n|  label" .. x .. " = " .. current.label .. "\n|   data" .. x .. " = " .. current.value
				nextLabel = nextLabel + 1
			elseif current.type == "image" then
				res = res .. "\n| " .. current.type .. " = [[File:" .. current.value .. "]]"
			else
				res = res .. "\n| " .. current.type .. " = " .. current.value
			end
		end
	end
	return "{{Infobox" .. res .. "\n}}"
end

function p.ModuleInfo(frame,bol)
	local page = ""
	local mainPage
	if(bol) then
		page = "Bat Grenades"
		langCode = "nl"
	else
		mw.log("this is not supported in debuger mate")
		mainPage = loopTilNil(
			function(start)
				return start:getParent()
			end
			,frame)
		titlearg = frame.args["Title"]
		page = frame.args["Title"] or mainPage:preprocess("{{Template:English Parent}}")
		langCode = frame.args["Language Code"] or mainPage:preprocess("{{Template:Language Code}}")
	end
	if not pcall(function() data = mw.loadData("Module:ModuleData/" .. page) end) then
		return "Couldn't find module:" .. page
	end
	if not pcall(function() order = mw.loadData("Module:ModuleData/Order") end) then
		return "An error has ocured, please contact staff\nErrorcode:ModuleData#orderNotFound"
	end
	if not pcall(function() translation = mw.loadData("Module:ModuleData/Translations/" .. langCode) end) then
		if not pcall(function() translation = mw.loadData("Module:ModuleData/Translations/en") end) then
			return "An error has ocured, please contact staff.\nErrorcode:ModuleData#translationNotFound"
		else
			english = translation
		end
	else
		if not pcall(function() english = mw.loadData("Module:ModuleData/Translations/en") end) then
			return "An error has ocured, please contact staff.\nErrorcode:ModuleData#translationNotFound"
		end
	end

	local mode = "latest"

	local boxData = {}

	if mode == "latest" then
		local i = 1
		while order[i] do
			boxData[i] = addData(data[1].moduleInfo[order[i].source], order[i])
			i = i + 1
		end
	end


	--[[
	local t = "<div style=\"width:280px; float:right;\"><tabber>\n"
	local i = 1
	while data[i] do
		if i > 1 then
			t = t .. "\n|-|"
			end
		t = t .. data[i].version .. "=<infobox>\n"
		local j = 1
		while order[j] do
			t = t .. addData(data[i].moduleInfo[order[j].source],order[j])
			j = j + 1
		end
		t = t .. "</infobox>\n"
		i = i + 1
	end
	]]
	local calc = calcInfoBox(boxData)
	if bol or frame.args.raw then
		return calc
	end
	return frame:preprocess(calc)
end

return p