Sharing outline color changes in real time

This commit is contained in:
David Négrier 2021-12-22 18:30:23 +01:00
parent 90f7287860
commit 482ba9690a
15 changed files with 268 additions and 56 deletions

View File

@ -2,7 +2,13 @@ import { PointInterface } from "./Websocket/PointInterface";
import { Group } from "./Group"; import { Group } from "./Group";
import { User, UserSocket } from "./User"; import { User, UserSocket } from "./User";
import { PositionInterface } from "_Model/PositionInterface"; import { PositionInterface } from "_Model/PositionInterface";
import { EmoteCallback, EntersCallback, LeavesCallback, MovesCallback } from "_Model/Zone"; import {
EmoteCallback,
EntersCallback,
LeavesCallback,
MovesCallback,
PlayerDetailsUpdatedCallback,
} from "_Model/Zone";
import { PositionNotifier } from "./PositionNotifier"; import { PositionNotifier } from "./PositionNotifier";
import { Movable } from "_Model/Movable"; import { Movable } from "_Model/Movable";
import { import {
@ -11,6 +17,7 @@ import {
EmoteEventMessage, EmoteEventMessage,
ErrorMessage, ErrorMessage,
JoinRoomMessage, JoinRoomMessage,
SetPlayerDetailsMessage,
SubToPusherRoomMessage, SubToPusherRoomMessage,
VariableMessage, VariableMessage,
VariableWithTagMessage, VariableWithTagMessage,
@ -56,10 +63,19 @@ export class GameRoom {
onEnters: EntersCallback, onEnters: EntersCallback,
onMoves: MovesCallback, onMoves: MovesCallback,
onLeaves: LeavesCallback, onLeaves: LeavesCallback,
onEmote: EmoteCallback onEmote: EmoteCallback,
onPlayerDetailsUpdated: PlayerDetailsUpdatedCallback
) { ) {
// A zone is 10 sprites wide. // A zone is 10 sprites wide.
this.positionNotifier = new PositionNotifier(320, 320, onEnters, onMoves, onLeaves, onEmote); this.positionNotifier = new PositionNotifier(
320,
320,
onEnters,
onMoves,
onLeaves,
onEmote,
onPlayerDetailsUpdated
);
} }
public static async create( public static async create(
@ -71,7 +87,8 @@ export class GameRoom {
onEnters: EntersCallback, onEnters: EntersCallback,
onMoves: MovesCallback, onMoves: MovesCallback,
onLeaves: LeavesCallback, onLeaves: LeavesCallback,
onEmote: EmoteCallback onEmote: EmoteCallback,
onPlayerDetailsUpdated: PlayerDetailsUpdatedCallback
): Promise<GameRoom> { ): Promise<GameRoom> {
const mapDetails = await GameRoom.getMapDetails(roomUrl); const mapDetails = await GameRoom.getMapDetails(roomUrl);
@ -85,7 +102,8 @@ export class GameRoom {
onEnters, onEnters,
onMoves, onMoves,
onLeaves, onLeaves,
onEmote onEmote,
onPlayerDetailsUpdated
); );
return gameRoom; return gameRoom;
@ -180,6 +198,14 @@ export class GameRoom {
this.updateUserGroup(user); this.updateUserGroup(user);
} }
updatePlayerDetails(user: User, playerDetailsMessage: SetPlayerDetailsMessage) {
if (playerDetailsMessage.getRemoveoutlinecolor()) {
user.outlineColor = undefined;
} else {
user.outlineColor = playerDetailsMessage.getOutlinecolor();
}
}
private updateUserGroup(user: User): void { private updateUserGroup(user: User): void {
user.group?.updatePosition(); user.group?.updatePosition();
user.group?.searchForNearbyUsers(); user.group?.searchForNearbyUsers();

View File

@ -8,12 +8,19 @@
* The PositionNotifier is important for performance. It allows us to send the position of players only to a restricted * The PositionNotifier is important for performance. It allows us to send the position of players only to a restricted
* number of players around the current player. * number of players around the current player.
*/ */
import { EmoteCallback, EntersCallback, LeavesCallback, MovesCallback, Zone } from "./Zone"; import {
EmoteCallback,
EntersCallback,
LeavesCallback,
MovesCallback,
PlayerDetailsUpdatedCallback,
Zone,
} from "./Zone";
import { Movable } from "_Model/Movable"; import { Movable } from "_Model/Movable";
import { PositionInterface } from "_Model/PositionInterface"; import { PositionInterface } from "_Model/PositionInterface";
import { ZoneSocket } from "../RoomManager"; import { ZoneSocket } from "../RoomManager";
import { User } from "../Model/User"; import { User } from "../Model/User";
import { EmoteEventMessage } from "../Messages/generated/messages_pb"; import { EmoteEventMessage, SetPlayerDetailsMessage } from "../Messages/generated/messages_pb";
interface ZoneDescriptor { interface ZoneDescriptor {
i: number; i: number;
@ -42,7 +49,8 @@ export class PositionNotifier {
private onUserEnters: EntersCallback, private onUserEnters: EntersCallback,
private onUserMoves: MovesCallback, private onUserMoves: MovesCallback,
private onUserLeaves: LeavesCallback, private onUserLeaves: LeavesCallback,
private onEmote: EmoteCallback private onEmote: EmoteCallback,
private onPlayerDetailsUpdated: PlayerDetailsUpdatedCallback
) {} ) {}
private getZoneDescriptorFromCoordinates(x: number, y: number): ZoneDescriptor { private getZoneDescriptorFromCoordinates(x: number, y: number): ZoneDescriptor {
@ -98,7 +106,15 @@ export class PositionNotifier {
let zone = this.zones[j][i]; let zone = this.zones[j][i];
if (zone === undefined) { if (zone === undefined) {
zone = new Zone(this.onUserEnters, this.onUserMoves, this.onUserLeaves, this.onEmote, i, j); zone = new Zone(
this.onUserEnters,
this.onUserMoves,
this.onUserLeaves,
this.onEmote,
this.onPlayerDetailsUpdated,
i,
j
);
this.zones[j][i] = zone; this.zones[j][i] = zone;
} }
return zone; return zone;
@ -132,4 +148,11 @@ export class PositionNotifier {
} }
} }
} }
public updatePlayerDetails(user: User, playerDetails: SetPlayerDetailsMessage) {
const position = user.getPosition();
const zoneDesc = this.getZoneDescriptorFromCoordinates(position.x, position.y);
const zone = this.getZone(zoneDesc.i, zoneDesc.j);
zone.updatePlayerDetails(user, playerDetails);
}
} }

View File

@ -9,6 +9,7 @@ import {
CompanionMessage, CompanionMessage,
PusherToBackMessage, PusherToBackMessage,
ServerToClientMessage, ServerToClientMessage,
SetPlayerDetailsMessage,
SubMessage, SubMessage,
} from "../Messages/generated/messages_pb"; } from "../Messages/generated/messages_pb";
import { CharacterLayer } from "_Model/Websocket/CharacterLayer"; import { CharacterLayer } from "_Model/Websocket/CharacterLayer";
@ -31,7 +32,8 @@ export class User implements Movable {
public readonly visitCardUrl: string | null, public readonly visitCardUrl: string | null,
public readonly name: string, public readonly name: string,
public readonly characterLayers: CharacterLayer[], public readonly characterLayers: CharacterLayer[],
public readonly companion?: CompanionMessage public readonly companion?: CompanionMessage,
private _outlineColor?: number | undefined
) { ) {
this.listenedZones = new Set<Zone>(); this.listenedZones = new Set<Zone>();
@ -69,4 +71,17 @@ export class User implements Movable {
}, 100); }, 100);
} }
} }
public set outlineColor(value: number | undefined) {
this._outlineColor = value;
const playerDetails = new SetPlayerDetailsMessage();
if (value === undefined) {
playerDetails.setRemoveoutlinecolor(true);
} else {
playerDetails.setOutlinecolor(value);
}
this.positionNotifier.updatePlayerDetails(this, playerDetails);
}
} }

View File

@ -3,12 +3,20 @@ import { PositionInterface } from "_Model/PositionInterface";
import { Movable } from "./Movable"; import { Movable } from "./Movable";
import { Group } from "./Group"; import { Group } from "./Group";
import { ZoneSocket } from "../RoomManager"; import { ZoneSocket } from "../RoomManager";
import { EmoteEventMessage } from "../Messages/generated/messages_pb"; import {
EmoteEventMessage,
SetPlayerDetailsMessage,
PlayerDetailsUpdatedMessage,
} from "../Messages/generated/messages_pb";
export type EntersCallback = (thing: Movable, fromZone: Zone | null, listener: ZoneSocket) => void; export type EntersCallback = (thing: Movable, fromZone: Zone | null, listener: ZoneSocket) => void;
export type MovesCallback = (thing: Movable, position: PositionInterface, listener: ZoneSocket) => void; export type MovesCallback = (thing: Movable, position: PositionInterface, listener: ZoneSocket) => void;
export type LeavesCallback = (thing: Movable, newZone: Zone | null, listener: ZoneSocket) => void; export type LeavesCallback = (thing: Movable, newZone: Zone | null, listener: ZoneSocket) => void;
export type EmoteCallback = (emoteEventMessage: EmoteEventMessage, listener: ZoneSocket) => void; export type EmoteCallback = (emoteEventMessage: EmoteEventMessage, listener: ZoneSocket) => void;
export type PlayerDetailsUpdatedCallback = (
playerDetailsUpdatedMessage: PlayerDetailsUpdatedMessage,
listener: ZoneSocket
) => void;
export class Zone { export class Zone {
private things: Set<Movable> = new Set<Movable>(); private things: Set<Movable> = new Set<Movable>();
@ -19,6 +27,7 @@ export class Zone {
private onMoves: MovesCallback, private onMoves: MovesCallback,
private onLeaves: LeavesCallback, private onLeaves: LeavesCallback,
private onEmote: EmoteCallback, private onEmote: EmoteCallback,
private onPlayerDetailsUpdated: PlayerDetailsUpdatedCallback,
public readonly x: number, public readonly x: number,
public readonly y: number public readonly y: number
) {} ) {}
@ -106,4 +115,14 @@ export class Zone {
this.onEmote(emoteEventMessage, listener); this.onEmote(emoteEventMessage, listener);
} }
} }
public updatePlayerDetails(user: User, playerDetails: SetPlayerDetailsMessage) {
const playerDetailsUpdatedMessage = new PlayerDetailsUpdatedMessage();
playerDetailsUpdatedMessage.setUserid(user.id);
playerDetailsUpdatedMessage.setDetails(playerDetails);
for (const listener of this.listeners) {
this.onPlayerDetailsUpdated(playerDetailsUpdatedMessage, listener);
}
}
} }

View File

@ -5,6 +5,7 @@ import {
AdminPusherToBackMessage, AdminPusherToBackMessage,
AdminRoomMessage, AdminRoomMessage,
BanMessage, BanMessage,
BanUserMessage,
BatchToPusherMessage, BatchToPusherMessage,
BatchToPusherRoomMessage, BatchToPusherRoomMessage,
EmotePromptMessage, EmotePromptMessage,
@ -16,7 +17,9 @@ import {
QueryJitsiJwtMessage, QueryJitsiJwtMessage,
RefreshRoomPromptMessage, RefreshRoomPromptMessage,
RoomMessage, RoomMessage,
SendUserMessage,
ServerToAdminClientMessage, ServerToAdminClientMessage,
SetPlayerDetailsMessage,
SilentMessage, SilentMessage,
UserMovesMessage, UserMovesMessage,
VariableMessage, VariableMessage,
@ -118,14 +121,17 @@ const roomManager: IRoomManagerServer = {
); );
} else if (message.hasSendusermessage()) { } else if (message.hasSendusermessage()) {
const sendUserMessage = message.getSendusermessage(); const sendUserMessage = message.getSendusermessage();
if (sendUserMessage !== undefined) { socketManager.handleSendUserMessage(user, sendUserMessage as SendUserMessage);
socketManager.handlerSendUserMessage(user, sendUserMessage);
}
} else if (message.hasBanusermessage()) { } else if (message.hasBanusermessage()) {
const banUserMessage = message.getBanusermessage(); const banUserMessage = message.getBanusermessage();
if (banUserMessage !== undefined) { socketManager.handlerBanUserMessage(room, user, banUserMessage as BanUserMessage);
socketManager.handlerBanUserMessage(room, user, banUserMessage); } else if (message.hasSetplayerdetailsmessage()) {
} const setPlayerDetailsMessage = message.getSetplayerdetailsmessage();
socketManager.handleSetPlayerDetails(
room,
user,
setPlayerDetailsMessage as SetPlayerDetailsMessage
);
} else { } else {
throw new Error("Unhandled message type"); throw new Error("Unhandled message type");
} }

View File

@ -33,6 +33,8 @@ import {
VariableMessage, VariableMessage,
BatchToPusherRoomMessage, BatchToPusherRoomMessage,
SubToPusherRoomMessage, SubToPusherRoomMessage,
SetPlayerDetailsMessage,
PlayerDetailsUpdatedMessage,
} from "../Messages/generated/messages_pb"; } from "../Messages/generated/messages_pb";
import { User, UserSocket } from "../Model/User"; import { User, UserSocket } from "../Model/User";
import { ProtobufUtils } from "../Model/Websocket/ProtobufUtils"; import { ProtobufUtils } from "../Model/Websocket/ProtobufUtils";
@ -151,20 +153,9 @@ export class SocketManager {
//room.setViewport(client, client.viewport); //room.setViewport(client, client.viewport);
} }
// Useless now, will be useful again if we allow editing details in game handleSetPlayerDetails(room: GameRoom, user: User, playerDetailsMessage: SetPlayerDetailsMessage) {
/*handleSetPlayerDetails(client: UserSocket, playerDetailsMessage: SetPlayerDetailsMessage) { room.updatePlayerDetails(user, playerDetailsMessage);
const playerDetails = { }
name: playerDetailsMessage.getName(),
characterLayers: playerDetailsMessage.getCharacterlayersList()
};
//console.log(SocketIoEvent.SET_PLAYER_DETAILS, playerDetails);
if (!isSetPlayerDetailsMessage(playerDetails)) {
emitError(client, 'Invalid SET_PLAYER_DETAILS message received: ');
return;
}
client.name = playerDetails.name;
client.characterLayers = SocketManager.mergeCharacterLayersAndCustomTextures(playerDetails.characterLayers, client.textures);
}*/
handleSilentMessage(room: GameRoom, user: User, silentMessage: SilentMessage) { handleSilentMessage(room: GameRoom, user: User, silentMessage: SilentMessage) {
room.setSilent(user, silentMessage.getSilent()); room.setSilent(user, silentMessage.getSilent());
@ -282,7 +273,9 @@ export class SocketManager {
(thing: Movable, newZone: Zone | null, listener: ZoneSocket) => (thing: Movable, newZone: Zone | null, listener: ZoneSocket) =>
this.onClientLeave(thing, newZone, listener), this.onClientLeave(thing, newZone, listener),
(emoteEventMessage: EmoteEventMessage, listener: ZoneSocket) => (emoteEventMessage: EmoteEventMessage, listener: ZoneSocket) =>
this.onEmote(emoteEventMessage, listener) this.onEmote(emoteEventMessage, listener),
(playerDetailsUpdatedMessage: PlayerDetailsUpdatedMessage, listener: ZoneSocket) =>
this.onPlayerDetailsUpdated(playerDetailsUpdatedMessage, listener)
) )
.then((gameRoom) => { .then((gameRoom) => {
gaugeManager.incNbRoomGauge(); gaugeManager.incNbRoomGauge();
@ -378,6 +371,13 @@ export class SocketManager {
emitZoneMessage(subMessage, client); emitZoneMessage(subMessage, client);
} }
private onPlayerDetailsUpdated(playerDetailsUpdatedMessage: PlayerDetailsUpdatedMessage, client: ZoneSocket) {
const subMessage = new SubToPusherMessage();
subMessage.setPlayerdetailsupdatedmessage(playerDetailsUpdatedMessage);
emitZoneMessage(subMessage, client);
}
private emitCreateUpdateGroupEvent(client: ZoneSocket, fromZone: Zone | null, group: Group): void { private emitCreateUpdateGroupEvent(client: ZoneSocket, fromZone: Zone | null, group: Group): void {
const position = group.getPosition(); const position = group.getPosition();
const pointMessage = new PointMessage(); const pointMessage = new PointMessage();
@ -572,7 +572,7 @@ export class SocketManager {
user.socket.write(serverToClientMessage); user.socket.write(serverToClientMessage);
} }
public handlerSendUserMessage(user: User, sendUserMessageToSend: SendUserMessage) { public handleSendUserMessage(user: User, sendUserMessageToSend: SendUserMessage) {
const sendUserMessage = new SendUserMessage(); const sendUserMessage = new SendUserMessage();
sendUserMessage.setMessage(sendUserMessageToSend.getMessage()); sendUserMessage.setMessage(sendUserMessageToSend.getMessage());
sendUserMessage.setType(sendUserMessageToSend.getType()); sendUserMessage.setType(sendUserMessageToSend.getType());

View File

@ -18,6 +18,7 @@ export enum EventMessage {
GROUP_DELETE = "group-delete", GROUP_DELETE = "group-delete",
SET_PLAYER_DETAILS = "set-player-details", // Send the name and character to the server (on connect), receive back the id. SET_PLAYER_DETAILS = "set-player-details", // Send the name and character to the server (on connect), receive back the id.
ITEM_EVENT = "item-event", ITEM_EVENT = "item-event",
USER_DETAILS_UPDATED = "user-details-updated",
CONNECT_ERROR = "connect_error", CONNECT_ERROR = "connect_error",
CONNECTING_ERROR = "connecting_error", CONNECTING_ERROR = "connecting_error",
@ -102,6 +103,12 @@ export interface ItemEventMessageInterface {
parameters: unknown; parameters: unknown;
} }
export interface PlayerDetailsUpdatedMessageInterface {
userId: number;
outlineColor: number;
removeOutlineColor: boolean;
}
export interface RoomJoinedMessageInterface { export interface RoomJoinedMessageInterface {
//users: MessageUserPositionInterface[], //users: MessageUserPositionInterface[],
//groups: GroupCreatedUpdatedMessageInterface[], //groups: GroupCreatedUpdatedMessageInterface[],

View File

@ -34,6 +34,7 @@ import {
BanUserMessage, BanUserMessage,
VariableMessage, VariableMessage,
ErrorMessage, ErrorMessage,
PlayerDetailsUpdatedMessage,
} from "../Messages/generated/messages_pb"; } from "../Messages/generated/messages_pb";
import type { UserSimplePeerInterface } from "../WebRtc/SimplePeer"; import type { UserSimplePeerInterface } from "../WebRtc/SimplePeer";
@ -45,6 +46,7 @@ import {
ItemEventMessageInterface, ItemEventMessageInterface,
MessageUserJoined, MessageUserJoined,
OnConnectInterface, OnConnectInterface,
PlayerDetailsUpdatedMessageInterface,
PlayGlobalMessageInterface, PlayGlobalMessageInterface,
PositionInterface, PositionInterface,
RoomJoinedMessageInterface, RoomJoinedMessageInterface,
@ -172,6 +174,9 @@ export class RoomConnection implements RoomConnection {
} else if (subMessage.hasEmoteeventmessage()) { } else if (subMessage.hasEmoteeventmessage()) {
const emoteMessage = subMessage.getEmoteeventmessage() as EmoteEventMessage; const emoteMessage = subMessage.getEmoteeventmessage() as EmoteEventMessage;
emoteEventStream.fire(emoteMessage.getActoruserid(), emoteMessage.getEmote()); emoteEventStream.fire(emoteMessage.getActoruserid(), emoteMessage.getEmote());
} else if (subMessage.hasPlayerdetailsupdatedmessage()) {
event = EventMessage.USER_DETAILS_UPDATED;
payload = subMessage.getPlayerdetailsupdatedmessage();
} else if (subMessage.hasErrormessage()) { } else if (subMessage.hasErrormessage()) {
const errorMessage = subMessage.getErrormessage() as ErrorMessage; const errorMessage = subMessage.getErrormessage() as ErrorMessage;
console.error("An error occurred server side: " + errorMessage.getMessage()); console.error("An error occurred server side: " + errorMessage.getMessage());
@ -276,7 +281,7 @@ export class RoomConnection implements RoomConnection {
} }
} }
public emitPlayerDetailsMessage(userName: string, characterLayersSelected: BodyResourceDescriptionInterface[]) { /*public emitPlayerDetailsMessage(userName: string, characterLayersSelected: BodyResourceDescriptionInterface[]) {
const message = new SetPlayerDetailsMessage(); const message = new SetPlayerDetailsMessage();
message.setName(userName); message.setName(userName);
message.setCharacterlayersList(characterLayersSelected.map((characterLayer) => characterLayer.name)); message.setCharacterlayersList(characterLayersSelected.map((characterLayer) => characterLayer.name));
@ -284,6 +289,20 @@ export class RoomConnection implements RoomConnection {
const clientToServerMessage = new ClientToServerMessage(); const clientToServerMessage = new ClientToServerMessage();
clientToServerMessage.setSetplayerdetailsmessage(message); clientToServerMessage.setSetplayerdetailsmessage(message);
this.socket.send(clientToServerMessage.serializeBinary().buffer);
}*/
public emitPlayerOutlineColor(color: number | null) {
const message = new SetPlayerDetailsMessage();
if (color === null) {
message.setRemoveoutlinecolor(true);
} else {
message.setOutlinecolor(color);
}
const clientToServerMessage = new ClientToServerMessage();
clientToServerMessage.setSetplayerdetailsmessage(message);
this.socket.send(clientToServerMessage.serializeBinary().buffer); this.socket.send(clientToServerMessage.serializeBinary().buffer);
} }
@ -596,6 +615,20 @@ export class RoomConnection implements RoomConnection {
}); });
} }
onPlayerDetailsUpdated(callback: (message: PlayerDetailsUpdatedMessageInterface) => void): void {
this.onMessage(EventMessage.USER_DETAILS_UPDATED, (message: PlayerDetailsUpdatedMessage) => {
const details = message.getDetails();
if (details === undefined) {
throw new Error("Malformed message. Missing details in PlayerDetailsUpdatedMessage");
}
callback({
userId: message.getUserid(),
outlineColor: details.getOutlinecolor(),
removeOutlineColor: details.getRemoveoutlinecolor(),
});
});
}
public uploadAudio(file: FormData) { public uploadAudio(file: FormData) {
return Axios.post(`${UPLOADER_URL}/upload-audio-message`, file) return Axios.post(`${UPLOADER_URL}/upload-audio-message`, file)
.then((res: { data: {} }) => { .then((res: { data: {} }) => {

View File

@ -414,8 +414,8 @@ export abstract class Character extends Container {
return this._pictureStore; return this._pictureStore;
} }
public setOutlineColor(red: number, green: number, blue: number): void { public setOutlineColor(color: number): void {
this.outlineColorStore.setColor((red << 16) | (green << 8) | blue); this.outlineColorStore.setColor(color);
} }
public removeOutlineColor(): void { public removeOutlineColor(): void {

View File

@ -55,6 +55,7 @@ import type {
MessageUserMovedInterface, MessageUserMovedInterface,
MessageUserPositionInterface, MessageUserPositionInterface,
OnConnectInterface, OnConnectInterface,
PlayerDetailsUpdatedMessageInterface,
PointInterface, PointInterface,
PositionInterface, PositionInterface,
RoomJoinedMessageInterface, RoomJoinedMessageInterface,
@ -88,6 +89,7 @@ import Tileset = Phaser.Tilemaps.Tileset;
import SpriteSheetFile = Phaser.Loader.FileTypes.SpriteSheetFile; import SpriteSheetFile = Phaser.Loader.FileTypes.SpriteSheetFile;
import FILE_LOAD_ERROR = Phaser.Loader.Events.FILE_LOAD_ERROR; import FILE_LOAD_ERROR = Phaser.Loader.Events.FILE_LOAD_ERROR;
import { MapStore } from "../../Stores/Utils/MapStore"; import { MapStore } from "../../Stores/Utils/MapStore";
import { SetPlayerDetailsMessage } from "../../Messages/generated/messages_pb";
export interface GameSceneInitInterface { export interface GameSceneInitInterface {
initPosition: PointInterface | null; initPosition: PointInterface | null;
reconnecting: boolean; reconnecting: boolean;
@ -123,6 +125,11 @@ interface DeleteGroupEventInterface {
groupId: number; groupId: number;
} }
interface PlayerDetailsUpdatedInterface {
type: "PlayerDetailsUpdated";
details: PlayerDetailsUpdatedMessageInterface;
}
export class GameScene extends DirtyScene { export class GameScene extends DirtyScene {
Terrains: Array<Phaser.Tilemaps.Tileset>; Terrains: Array<Phaser.Tilemaps.Tileset>;
CurrentPlayer!: Player; CurrentPlayer!: Player;
@ -135,20 +142,14 @@ export class GameScene extends DirtyScene {
groups: Map<number, Sprite>; groups: Map<number, Sprite>;
circleTexture!: CanvasTexture; circleTexture!: CanvasTexture;
circleRedTexture!: CanvasTexture; circleRedTexture!: CanvasTexture;
pendingEvents: Queue< pendingEvents = new Queue<
| InitUserPositionEventInterface
| AddPlayerEventInterface
| RemovePlayerEventInterface
| UserMovedEventInterface
| GroupCreatedUpdatedEventInterface
| DeleteGroupEventInterface
> = new Queue<
| InitUserPositionEventInterface | InitUserPositionEventInterface
| AddPlayerEventInterface | AddPlayerEventInterface
| RemovePlayerEventInterface | RemovePlayerEventInterface
| UserMovedEventInterface | UserMovedEventInterface
| GroupCreatedUpdatedEventInterface | GroupCreatedUpdatedEventInterface
| DeleteGroupEventInterface | DeleteGroupEventInterface
| PlayerDetailsUpdatedInterface
>(); >();
private initPosition: PositionInterface | null = null; private initPosition: PositionInterface | null = null;
private playersPositionInterpolator = new PlayersPositionInterpolator(); private playersPositionInterpolator = new PlayersPositionInterpolator();
@ -735,6 +736,13 @@ export class GameScene extends DirtyScene {
item.fire(message.event, message.state, message.parameters); item.fire(message.event, message.state, message.parameters);
}); });
this.connection.onPlayerDetailsUpdated((message) => {
this.pendingEvents.enqueue({
type: "PlayerDetailsUpdated",
details: message,
});
});
/** /**
* Triggered when we receive the JWT token to connect to Jitsi * Triggered when we receive the JWT token to connect to Jitsi
*/ */
@ -1306,11 +1314,14 @@ ${escapedMessage}
const red = normalizeColor(message.red); const red = normalizeColor(message.red);
const green = normalizeColor(message.green); const green = normalizeColor(message.green);
const blue = normalizeColor(message.blue); const blue = normalizeColor(message.blue);
this.CurrentPlayer.setOutlineColor(red, green, blue); const color = (red << 16) | (green << 8) | blue;
this.CurrentPlayer.setOutlineColor(color);
this.connection?.emitPlayerOutlineColor(color);
}); });
iframeListener.registerAnswerer("removePlayerOutline", (message) => { iframeListener.registerAnswerer("removePlayerOutline", (message) => {
this.CurrentPlayer.removeOutlineColor(); this.CurrentPlayer.removeOutlineColor();
this.connection?.emitPlayerOutlineColor(null);
}); });
} }
@ -1689,6 +1700,11 @@ ${escapedMessage}
case "DeleteGroupEvent": case "DeleteGroupEvent":
this.doDeleteGroup(event.groupId); this.doDeleteGroup(event.groupId);
break; break;
case "PlayerDetailsUpdated":
this.doUpdatePlayerDetails(event.details);
break;
default:
const tmp: never = event;
} }
} }
// Let's move all users // Let's move all users
@ -1865,6 +1881,23 @@ ${escapedMessage}
this.groups.delete(groupId); this.groups.delete(groupId);
} }
doUpdatePlayerDetails(message: PlayerDetailsUpdatedMessageInterface): void {
const character = this.MapPlayersByKey.get(message.userId);
if (character === undefined) {
console.log(
"Could not set new details to character with ID ",
message.userId,
". Did he/she left before te message was received?"
);
return;
}
if (message.removeOutlineColor) {
character.removeOutlineColor();
} else {
character.setOutlineColor(message.outlineColor);
}
}
/** /**
* Sends to the server an event emitted by one of the ActionableItems. * Sends to the server an event emitted by one of the ActionableItems.
*/ */

View File

@ -365,7 +365,9 @@ function applyCameraConstraints(currentStream: MediaStream | null, constraints:
return; return;
} }
for (const track of currentStream.getVideoTracks()) { for (const track of currentStream.getVideoTracks()) {
toggleConstraints(track, constraints); toggleConstraints(track, constraints).catch((e) =>
console.error("Error while setting new camera constraints:", e)
);
} }
} }
@ -380,19 +382,21 @@ function applyMicrophoneConstraints(
return; return;
} }
for (const track of currentStream.getAudioTracks()) { for (const track of currentStream.getAudioTracks()) {
toggleConstraints(track, constraints); toggleConstraints(track, constraints).catch((e) =>
console.error("Error while setting new audio constraints:", e)
);
} }
} }
function toggleConstraints(track: MediaStreamTrack, constraints: MediaTrackConstraints | boolean): void { async function toggleConstraints(track: MediaStreamTrack, constraints: MediaTrackConstraints | boolean): Promise<void> {
if (implementCorrectTrackBehavior) { if (implementCorrectTrackBehavior) {
track.enabled = constraints !== false; track.enabled = constraints !== false;
} else if (constraints === false) { } else if (constraints === false) {
track.stop(); track.stop();
} }
// @ts-ignore
if (typeof constraints !== "boolean" && constraints !== true) { if (typeof constraints !== "boolean" && constraints !== true) {
track.applyConstraints(constraints); return track.applyConstraints(constraints);
} }
} }
@ -484,7 +488,12 @@ export const localStreamStore = derived<Readable<MediaStreamConstraints>, LocalS
type: "success", type: "success",
stream: null, stream: null,
}); });
initStream(constraints); initStream(constraints).catch((e) => {
set({
type: "error",
error: e instanceof Error ? e : new Error("An unknown error happened"),
});
});
} }
} else { } else {
//on bad navigators like chrome, we have to stop the tracks when we mute and reinstantiate the stream when we need to unmute //on bad navigators like chrome, we have to stop the tracks when we mute and reinstantiate the stream when we need to unmute
@ -496,7 +505,12 @@ export const localStreamStore = derived<Readable<MediaStreamConstraints>, LocalS
}); });
} //we reemit the stream if it was muted just to be sure } //we reemit the stream if it was muted just to be sure
else if (constraints.audio /* && !oldConstraints.audio*/ || (!oldConstraints.video && constraints.video)) { else if (constraints.audio /* && !oldConstraints.audio*/ || (!oldConstraints.video && constraints.video)) {
initStream(constraints); initStream(constraints).catch((e) => {
set({
type: "error",
error: e instanceof Error ? e : new Error("An unknown error happened"),
});
});
} }
oldConstraints = { oldConstraints = {
video: !!constraints.video, video: !!constraints.video,

View File

@ -98,7 +98,7 @@ export class SimplePeer {
private receiveWebrtcStart(user: UserSimplePeerInterface): void { private receiveWebrtcStart(user: UserSimplePeerInterface): void {
this.Users.push(user); this.Users.push(user);
// Note: the clients array contain the list of all clients (even the ones we are already connected to in case a user joints a group) // Note: the clients array contain the list of all clients (even the ones we are already connected to in case a user joins a group)
// So we can receive a request we already had before. (which will abort at the first line of createPeerConnection) // So we can receive a request we already had before. (which will abort at the first line of createPeerConnection)
// This would be symmetrical to the way we handle disconnection. // This would be symmetrical to the way we handle disconnection.

View File

@ -13,7 +13,6 @@ message PositionMessage {
} }
Direction direction = 3; Direction direction = 3;
bool moving = 4; bool moving = 4;
uint32 outlineColor = 5;
} }
message PointMessage { message PointMessage {
@ -48,8 +47,12 @@ message PingMessage {
} }
message SetPlayerDetailsMessage { message SetPlayerDetailsMessage {
string name = 1; //string name = 1;
repeated string characterLayers = 2; //repeated string characterLayers = 2;
// TODO: switch to google.protobuf.Int32Value when we migrate to ts-proto
int32 outlineColor = 3;
bool removeOutlineColor = 4;
} }
message UserMovesMessage { message UserMovesMessage {
@ -151,6 +154,7 @@ message SubMessage {
EmoteEventMessage emoteEventMessage = 7; EmoteEventMessage emoteEventMessage = 7;
VariableMessage variableMessage = 8; VariableMessage variableMessage = 8;
ErrorMessage errorMessage = 9; ErrorMessage errorMessage = 9;
PlayerDetailsUpdatedMessage playerDetailsUpdatedMessage = 10;
} }
} }
@ -333,6 +337,10 @@ message GroupLeftZoneMessage {
Zone toZone = 2; Zone toZone = 2;
} }
message PlayerDetailsUpdatedMessage {
int32 userId = 1;
SetPlayerDetailsMessage details = 2;
}
message Zone { message Zone {
int32 x = 1; int32 x = 1;
@ -385,6 +393,7 @@ message SubToPusherMessage {
BanUserMessage banUserMessage = 8; BanUserMessage banUserMessage = 8;
EmoteEventMessage emoteEventMessage = 9; EmoteEventMessage emoteEventMessage = 9;
ErrorMessage errorMessage = 10; ErrorMessage errorMessage = 10;
PlayerDetailsUpdatedMessage playerDetailsUpdatedMessage = 11;
} }
} }

View File

@ -16,6 +16,7 @@ import {
EmoteEventMessage, EmoteEventMessage,
CompanionMessage, CompanionMessage,
ErrorMessage, ErrorMessage,
PlayerDetailsUpdatedMessage,
} from "../Messages/generated/messages_pb"; } from "../Messages/generated/messages_pb";
import { ClientReadableStream } from "grpc"; import { ClientReadableStream } from "grpc";
import { PositionDispatcher } from "_Model/PositionDispatcher"; import { PositionDispatcher } from "_Model/PositionDispatcher";
@ -32,6 +33,7 @@ export interface ZoneEventListener {
onGroupLeaves(groupId: number, listener: ExSocketInterface): void; onGroupLeaves(groupId: number, listener: ExSocketInterface): void;
onEmote(emoteMessage: EmoteEventMessage, listener: ExSocketInterface): void; onEmote(emoteMessage: EmoteEventMessage, listener: ExSocketInterface): void;
onError(errorMessage: ErrorMessage, listener: ExSocketInterface): void; onError(errorMessage: ErrorMessage, listener: ExSocketInterface): void;
onPlayerDetailsUpdated(playerDetailsUpdatedMessage: PlayerDetailsUpdatedMessage, listener: ExSocketInterface): void;
} }
/*export type EntersCallback = (thing: Movable, listener: User) => void; /*export type EntersCallback = (thing: Movable, listener: User) => void;
@ -219,6 +221,10 @@ export class Zone {
} else if (message.hasEmoteeventmessage()) { } else if (message.hasEmoteeventmessage()) {
const emoteEventMessage = message.getEmoteeventmessage() as EmoteEventMessage; const emoteEventMessage = message.getEmoteeventmessage() as EmoteEventMessage;
this.notifyEmote(emoteEventMessage); this.notifyEmote(emoteEventMessage);
} else if (message.hasPlayerdetailsupdatedmessage()) {
const playerDetailsUpdatedMessage =
message.getPlayerdetailsupdatedmessage() as PlayerDetailsUpdatedMessage;
this.notifyPlayerDetailsUpdated(playerDetailsUpdatedMessage);
} else if (message.hasErrormessage()) { } else if (message.hasErrormessage()) {
const errorMessage = message.getErrormessage() as ErrorMessage; const errorMessage = message.getErrormessage() as ErrorMessage;
this.notifyError(errorMessage); this.notifyError(errorMessage);
@ -308,6 +314,15 @@ export class Zone {
} }
} }
private notifyPlayerDetailsUpdated(playerDetailsUpdatedMessage: PlayerDetailsUpdatedMessage) {
for (const listener of this.listeners) {
if (listener.userId === playerDetailsUpdatedMessage.getUserid()) {
continue;
}
this.socketListener.onPlayerDetailsUpdated(playerDetailsUpdatedMessage, listener);
}
}
private notifyError(errorMessage: ErrorMessage) { private notifyError(errorMessage: ErrorMessage) {
for (const listener of this.listeners) { for (const listener of this.listeners) {
this.socketListener.onError(errorMessage, listener); this.socketListener.onError(errorMessage, listener);

View File

@ -34,6 +34,7 @@ import {
VariableMessage, VariableMessage,
ErrorMessage, ErrorMessage,
WorldFullMessage, WorldFullMessage,
PlayerDetailsUpdatedMessage,
} from "../Messages/generated/messages_pb"; } from "../Messages/generated/messages_pb";
import { ProtobufUtils } from "../Model/Websocket/ProtobufUtils"; import { ProtobufUtils } from "../Model/Websocket/ProtobufUtils";
import { ADMIN_API_URL, JITSI_ISS, JITSI_URL, SECRET_JITSI_KEY } from "../Enum/EnvironmentVariable"; import { ADMIN_API_URL, JITSI_ISS, JITSI_URL, SECRET_JITSI_KEY } from "../Enum/EnvironmentVariable";
@ -55,6 +56,7 @@ const debug = Debug("socket");
interface AdminSocketRoomsList { interface AdminSocketRoomsList {
[index: string]: number; [index: string]: number;
} }
interface AdminSocketUsersList { interface AdminSocketUsersList {
[index: string]: boolean; [index: string]: boolean;
} }
@ -276,6 +278,16 @@ export class SocketManager implements ZoneEventListener {
emitInBatch(listener, subMessage); emitInBatch(listener, subMessage);
} }
onPlayerDetailsUpdated(
playerDetailsUpdatedMessage: PlayerDetailsUpdatedMessage,
listener: ExSocketInterface
): void {
const subMessage = new SubMessage();
subMessage.setPlayerdetailsupdatedmessage(playerDetailsUpdatedMessage);
emitInBatch(listener, subMessage);
}
onError(errorMessage: ErrorMessage, listener: ExSocketInterface): void { onError(errorMessage: ErrorMessage, listener: ExSocketInterface): void {
const subMessage = new SubMessage(); const subMessage = new SubMessage();
subMessage.setErrormessage(errorMessage); subMessage.setErrormessage(errorMessage);