import { writable } from "svelte/store"; import type { PlayerInterface } from "../Phaser/Game/PlayerInterface"; import type { RoomConnection } from "../Connexion/RoomConnection"; /** * A store that contains the list of players currently known. */ function createPlayersStore() { let players = new Map(); const { subscribe, set, update } = writable(players); return { subscribe, connectToRoomConnection: (roomConnection: RoomConnection) => { players = new Map(); set(players); roomConnection.onUserJoins((message) => { update((users) => { users.set(message.userId, { userId: message.userId, name: message.name, characterLayers: message.characterLayers, visitCardUrl: message.visitCardUrl, companion: message.companion, userUuid: message.userUuid, }); return users; }); }); roomConnection.onUserLeft((userId) => { update((users) => { users.delete(userId); return users; }); }); }, getPlayerById(userId: number): PlayerInterface | undefined { return players.get(userId); }, }; } export const playersStore = createPlayersStore();