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 | e69bd22 | 2022-12-04 22:18:20 +0000 | [diff] [blame] | 5 | function getSeason() { |
| 6 | let year = new Date().getFullYear(); |
| 7 | |
| 8 | const dates = { |
| 9 | pride: [new Date(`${year}-6-1`), new Date(`${year}-6-31`)], |
| 10 | halloween: [new Date(`${year}-10-25`), new Date(`${year}-11-1`)], |
| 11 | trans: [new Date(`${year}-11-13`), new Date(`${year}-11-19`)], |
PineappleFan | 37476d2 | 2023-02-03 21:36:40 +0000 | [diff] [blame] | 12 | christmas: [new Date(`${year}-12-1`), new Date(`${year}-12-26`)], |
| 13 | aprilFools: [new Date(`${year}-04-01`), new Date(`${year}-04-01`)] |
PineaFan | e69bd22 | 2022-12-04 22:18:20 +0000 | [diff] [blame] | 14 | } |
| 15 | const filePaths = { |
| 16 | normal: "normal", |
| 17 | halloween: "seasonal/halloween", |
| 18 | christmas: "seasonal/christmas", |
| 19 | pride: "seasonal/pride", |
PineappleFan | 37476d2 | 2023-02-03 21:36:40 +0000 | [diff] [blame] | 20 | trans: "seasonal/trans", |
| 21 | aprilFools: "seasonal/aprilFools" |
PineaFan | e69bd22 | 2022-12-04 22:18:20 +0000 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | let current = new Date(); |
| 25 | let currentSeason = Object.keys(dates).find((str) => current >= dates[str][0] && current <= dates[str][1]) || "normal"; |
Ash | efe6f6b | 2023-02-13 22:07:43 +0000 | [diff] [blame] | 26 | let daysIntoSeason; |
PineappleFan | e9ce4cc | 2023-02-13 22:05:07 +0000 | [diff] [blame] | 27 | if (currentSeason !== "normal") { |
PineappleFan | 459a2e6 | 2023-02-14 08:50:54 +0000 | [diff] [blame^] | 28 | daysIntoSeason = daysIntoSeason(dates[currentSeason][0]) |
Ash | efe6f6b | 2023-02-13 22:07:43 +0000 | [diff] [blame] | 29 | } else { |
PineappleFan | 459a2e6 | 2023-02-14 08:50:54 +0000 | [diff] [blame^] | 30 | // Calculate the days from the end of each season |
| 31 | let days = Object.keys(dates).map((str) => daysIntoSeason(dates[str][1])).filter((num) => num > 0); |
| 32 | daysIntoSeason = Math.min(...days); |
PineappleFan | e9ce4cc | 2023-02-13 22:05:07 +0000 | [diff] [blame] | 33 | } |
PineaFan | e69bd22 | 2022-12-04 22:18:20 +0000 | [diff] [blame] | 34 | |
| 35 | return { |
| 36 | season: currentSeason, |
PineappleFan | e9ce4cc | 2023-02-13 22:05:07 +0000 | [diff] [blame] | 37 | filePath: filePaths[currentSeason], |
| 38 | daysIntoSeason: daysIntoSeason |
PineaFan | e69bd22 | 2022-12-04 22:18:20 +0000 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
| 42 | const Season = async (req, res) => { |
| 43 | return res.status(200).send(getSeason()); |
| 44 | } |
| 45 | |
| 46 | export default Season; |
| 47 | export { getSeason }; |