FANDOM


local shiny = {}
--Below is an event table system i am working on, it seems fine for now i just need to test it.
function shiny.eventtable(frame)
    --This is a function to manage drops. It turn them to images and links outsourcing the job to 
    --cheap chineese child labor (Module:Index, for srs). It's a function so i can tune it easier.
    function loaddrops(ldvalue)
        if ldvalue then
            ldcompletion = 'comp' or 'complete' or 'completion'
            if string.find('tail:'..ldvalue, ldcompletion) then
                ldcvalue = (string.gsub(string.gsub('tail:'..ldvalue, ldcompletion, ''), '%p', ''))
                ldcomptext = '|completion=y'
            else
                ldcvalue = (string.gsub(string.gsub('tail:'..ldvalue, ldcompletion, ''), '%p', ''))
                ldcomptext = ''
            end
            if string.find(ldcvalue, "tails") then
                indexinq = (string.gsub(ldcvalue,'tails',''))
                loaddropsout = (
                    frame:preprocess(
                        '\n|{{#invoke:Index|TAILS|'..indexinq..ldcomptext..'}}'))
            elseif string.find(ldcvalue, "tail") then
                indexinq = (string.gsub(ldcvalue,'tail',''))
                loaddropsout = (
                    frame:preprocess(
                        '\n|{{#invoke:Index|TAIL|'..indexinq..ldcomptext..'}}'))
            else
                loaddropsout = '\n'..ldvalue
            end
        end
        return loaddropsout
    end
 
    --a utility function for finding relevant values in a string with multiple irrelevant values.
    function getnum(getnumber,gettable)
        if string.find(gettable, getnumber) then
            local string = gettable
            local marker = string.find(string, getnumber)
            if string.find(string, ",", marker) then
                getfinish = string.find(string, ",", marker) else
                getfinish = string.len(string)
            end
            if (string.find(string.gsub(string,'%d',''),getnumber)) == 1 then
                getstart = 0 else
                local rstring = string.reverse(string.sub(string,0,getfinish-1))
                local rmarker = string.find(rstring, ",")
                getstart = getfinish-rmarker 
            end
            local cstring = string.sub(string, getstart+1, getfinish-1)
            getnumoutput = mw.text.nowiki(string.gsub(cstring,'%a','')) else
            getnumoutput = " - "
        end
        return getnumoutput
    end
 
    --We check through the head arguments and search for values we need to build the table, and if 
    --they exist we set them as true. We also shorten a few arguments to shorter variables..
    ehcfg = frame.args["head"]
    if string.find(ehcfg, "sta") then
        eventhassta = true
    end
    if string.find(ehcfg, "cha") then
        eventhascha = true
    end
    if string.find(ehcfg, "con") then
        eventhascon = true
    end
    if string.find(ehcfg, "note") then
        eventhasnote = true
    end
    if string.find(ehcfg, "drops") then
        eventhasdrops = true
    end
 
    --Here, if the amount of drops is specified, we find the number then divide it by 2 and round up
    --and set it as the number of drop columns. If none specified, sets it to the default of 2
    --columns.
    if eventhasdrops == true then
        local marker = string.find(ehcfg, "drops")
        local start = string.find(ehcfg, "=", marker)
        local finish = string.find(ehcfg, ";", marker)
        if string.find(ehcfg, ";", marker) then --checks for ; so if you want to omit ; (last input)
            eventdropnum = string.sub(ehcfg,start+1,finish-1) else            
            eventdropnum = string.sub(ehcfg,start+1)
        end
        if eventdropnum %2 == 0 then
            eventdropcol = eventdropnum else
            eventdropcol = (eventdropnum + 1)
        end else
        eventdropcol = 4
        eventdropnum = 4
    end
 
    --Event header configeration
    eventcolspan = 7+eventdropcol
    eventheadcost1 = ''
    eventheadcost2 = ''
    --checks to see what cost fields we need and set them.
    if eventhassta and eventhascha then 
        eventheadcost1 = '\n'..'! Cha'
        eventheadcost2 = '\n'..'! Sta'
    elseif eventhassta then
        eventheadcost1 = '\n'..'! rowspan="2" | Sta'
    elseif eventhascha then
        eventheadcost1 = '\n'..'! rowspan="2" | Cha'
    else 
        eventcolspan = eventcolspan-1
    end
 
    --Now we check to see if certain notation fields are flagged then add them to the header.
    if eventhascon then
        eventheadcon = '\n'..'! colspan="2" rowspan="2" | Special<br />Conditions'
        eventcolspan = eventcolspan+2 else
        eventheadcon = ''
    end
    if eventhasnote then
        eventheadnote = '\n'..'! colspan="2" rowspan="2" | Notes'
        eventcolspan = eventcolspan+2 else
        eventheadnote = ''
    end
    if eventdropnum == "0" then
    eventheaddrops = '' else
    eventheaddrops = '\n'..'! colspan='..eventdropcol..' rowspan="2" | Drops'
    end
 
    --Build event header.
    eventhead = 
        '\n'..'{| class="wikitable" style="text-align:center"'..
        '\n'..'|-'..
        '\n'..'! rowspan="2" | Stage Name'..
        eventheadcost1..
        '\n'..'! rowspan="2" | Exp'..
        '\n'..'! rowspan="2" | Gold'..
        '\n'..'! Points'..
        '\n'..'! Units'..
        '\n'..'! rowspan="2" |'..
        eventheaddrops..
        eventheadcon..
        eventheadnote..
        '\n'..'|-'..
        eventheadcost2..
        '\n'..'! Life'..
        '\n'..'! Enemies'
 
    --Event Gradient setup
    if frame.args['grad'] then
        if frame.args['grad'] == 'loes' then
            eventgrad = 
                '\n'..'|- style="background:linear-gradient'
                ..'(to right, rgb(0,0,102), rgb(0,0,204), rgb(204,0,102));"'
                ..'\n'..'| colspan="'..eventcolspan..'" |'
        elseif frame.args['grad'] == 'goldrush' then
            eventgrad = 
                '\n'..'|- style="background:linear-gradient'
                ..'(to right, rgb(0,0,102), rgb(0,0,204), rgb(204,0,102));"'
                ..'\n'..'| colspan="'..eventcolspan..'" |'
        else
            eventgrad = 
                '\n'..'|- style="background:linear-gradient'
                ..'('..frame.args['grad']..');"'
                ..'\n'..'| colspan="'..eventcolspan..'" |'
        end
        eventgrad = 
            '\n'..'|- style="background:linear-gradient'
            ..'();"'
            ..'\n'..'| colspan="'..eventcolspan..'" |'
    end
 
    --Here we make a table of all the event names, and use them as an anchor for the rest of the
    --fields when we build the mission enties. Before that, we sort the table so it's in order.
    n = 0
    eventnametable = {}
    for k,v in pairs(frame.args) do
        if string.find(k, "name") then
            n = n + 1
            eventnametable[n] = k
        end
    end
    table.sort(eventnametable)
 
    --We make a counter for entering mission entries into the mission table, also we use it so we
    --know if it's the first entry, so later we can skip adding a gradient above that table.
    --We then find the name of the argument key, strip it of everything but its sorting prefix and
    --we then use that prefix to link to other argument keys, so we can access the data in order
    --to build the mission entry one map at a time. 
    mn = 0
    missiontable = {}
    for k,v in pairs(eventnametable) do
        mn = mn + 1
        eventmapprefix = (string.gsub(eventnametable[mn],'name',''))
        eventvalues = frame.args[eventmapprefix.."values"]
        eventdrops = frame.args[eventmapprefix.."drops"]
        eventname = frame.args[eventmapprefix.."name"]
        eventnotebottomarg = frame.args[eventmapprefix.."bottomnote"]
        eventnotesidearg = frame.args[eventmapprefix.."sidenote"]
        eventcondarg= frame.args[eventmapprefix.."cond"]
        eventbodycost1 = ''
        eventbodycost2 = ''
 
        --skip grad for first entry
        if mn > 1 then
            eventgradient = eventgrad
        else
            eventgradient = ''
        end
        if eventhassta and eventhascha then 
            eventbodycost1 = '\n'..'|'..getnum("cha",eventvalues)
            eventbodycost2 = '\n'..'|'..getnum("sta",eventvalues)
        elseif eventhassta then
            eventbodycost1 = '\n'..'| rowspan="2" |'..getnum("sta",eventvalues)
        elseif eventhascha then
            eventbodycost1 = '\n'..'| rowspan="2" |'..getnum("cha",eventvalues)
        end
 
        --Check flags, configure fields. 
        if eventhascon == true then
            eventcond = '\n'..'| colspan="2" rowspan="2" |'..eventcondarg
        else
            eventcond = ''
        end
        if eventhasnote == true then
            eventnoteside = '\n'..'| colspan="2" rowspan="2" |'..eventnotesidearg
        else
            eventnoteside = ''
        end
        if eventnotebottomarg then
            eventnotebottom =  '\n'..'|-'..'\n'..'| colspan="'
            ..eventcolspan..'" |'..eventnotebottomarg
        else
            eventnotebottom = ''
        end
 
        --We build 2 drop tables from the drop argument, we send all data through to my other module
        --called Index to add an image and a link to each field. This can be configured in the above
        --function though, if you know how to mess with it.
        n = 0
        nr = eventdropcol
        ns = 0
        eventdroptableupper = {}
        eventdroptablelower = {}
        while nr > 0 do
            ns = ns + 1
            nr = nr - 1
            if string.find(eventdrops, ',') then
                testlen = string.find(eventdrops, ',')
            else
                testlen = string.len(eventdrops)+1
            end
            eventdropitem = string.sub(eventdrops,1,testlen-1)
            eventdrops = string.sub(eventdrops,testlen+1)
 
            --Basically checks if the number is odd or even puts into a table accordingly.
            if ns %2 == 0 then
                if string.len(eventdropitem) > 0 then
                    eventdroptablelower[n] = loaddrops(string.lower(eventdropitem))
                else
                    eventdroptablelower[n] = '\n| - '..'\n| - '
                end
            else
                n = n + 1
                if string.len(eventdropitem) > 0 then
                    eventdroptableupper[n] = loaddrops(string.lower(eventdropitem)) 
                else
                    eventdroptableupper[n] = '\n| - '..'\n| - '
 
                end
            end
        end
 
        --Build mission entry
        eventbody =
            eventgradient..
            '\n'..'|-'..
            '\n'..'| rowspan="2" |'..eventname..
            eventbodycost1..
            '\n'..'| rowspan="2" |'..getnum("exp",eventvalues)..
            '\n'..'| rowspan="2" |'..getnum("gold",eventvalues)..
            '\n'..'| '..getnum("up",eventvalues)..
            '\n'..'| '..getnum("units",eventvalues)..
            '\n'..'| rowspan="2" |'..
            '\n'..table.concat(eventdroptableupper)..
            eventcond..
            eventnoteside..
            '\n'..'|-'..
            eventbodycost1..
            '\n'..'|'..getnum("life",eventvalues)..
            '\n'..'|'..getnum("enemies",eventvalues)..
            '\n'..table.concat(eventdroptablelower)..
            eventnotebottom
        --Add mission entry to table.
        missiontable[mn] = eventbody
    end
 
    --Build event table. 
    eventtable = eventhead..table.concat(missiontable)..'\n'..'|}'
 
    --Output to page.
    return eventtable
end
return shiny
Community content is available under CC-BY-SA unless otherwise noted.