Module:Patch

From Artifact Wiki
Jump to: navigation, search

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

local cargo = mw.ext.cargo -- The Cargo extension for cargo tables.
local getArgs = require( 'Module:Arguments' ).main
local util = require( 'Module:Util')
local p = {}


function p.main(frame)
	local args = getArgs()
	return p._main( args )
end


function p._main(args)
	-- Arguments needed to format the output.
	local thisPatch = p.thisPatch(args['date'], args['version'])
	local nextPatch = p.getNext(args['date'])
	local latestPatch = p.getLatest()
	local lastPatch = p.getLast(args['last'])
	local artifact = p.getArtifact(args['artifact'])
	local steam = p.getSteam(args['steam'])
	local gitHub = p.getGitHub(args['gitHub'])
	local reddit = p.getReddit(args['reddit'])
	local patchnotes = p.getPatchnotes(args['patchnotes'])

	-- The actual output.
	return string.format([=[
<table style="margin: auto; width: 100&#037;; max-width: 1300px; height: 25px; border: solid #9999CC 1px;  border-collapse: collapse;"><tr>
<th style="padding: 5px; border: solid #9999CC 1px; background-color: AntiqueWhite;"> ≣ [[:Category:Patches|Patch List]] </th>
<th style="padding: 5px; border: solid #9999CC 1px; background-color: lightblue;"   > %s </th> <!-- Last Patch    -->
<th style="padding: 5px; border: solid #9999CC 1px; background-color: lightgreen; font-size: 17px;"  > %s </th> <!-- Current Patch -->
<th style="padding: 5px; border: solid #9999CC 1px; background-color: lightblue;"   > %s </th> <!-- Next Patch    -->
<th style="padding: 5px; border: solid #9999CC 1px; background-color: gold;"        > %s </th> <!-- Latest Patch  -->
%s                                                                                             <!-- Artifact Link -->
%s                                                                                             <!-- Steam Link    -->
%s                                                                                             <!-- GitHub Link   -->
%s                                                                                             <!-- Reddit Link   -->
</tr></table>
<div style="margin: auto; display: box; background-color: beige;  width: 100&#037;; max-width: 1300px;">
<div style="padding: 7px; border: solid #9999CC 1px; border-top: none;">
%s                                                                                             <!-- Patch notes   -->
</div>
</div>
]=], lastPatch, thisPatch, nextPatch, latestPatch, artifact, steam, gitHub, reddit, patchnotes)
end

-- This Patch.
function p.thisPatch(date, version)
	if(date) then
		if(version) then
			return 'Update ' .. version
		else
			return 'Patch ' .. date
		end
	end
	return 'Current Patch'
end


-- Last Patch.
function p.getLast(last)
	if(last) then
		local cargoOutput = cargo.query('Patches', '_pageName, Date, Version', { where='Patches.Date="' .. last .. '"'})[1]
		if(cargoOutput) then
			if(cargoOutput['_pageName']) then
				if(not util.isempty(cargoOutput['Version'])) then
	    			return string.format([=[← [[%s|Update %s]]]=], cargoOutput['_pageName'], cargoOutput['Version'])
	    		else
	    			return string.format([=[← [[%s|Patch %s]]]=], cargoOutput['_pageName'], last)
	    		end
			end
		end
	end
	return '← Last Patch'
end


-- Next Patch.
function p.getNext(date)
	-- Retrieves the date of the next patch from the Patches cargo table.
	local cargoOutput = cargo.query('Patches', '_pageName, Date, Version', { where='Patches.Last="' .. date .. '"'})[1]
	if(cargoOutput) then
		if(cargoOutput['Date'] and cargoOutput['_pageName']) then
			if(not util.isempty(cargoOutput['Version'])) then
    			return string.format([=[[[%s|Update %s]] →]=], cargoOutput['_pageName'], cargoOutput['Version'])
    		else
    			return string.format([=[[[%s|Patch %s]] →]=], cargoOutput['_pageName'], cargoOutput['Date'])
    		end
		end
	end
	return 'Next Patch →'
end


-- Latest Patch.
function p.getLatest()
  -- Retrieves the date of the latest patch from the Patches cargo table.
  local cargoOutput = cargo.query('Patches', '_pageName, Date, Version', {orderBy = 'Date DESC'})[1]
  if(cargoOutput) then
    if(cargoOutput['Date'] and cargoOutput['_pageName']) then
    	if(not util.isempty(cargoOutput['Version'])) then
    		return string.format([=[[[%s|Update %s]] ⇒]=], cargoOutput['_pageName'], cargoOutput['Version'])
    	else
    		return string.format([=[[[%s|Patch %s]] ⇒]=], cargoOutput['_pageName'], cargoOutput['Date'])
    	end
    end
  end
  return 'Latest Patch ⇒'
end

-- Link to Artifact update webpage.
function p.getArtifact(artifact)
  if(artifact) then
    return string.format([=[<td style="padding: 5px; border: solid #9999CC 1px; width: 25px; background-color: black;">
    [[File:Artifact Cutout.png|link=%s|25px|Artifact update page]]</td>]=], artifact)
  end
  return ''
end

-- Link to Steam update page.
function p.getSteam(steam)
  if(steam) then
    return string.format([=[<td style="padding: 5px; border: solid #9999CC 1px; width: 25px; background-color: black;">
    [[File:Steam Icon.png|link=%s|25px|Steam update page]]</td>]=], steam)
  end
  return ''
end

-- Link to GitHub commit.
function p.getGitHub(gitHub)
  if(gitHub) then
    local gitHubLink = ''
    if(string.find(gitHub, "^http")) then
      gitHubLink = gitHub
    else 
      gitHubLink = string.format("https://github.com/SteamDatabase/GameTracking-Artifact/commit/%s", gitHub)
    end
    return string.format([=[<td style="padding: 5px; border: solid #9999CC 1px; width: 25px; background-color: black;">
    [[File:GitHub Icon Light.png|link=%s|25px|GitHub update commit]]</td>]=], gitHubLink)
  end
  return ''
end

-- Link to reddit analysis.
function p.getReddit(reddit)
    if(reddit) then
        return string.format([=[<td style="padding: 5px; border: solid #9999CC 1px; width: 25px; background-color: black;">
        [[File:Reddit Icon.png|link=%s|25px|Reddit patch analysis]]</td>]=], reddit)
    end
    return ''
end

-- Patch notes.
function p.getPatchnotes(text)
  if(text) then
     return '= Patch notes =\r\n' .. text
  end
  return '= Patch notes =\r\nNone yet.' -- Default text.
end


return p