MCDM Wiki
Line 31: Line 31:
   
 
function formatVodLink(link, s)
 
function formatVodLink(link, s)
if link == nil then return s end
+
if link == nil or link == "" then return s end
 
 
 
if s == nil or s == "" then
 
if s == nil or s == "" then

Revision as of 02:48, 5 February 2019

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

local episode = {}

local getArgs -- lazily initialised

local episodeData = mw.loadData( 'Module:EpisodeData' )

function getframeargs(frame)
    if not getArgs then
        getArgs = require('Module:Arguments').getArgs
    end
    args = getArgs(frame)
    
    return 
        args['show'] or args[1], 
        args['number'] or args[2], 
        args ['field'] or args[3]
    
end

function formatDateString(d, formatstring)
    local formatstring = formatstring or "%Y-%m-%d %H:%M PT"
    return os.date(formatstring, d) 
end

function formatEpisodeNameString(name, number)
    local suffix = mw.html.create('small')
    suffix:wikitext("(Episode " .. number .. ")")

    return "\"'''[[" .. name .. "]]'''\"" .. " " .. tostring(suffix)
end

function formatVodLink(link, s)
    if link == nil or link == "" then return s end
    
    if s == nil or s == "" then
        return "[" .. link .. "]"
    else
        return "[" .. link .. " " .. s .. "]"
    end
end

function episode_lookup(show, number, field)

    local e = false
    local result = nil
    -- Look up the result in the episode data, catching errors
    e, result = pcall(function () return episodeData[show]["episodes"][tonumber(number)][field] end)
    if e and result then
        return result
    else
        return e, string.format("Something went wrong looking up episode metadata for %s %s %s. Check [[Module:EpisodeData]] to make sure that the data you asked for is defined.", tostring(show), tostring(number), tostring(field))
    end
end

function episode.episode_metadata (frame)

    local show, number, field = getframeargs(frame)
    
    result, e = episode_lookup(show, number, field)
    
    if not result and e then
        return e
    else
        return result
    end

end

function episode.episode_name (frame)
    
    local show, number = getframeargs(frame)

    local name, e = episode_lookup(show, number, "name")

    if not name and e then
        return e
    else 
        return formatEpisodeNameString(name, number)
    end
    
end

function episode.episode_airdate (frame)
    
    local show, number = getframeargs(frame)

    local airdate, e = episode_lookup(show, number, "airdate")
    
    if not airdate and e then
        return e
    else
        return formatDateString(airdate)
    end
    
end

function episode.episode_vodlinks (frame)
    
    local show, number = getframeargs(frame)

    local twitch_vod, t_e = episode_lookup(show, number, "twitch_vod")
    local youtube_vod, y_e = episode_lookup(show, number, "youtube_vod")
    
    local twitch_string = "T"
    if not (not twitch_vod and t_e) then 
        twitch_string = formatVodLink(twitch_vod, "T")
    end
    
    local yt_string = "Y"
    if not (not youtube_vod and y_e) then 
        yt_string = formatVodLink(youtube_vod, "Y")
    end

    return twitch_string .. ", " .. yt_string
    
end

function episode.episode_verbose_vodlinks (frame)
    local show, number = getframeargs(frame)
    
    local twitch_vod, t_e = episode_lookup(show, number, "twitch_vod")
    local youtube_vod, y_e = episode_lookup(show, number, "youtube_vod")
    local campaign_diary, c_e = episode_lookup(show, number, "campaign_diary")

    local list = mw.html.create("ul")
    
    if twitch_vod and not t_e then 
        list:tag('li'):wikitext(formatVodLink(twitch_vod, "Twitch VOD"))
    end
    if youtube_vod and not y_e then 
        list:tag('li'):wikitext(formatVodLink(youtube_vod, "Youtube VOD"))
    end
    if campaign_diary and not c_e then 
        list:tag('li'):wikitext(formatVodLink(campaign_diary, "Campaign Diary"))
    end
    
    return list
    
end

function episode.episode_list (frame)
    if not getArgs then
        getArgs = require('Module:Arguments').getArgs
    end
    args = getArgs(frame)
    
    show = args['show'] or args[1] or "chain"
    
    local e, show_object = pcall(function () return episodeData[show] end)
    if (not e and show_object) or not show_object.episodes then
        return string.format("Something went wrong looking up episode metadata for show %s. Check [[Module:EpisodeData]] to make sure that the show you asked for is defined and has a .episodes table.", tostring(show))
    end
    
    headings = {
        "No. in series",
        "Title",
        "Airdate",
        "VOD links",
        "Campaign Diary"
    }
    
    local root = mw.html.create('table')
    root:addClass('article-table')
            
    local head = root:tag('tr')
    for _,heading_text in ipairs(headings) do
        head:tag('th'):wikitext(heading_text)
    end
    
    for n,episode_object in ipairs(show_object.episodes) do
        local row = root:tag('tr')
        row:tag('td'):wikitext(n)
        row:tag('td'):wikitext(formatEpisodeNameString(episode_object.name, n))
        row:tag('td'):wikitext(formatDateString(episode_object.airdate))
        row:tag('td'):wikitext(
            formatVodLink(episode_object.twitch_vod, "T")
            .. ", " ..
            formatVodLink(episode_object.youtube_vod, "Y")
        )
        row:tag('td'):wikitext(formatVodLink(episode_object.campaign_diary, "Y"))
    end
    
    

    return tostring(root)
end

return episode