Module:Calendar

--[=[

Lua Module for Calendar Functions

--]=]

local p = {} local util = require("Module:Utility") local cData = mw.loadData("Module:ItemCollection/data") local EC = require("Module:EC").getEC local CHEST = require("Module:EnchantedChest").getChest local TOK = require("Module:CharacterToken").getToken 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 oneItem(allitems) if not allitems then return " Unknown Reward " end local tOut = {} local space = "" local conv = { ["ch"] = "cp", ["cos"] = "cp", }

for item in mw.text.gsplit(allitems, "%s*%$%s*") do		local tt = mw.text.split(item, "%s*#%s*") local iName = tt[1] or item local nCount = tonumber((string.gsub(tt[2] or "", "[,]", ""))) or 1

local tItem = {space, "", " " .. util.th_comma(nCount), " ",				"", "",				" "		}

local iData = cData["Items"][iName]

if iName == "X" then return "" elseif iName:match("Chests") then tItem[2] = CHEST({iName, 35}) tItem[5] = CHEST({iName, "L"}) if nCount > 1 then tItem[6] = "s" end iName = "CHEST-" .. iName elseif iData and iData[2] then local iDisp = iName:gsub("^([^%(]-)%/([^%(]-)$", "%2") local fname = iName:lower:gsub("[':%,%.]", ""):gsub(" ", "_") if iData[2] == "pf" then fname = fname:gsub("_float$", "") else fname = fname:gsub("^([^%(]-)%/([^%(]-)$", "%1-%2") fname = mw.ustring.gsub(fname, "^([^%(]-) *%(([^%(]-)%)$", "%1_%2") end tItem[2] = table.concat({""					})

if iData["C"] and cData["Categories"][iData["C"]] then local category = iData["C"] tItem[2] = ' ' .. tItem[2] .. '  '				tItem[4] = ' ' end

tItem[3] = "" tItem[5] = "" .. (iDisp or iName) .. "" iName = "ZITM-" .. iName elseif item:match("^EC") then tItem[2] = EC({iName, nil, nil, nil, 30}) tItem[5] = EC({iName, "L", S=(nCount == 1)}) else tItem[2] = TOK({iName, 35, "B"}) tItem[5] = TOK({iName, "L"}) if nCount > 1 then tItem[6] = "s" end iName = "TOK-" .. iName end space = " " table.insert(tOut, table.concat(tItem))

if not total[iName] then total[iName] = {I = (tItem[2] .. tItem[4] .. tItem[5] .. tItem[6])} end if not total[iName][1] then total[iName][1] = nCount else total[iName][1] = (total[iName][1] + nCount) end end

return table.concat(tOut) 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 2016 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, ('|' .. oneItem(tArgs[a + 1]) .. '\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|') table.insert(tOut, oneItem(v[2])) table.insert(tOut, '\n|') if v[3] then table.insert(tOut, oneItem(v[3])) 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 ""), " Reward \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;"|' .. oneItem(data) .. '\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 ', (tArgs["IP"] or ""), 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[12] = "" 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[12] = tOut[12] .. " and " elseif idx ~= 1 then tOut[12] = tOut[12] .. ", " 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[14] = tCalendar[tArgs[1]] .. year .. "|" .. tCalendar[tArgs[1]] tOut[12] = tOut[12] .. "" .. tCalendar[tArgs[1 .. year .. "#" .. d .. "|" .. util.strMultiDate({d}) .. "]]"			elseif tArgs[1] == "R" then tOut[12] = tOut[12] .. d			else tOut[12] = tOut[12] .. "" .. tCalendar[tArgs[1 .. "#Day " .. d .. "|Day " .. d .. "]]"			end end end

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

return table.concat(tOut) end

return p