Files
galvanic-corrosion-rewrite/src/server/instances/Instance.ts
2025-09-06 22:22:36 -04:00

81 lines
2.2 KiB
TypeScript

import { CloudRegionCode } from "../../util/photon.ts";
import type Profile from "../profiles/profile.ts";
import { RoomInstance, RoomLocation } from "./base.ts";
export interface InstanceCreationOptions {
roomId: number,
subRoomId: number,
name: string,
maxCapacity: number,
private?: boolean
}
export class Instance {
#createdAt = new Date();
#players: Set<Profile> = new Set();
#instanceId: number;
#roomId: number;
#subRoomId: number;
#location: RoomLocation;
#name: string;
#maxCapacity: number;
#isFull: boolean = false;
#isPrivate: boolean;
#isInProgress: boolean = false;
#photonRegionId: string = CloudRegionCode.us;
#photonRoomId: string;
#dataBlob?: string;
#eventId?: number
constructor(id: number, location: RoomLocation, options: InstanceCreationOptions) {
this.#instanceId = id;
this.#location = location;
this.#roomId = options.roomId;
this.#subRoomId = options.subRoomId;
this.#isPrivate = typeof options.private == 'boolean' ? options.private : false;
this.#name = options.name;
this.#maxCapacity = options.maxCapacity;
this.#photonRoomId = `GCR-${this.#instanceId}`;
}
getPlayers() {
return this.#players.values().toArray();
}
hasPlayer(profile: Profile) {
return this.#players.values().some(prof => prof.getId() === profile.getId());
}
removePlayer(profile: Profile) {
this.#players.delete(profile);
}
addPlayer(profile: Profile) {
this.#players.add(profile);
}
getCreatedAt() {
return this.#createdAt;
}
export() {
const inst: RoomInstance = {
roomInstanceId: this.#instanceId,
roomId: this.#roomId,
subRoomId: this.#subRoomId,
location: this.#location,
name: this.#name,
maxCapacity: this.#maxCapacity,
isFull: this.#isFull,
isPrivate: this.#isPrivate,
isInProgress: this.#isInProgress,
photonRegionId: this.#photonRegionId,
photonRoomId: this.#photonRoomId,
dataBlob: this.#dataBlob,
eventId: this.#eventId
};
return inst;
}
}