2021-07-06 17:13:08 +02:00
|
|
|
import { writable } from "svelte/store";
|
2021-07-07 11:24:51 +02:00
|
|
|
import type { PlayerInterface } from "../Phaser/Game/PlayerInterface";
|
|
|
|
import type { RoomConnection } from "../Connexion/RoomConnection";
|
2021-07-06 17:13:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A store that contains the list of players currently known.
|
|
|
|
*/
|
|
|
|
function createPlayersStore() {
|
|
|
|
let players = new Map<number, PlayerInterface>();
|
|
|
|
|
|
|
|
const { subscribe, set, update } = writable(players);
|
|
|
|
|
|
|
|
return {
|
|
|
|
subscribe,
|
|
|
|
connectToRoomConnection: (roomConnection: RoomConnection) => {
|
|
|
|
players = new Map<number, PlayerInterface>();
|
|
|
|
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,
|
2021-07-07 11:24:51 +02:00
|
|
|
userUuid: message.userUuid,
|
2021-07-06 17:13:08 +02:00
|
|
|
});
|
|
|
|
return users;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
roomConnection.onUserLeft((userId) => {
|
|
|
|
update((users) => {
|
|
|
|
users.delete(userId);
|
|
|
|
return users;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2021-07-07 11:24:51 +02:00
|
|
|
getPlayerById(userId: number): PlayerInterface | undefined {
|
2021-07-06 17:13:08 +02:00
|
|
|
return players.get(userId);
|
2021-07-07 11:24:51 +02:00
|
|
|
},
|
2021-07-06 17:13:08 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export const playersStore = createPlayersStore();
|