EventMapper/assets/scripts/KaplayMap/events.js
2024-06-10 12:37:53 -05:00

82 lines
2.1 KiB
JavaScript

export class EventManager {
mainmanager;
#events = new Map([]);
constructor() {}
async getAllEvents() {
// DO NOT use this function for general use - may overload the server(s). get ONCE and then cache!
const currentLocalization = this.mainmanager.getCurrentLangCode();
let allEventsReqSend = fetch(`/data/${currentLocalization}/events/`);
let allEventsReq = await allEventsReqSend;
let allEvents;
if (allEventsReq.ok) allEvents = await allEventsReq.json();
allEvents.sort(
(e1, e2) =>
new Date(e2.when.start).getTime() - new Date(e1.when.start).getTime()
);
return allEvents;
}
async getEvents() {
const currentLocalization = this.mainmanager.getCurrentLangCode();
const currentFloor = this.mainmanager.getCurrentFocus("floor");
let allEventsReqSend = fetch(
`/data/${currentLocalization}/events/${currentFloor}`
);
let allEventsReq = await allEventsReqSend;
let allEvents;
if (allEventsReq.ok) allEvents = await allEventsReq.json();
// Sort by date
allEvents.sort(
(e1, e2) =>
new Date(e2.when.start).getTime() - new Date(e1.when.start).getTime()
);
this.#events.clear();
allEvents.forEach((event) => this.#events.set(event.id, event));
}
setCurrentEvent(id) {
if (id == null) this.mainmanager.setCurrentFocus("event", id);
else {
const event = this.#events.get(id);
if (event != null) this.mainmanager.setCurrentFocus("event", id);
else throw "Event does not exist";
}
}
get currentRoomEvents() {
const currentFocus = this.mainmanager.getCurrentFocus("room");
let currentFloorRooms = new Map([]);
this.#events.forEach((v, k) => {
if (k.startsWith(currentFocus)) currentFloorRooms.set(k, v);
});
return currentFloorRooms;
}
get allEvents() {
return this.#events;
}
get currentEventId() {
return this.mainmanager.getCurrentFocus("event");
}
get currentEvent() {
const currentEvent = this.#events.get(
this.mainmanager.getCurrentFocus("event")
);
return currentEvent;
}
}