EventMapper/assets/scripts/KaplayMap/floors.js

95 lines
2.2 KiB
JavaScript
Raw Permalink Normal View History

2024-06-02 05:39:23 +00:00
export class FloorManager {
2024-06-05 07:30:05 +00:00
mainmanager;
#floors = new Map([]);
#rooms = new Map([]);
constructor() {}
2024-06-02 05:39:23 +00:00
2024-06-05 07:30:05 +00:00
async getFloors() {
const currentLocalization = this.mainmanager.getCurrentLangCode();
2024-06-02 05:39:23 +00:00
2024-06-05 07:30:05 +00:00
let allFloorsReqSend = fetch(`/data/${currentLocalization}/floors`);
2024-06-02 05:39:23 +00:00
let allFloorsReq = await allFloorsReqSend;
let allFloors;
if (allFloorsReq.ok) allFloors = await allFloorsReq.json();
2024-06-05 07:30:05 +00:00
allFloors.forEach((floor) => this.#floors.set(floor.id, floor));
2024-06-02 05:39:23 +00:00
}
2024-06-05 07:30:05 +00:00
async getRooms() {
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
const curRoom = this.#rooms.get(currentFloor);
2024-06-02 05:39:23 +00:00
2024-06-05 07:30:05 +00:00
if (curRoom != null) return curRoom;
2024-06-02 05:39:23 +00:00
2024-06-05 07:30:05 +00:00
let allRoomsReqSend = fetch(
`/data/${currentLocalization}/rooms/${currentFloor}`
);
2024-06-02 05:39:23 +00:00
let allRoomsReq = await allRoomsReqSend;
let allRooms;
if (allRoomsReq.ok) allRooms = await allRoomsReq.json();
2024-06-03 03:54:02 +00:00
const roomMap = new Map([]);
allRooms.forEach((room) => roomMap.set(room.id, room));
2024-06-05 07:30:05 +00:00
this.#rooms.set(currentFloor, roomMap);
2024-06-02 05:39:23 +00:00
}
2024-06-04 22:54:40 +00:00
setCurrentFloor(id) {
2024-06-05 07:30:05 +00:00
const floor = this.#floors.get(id);
if (floor != null) this.mainmanager.setCurrentFocus("floor", id);
else throw "Floor does not exist";
}
get allFloors() {
return this.#floors;
}
get currentFloorId() {
return this.mainmanager.getCurrentFocus("floor");
2024-06-04 22:54:40 +00:00
}
2024-06-02 05:39:23 +00:00
2024-06-04 22:54:40 +00:00
get currentFloor() {
2024-06-05 07:30:05 +00:00
const currentFloor = this.#floors.get(
this.mainmanager.getCurrentFocus("floor")
);
return currentFloor;
}
get currentFloorRooms() {
const currentFloorRooms = this.#rooms.get(
this.mainmanager.getCurrentFocus("floor")
);
return currentFloorRooms;
2024-06-04 22:54:40 +00:00
}
2024-06-03 03:54:02 +00:00
2024-06-04 22:54:40 +00:00
setCurrentRoom(id) {
2024-06-05 07:30:05 +00:00
if (id == null) this.mainmanager.setCurrentFocus("room", id);
2024-06-04 22:54:40 +00:00
else {
2024-06-05 07:30:05 +00:00
const room = this.currentFloorRooms.get(id);
if (room != null) this.mainmanager.setCurrentFocus("room", id);
else throw "Room 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 allRooms() {
return this.#rooms;
}
get currentRoomId() {
return this.mainmanager.getCurrentFocus("room");
}
2024-06-04 22:54:40 +00:00
get currentRoom() {
2024-06-05 07:30:05 +00:00
const currentFloor = this.currentFloorRooms.get(
this.mainmanager.getCurrentFocus("room")
);
return currentFloor;
2024-06-02 05:39:23 +00:00
}
}