EventMapper/assets/scripts/KaplayMap/events.js

83 lines
2.1 KiB
JavaScript
Raw Normal View History

2024-06-02 05:39:23 +00:00
export class EventManager {
2024-06-05 07:30:05 +00:00
mainmanager;
#events = new Map([]);
constructor() {}
2024-06-02 05:39:23 +00:00
2024-06-09 12:23:14 +00:00
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();
2024-06-10 17:37:53 +00:00
allEvents.sort(
(e1, e2) =>
new Date(e2.when.start).getTime() - new Date(e1.when.start).getTime()
);
2024-06-09 12:23:14 +00:00
return allEvents;
}
2024-06-05 07:30:05 +00:00
async getEvents() {
const currentLocalization = this.mainmanager.getCurrentLangCode();
const currentFloor = this.mainmanager.getCurrentFocus("floor");
2024-06-02 05:39:23 +00:00
2024-06-05 07:30:05 +00:00
let allEventsReqSend = fetch(
`/data/${currentLocalization}/events/${currentFloor}`
);
2024-06-02 05:39:23 +00:00
2024-06-04 22:54:40 +00:00
let allEventsReq = await allEventsReqSend;
let allEvents;
if (allEventsReq.ok) allEvents = await allEventsReq.json();
2024-06-02 05:39:23 +00:00
2024-06-10 17:37:53 +00:00
// Sort by date
allEvents.sort(
(e1, e2) =>
new Date(e2.when.start).getTime() - new Date(e1.when.start).getTime()
);
2024-06-05 07:30:05 +00:00
this.#events.clear();
2024-06-02 05:39:23 +00:00
2024-06-05 07:30:05 +00:00
allEvents.forEach((event) => this.#events.set(event.id, event));
2024-06-04 22:54:40 +00:00
}
setCurrentEvent(id) {
2024-06-05 07:30:05 +00:00
if (id == null) this.mainmanager.setCurrentFocus("event", id);
2024-06-04 22:54:40 +00:00
else {
2024-06-05 07:30:05 +00:00
const event = this.#events.get(id);
if (event != null) this.mainmanager.setCurrentFocus("event", id);
else throw "Event does not exist";
2024-06-04 22:54:40 +00:00
}
}
2024-06-02 05:39:23 +00:00
2024-06-05 07:30:05 +00:00
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");
}
2024-06-04 22:54:40 +00:00
get currentEvent() {
2024-06-05 07:30:05 +00:00
const currentEvent = this.#events.get(
this.mainmanager.getCurrentFocus("event")
);
return currentEvent;
2024-06-02 05:39:23 +00:00
}
2024-06-05 07:30:05 +00:00
}