diff --git a/back/src/Model/User.ts b/back/src/Model/User.ts index 370781e5..52a96b61 100644 --- a/back/src/Model/User.ts +++ b/back/src/Model/User.ts @@ -4,7 +4,7 @@ import {Zone} from "_Model/Zone"; import {Movable} from "_Model/Movable"; import {PositionNotifier} from "_Model/PositionNotifier"; import {ServerDuplexStream} from "grpc"; -import {BatchMessage, Companion, PusherToBackMessage, ServerToClientMessage, SubMessage} from "../Messages/generated/messages_pb"; +import {BatchMessage, CompanionMessage, PusherToBackMessage, ServerToClientMessage, SubMessage} from "../Messages/generated/messages_pb"; import {CharacterLayer} from "_Model/Websocket/CharacterLayer"; export type UserSocket = ServerDuplexStream; @@ -24,7 +24,7 @@ export class User implements Movable { public readonly tags: string[], public readonly name: string, public readonly characterLayers: CharacterLayer[], - public readonly companion?: Companion + public readonly companion?: CompanionMessage ) { this.listenedZones = new Set(); diff --git a/front/src/Connexion/LocalUserStore.ts b/front/src/Connexion/LocalUserStore.ts index a766f5dc..74e45049 100644 --- a/front/src/Connexion/LocalUserStore.ts +++ b/front/src/Connexion/LocalUserStore.ts @@ -52,7 +52,13 @@ class LocalUserStore { return localStorage.setItem(companionKey, JSON.stringify(companion)); } getCompanion(): string|null { - return JSON.parse(localStorage.getItem(companionKey) || "null"); + const companion = JSON.parse(localStorage.getItem(companionKey) || "null"); + + if (typeof companion !== "string" || companion === "") { + return null; + } + + return companion; } wasCompanionSet(): boolean { return localStorage.getItem(companionKey) ? true : false; diff --git a/front/src/Phaser/Companion/Companion.ts b/front/src/Phaser/Companion/Companion.ts index e578dd3d..48a3fa4b 100644 --- a/front/src/Phaser/Companion/Companion.ts +++ b/front/src/Phaser/Companion/Companion.ts @@ -1,6 +1,6 @@ import Sprite = Phaser.GameObjects.Sprite; import Container = Phaser.GameObjects.Container; -import { lazyLoadResource } from "./CompanionTexturesLoadingManager"; +import { lazyLoadCompanionResource } from "./CompanionTexturesLoadingManager"; import { PlayerAnimationDirections, PlayerAnimationTypes } from "../Player/Animation"; export interface CompanionStatus { @@ -37,7 +37,7 @@ export class Companion extends Container { this.companionName = name; - lazyLoadResource(this.scene.load, this.companionName) + lazyLoadCompanionResource(this.scene.load, this.companionName) .then(resource => { this.addResource(resource); this.invisible = false; diff --git a/front/src/Phaser/Companion/CompanionTexturesLoadingManager.ts b/front/src/Phaser/Companion/CompanionTexturesLoadingManager.ts index 1c74b64f..354c5740 100644 --- a/front/src/Phaser/Companion/CompanionTexturesLoadingManager.ts +++ b/front/src/Phaser/Companion/CompanionTexturesLoadingManager.ts @@ -1,23 +1,15 @@ import LoaderPlugin = Phaser.Loader.LoaderPlugin; import { COMPANION_RESOURCES, CompanionResourceDescriptionInterface } from "./CompanionTextures"; -export const getAllResources = (): CompanionResourceDescriptionInterface[] => { +export const getAllCompanionResources = (loader: LoaderPlugin): CompanionResourceDescriptionInterface[] => { + COMPANION_RESOURCES.forEach((resource: CompanionResourceDescriptionInterface) => { + lazyLoadCompanionResource(loader, resource.name); + }); + return COMPANION_RESOURCES; } -export const lazyLoadAllResources = (loader: LoaderPlugin): Promise => { - const promises: Promise[] = []; - - COMPANION_RESOURCES.forEach((resource: CompanionResourceDescriptionInterface) => { - promises.push(lazyLoadResource(loader, resource.name)); - }); - - return Promise.all(promises).then(() => { - return COMPANION_RESOURCES; - }); -} - -export const lazyLoadResource = (loader: LoaderPlugin, name: string): Promise => { +export const lazyLoadCompanionResource = (loader: LoaderPlugin, name: string): Promise => { return new Promise((resolve, reject) => { const resource = COMPANION_RESOURCES.find(item => item.name === name); diff --git a/front/src/Phaser/Login/SelectCompanionScene.ts b/front/src/Phaser/Login/SelectCompanionScene.ts index 5a6cc160..3d20d35a 100644 --- a/front/src/Phaser/Login/SelectCompanionScene.ts +++ b/front/src/Phaser/Login/SelectCompanionScene.ts @@ -7,7 +7,7 @@ import { TextField } from "../Components/TextField"; import { EnableCameraSceneName } from "./EnableCameraScene"; import { localUserStore } from "../../Connexion/LocalUserStore"; import { CompanionResourceDescriptionInterface } from "../Companion/CompanionTextures"; -import { getAllResources, lazyLoadAllResources } from "../Companion/CompanionTexturesLoadingManager"; +import { getAllCompanionResources } from "../Companion/CompanionTexturesLoadingManager"; export const SelectCompanionSceneName = "SelectCompanionScene"; @@ -38,11 +38,9 @@ export class SelectCompanionScene extends ResizableScene { } preload() { - lazyLoadAllResources(this.load).then(() => { - console.log("Loaded all companion textures."); - }); + addLoader(this); - getAllResources().forEach(model => { + getAllCompanionResources(this.load).forEach(model => { this.companionModels.push(model); }); diff --git a/messages/protos/messages.proto b/messages/protos/messages.proto index 372f00b7..b3d4e755 100644 --- a/messages/protos/messages.proto +++ b/messages/protos/messages.proto @@ -36,7 +36,7 @@ message CharacterLayerMessage { string name = 2; } -message Companion { +message CompanionMessage { string name = 1; } @@ -145,7 +145,7 @@ message UserJoinedMessage { string name = 2; repeated CharacterLayerMessage characterLayers = 3; PositionMessage position = 4; - Companion companion = 5; + CompanionMessage companion = 5; } message UserLeftMessage { @@ -256,7 +256,7 @@ message JoinRoomMessage { string roomId = 5; repeated string tag = 6; string IPAddress = 7; - Companion companion = 8; + CompanionMessage companion = 8; } message UserJoinedZoneMessage { @@ -265,7 +265,7 @@ message UserJoinedZoneMessage { repeated CharacterLayerMessage characterLayers = 3; PositionMessage position = 4; Zone fromZone = 5; - Companion companion = 6; + CompanionMessage companion = 6; } message UserLeftZoneMessage { diff --git a/pusher/src/Controller/IoSocketController.ts b/pusher/src/Controller/IoSocketController.ts index 2079548f..87051bbc 100644 --- a/pusher/src/Controller/IoSocketController.ts +++ b/pusher/src/Controller/IoSocketController.ts @@ -12,7 +12,7 @@ import { WebRtcSignalToServerMessage, PlayGlobalMessage, ReportPlayerMessage, - QueryJitsiJwtMessage, SendUserMessage, ServerToClientMessage, Companion + QueryJitsiJwtMessage, SendUserMessage, ServerToClientMessage, CompanionMessage } from "../Messages/generated/messages_pb"; import {UserMovesMessage} from "../Messages/generated/messages_pb"; import {TemplatedApp} from "uWebSockets.js" @@ -139,10 +139,10 @@ export class IoSocketController { const right = Number(query.right); const name = query.name; - let companion: Companion|undefined = undefined; + let companion: CompanionMessage|undefined = undefined; if (typeof query.companion === 'string') { - companion = new Companion(); + companion = new CompanionMessage(); companion.setName(query.companion); } diff --git a/pusher/src/Model/Websocket/ExSocketInterface.ts b/pusher/src/Model/Websocket/ExSocketInterface.ts index 135c6f10..5b9a4f7e 100644 --- a/pusher/src/Model/Websocket/ExSocketInterface.ts +++ b/pusher/src/Model/Websocket/ExSocketInterface.ts @@ -3,7 +3,7 @@ import {Identificable} from "./Identificable"; import {ViewportInterface} from "_Model/Websocket/ViewportMessage"; import { BatchMessage, - Companion, + CompanionMessage, PusherToBackMessage, ServerToClientMessage, SubMessage @@ -30,7 +30,7 @@ export interface ExSocketInterface extends WebSocket, Identificable { characterLayers: CharacterLayer[]; position: PointInterface; viewport: ViewportInterface; - companion?: Companion; + companion?: CompanionMessage; /** * Pushes an event that will be sent in the next batch of events */ diff --git a/pusher/src/Model/Zone.ts b/pusher/src/Model/Zone.ts index a54481a5..3f39a5ed 100644 --- a/pusher/src/Model/Zone.ts +++ b/pusher/src/Model/Zone.ts @@ -6,7 +6,7 @@ import { PointMessage, PositionMessage, UserJoinedMessage, UserJoinedZoneMessage, UserLeftZoneMessage, UserMovedMessage, ZoneMessage, - Companion + CompanionMessage } from "../Messages/generated/messages_pb"; import * as messages_pb from "../Messages/generated/messages_pb"; import {ClientReadableStream} from "grpc"; @@ -31,7 +31,7 @@ export type MovesCallback = (thing: Movable, position: PositionInterface, listen export type LeavesCallback = (thing: Movable, listener: User) => void;*/ export class UserDescriptor { - private constructor(public readonly userId: number, private name: string, private characterLayers: CharacterLayerMessage[], private position: PositionMessage, private companion?: Companion) { + private constructor(public readonly userId: number, private name: string, private characterLayers: CharacterLayerMessage[], private position: PositionMessage, private companion?: CompanionMessage) { if (!Number.isInteger(this.userId)) { throw new Error('UserDescriptor.userId is not an integer: '+this.userId); }