Module:Calendar

--[=[

Lua Module for Calendar Functions

--]=]

local p = {} local util = require("Module:Utility") local oneItem = require("Module:MultiItems").getOneItem local date = require("Dev:Date")

local total = {}

local function day_of_week(dd, mm, yy) dw=os.date('*t',os.time{year=yy,month=mm,day=dd})['wday'] return ({"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday" })[dw] .. " " .. util.ordinal(dd) end

local function days_in_month(month, year) local days_in_month = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} local d = days_in_month[month] if (month == 2) then if year % 4 == 0 then if year % 100 == 0 then if year % 400 == 0 then d = 29 end else d = 29 end end end return d end

local function TotalTable local tOut = {'|-|Total Rewards=\n', '{| class="article-table dmk-seasons sortable" style="width: 100%; line-height: 1.0;"\n', '! style="width: 50%;"|Item Name\n', '! style="width: 50%;"|Total\n', }

for k, rwData in pairs(total) do		table.insert(tOut, '|-\n| data-sort-value="')		table.insert(tOut, k)		table.insert(tOut, '"|') table.insert(tOut, rwData["I"]) table.insert(tOut, '\n!') table.insert(tOut, util.th_comma((rwData[1] or 0))) table.insert(tOut, '\n') end

return table.concat(tOut) .. "|}\n" end

function p.main(frame) local tArgs = util.getArgs(frame) local year = tArgs[1] or 0001 local tOut = {'|-|Calendar=\n{| class="article-table dmk-calendar" style="width: 100%;"\n'} local tOut1 = {'|-\n! style="width: 12%"|Week 1 January\n'} local tOut2 = {"|-\n! Reward \n"}

local dw = os.date('*t',os.time{year=year,month=1,day=1})['wday'] dw = dw - 1 for n=1, dw do table.insert(tOut1, '! style="width: 12%"|\n') table.insert(tOut2, "|\n") end local d = dw - 1

local months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"} local i = 2 local a = 1

for k, v in ipairs(months) do		local monthdays = days_in_month(k, year) for n=1, monthdays do			d = d + 1 if d == 7 then table.insert(tOut, table.concat(tOut1)) table.insert(tOut, table.concat(tOut2)) tOut1 = {'|-\n! style="width: 12%"|Week ' .. i .. " " .. v .. "\n"} tOut2 = {"|-\n! Reward \n"} d = 0 i = i + 1 end table.insert(tOut1, '! style="width: 12%" id="' .. date(year .. "/" .. k .. "/" .. n):fmt("%Y/%m/%d") .. '"| ' .. day_of_week(n, k, year) .. " \n")

table.insert(tOut2, '|') local space = "" for item in mw.text.gsplit((tArgs[a + 1] or ""), "%s*%$%s*") do				if not tArgs[a + 1] then table.insert(tOut2, " Unknown Reward ") break elseif tArgs[a + 1]=="X" then break end local tt = mw.text.split(item, "%s*#%s*") table.insert(tOut2, space) table.insert(tOut2, oneItem(tt[1], tt[2], nil, true)) total = oneItem(tt[1], tt[2], nil, true, total, 1) space = " " end table.insert(tOut2, '\n') a = a + 1 end end

if d < 7 then d = 7 - d - 1 for n=1, d do table.insert(tOut1, '! style="width: 12%"|\n') table.insert(tOut2, "|\n") end table.insert(tOut, table.concat(tOut1)) table.insert(tOut, table.concat(tOut2)) end

return table.concat(tOut) .. "|}\n" .. TotalTable end

function p.getStreak(frame) local tArgs = util.getArgs(frame) local tOut = {'{| class="article-table dmk-calendar" style="text-align: center;line-height: 1.0;width: 100%;"\n!#\n! style="width: 15%;"|Streak Days\n! style="width: 15%;"|Total Days\n! style="width: 30%;"|Base Reward\n! style="width: 30%;"|Alternate Reward', " (If base reward already unlocked) "} local i = 0

for k, data in ipairs(tArgs) do		local v = mw.text.split(data, "%s*%$%s*") table.insert(tOut, '\n|-\n!') table.insert(tOut, k)		table.insert(tOut, '\n|') table.insert(tOut, v[1]) table.insert(tOut, '\n| id="Day ')		i = i + v[1]		table.insert(tOut, i)		table.insert(tOut, '"|') table.insert(tOut, i)		table.insert(tOut, '\n|') local tt = mw.text.split(v[2], "%s*#%s*") table.insert(tOut, oneItem(tt[1], (tt[2] or 1), nil, true)) table.insert(tOut, '\n|') if v[3] then local tt = mw.text.split(v[3], "%s*#%s*") table.insert(tOut, oneItem(tt[1], (tt[2] or 1), nil, true)) end end

return table.concat(tOut) .. "\n|}" end

function p.getDefault(frame) local tArgs = util.getArgs(frame)

local colheader = {} if tArgs["colheader1"] then colheader[1] = mw.text.split(tArgs["colheader1"], "%s*%$%s*") end if tArgs["colheader2"] then colheader[2] = mw.text.split(tArgs["colheader2"], "%s*%$%s*") end if tArgs["colheader3"] then colheader[3] = mw.text.split(tArgs["colheader3"], "%s*%$%s*") end if tArgs["colheader4"] then colheader[4] = mw.text.split(tArgs["colheader4"], "%s*%$%s*") end

local tOut = {'{| class="article-table dmk-calendar" style="width: 100%;"\n'} local tOut1 = {} local tOut2 = {} local d = 0 local w = 0

for k, data in ipairs(tArgs) do		d = d + 1 if d == 1 then w = w + 1 tOut1 = {'|-\n! style="width: 12%"|\n'} tOut2 = {"|-\n! ", (tArgs["rowheader" .. w] or "Rewards"), " \n"} end table.insert(tOut1, ('! style="width: 12%" id="Day ' .. k .. '"| ' .. (colheader[w] and colheader[w][d] or ('Day ' .. k)) .. " \n"))

table.insert(tOut2, '| style="vertical-align: top;"|') local space = "" for item in mw.text.gsplit(data, "%s*%$%s*") do			local tt = mw.text.split(item, "%s*#%s*") table.insert(tOut2, space) table.insert(tOut2, oneItem(tt[1], tt[2], nil, true)) space = " " end table.insert(tOut2, '\n') if d == 7 then table.insert(tOut, table.concat(tOut1)) table.insert(tOut, table.concat(tOut2)) d = 0 end end

if d ~= 0 then d = 7 - d		for n=1, d do table.insert(tOut1, '! style="width: 12%"|\n') table.insert(tOut2, "|\n") end table.insert(tOut, table.concat(tOut1)) table.insert(tOut, table.concat(tOut2)) end

return table.concat(tOut) .. "|}" end

function p.getTrivia(frame) local tArgs = util.getArgs(frame) local UPD = require("Module:UpdateNumber").getVersion

local tCalendar = {	["D"] = "Daily Login Rewards", ["E"] = "Earth Day Gifting 2017", ["H"] = "December Holiday Gifting 2017", ["S"] = "Streak Rewards Calendar", ["W"] = "Welcome Rewards Calendar", ["R"] = "Daily Rewards Calendar" }

local pagename = mw.title.getCurrentTitle.text:gsub("s?$","",1) if not tArgs[1] or not tCalendar[tArgs[1]] then tArgs[1] = "R" end

local tOut = {' Players were awarded with ', (tArgs["s"] or "a"), ' free ', pagename, (tArgs["s"] and "s" or ""), ' on ', "?", ' as part of the ', tCalendar[tArgs[1, ']]', "", "", "", "", '.'}

if tArgs[2] then local today = "/" .. os.date("%Y") local year = "" tOut[11] = "" local dList = mw.text.split(tArgs[2], "%s*$%s*") for idx, d in ipairs(dList) do if idx ~= 1 and not dList[idx + 1] then tOut[11] = tOut[11] .. " and " elseif idx ~= 1 then tOut[11] = tOut[11] .. ", " end if tArgs[1] == "R" and d:match("^%d%d%d%d%/%d%d%/%d%d$") then year = "/" .. date(d):fmt("%Y") if year == today then year = "" end tOut[2] = tCalendar[tArgs[1]] .. year tOut[4] = tCalendar[tArgs[1]] .. year tOut[13] = tCalendar[tArgs[1]] .. year .. "|" .. tCalendar[tArgs[1]] tOut[11] = tOut[11] .. "" .. tCalendar[tArgs[1 .. year .. "#" .. d .. "|" .. util.strMultiDate({d}) .. "]]"			elseif tArgs[1] == "R" then tOut[11] = tOut[11] .. d			else tOut[11] = tOut[11] .. "" .. tCalendar[tArgs[1 .. "#Day " .. d .. "|Day " .. d .. "]]"			end end end

if tArgs[3] and tArgs[4] then tOut[15] = " from " tOut[16] = UPD({tArgs[3], "D"}) tOut[17] = " to " tOut[18] = UPD({tArgs[4], "D"}) elseif tArgs[3] then tOut[15] = " since " tOut[16] = UPD({tArgs[3], "D"}) end

return table.concat(tOut) end

return p