FANDOM


local p = {}
-- retrieves the database
local scenes = mw.loadData("Module:Scenes/data")
-- constants
local stdFooter = '\'\'List retrieved from the [[Project:GUDSDb|Grand Unified Downfall Scenes Database]].\'\''
local noDl_fmt = '<span class="label label-default hidden" title="%s">noDl</span><small class="mobile-only">[noDl]</small>'
local noDl_desc = 'This scene has no download link'
local diff_fmt = '<span class="label label-default hidden" title="%s">diff</span><small class="mobile-only">[diff]</small>'
local diff_desc = 'This scene differs between the Theatrical and Extended editions'
local extended_fmt = '<span class="label label-default hidden" title="%s">ext</span><small class="mobile-only">[ext]</small>'
local extended_desc = 'This scene is only available in the Extended edition'
 
-- from stackoverflow/2705793
function tablelength(T)
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end
 
-- finds and returns the position/key of item in table
function tablefind(s,t)
    for k,v in pairs(t) do 
        if v == s then
            return k
        end
    end 
    return nil -- default return value if not found
end
 
-- takes a table of strings and returns a HTML ul
function table2ul(t)
    if tablelength(t) == 0 then return '' end
    if type(t) == 'string' then return t end
    local s = {}
    table.insert(s, '<ul>')
    for _,v in pairs(t) do
        table.insert(s, '<li>' .. v .. '</li>')
    end
    table.insert(s, '</ul>')
    return table.concat(s)
end
 
 
function buildflags(T)
   local f = {}
   if T.extended then table.insert(f, string.format(extended_fmt, extended_desc) ) end
   if T.diff then table.insert(f, string.format(diff_fmt, diff_desc) ) end
   if T.noDl then table.insert(f, string.format(noDl_fmt, noDl_desc) ) end
   return ' <span class="sceneflags">' .. table.concat(f, " ") .. "</span>"
end
 
-- returns length of List
function p.length()
   return tablelength(scenes)
end
 
-- returns the List of Scenes
function p.list(frame)
    local noFlags = tonumber(frame.args.noflags)
    local str
    local items = {}
    local l = tablelength(scenes)
    for n = 1, l do
        if noFlags then
            str = scenes[n].name
        else
            str = scenes[n].name .. buildflags(scenes[n])
        end
        -- todo: append asterisks etc to s here
        --str = name
        items[n] = '<li>' .. str .. '</li>\n'
    end 
    return '<ol class="scenes list lua">' .. table.concat(items) .. '</ol>'
end
 
-- returns an iten in the List
--  args[1] = id or index of the item
--  args[2] = property to return. defaults to "name"
function p.item(frame)
    local id = frame.args[1] or 1
    id = tonumber(id) or id -- tries to convert index input
    local rettype = frame.args[2] or "name"
    local noFlags = tonumber(frame.args.noflags)
 
    for k,v in pairs(scenes) do
        if k == id or v.id == id then
            --or tablefind(id,v.id) ~= nil
            if type( v[rettype] ) == "table" then
                return table2ul( v[rettype] )
            elseif rettype == 'name' and not noFlags then
                return v[rettype] .. buildflags(v)
            else
                return v[rettype]
            end
        end
    end
end
 
-- generates the table of Scenes 
function p.table(frame)
    local noFlags = tonumber(frame.args.noflags)
    local rows = {}
    local c = tablelength(scenes)
    for n = 1, c do
        local idx, name, desc, dl 
        if scenes[n].id ~= nil then
            idx = frame:expandTemplate{ title='H:title', args={scenes[n].id,n} }
        else
            idx = n
        end
        if noFlags then
            name = scenes[n].name
        else
            name = scenes[n].name .. buildflags(scenes[n])
        end
        if type(scenes[n].desc) == 'table' then
            desc = table2ul(scenes[n].desc)
        else
            desc = scenes[n].desc
        end
        if type(scenes[n].dl) == 'table' then
            dl = table2ul(scenes[n].dl)
        else
            dl = scenes[n].dl
        end
 
        rows[n] = '<tr><td>' .. idx .. '</td><td>' .. name .. '</td><td> ' .. desc .. ' </td><td> ' .. dl .. ' </td></tr>'
    end
 
    local tbl = '<table class="scenes wikitable hilight floatheader fullwidth"><tr><th>#</th><th>Name</th><th>Description</th><th>Links</th></tr>' .. table.concat(rows) .. '<tr><td colspan="4">' .. stdFooter .. '</td></tr></table>'
    return tbl
end
 
-- returns a successor box given the scene ID
function p.successor(frame)
    local id = frame.args[1] or 1
    id = tonumber(id) or id -- tries to convert index input
    local ext = frame.args.ext or false -- whether to use extension template
    local cont = frame.args.continue or nil --set to "yes" to enable
    local targettest --ultimately nused atm
    local prevtext='N/A'
    local nexttext='N/A'
 
    local template = 'Successor Box'
    if ext then template = 'Successor Ext' end
 
    local c = tablelength(scenes)
    for n = 1, c do
        if n == id or scenes[n].id == id then
           targettext = scenes[n].name
           if n > 1 then prevtext = scenes[n-1].name end
           if n < c then nexttext = scenes[n+1].name end
        end
        if targettext ~= nil then
           local s = frame:expandTemplate{ 
               title = template, 
               args = {Previous = prevtext, Next = nexttext, Continue = cont} 
            }
           return s
        end
    end
end
 
--for Template:Infobox scene
function p.neighbour(id,dir)
    local c = tablelength(scenes)
    local s = 'N/A'
    for n = 1, c do
        if n == id or scenes[n].id == id then
            if (n+dir >= 1 and n+dir <= c) then
                s = scenes[n+dir].name or 'N/A'
            end
        end
    end
    return s
end
function p.prevscene(frame)
    local id = frame.args[1] or 1
    id = tonumber(id) or id -- tries to convert index input
    return p.neighbour(id,-1)
end
function p.nextscene(frame)
    local id = frame.args[1] or 1
    id = tonumber(id) or id -- tries to convert index input
    return p.neighbour(id,1)
end
 
return p

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.