galvanic corrosion rewrite

commit this before something goes horribly wrong
This commit is contained in:
2025-08-12 21:04:52 -04:00
parent 941c8400c0
commit f19552929e
40 changed files with 28149 additions and 73212 deletions

View File

@@ -1,16 +1,132 @@
import Logging from "@proxnet/undead-logging";
import { ServerContentBase } from "../ContentBase.ts";
import type Profile from "../profiles/profile.ts";
import { DeviceClass } from "../platforms/types.ts";
import Profile from "../profiles/profile.ts";
import { type ServerBase } from "../server.ts";
import { RoomInstance } from "../instances/base.ts";
class Presence {
export enum VRMovementMode {
TELEPORT,
WALK
}
export enum PlayerStatusVisibility {
Public,
FriendsOnly,
FavoriteFriendsOnly,
Offline
}
export interface PresenceExport {
playerId: number,
statusVisibility: PlayerStatusVisibility,
deviceClass: DeviceClass,
vrMovementMode?: VRMovementMode,
roomInstance: RoomInstance | null
}
export class Presence {
#server: ServerBase;
#profile: Profile;
#statusVisibility: PlayerStatusVisibility = PlayerStatusVisibility.Offline;
#deviceClass: DeviceClass = DeviceClass.Unknown;
#roomInstance: RoomInstance | null = null;
#vrMovementMove: VRMovementMode | undefined;
#lastExported: Date = new Date();
constructor(profile: Profile, server: ServerBase) {
this.#profile = profile;
this.#server = server;
}
/** Refer to `Profile.Matchmaking.updateLastSeen` */
updateLastSeen() {
this.#profile.Matchmaking.updateLastSeen();
}
async update() {
this.#deviceClass = (await this.#profile.Matchmaking.getLastDeviceClass()) || DeviceClass.Unknown;
const isOnline = (Date.now() - (await this.#profile.Matchmaking.getLastSeen()).getTime()) < 90_000;
this.#statusVisibility =
isOnline ?
this.#statusVisibility :
PlayerStatusVisibility.Offline;
this.#roomInstance = this.#profile.getInstance();
this.#server.emit('presence.update', { profile: this.#profile, presence: this });
}
setStatusVisibility(sv: PlayerStatusVisibility) {
this.#statusVisibility = sv;
this.updateLastSeen();
this.update();
}
setVRMovementMode(mm: VRMovementMode) {
this.#vrMovementMove = mm;
this.updateLastSeen();
this.update();
}
getLastExported() {
return this.#lastExported;
}
logout() {
this.updateLastSeen();
this.#statusVisibility = PlayerStatusVisibility.Offline;
}
export() {
this.#lastExported = new Date();
const e: PresenceExport = {
playerId: this.#profile.getId(),
statusVisibility: this.#statusVisibility,
deviceClass: this.#deviceClass,
vrMovementMode: this.#vrMovementMove,
roomInstance: this.#roomInstance
}
return e;
}
}
export class PresenceBase extends ServerContentBase {
#log = new Logging("Presence");
#presenceMap: Map<Profile, Presence> = new Map();
getPresence() {
#intervalId?: number;
#deleteDeadPresences() {
for (const pres of this.#presenceMap.values()) {
if (Date.now() - pres.getLastExported().getTime() > 300_000) pres
}
}
getPresence(profile: Profile) {
let pres = this.#presenceMap.get(profile);
if (!pres) {
pres = new Presence(profile, this.server);
this.#presenceMap.set(profile, pres);
}
return pres;
}
override start() {
this.#intervalId = setInterval(() => {
this.#log.i('Clearing dead presences');
this.#deleteDeadPresences();
}, 300_000);
}
override destroy() {
clearInterval(this.#intervalId ?? undefined);
}
}