PineappleFan | 459a2e6 | 2023-02-14 08:50:54 +0000 | [diff] [blame] | 1 | function daysIntoSeason(date) { |
| 2 | return Math.floor((new Date().getTime() - date.getTime()) / (1000 * 60 * 60 * 24)) |
| 3 | } |
| 4 | |
pineafan | f5dd187 | 2023-02-28 17:33:16 +0000 | [diff] [blame] | 5 | function daysSinceDate(date) { |
| 6 | // If the date is after today, make it last year |
| 7 | if (date > new Date()) { |
| 8 | date.setFullYear(date.getFullYear() - 1); |
| 9 | } |
| 10 | return Math.floor((new Date().getTime() - date.getTime()) / (1000 * 60 * 60 * 24)); |
| 11 | } |
| 12 | |
PineaFan | e69bd22 | 2022-12-04 22:18:20 +0000 | [diff] [blame] | 13 | function getSeason() { |
| 14 | let year = new Date().getFullYear(); |
| 15 | |
| 16 | const dates = { |
| 17 | pride: [new Date(`${year}-6-1`), new Date(`${year}-6-31`)], |
| 18 | halloween: [new Date(`${year}-10-25`), new Date(`${year}-11-1`)], |
| 19 | trans: [new Date(`${year}-11-13`), new Date(`${year}-11-19`)], |
PineappleFan | 37476d2 | 2023-02-03 21:36:40 +0000 | [diff] [blame] | 20 | christmas: [new Date(`${year}-12-1`), new Date(`${year}-12-26`)], |
PineappleFan | 85b4cc4 | 2023-03-01 08:20:00 +0000 | [diff] [blame^] | 21 | aprilFools: [new Date(`${year}-04-01`), new Date(`${year}-04-01`)] |
PineaFan | e69bd22 | 2022-12-04 22:18:20 +0000 | [diff] [blame] | 22 | } |
| 23 | const filePaths = { |
| 24 | normal: "normal", |
| 25 | halloween: "seasonal/halloween", |
| 26 | christmas: "seasonal/christmas", |
| 27 | pride: "seasonal/pride", |
PineappleFan | 37476d2 | 2023-02-03 21:36:40 +0000 | [diff] [blame] | 28 | trans: "seasonal/trans", |
| 29 | aprilFools: "seasonal/aprilFools" |
PineaFan | e69bd22 | 2022-12-04 22:18:20 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | let current = new Date(); |
| 33 | let currentSeason = Object.keys(dates).find((str) => current >= dates[str][0] && current <= dates[str][1]) || "normal"; |
pineafan | f5dd187 | 2023-02-28 17:33:16 +0000 | [diff] [blame] | 34 | let currentDaysIntoSeason; |
PineappleFan | e9ce4cc | 2023-02-13 22:05:07 +0000 | [diff] [blame] | 35 | if (currentSeason !== "normal") { |
pineafan | f5dd187 | 2023-02-28 17:33:16 +0000 | [diff] [blame] | 36 | currentDaysIntoSeason = daysIntoSeason(dates[currentSeason][0]) |
Ash | efe6f6b | 2023-02-13 22:07:43 +0000 | [diff] [blame] | 37 | } else { |
pineafan | f5dd187 | 2023-02-28 17:33:16 +0000 | [diff] [blame] | 38 | // Calculate how many days it has been since each season ended |
| 39 | let days = Object.keys(dates).map((str) => daysSinceDate(dates[str][1])) |
| 40 | currentDaysIntoSeason = Math.min(...days); |
PineappleFan | e9ce4cc | 2023-02-13 22:05:07 +0000 | [diff] [blame] | 41 | } |
PineaFan | e69bd22 | 2022-12-04 22:18:20 +0000 | [diff] [blame] | 42 | |
| 43 | return { |
| 44 | season: currentSeason, |
PineappleFan | e9ce4cc | 2023-02-13 22:05:07 +0000 | [diff] [blame] | 45 | filePath: filePaths[currentSeason], |
pineafan | f5dd187 | 2023-02-28 17:33:16 +0000 | [diff] [blame] | 46 | daysIntoSeason: currentDaysIntoSeason |
PineaFan | e69bd22 | 2022-12-04 22:18:20 +0000 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | |
| 50 | const Season = async (req, res) => { |
| 51 | return res.status(200).send(getSeason()); |
| 52 | } |
| 53 | |
| 54 | export default Season; |
| 55 | export { getSeason }; |