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 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 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") 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) .. "" 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}) tItem[5] = TOK({iName, "L"}) if nCount > 1 then tItem[6] = "s" end end space = " " table.insert(tOut, table.concat(tItem)) end

return table.concat(tOut) end

function p.main(frame) local tArgs = util.getArgs(frame) local year = tArgs[1] or 2016 local tOut = {'{| 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%"| ' .. 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) .. "|}" 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|') i = i + v[1] 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

return p