Module:Events

From Coromon Wiki
Jump to navigation Jump to search

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

local p = {}

local values = {
	FIRST = 1,
	SECOND = 2,
	THIRD = 3,
	FOURTH = 4,
	MONDAY = 0,
	TUESDAY = 1,
	WEDNESDAY = 2,
	THURSDAY = 3,
	FRIDAY = 4,
	SATURDAY = 5,
	SUNDAY = 6,
	JUNUARY = 1,
	FEBRUARY = 2,
	MARCH = 3,
	APRIL = 4,
	MAY = 5,
	JUNE = 6,
	JULY = 7,
	AUGUST = 8,
	SEPTEMBER = 9,
	OCTOBER = 10,
	NOVEMBER = 11,
	DECEMBER = 12
}

function is (ordinal, weekDay, month)
  return isMonth(month) and tonumber(os.date("%w")) == weekDay and mtonumber(os.date("%d")), 7 == ordinal
end

function isDay (month, dayOfMonth)
  return isMonth(month) and tonumber(os.date("%d")) == dayOfMonth
end

function isMonth (month)
	return tonumber(os.date("%m")) == month
end

function isDayWithOffset (month, dayOfMonth, offsetInDays)
  local currentTimeWithOffset = os.time() - offsetInDays
  return tonumber(os.date("%m", currentTimeWithOffset)) == month and tonumber(os.date("%d", currentTimeWithOffset)) == dayOfMonth
end

function p.christmas ()
  return isDay(values.DECEMBER, 25) or isDay(values.DECEMBER, 26)
end

function p.halloween ()
  return isDay(values.OCTOBER, 31)
end

function p.easter ()
	
  local easter = getEaster()
  
  return isDay(easter.month, easter.day) or isDayWithOffset(easter.month, easter.day, 1)
end

function p.easterDate ()
	
  local easter = getEaster()
  
  return easter.month .. '/' .. easter.day
end

function getEaster ()
	
	local year = tonumber(os.date("%Y"))
  local a = year % 19
  local b = math.floor(year / 100)
  local c = year % 100
  local d = math.floor(b / 4)
  local e = b % 4
  local f = math.floor((b + 8) / 25)
  local g = math.floor((b - f + 1) / 3)
  local h = (19 * a + b - d - g + 15) % 30
  local i = math.floor(c / 4)
  local k = c % 4
  local l = (32 + 2 * e + 2 * i - h - k) % 7
  local m = math.floor((a + 11 * h + 22 * l) / 451)
  local _day = (h + l - 7 * m + 114) % 31 + 1
  local _month = math.floor((h + l - 7 * m + 114) / 31)
  
  return {day = _day, month = _month}
end

function p.getCurrentEvent ()
	
	if p.easter () then
		return "Easter"
	end
	if p.halloween () then
		return "Halloween"
	end
	if p.christmas () then
		return "Christmas"
	end
	
	return "None"
end

return p