Add calendar.config function

master
Wuzzy 2020-08-28 14:54:34 +02:00
parent df8059d27f
commit c83e5ce1b2
3 changed files with 62 additions and 22 deletions

41
API.md
View File

@ -20,23 +20,38 @@ The calendar mod uses cardinal numbers internally to store the date internally,
this is only done to make calculations a bit simpler.
`total_days` is also a cardinal number.
## Calendar configiguration
In `gameconfig.lua`, there several variables that customize the calendar:
## Calendar configuration
Before calling any other function, you probably want to configure the calendar first.
Call `calendar.config` to do so. This is not required, however, the calendar
has a default configuration:
* `calendar.MONTH_DAYS`: Days in a month
* `calendar.month_names`: List of month names
* `calendar.month_names_short`: List of short month names
* `calendar.weekday_names`: List of weekday names
* `calendar.weekday_names_short`: List of short weekday names
* `calendar.FIRST_WEEK_DAY`: Cardinal number of the weekday that
marks the beginning of a week
and the calendar
By default, a year has 12 months with 30 days each,
By default, ayear has 12 months with 30 days each,
the months are January to December,
the week has 7 weekdays from Monday to Sunday, starting at Monday.
For convenience, there are also these shortcuts, derived from the other values:
### `calendar.config(config)`
Configure calendar. Call this function before any other function.
`config` is a table with the following fields (all fields are optional):
* `MONTH_DAYS`: Days in a month
* `month_names`: List of month names (also determines number of months)
* `month_names_short`: List of short month names
* `weekday_names`: List of weekday names (also determines number of weekdays)
* `weekday_names_short`: List of short weekday names
* `FIRST_WEEK_DAY`: Cardinal number of the weekday that
marks the beginning of a week
and the calendar
Fields that were `nil` won't change. You can access each of the calender settings
later with `calender.<field name>`, e.g. `calendar.MONTH_DAYS`. Note this is
read-only access, you must never write to these fields directly. Only
use `calendar.config` to change these fields.
### Convenience variables
For convenience, there are also these shortcuts that are automatically set,
derived from the other values:
* `calendar.MONTHS`: Number of months in a year
* `calendar.WEEK_DAYS`: Number of days in a week
* `calendar.YEAR_DAYS`: Number of days in a year

View File

@ -1,9 +1,9 @@
-- For translation collector script
local N = function(s) return s end
----------------------------------
-- BEGINNING OF CALENDAR CONFIG --
----------------------------------
-----------------------------
-- DEFAULT CALENDAR CONFIG --
-----------------------------
-- Number of days in a month
calendar.MONTH_DAYS = 30

View File

@ -34,15 +34,40 @@ local F = minetest.formspec_escape
dofile(minetest.get_modpath("calendar").."/gameconfig.lua")
-- Number of months in a year
calendar.MONTHS = #calendar.month_names
-- Number of days in a week
calendar.WEEK_DAYS = #calendar.weekday_names
-- Number of days in a year
calendar.YEAR_DAYS = calendar.MONTHS * calendar.MONTH_DAYS
local function update_helper_vars()
-- Number of months in a year
calendar.MONTHS = #calendar.month_names
-- Number of days in a week
calendar.WEEK_DAYS = #calendar.weekday_names
-- Number of days in a year
calendar.YEAR_DAYS = calendar.MONTHS * calendar.MONTH_DAYS
end
update_helper_vars()
local holidays = {}
calendar.config = function(config)
if config.MONTH_DAYS then
calendar.MONTH_DAYS = config.MONTH_DAYS
end
if config.month_names then
calendar.month_names = config.month_names
end
if config.month_names_short then
calendar.month_names_short = config.month_names_short
end
if config.weekday_names then
calendar.weekday_names = config.weekday_names
end
if config.weekday_names_short then
calendar.weekday_names_short = config.weekday_names_short
end
if config.FIRST_WEEK_DAY then
calendar.FIRST_WEEK_DAY = config.FIRST_WEEK_DAY
end
update_helper_vars()
end
calendar.register_holiday = function(def)
table.insert(holidays, def)
end