FANDOM


local p = {}
 
function trim( str )
    if (not(str) or #str == 0) then
        return ""
    end
    return mw.text.trim(str)
end
 
function replace( str, chr)
    local sarr = mw.text.split(str,'')
    local result = ""
    for i = 1, #sarr do
        if chr[sarr[i]] then
            result = result .. chr[sarr[i]]
        else 
            result = result .. sarr[i]
        end
    end
    return result
end
 
function max( a, b)
    if a > b then
        return a
    else
        return b
    end
end
 
 
function p.usernameParser( frame )
    local names = {}
    local namesi = 1
    local result = ""
    local usernames = mw.text.split(frame.args[1], ",", true)
    local displaynames = mw.text.split(frame.args[2], ",", true)
    for i = 1, max(#usernames, #displaynames) do
        if i > #usernames or trim(usernames[i]) == "" then
            if not(trim(displaynames[i]) == "") then
                names[namesi] = displaynames[i]
                namesi = namesi + 1
            end
        elseif i > #displaynames or trim(displaynames[i]) == "" then
            names[namesi] = "[[User:" .. trim(usernames[i]) .. "|" .. trim(usernames[i]) .. "]]"
            namesi = namesi + 1
        else
            names[namesi] = "[[User:" .. trim(usernames[i]) .. "|" .. trim(displaynames[i]) .. "]]"
            namesi = namesi + 1
        end
    end
 
 
 
    if #names == 0 then
        return "the community"
    elseif #names == 1 then
        return trim(names[1])
    end
 
    result = trim(names[1])
    for i = 2, #names-1 do
        result = result .. trim(frame.args[3]) .. " " .. trim(names[i])
    end
    result = result .. " " .. trim(frame.args[4]) .. " " .. trim(names[#names])
    return result
end
 
function getBetweenBrackets( string )
    local tmp0 = mw.text.split( string, "%(")
    mw.log(tmp0, #tmp0)
    if #tmp0 > 1 then
        local tmp1 = mw.text.split( tmp0[2], "%)")
        mw.log(tmp1, #tmp1)
        return tmp1[1]
    end
    return ""
end
 
function getOut( string, checkTable )
    local chk = false
    local splitString = mw.text.split( mw.ustring.lower(string), " ")
    mw.log(string, checkTable)
    for k,v in pairs(checkTable) do
        mw.log(k,v)
        local splitTable = mw.text.split(k, " ")
        local ii = 1
        for i = 1, #splitString do
            local word = splitString[i]
            mw.log(i,word,ii,splitTable[ii])
            if word == mw.ustring.lower(splitTable[ii]) then
                ii = ii + 1
                chk = true
            else
                ii = 1
                chk = false
            end
            mw.log(i, #splitString,ii,splitTable,chk)
            if (ii > #splitTable) then
                break
            end
            if (i + #splitTable - ii >= #splitString) then
                chk = false
                break
            end
        end
        if chk then
           return v 
        end
    end
    return nil
end
 
function swap( table ) 
    local swapped = {}
    for k,v in pairs(table) do
       swapped[v] = k
    end
    return swapped
end
 
function startCapital (string)
    local letters = mw.text.split("abcdefghijklmnopqrstvuwxyz", '')
    local capital = {}
    for l in letters do
        capital[l] = mw.ustring.upper(l)
    end
    string[1] = capital[string[1]] or string[1]
end
 
 
 
function p.getServer( frame )
    mw.log("server")
    local result = ""
    local versions = {
        ["Public Server I"] = "Public Server I",
        ["Public Server II"]="Public Server II",
        ["Public Server III"]="Public Server III",
        ["Gamemode 4D"]="GM4D",
        ["GM4D"]="GM4D"
    }
    local checkInfoBox = { town = "TownInfo", event = "EventInfo", location = "LocationInfo"}
    mw.log(mw.title.getCurrentTitle().text)
    local info = getBetweenBrackets(mw.title.getCurrentTitle().text)
    local txt = mw.title.getCurrentTitle():getContent()
    local version = getOut(info, versions)
    if (not version) and (not getOut(mw.title.getCurrentTitle().text,versions)) then
        return "[[Category:Invalid Public Server Page]]"
    end
    mw.log(version)
    if version then
        result = result .. "[[Category:" .. version .. "]]"
    end
 
    local infoBox = getOut(info, checkInfoBox)
    if infoBox then
        if #(mw.text.split(txt, "{{" .. infoBox)) < 2 then
            result = result .. "[[Category:Missing infobox " .. infoBox .. "]]"
        else
            result = result .. "[[Category:" .. version .. " " .. swap(checkInfoBox)[infoBox] .. "]]"
       end     
    end
    return result
end
 
return p