minetest_calendar/init.lua

133 lines
3.9 KiB
Lua
Raw Normal View History

2020-08-26 17:24:25 -07:00
calendar = {}
local S
if minetest.get_translator then
S = minetest.get_translator("calendar")
else
S = function(s) return s end
end
local F = minetest.formspec_escape
2020-08-28 02:17:16 -07:00
dofile(minetest.get_modpath("calendar").."/gameconfig.lua")
2020-08-26 17:24:25 -07:00
2020-08-28 02:17:16 -07:00
-- Number of months in a year
2020-08-26 17:24:25 -07:00
calendar.MONTHS = #calendar.month_names
2020-08-28 02:17:16 -07:00
-- Number of days in a week
2020-08-26 17:24:25 -07:00
calendar.WEEK_DAYS = #calendar.weekday_names
2020-08-28 02:17:16 -07:00
-- Number of days in a year
calendar.YEAR_DAYS = calendar.MONTHS * calendar.MONTH_DAYS
2020-08-26 17:24:25 -07:00
2020-08-27 01:40:04 -07:00
local holidays = {}
calendar.register_holiday = function(def)
table.insert(holidays, def)
end
2020-08-27 15:06:46 -07:00
-- Example holidays
2020-08-27 01:40:04 -07:00
calendar.register_holiday({
name = S("New Year's Eve"),
2020-08-27 15:06:46 -07:00
type = "monthday",
2020-08-27 01:40:04 -07:00
days = 0,
months = 0
})
2020-08-26 17:24:25 -07:00
2020-08-27 15:06:46 -07:00
calendar.register_holiday({
name = S("Mother's Day"),
type = "custom",
-- First Sunday in May
is_holiday = function(total_days)
local d, m, y = calendar.get_date(total_days)
local wday = calendar.get_weekday(total_days)
return wday == 6 and m == 4 and d <= 6
end,
})
2020-08-26 17:24:25 -07:00
calendar.get_holidays = function(total_days)
2020-08-28 01:48:29 -07:00
if not total_days then
total_days = minetest.get_day_count()
end
2020-08-26 17:24:25 -07:00
local days, months, years = calendar.get_date(total_days)
local found_holidays = {}
for h=1, #holidays do
local holiday = holidays[h]
2020-08-27 15:06:46 -07:00
if holiday.type == "monthday" then
if holiday.days == days and holiday.months == months and (holiday.years == nil or holiday.years == years) then
table.insert(found_holidays, holiday)
end
elseif holiday.type == "custom" then
if holiday.is_holiday(total_days) == true then
table.insert(found_holidays, holiday)
end
else
minetest.log("error", "[calender] Invalid holiday type: "..tostring(holiday.type))
2020-08-26 17:24:25 -07:00
end
end
return found_holidays
end
2020-08-27 00:42:22 -07:00
calendar.get_weekday = function(total_days)
2020-08-28 01:48:29 -07:00
if not total_days then
total_days = minetest.get_day_count()
end
2020-08-27 00:42:22 -07:00
return (total_days + calendar.FIRST_WEEK_DAY) % calendar.WEEK_DAYS
end
2020-08-28 01:48:29 -07:00
calendar.get_date = function(total_days, ordinal)
if not total_days then
total_days = minetest.get_day_count()
2020-08-26 17:24:25 -07:00
end
2020-08-28 01:48:29 -07:00
local y = math.floor(total_days / calendar.YEAR_DAYS) -- elapsed years in epoch
local m = math.floor(total_days % calendar.YEAR_DAYS / calendar.MONTH_DAYS) -- elapsed months in year
local d = math.floor(total_days % calendar.YEAR_DAYS % calendar.MONTH_DAYS) -- elapsed days in month
local j = math.floor(total_days % calendar.YEAR_DAYS) -- elapsed days in year
2020-08-26 17:24:25 -07:00
if ordinal == nil then
ordinal = false
end
if ordinal then
y = y + 1
m = m + 1
d = d + 1
j = j + 1
end
return d, m, y, j
end
2020-08-28 01:48:29 -07:00
calendar.get_date_string = function(format, total_days)
if not total_days then
total_days = minetest.get_day_count()
2020-08-26 17:24:25 -07:00
end
2020-08-28 01:48:29 -07:00
if format == nil then
format = S("%D days, %M months, %Y years")
2020-08-26 17:24:25 -07:00
end
2020-08-28 01:48:29 -07:00
local y, m, d, j = calendar.get_date(total_days)
2020-08-26 17:24:25 -07:00
-- cardinal values
2020-08-28 01:48:29 -07:00
format = string.gsub( format, "%%Y", y ) -- elapsed years in epoch
format = string.gsub( format, "%%M", m ) -- elapsed months in year
format = string.gsub( format, "%%D", d ) -- elapsed days in month
format = string.gsub( format, "%%J", j ) -- elapsed days in year
2020-08-26 17:24:25 -07:00
-- ordinal values
2020-08-28 01:48:29 -07:00
format = string.gsub( format, "%%y", y + 1 ) -- current year of epoch
format = string.gsub( format, "%%m", m + 1 ) -- current month of year
format = string.gsub( format, "%%d", d + 1 ) -- current day of month
format = string.gsub( format, "%%j", j + 1 ) -- current day of year
2020-08-26 17:24:25 -07:00
format = string.gsub( format, "%%b", S(calendar.month_names[ m + 1 ]) ) -- current month long name
format = string.gsub( format, "%%h", S(calendar.month_names_short[ m + 1 ]) ) -- current month short name
2020-08-26 17:24:25 -07:00
2020-08-28 03:49:13 -07:00
format = string.gsub( format, "%%b", S(calendar.month_names[ m + 1 ]) ) -- current month long name
format = string.gsub( format, "%%h", S(calendar.month_names_short[ m + 1 ]) ) -- current month short name
2020-08-28 01:48:29 -07:00
format = string.gsub( format, "%%z", "%%" )
2020-08-26 17:24:25 -07:00
2020-08-28 01:48:29 -07:00
return format
2020-08-26 17:24:25 -07:00
end
dofile(minetest.get_modpath("calendar").."/gui.lua")
2020-08-27 14:08:58 -07:00
dofile(minetest.get_modpath("calendar").."/command.lua")
dofile(minetest.get_modpath("calendar").."/node.lua")