Initial commit

master
Wuzzy 2020-08-27 02:24:25 +02:00
commit 3628007ecd
2 changed files with 278 additions and 0 deletions

149
gui.lua Normal file
View File

@ -0,0 +1,149 @@
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
local COLOR_HOLIDAY = "#00FF00"
local COLOR_DAYBOX = "#00000080"
local COLOR_DAYBOX_TODAY = "#FFFFFF80"
local player_current_calendars = {}
local ORDINAL = false
local function show_calendar(player_name, ordinal, wanted_months, wanted_years)
local days, months, years = calendar.get_date()
local ddays, dmonths, dyears = calendar.get_date(nil, ordinal)
local wanted_dmonths, wanted_dyears
if not wanted_months then
wanted_months = months
end
if not wanted_years then
wanted_years = years
end
wanted_dmonths = wanted_months
wanted_dyears = wanted_years
if ordinal then
wanted_dmonths = wanted_dmonths + 1
wanted_dyears = wanted_dyears + 1
end
local weekday = 0
local formspec = "formspec_version[3]size["..(calendar.WEEK_DAYS+2)..",9]"
if ordinal then
formspec = formspec .. "label[0.5,0.5;"..F(S("Month @1, year @2", wanted_dmonths, wanted_dyears)).."]"
else
formspec = formspec .. "label[0.5,0.5;"..F(S("@1 months, @2 years", wanted_dmonths, wanted_dyears)).."]"
end
local start_day, end_day
local tdays = 0
tdays = tdays + wanted_years * (calendar.MONTHS * calendar.MONTH_DAYS)
tdays = tdays + wanted_months * calendar.MONTH_DAYS
local x, y = 0.75, 1.2
for w=1, #calendar.weekday_names_short do
formspec = formspec .. "label["..x..","..y..";"..calendar.weekday_names_short[w].."]"
x = x + 1.1
end
x, y = 0.5, 1.7
for iday=0, calendar.MONTH_DAYS - 1 do
weekday = weekday + 1
if weekday > calendar.WEEK_DAYS then
weekday = 1
y = y + 1.1
end
x = (weekday*1.1)- 0.5
local pday = iday
if ordinal then
pday = iday + 1
end
local day_str = tostring(pday)
local box_color = COLOR_DAYBOX
local holidays = calendar.get_holidays(tdays)
local tooltip_lines = {}
if #holidays > 0 then
table.insert(tooltip_lines, minetest.colorize(COLOR_HOLIDAY, holidays[1].name))
day_str = minetest.colorize(COLOR_HOLIDAY, day_str)
end
if tdays == days then
box_color = COLOR_DAYBOX_TODAY
table.insert(tooltip_lines, S("Today"))
end
if #tooltip_lines > 0 then
formspec = formspec .. "tooltip["..x..","..y..";1,1;"..F(table.concat(tooltip_lines, "\n")).."]"
end
day_str = F(day_str)
formspec = formspec .. "box["..x..","..y..";1,1;"..box_color.."]" ..
"label["..(x+0.15)..","..(y+0.3)..";"..day_str.."]"
tdays = tdays + 1
end
y = y + 1.2
if wanted_months > 0 or wanted_years > 0 then
formspec = formspec .. "button[0.5,"..y..";1,1;prev_year;<<]"
.. "button[1.5,"..y..";1,1;prev_month;<]"
end
formspec = formspec .. "button[2.5,"..y..";1,1;next_month;>]"
.. "button[3.5,"..y..";1,1;next_year;>>]"
minetest.show_formspec(player_name, "calendar:calendar", formspec)
player_current_calendars[player_name] = { years = wanted_years, months = wanted_months }
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "calendar:calendar" then
return
end
if not player:is_player() then
return
end
local name = player:get_player_name()
local cur_years, cur_months
if not player_current_calendars then
return
end
cur_years = player_current_calendars[name].years
cur_months = player_current_calendars[name].months
if fields.next_year then
cur_years = cur_years + 1
show_calendar(name, ORDINAL, cur_months, cur_years)
elseif fields.prev_year then
if cur_years == 0 then
cur_months = 0
else
cur_years = cur_years - 1
end
show_calendar(name, ORDINAL, cur_months, cur_years)
elseif fields.next_month then
cur_months = cur_months + 1
if cur_months > calendar.MONTHS - 1 then
cur_months = 0
cur_years = cur_years + 1
end
show_calendar(name, ORDINAL, cur_months, cur_years)
elseif fields.prev_month then
cur_months = cur_months - 1
if cur_months < 0 then
if cur_years > 0 then
cur_months = calendar.MONTHS - 1
cur_years = cur_years - 1
end
end
show_calendar(name, ORDINAL, cur_months, cur_years)
end
end)
minetest.register_on_leaveplayer(function(player)
local name = player:get_player_name()
player_current_calendars[name] = nil
end)
minetest.register_chatcommand("calendar", {
param = "",
description = S("Display calendar"),
func = function( name, param )
show_calendar(name, ORDINAL)
end,
})

129
init.lua Normal file
View File

@ -0,0 +1,129 @@
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
calendar.MONTH_DAYS = 30
-- Long month names
calendar.month_names = { S('January'), S('February'), S('March'), S('April'), S('May'), S('June'), S('July'), S('August'), S('September'), S('October'), S('November'), S('December') }
--calendar.month_names = { S('Spring'), S('Summer'), S('Harvest'), S('Winter') }
-- Short month names
calendar.month_names_short = { S('Jan'), S('Feb'), S('Mar'), S('Apr'), S('May'), S('Jun'), S('Jul'), S('Aug'), S('Sep'), S('Oct'), S('Nov'), S('Dec') }
--calendar.month_names_short = { S('Spr'), S('Sum'), S('Har'), S('Win') }
calendar.MONTHS = #calendar.month_names
calendar.month_lengths = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
--calendar.month_lengths = { 30, 30, 30, 30 }
-- Week day names
calendar.weekday_names = { S("Monday"), S("Tuesday"), S("Wednesday"), S("Thursday"), S("Friday"), S("Saturday"), S("Sunday") }
calendar.weekday_names_short = { S("Mo"), S("Tu"), S("We"), S("Th"), S("Fr"), S("Sa"), S("Su") }
calendar.WEEK_DAYS = #calendar.weekday_names
local holidays = {
{ name = S("New Year's Eve"),
days = 0,
months = 0
},
{ name = S("Lucky Day"),
days = 7,
months = 7
},
{ name = S("Bad Luck Day"),
days = 13,
months = 2
},
{ name = S("Boxing Day"),
days = 23,
months = 11
},
{ name = S("Scary Day"),
days = 29,
months = 8
},
{ name = S("Fool's Day"),
days = 0,
months = 3
},
{ name = S("Sylvester"),
days = 29,
months = 11
},
}
calendar.get_holidays = function(total_days)
local days, months, years = calendar.get_date(total_days)
local found_holidays = {}
for h=1, #holidays do
local holiday = holidays[h]
if holiday.days == days and holiday.months == months then
table.insert(found_holidays, holiday)
end
end
return found_holidays
end
-- Returns day, month, year, days in year
-- * days: elapsed days since start of world
-- * ordinal: if true, use ordinal numbers (day, month, year start counting at 1).
-- if false, use cardinal numbers (day, month, year start counting at 0).
-- default: false
calendar.get_date = function(days, ordinal)
if not days then
days = minetest.get_day_count()
end
local y = math.floor( days / 360 ) -- elapsed years in epoch
local m = math.floor( days % 360 / 30 ) -- elapsed months in year
local d = math.floor( days % 360 % 30 ) -- elapsed days in month
local j = math.floor( days % 360 ) -- elapsed days in year
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
calendar.get_date_string = function( str, env_date )
if not env_date then
env_date = minetest.get_day_count( )
end
if str == nil then
str = S("%D days, %M months, %Y years")
end
local y, m, d, j = calendar.get_date()
-- cardinal values
str = string.gsub( str, "%%Y", y ) -- elapsed years in epoch
str = string.gsub( str, "%%M", m ) -- elapsed months in year
str = string.gsub( str, "%%D", d ) -- elapsed days in month
str = string.gsub( str, "%%J", j ) -- elapsed days in year
-- ordinal values
str = string.gsub( str, "%%y", y + 1 ) -- current year of epoch
str = string.gsub( str, "%%m", m + 1 ) -- current month of year
str = string.gsub( str, "%%d", d + 1 ) -- current day of month
str = string.gsub( str, "%%j", j + 1 ) -- current day of year
str = string.gsub( str, "%%b", calendar.month_names[ m + 1 ] ) -- current month long name
str = string.gsub( str, "%%h", calendar.month_names_short[ m + 1 ] ) -- current month short name
str = string.gsub( str, "%%z", "%%" )
return str
end
dofile(minetest.get_modpath("calendar").."/gui.lua")