blob: fe3e26261a72780291e343c8c22439965b5c331c [file] [log] [blame]
PineappleFan459a2e62023-02-14 08:50:54 +00001function daysIntoSeason(date) {
2 return Math.floor((new Date().getTime() - date.getTime()) / (1000 * 60 * 60 * 24))
3}
4
pineafanf5dd1872023-02-28 17:33:16 +00005function 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
PineaFane69bd222022-12-04 22:18:20 +000013function 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`)],
PineappleFan37476d22023-02-03 21:36:40 +000020 christmas: [new Date(`${year}-12-1`), new Date(`${year}-12-26`)],
PineappleFan85b4cc42023-03-01 08:20:00 +000021 aprilFools: [new Date(`${year}-04-01`), new Date(`${year}-04-01`)]
PineaFane69bd222022-12-04 22:18:20 +000022 }
23 const filePaths = {
24 normal: "normal",
25 halloween: "seasonal/halloween",
26 christmas: "seasonal/christmas",
27 pride: "seasonal/pride",
PineappleFan37476d22023-02-03 21:36:40 +000028 trans: "seasonal/trans",
29 aprilFools: "seasonal/aprilFools"
PineaFane69bd222022-12-04 22:18:20 +000030 }
31
32 let current = new Date();
33 let currentSeason = Object.keys(dates).find((str) => current >= dates[str][0] && current <= dates[str][1]) || "normal";
pineafanf5dd1872023-02-28 17:33:16 +000034 let currentDaysIntoSeason;
PineappleFane9ce4cc2023-02-13 22:05:07 +000035 if (currentSeason !== "normal") {
pineafanf5dd1872023-02-28 17:33:16 +000036 currentDaysIntoSeason = daysIntoSeason(dates[currentSeason][0])
Ashefe6f6b2023-02-13 22:07:43 +000037 } else {
pineafanf5dd1872023-02-28 17:33:16 +000038 // Calculate how many days it has been since each season ended
39 let days = Object.keys(dates).map((str) => daysSinceDate(dates[str][1]))
TheCodedProf0c272892023-03-01 11:53:51 -050040 currentDaysIntoSeason = Math.min(...days) - 1;
PineappleFane9ce4cc2023-02-13 22:05:07 +000041 }
PineaFane69bd222022-12-04 22:18:20 +000042
43 return {
44 season: currentSeason,
PineappleFane9ce4cc2023-02-13 22:05:07 +000045 filePath: filePaths[currentSeason],
pineafanf5dd1872023-02-28 17:33:16 +000046 daysIntoSeason: currentDaysIntoSeason
PineaFane69bd222022-12-04 22:18:20 +000047 }
48}
49
50const Season = async (req, res) => {
51 return res.status(200).send(getSeason());
52}
53
54export default Season;
55export { getSeason };