Merge branch 'develop' into player-report

# Conflicts:
#	back/src/Controller/IoSocketController.ts
#	front/src/Phaser/Game/GameScene.ts
#	front/src/index.ts
#	messages/messages.proto
This commit is contained in:
Gregoire Parant 2020-10-13 20:39:29 +02:00
commit 05a1ea8469
28 changed files with 1189 additions and 975 deletions

View File

@ -1,38 +1,30 @@
import Jwt from "jsonwebtoken";
import {ADMIN_API_TOKEN, ADMIN_API_URL, SECRET_KEY, URL_ROOM_STARTED} from "../Enum/EnvironmentVariable"; //TODO fix import by "_Enum/..."
import { v4 } from 'uuid'; import { v4 } from 'uuid';
import {HttpRequest, HttpResponse, TemplatedApp} from "uWebSockets.js"; import {HttpRequest, HttpResponse, TemplatedApp} from "uWebSockets.js";
import {BaseController} from "./BaseController"; import {BaseController} from "./BaseController";
import Axios from "axios"; import {adminApi} from "../Services/AdminApi";
import {jwtTokenManager} from "../Services/JWTTokenManager";
export interface TokenInterface { export interface TokenInterface {
userUuid: string userUuid: string
} }
interface AdminApiData {
organizationSlug: string
worldSlug: string
roomSlug: string
mapUrlStart: string
userUuid: string
}
export class AuthenticateController extends BaseController { export class AuthenticateController extends BaseController {
constructor(private App : TemplatedApp) { constructor(private App : TemplatedApp) {
super(); super();
this.login(); this.register();
this.anonymLogin();
} }
//permit to login on application. Return token to connect on Websocket IO. //Try to login with an admin token
login(){ register(){
this.App.options("/login", (res: HttpResponse, req: HttpRequest) => { this.App.options("/register", (res: HttpResponse, req: HttpRequest) => {
this.addCorsHeaders(res); this.addCorsHeaders(res);
res.end(); res.end();
}); });
this.App.post("/login", (res: HttpResponse, req: HttpRequest) => { this.App.post("/register", (res: HttpResponse, req: HttpRequest) => {
(async () => { (async () => {
this.addCorsHeaders(res); this.addCorsHeaders(res);
@ -46,34 +38,23 @@ export class AuthenticateController extends BaseController {
const organizationMemberToken:string|null = param.organizationMemberToken; const organizationMemberToken:string|null = param.organizationMemberToken;
try { try {
let userUuid; if (typeof organizationMemberToken != 'string') throw new Error('No organization token');
let mapUrlStart; const data = await adminApi.fetchMemberDataByToken(organizationMemberToken);
let newUrl: string|null = null;
if (organizationMemberToken) { const userUuid = data.userUuid;
if (!ADMIN_API_URL) { const organizationSlug = data.organizationSlug;
return res.status(401).send('No admin backoffice set!'); const worldSlug = data.worldSlug;
} const roomSlug = data.roomSlug;
//todo: this call can fail if the corresponding world is not activated or if the token is invalid. Handle that case. const mapUrlStart = data.mapUrlStart;
const data = await Axios.get(ADMIN_API_URL+'/api/login-url/'+organizationMemberToken,
{ headers: {"Authorization" : `${ADMIN_API_TOKEN}`} }
).then((res): AdminApiData => res.data);
userUuid = data.userUuid; const authToken = jwtTokenManager.createJWTToken(userUuid);
mapUrlStart = data.mapUrlStart;
newUrl = this.getNewUrlOnAdminAuth(data)
} else {
userUuid = v4();
mapUrlStart = host.replace('api.', 'maps.') + URL_ROOM_STARTED;
newUrl = null;
}
const authToken = Jwt.sign({userUuid: userUuid}, SECRET_KEY, {expiresIn: '24h'});
res.writeStatus("200 OK").end(JSON.stringify({ res.writeStatus("200 OK").end(JSON.stringify({
authToken, authToken,
userUuid, userUuid,
organizationSlug,
worldSlug,
roomSlug,
mapUrlStart, mapUrlStart,
newUrl,
})); }));
} catch (e) { } catch (e) {
@ -84,12 +65,30 @@ export class AuthenticateController extends BaseController {
})(); })();
}); });
} }
private getNewUrlOnAdminAuth(data:AdminApiData): string { //permit to login on application. Return token to connect on Websocket IO.
const organizationSlug = data.organizationSlug; anonymLogin(){
const worldSlug = data.worldSlug; this.App.options("/anonymLogin", (res: HttpResponse, req: HttpRequest) => {
const roomSlug = data.roomSlug; this.addCorsHeaders(res);
return '/@/'+organizationSlug+'/'+worldSlug+'/'+roomSlug;
res.end();
});
this.App.post("/anonymLogin", (res: HttpResponse, req: HttpRequest) => {
this.addCorsHeaders(res);
res.onAborted(() => {
console.warn('Login request was aborted');
})
const userUuid = v4();
const authToken = jwtTokenManager.createJWTToken(userUuid);
res.writeStatus("200 OK").end(JSON.stringify({
authToken,
userUuid,
}));
});
} }
} }

View File

@ -1,30 +1,11 @@
import * as http from "http";
import {MessageUserPosition, Point} from "../Model/Websocket/MessageUserPosition"; //TODO fix import by "_Model/.."
import {ExSocketInterface} from "../Model/Websocket/ExSocketInterface"; //TODO fix import by "_Model/.." import {ExSocketInterface} from "../Model/Websocket/ExSocketInterface"; //TODO fix import by "_Model/.."
import Jwt, {JsonWebTokenError} from "jsonwebtoken"; import {MINIMUM_DISTANCE, GROUP_RADIUS} from "../Enum/EnvironmentVariable"; //TODO fix import by "_Enum/..."
import { import {GameRoom} from "../Model/GameRoom";
SECRET_KEY,
MINIMUM_DISTANCE,
GROUP_RADIUS,
ALLOW_ARTILLERY,
ADMIN_API_URL,
ADMIN_API_TOKEN
} from "../Enum/EnvironmentVariable"; //TODO fix import by "_Enum/..."
import {World} from "../Model/World";
import {Group} from "../Model/Group"; import {Group} from "../Model/Group";
import {User} from "../Model/User"; import {User} from "../Model/User";
import {isSetPlayerDetailsMessage,} from "../Model/Websocket/SetPlayerDetailsMessage"; import {isSetPlayerDetailsMessage,} from "../Model/Websocket/SetPlayerDetailsMessage";
import {MessageUserJoined} from "../Model/Websocket/MessageUserJoined";
import si from "systeminformation";
import {Gauge} from "prom-client"; import {Gauge} from "prom-client";
import {TokenInterface} from "../Controller/AuthenticateController";
import {isJoinRoomMessageInterface} from "../Model/Websocket/JoinRoomMessage";
import {PointInterface} from "../Model/Websocket/PointInterface"; import {PointInterface} from "../Model/Websocket/PointInterface";
import {isWebRtcSignalMessageInterface} from "../Model/Websocket/WebRtcSignalMessage";
import {UserInGroupInterface} from "../Model/Websocket/UserInGroupInterface";
import {isItemEventMessageInterface} from "../Model/Websocket/ItemEventMessage";
import { v4 as uuidv4 } from 'uuid';
import {GroupUpdateInterface} from "_Model/Websocket/GroupUpdateInterface";
import {Movable} from "../Model/Movable"; import {Movable} from "../Model/Movable";
import { import {
PositionMessage, PositionMessage,
@ -40,23 +21,28 @@ import {
ItemEventMessage, ItemEventMessage,
ViewportMessage, ViewportMessage,
ClientToServerMessage, ClientToServerMessage,
JoinRoomMessage,
ErrorMessage, ErrorMessage,
RoomJoinedMessage, RoomJoinedMessage,
ItemStateMessage, ItemStateMessage,
ServerToClientMessage, ServerToClientMessage,
SetUserIdMessage,
SilentMessage, SilentMessage,
WebRtcSignalToClientMessage, WebRtcSignalToClientMessage,
WebRtcSignalToServerMessage, WebRtcSignalToServerMessage,
WebRtcStartMessage, WebRtcDisconnectMessage, PlayGlobalMessage, ReportPlayerMessage, TeleportMessageMessage WebRtcStartMessage,
WebRtcDisconnectMessage,
PlayGlobalMessage,
TeleportMessageMessage
} from "../Messages/generated/messages_pb"; } from "../Messages/generated/messages_pb";
import {UserMovesMessage} from "../Messages/generated/messages_pb"; import {UserMovesMessage} from "../Messages/generated/messages_pb";
import Direction = PositionMessage.Direction; import Direction = PositionMessage.Direction;
import {ProtobufUtils} from "../Model/Websocket/ProtobufUtils"; import {ProtobufUtils} from "../Model/Websocket/ProtobufUtils";
import {App, HttpRequest, TemplatedApp, WebSocket} from "uWebSockets.js" import {TemplatedApp} from "uWebSockets.js"
import {parse} from "query-string"; import {parse} from "query-string";
import {cpuTracker} from "../Services/CpuTracker"; import {cpuTracker} from "../Services/CpuTracker";
import {ViewportInterface} from "../Model/Websocket/ViewportMessage";
import {jwtTokenManager} from "../Services/JWTTokenManager";
import {adminApi} from "../Services/AdminApi";
import {RoomIdentifier} from "../Model/RoomIdentifier";
import Axios from "axios"; import Axios from "axios";
function emitInBatch(socket: ExSocketInterface, payload: SubMessage): void { function emitInBatch(socket: ExSocketInterface, payload: SubMessage): void {
@ -79,7 +65,7 @@ function emitInBatch(socket: ExSocketInterface, payload: SubMessage): void {
} }
export class IoSocketController { export class IoSocketController {
private Worlds: Map<string, World> = new Map<string, World>(); private Worlds: Map<string, GameRoom> = new Map<string, GameRoom>();
private sockets: Map<number, ExSocketInterface> = new Map<number, ExSocketInterface>(); private sockets: Map<number, ExSocketInterface> = new Map<number, ExSocketInterface>();
private nbClientsGauge: Gauge<string>; private nbClientsGauge: Gauge<string>;
private nbClientsPerRoomGauge: Gauge<string>; private nbClientsPerRoomGauge: Gauge<string>;
@ -101,76 +87,8 @@ export class IoSocketController {
this.ioConnection(); this.ioConnection();
} }
private isValidToken(token: object): token is TokenInterface {
if (typeof((token as TokenInterface).userUuid) !== 'string') {
return false;
}
return true;
}
private async authenticate(req: HttpRequest): Promise<{ token: string, userUuid: string }> {
//console.log(socket.handshake.query.token);
const query = parse(req.getQuery());
if (!query.token) {
throw new Error('An authentication error happened, a user tried to connect without a token.');
}
const token = query.token;
if (typeof(token) !== "string") {
throw new Error('Token is expected to be a string');
}
if(token === 'test') {
if (ALLOW_ARTILLERY) {
return {
token,
userUuid: uuidv4()
}
} else {
throw new Error("In order to perform a load-testing test on this environment, you must set the ALLOW_ARTILLERY environment variable to 'true'");
}
}
/*if(this.searchClientByToken(socket.handshake.query.token)){
console.error('An authentication error happened, a user tried to connect while its token is already connected.');
return next(new Error('Authentication error'));
}*/
const promise = new Promise<{ token: string, userUuid: string }>((resolve, reject) => {
Jwt.verify(token, SECRET_KEY, {},(err, tokenDecoded) => {
if (err) {
console.error('An authentication error happened, invalid JsonWebToken.', err);
reject(new Error('An authentication error happened, invalid JsonWebToken. '+err.message));
return;
}
if (tokenDecoded === undefined) {
console.error('Empty token found.');
reject(new Error('Empty token found.'));
return;
}
const tokenInterface = tokenDecoded as TokenInterface;
if (!this.isValidToken(tokenInterface)) {
reject(new Error('Authentication error, invalid token structure.'));
return;
}
resolve({
token,
userUuid: tokenInterface.userUuid
});
});
});
return promise;
}
ioConnection() { ioConnection() {
this.app.ws('/*', { this.app.ws('/room', {
/* Options */ /* Options */
//compression: uWS.SHARED_COMPRESSOR, //compression: uWS.SHARED_COMPRESSOR,
maxPayloadLength: 16 * 1024 * 1024, maxPayloadLength: 16 * 1024 * 1024,
@ -179,7 +97,6 @@ export class IoSocketController {
upgrade: (res, req, context) => { upgrade: (res, req, context) => {
//console.log('An Http connection wants to become WebSocket, URL: ' + req.getUrl() + '!'); //console.log('An Http connection wants to become WebSocket, URL: ' + req.getUrl() + '!');
(async () => { (async () => {
/* Keep track of abortions */ /* Keep track of abortions */
const upgradeAborted = {aborted: false}; const upgradeAborted = {aborted: false};
@ -189,7 +106,54 @@ export class IoSocketController {
}); });
try { try {
const result = await this.authenticate(req); const url = req.getUrl();
const query = parse(req.getQuery());
const websocketKey = req.getHeader('sec-websocket-key');
const websocketProtocol = req.getHeader('sec-websocket-protocol');
const websocketExtensions = req.getHeader('sec-websocket-extensions');
const roomId = query.roomId;
//todo: better validation: /\/_\/.*\/.*/ or /\/@\/.*\/.*\/.*/
if (typeof roomId !== 'string') {
throw new Error('Undefined room ID: ');
}
const roomIdentifier = new RoomIdentifier(roomId);
const token = query.token;
const x = Number(query.x);
const y = Number(query.y);
const top = Number(query.top);
const bottom = Number(query.bottom);
const left = Number(query.left);
const right = Number(query.right);
const name = query.name;
if (typeof name !== 'string') {
throw new Error('Expecting name');
}
if (name === '') {
throw new Error('No empty name');
}
let characterLayers = query.characterLayers;
if (characterLayers === null) {
throw new Error('Expecting skin');
}
if (typeof characterLayers === 'string') {
characterLayers = [ characterLayers ];
}
const userUuid = await jwtTokenManager.getUserUuidFromToken(token);
console.log('uuid', userUuid);
if (roomIdentifier.anonymous === false) {
const isGranted = await adminApi.memberIsGrantedAccessToRoom(userUuid, roomIdentifier);
if (!isGranted) {
console.log('access not granted for user '+userUuid+' and room '+roomId);
throw new Error('Client cannot acces this ressource.')
} else {
console.log('access granted for user '+userUuid+' and room '+roomId);
}
}
if (upgradeAborted.aborted) { if (upgradeAborted.aborted) {
console.log("Ouch! Client disconnected before we could upgrade it!"); console.log("Ouch! Client disconnected before we could upgrade it!");
@ -200,22 +164,37 @@ export class IoSocketController {
/* This immediately calls open handler, you must not use res after this call */ /* This immediately calls open handler, you must not use res after this call */
res.upgrade({ res.upgrade({
// Data passed here is accessible on the "websocket" socket object. // Data passed here is accessible on the "websocket" socket object.
url: req.getUrl(), url,
token: result.token, token,
userUuid: result.userUuid userUuid,
roomId,
name,
characterLayers,
position: {
x: x,
y: y,
direction: 'down',
moving: false
} as PointInterface,
viewport: {
top,
right,
bottom,
left
}
}, },
/* Spell these correctly */ /* Spell these correctly */
req.getHeader('sec-websocket-key'), websocketKey,
req.getHeader('sec-websocket-protocol'), websocketProtocol,
req.getHeader('sec-websocket-extensions'), websocketExtensions,
context); context);
} catch (e) { } catch (e) {
if (e instanceof Error) { if (e instanceof Error) {
console.warn(e.message); console.log(e.message);
res.writeStatus("401 Unauthorized").end(e.message); res.writeStatus("401 Unauthorized").end(e.message);
} else { } else {
console.warn(e); console.log(e);
res.writeStatus("500 Internal Server Error").end('An error occurred'); res.writeStatus("500 Internal Server Error").end('An error occurred');
} }
return; return;
@ -235,20 +214,25 @@ export class IoSocketController {
emitInBatch(client, payload); emitInBatch(client, payload);
} }
client.disconnecting = false; client.disconnecting = false;
client.name = ws.name;
client.characterLayers = ws.characterLayers;
client.roomId = ws.roomId;
this.sockets.set(client.userId, client); this.sockets.set(client.userId, client);
// Let's log server load when a user joins // Let's log server load when a user joins
this.nbClientsGauge.inc(); this.nbClientsGauge.inc();
console.log(new Date().toISOString() + ' A user joined (', this.sockets.size, ' connected users)'); console.log(new Date().toISOString() + ' A user joined (', this.sockets.size, ' connected users)');
// Let's join the room
this.handleJoinRoom(client, client.roomId, client.position, client.viewport, client.name, client.characterLayers);
}, },
message: (ws, arrayBuffer, isBinary) => { message: (ws, arrayBuffer, isBinary): void => {
const client = ws as ExSocketInterface; const client = ws as ExSocketInterface;
const message = ClientToServerMessage.deserializeBinary(new Uint8Array(arrayBuffer)); const message = ClientToServerMessage.deserializeBinary(new Uint8Array(arrayBuffer));
if (message.hasJoinroommessage()) { if (message.hasViewportmessage()) {
this.handleJoinRoom(client, message.getJoinroommessage() as JoinRoomMessage);
} else if (message.hasViewportmessage()) {
this.handleViewport(client, message.getViewportmessage() as ViewportMessage); this.handleViewport(client, message.getViewportmessage() as ViewportMessage);
} else if (message.hasUsermovesmessage()) { } else if (message.hasUsermovesmessage()) {
this.handleUserMovesMessage(client, message.getUsermovesmessage() as UserMovesMessage); this.handleUserMovesMessage(client, message.getUsermovesmessage() as UserMovesMessage);
@ -327,26 +311,12 @@ export class IoSocketController {
console.warn(message); console.warn(message);
} }
private handleJoinRoom(Client: ExSocketInterface, message: JoinRoomMessage): void { private handleJoinRoom(client: ExSocketInterface, roomId: string, position: PointInterface, viewport: ViewportInterface, name: string, characterLayers: string[]): void {
try { try {
/*if (!isJoinRoomMessageInterface(message.toObject())) {
console.log(message.toObject())
this.emitError(Client, 'Invalid JOIN_ROOM message received: ' + message.toObject().toString());
return;
}*/
const roomId = message.getRoomid();
if (Client.roomId === roomId) {
return;
}
//leave previous room
//this.leaveRoom(Client); // Useless now, there is only one room per connection
//join new previous room //join new previous room
const world = this.joinRoom(Client, roomId, ProtobufUtils.toPointInterface(message.getPosition() as PositionMessage)); const gameRoom = this.joinRoom(client, roomId, position);
const things = world.setViewport(Client, (message.getViewport() as ViewportMessage).toObject()); const things = gameRoom.setViewport(client, viewport);
const roomJoinedMessage = new RoomJoinedMessage(); const roomJoinedMessage = new RoomJoinedMessage();
@ -376,7 +346,7 @@ export class IoSocketController {
} }
} }
for (const [itemId, item] of world.getItemsState().entries()) { for (const [itemId, item] of gameRoom.getItemsState().entries()) {
const itemStateMessage = new ItemStateMessage(); const itemStateMessage = new ItemStateMessage();
itemStateMessage.setItemid(itemId); itemStateMessage.setItemid(itemId);
itemStateMessage.setStatejson(JSON.stringify(item)); itemStateMessage.setStatejson(JSON.stringify(item));
@ -384,11 +354,13 @@ export class IoSocketController {
roomJoinedMessage.addItem(itemStateMessage); roomJoinedMessage.addItem(itemStateMessage);
} }
roomJoinedMessage.setCurrentuserid(client.userId);
const serverToClientMessage = new ServerToClientMessage(); const serverToClientMessage = new ServerToClientMessage();
serverToClientMessage.setRoomjoinedmessage(roomJoinedMessage); serverToClientMessage.setRoomjoinedmessage(roomJoinedMessage);
if (!Client.disconnecting) { if (!client.disconnecting) {
Client.send(serverToClientMessage.serializeBinary().buffer, true); client.send(serverToClientMessage.serializeBinary().buffer, true);
} }
} catch (e) { } catch (e) {
console.error('An error occurred on "join_room" event'); console.error('An error occurred on "join_room" event');
@ -474,6 +446,7 @@ export class IoSocketController {
} }
} }
// Useless now, will be useful again if we allow editing details in game
private handleSetPlayerDetails(client: ExSocketInterface, playerDetailsMessage: SetPlayerDetailsMessage) { private handleSetPlayerDetails(client: ExSocketInterface, playerDetailsMessage: SetPlayerDetailsMessage) {
const playerDetails = { const playerDetails = {
name: playerDetailsMessage.getName(), name: playerDetailsMessage.getName(),
@ -487,16 +460,6 @@ export class IoSocketController {
client.name = playerDetails.name; client.name = playerDetails.name;
client.characterLayers = playerDetails.characterLayers; client.characterLayers = playerDetails.characterLayers;
const setUserIdMessage = new SetUserIdMessage();
setUserIdMessage.setUserid(client.userId);
const serverToClientMessage = new ServerToClientMessage();
serverToClientMessage.setSetuseridmessage(setUserIdMessage);
if (!client.disconnecting) {
client.send(serverToClientMessage.serializeBinary().buffer, true);
}
} }
private handleSilentMessage(client: ExSocketInterface, silentMessage: SilentMessage) { private handleSilentMessage(client: ExSocketInterface, silentMessage: SilentMessage) {
@ -617,7 +580,7 @@ export class IoSocketController {
if(Client.roomId){ if(Client.roomId){
try { try {
//user leave previous world //user leave previous world
const world: World | undefined = this.Worlds.get(Client.roomId); const world: GameRoom | undefined = this.Worlds.get(Client.roomId);
if (world) { if (world) {
world.leave(Client); world.leave(Client);
if (world.isEmpty()) { if (world.isEmpty()) {
@ -633,17 +596,17 @@ export class IoSocketController {
} }
} }
private joinRoom(Client : ExSocketInterface, roomId: string, position: PointInterface): World { private joinRoom(client : ExSocketInterface, roomId: string, position: PointInterface): GameRoom {
//join user in room //join user in room
//Client.join(roomId);
this.nbClientsPerRoomGauge.inc({ room: roomId }); this.nbClientsPerRoomGauge.inc({ room: roomId });
Client.roomId = roomId; client.roomId = roomId;
Client.position = position; client.position = position;
//check and create new world for a room //check and create new world for a room
let world = this.Worlds.get(roomId) let world = this.Worlds.get(roomId)
if(world === undefined){ if(world === undefined){
world = new World((user1: User, group: Group) => { world = new GameRoom((user1: User, group: Group) => {
this.joinWebRtcRoom(user1, group); this.joinWebRtcRoom(user1, group);
}, (user1: User, group: Group) => { }, (user1: User, group: Group) => {
this.disConnectedUser(user1, group); this.disConnectedUser(user1, group);
@ -706,10 +669,10 @@ export class IoSocketController {
// Dispatch groups position to newly connected user // Dispatch groups position to newly connected user
world.getGroups().forEach((group: Group) => { world.getGroups().forEach((group: Group) => {
this.emitCreateUpdateGroupEvent(Client, group); this.emitCreateUpdateGroupEvent(client, group);
}); });
//join world //join world
world.join(Client, Client.position); world.join(client, client.position);
return world; return world;
} }
@ -899,7 +862,7 @@ export class IoSocketController {
} }
public getWorlds(): Map<string, World> { public getWorlds(): Map<string, GameRoom> {
return this.Worlds; return this.Worlds;
} }

View File

@ -2,6 +2,8 @@ import {OK} from "http-status-codes";
import {URL_ROOM_STARTED} from "../Enum/EnvironmentVariable"; import {URL_ROOM_STARTED} from "../Enum/EnvironmentVariable";
import {HttpRequest, HttpResponse, TemplatedApp} from "uWebSockets.js"; import {HttpRequest, HttpResponse, TemplatedApp} from "uWebSockets.js";
import {BaseController} from "./BaseController"; import {BaseController} from "./BaseController";
import {parse} from "query-string";
import {adminApi} from "../Services/AdminApi";
//todo: delete this //todo: delete this
export class MapController extends BaseController{ export class MapController extends BaseController{
@ -9,26 +11,51 @@ export class MapController extends BaseController{
constructor(private App : TemplatedApp) { constructor(private App : TemplatedApp) {
super(); super();
this.App = App; this.App = App;
this.getStartMap(); this.getMapUrl();
} }
// Returns a map mapping map name to file name of the map // Returns a map mapping map name to file name of the map
getStartMap() { getMapUrl() {
this.App.options("/start-map", (res: HttpResponse, req: HttpRequest) => { this.App.options("/map", (res: HttpResponse, req: HttpRequest) => {
this.addCorsHeaders(res); this.addCorsHeaders(res);
res.end(); res.end();
}); });
this.App.get("/start-map", (res: HttpResponse, req: HttpRequest) => { this.App.get("/map", (res: HttpResponse, req: HttpRequest) => {
this.addCorsHeaders(res); this.addCorsHeaders(res);
const url = req.getHeader('host').replace('api.', 'maps.') + URL_ROOM_STARTED; res.onAborted(() => {
res.writeStatus("200 OK").end(JSON.stringify({ console.warn('/map request was aborted');
mapUrlStart: url, })
startInstance: "global"
})); const query = parse(req.getQuery());
if (typeof query.organizationSlug !== 'string') {
console.error('Expected organizationSlug parameter');
res.writeStatus("400 Bad request").end("Expected organizationSlug parameter");
}
if (typeof query.worldSlug !== 'string') {
console.error('Expected worldSlug parameter');
res.writeStatus("400 Bad request").end("Expected worldSlug parameter");
}
if (typeof query.roomSlug !== 'string' && query.roomSlug !== undefined) {
console.error('Expected only one roomSlug parameter');
res.writeStatus("400 Bad request").end("Expected only one roomSlug parameter");
}
(async () => {
try {
const mapDetails = await adminApi.fetchMapDetails(query.organizationSlug as string, query.worldSlug as string, query.roomSlug as string|undefined);
res.writeStatus("200 OK").end(JSON.stringify(mapDetails));
} catch (e) {
console.error(e);
res.writeStatus("500 Internal Server Error").end("An error occurred");
}
})();
}); });
} }
} }

View File

@ -1,12 +1,10 @@
import {MessageUserPosition, Point} from "./Websocket/MessageUserPosition";
import {PointInterface} from "./Websocket/PointInterface"; import {PointInterface} from "./Websocket/PointInterface";
import {Group} from "./Group"; import {Group} from "./Group";
import {Distance} from "./Distance";
import {User} from "./User"; import {User} from "./User";
import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface"; import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
import {PositionInterface} from "_Model/PositionInterface"; import {PositionInterface} from "_Model/PositionInterface";
import {Identificable} from "_Model/Websocket/Identificable"; import {Identificable} from "_Model/Websocket/Identificable";
import {EntersCallback, LeavesCallback, MovesCallback, Zone} from "_Model/Zone"; import {EntersCallback, LeavesCallback, MovesCallback} from "_Model/Zone";
import {PositionNotifier} from "./PositionNotifier"; import {PositionNotifier} from "./PositionNotifier";
import {ViewportInterface} from "_Model/Websocket/ViewportMessage"; import {ViewportInterface} from "_Model/Websocket/ViewportMessage";
import {Movable} from "_Model/Movable"; import {Movable} from "_Model/Movable";
@ -14,7 +12,7 @@ import {Movable} from "_Model/Movable";
export type ConnectCallback = (user: User, group: Group) => void; export type ConnectCallback = (user: User, group: Group) => void;
export type DisconnectCallback = (user: User, group: Group) => void; export type DisconnectCallback = (user: User, group: Group) => void;
export class World { export class GameRoom {
private readonly minDistance: number; private readonly minDistance: number;
private readonly groupRadius: number; private readonly groupRadius: number;
@ -123,7 +121,7 @@ export class World {
} else { } else {
// If the user is part of a group: // If the user is part of a group:
// should he leave the group? // should he leave the group?
const distance = World.computeDistanceBetweenPositions(user.getPosition(), user.group.getPosition()); const distance = GameRoom.computeDistanceBetweenPositions(user.getPosition(), user.group.getPosition());
if (distance > this.groupRadius) { if (distance > this.groupRadius) {
this.leaveGroup(user); this.leaveGroup(user);
} }
@ -199,53 +197,19 @@ export class World {
return; return;
} }
const distance = World.computeDistance(user, currentUser); // compute distance between peers. const distance = GameRoom.computeDistance(user, currentUser); // compute distance between peers.
if(distance <= minimumDistanceFound && distance <= this.minDistance) { if(distance <= minimumDistanceFound && distance <= this.minDistance) {
minimumDistanceFound = distance; minimumDistanceFound = distance;
matchingItem = currentUser; matchingItem = currentUser;
} }
/*if (typeof currentUser.group === 'undefined' || !currentUser.group.isFull()) {
// We found a user we can bind to.
return;
}*/
/*
if(context.groups.length > 0) {
context.groups.forEach(group => {
if(group.isPartOfGroup(userPosition)) { // Is the user in a group ?
if(group.isStillIn(userPosition)) { // Is the user leaving the group ? (is the user at more than max distance of each player)
// Should we split the group? (is each player reachable from the current player?)
// This is needed if
// A <==> B <==> C <===> D
// becomes A <==> B <=====> C <> D
// If C moves right, the distance between B and C is too great and we must form 2 groups
}
} else {
// If the user is in no group
// Is there someone in a group close enough and with room in the group ?
}
});
} else {
// Aucun groupe n'existe donc je stock les users assez proches de moi
let dist: Distance = {
distance: distance,
first: userPosition,
second: user // TODO: convertir en messageUserPosition
}
usersToBeGroupedWith.push(dist);
}
*/
}); });
this.groups.forEach((group: Group) => { this.groups.forEach((group: Group) => {
if (group.isFull()) { if (group.isFull()) {
return; return;
} }
const distance = World.computeDistanceBetweenPositions(user.getPosition(), group.getPosition()); const distance = GameRoom.computeDistanceBetweenPositions(user.getPosition(), group.getPosition());
if(distance <= minimumDistanceFound && distance <= this.groupRadius) { if(distance <= minimumDistanceFound && distance <= this.groupRadius) {
minimumDistanceFound = distance; minimumDistanceFound = distance;
matchingItem = group; matchingItem = group;
@ -275,66 +239,7 @@ export class World {
return this.itemsState; return this.itemsState;
} }
/*getDistancesBetweenGroupUsers(group: Group): Distance[]
{
let i = 0;
let users = group.getUsers();
let distances: Distance[] = [];
users.forEach(function(user1, key1) {
users.forEach(function(user2, key2) {
if(key1 < key2) {
distances[i] = {
distance: World.computeDistance(user1, user2),
first: user1,
second: user2
};
i++;
}
});
});
distances.sort(World.compareDistances);
return distances;
}
filterGroup(distances: Distance[], group: Group): void
{
let users = group.getUsers();
let usersToRemove = false;
let groupTmp: MessageUserPosition[] = [];
distances.forEach(dist => {
if(dist.distance <= World.MIN_DISTANCE) {
let users = [dist.first];
let usersbis = [dist.second]
groupTmp.push(dist.first);
groupTmp.push(dist.second);
} else {
usersToRemove = true;
}
});
if(usersToRemove) {
// Detecte le ou les users qui se sont fait sortir du groupe
let difference = users.filter(x => !groupTmp.includes(x));
// TODO : Notify users un difference that they have left the group
}
let newgroup = new Group(groupTmp);
this.groups.push(newgroup);
}
private static compareDistances(distA: Distance, distB: Distance): number
{
if (distA.distance < distB.distance) {
return -1;
}
if (distA.distance > distB.distance) {
return 1;
}
return 0;
}*/
setViewport(socket : Identificable, viewport: ViewportInterface): Movable[] { setViewport(socket : Identificable, viewport: ViewportInterface): Movable[] {
const user = this.users.get(socket.userId); const user = this.users.get(socket.userId);
if(typeof user === 'undefined') { if(typeof user === 'undefined') {

View File

@ -1,4 +1,4 @@
import { ConnectCallback, DisconnectCallback } from "./World"; import { ConnectCallback, DisconnectCallback } from "./GameRoom";
import { User } from "./User"; import { User } from "./User";
import {PositionInterface} from "_Model/PositionInterface"; import {PositionInterface} from "_Model/PositionInterface";
import {Movable} from "_Model/Movable"; import {Movable} from "_Model/Movable";

View File

@ -0,0 +1,14 @@
export class RoomIdentifier {
public anonymous: boolean;
public id:string
constructor(roomID: string) {
if (roomID.startsWith('_/')) {
this.anonymous = true;
} else if(roomID.startsWith('@/')) {
this.anonymous = false;
} else {
throw new Error('Incorrect room ID: '+roomID);
}
this.id = roomID; //todo: extract more data from the id (like room slug, organization name, etc);
}
}

View File

@ -0,0 +1,66 @@
import {ADMIN_API_TOKEN, ADMIN_API_URL} from "../Enum/EnvironmentVariable";
import Axios from "axios";
import {RoomIdentifier} from "../Model/RoomIdentifier";
export interface AdminApiData {
organizationSlug: string
worldSlug: string
roomSlug: string
mapUrlStart: string
userUuid: string
}
class AdminApi {
async fetchMapDetails(organizationSlug: string, worldSlug: string, roomSlug: string|undefined): Promise<AdminApiData> {
if (!ADMIN_API_URL) {
return Promise.reject('No admin backoffice set!');
}
const params: { organizationSlug: string, worldSlug: string, roomSlug?: string } = {
organizationSlug,
worldSlug
};
if (roomSlug) {
params.roomSlug = roomSlug;
}
const res = await Axios.get(ADMIN_API_URL+'/api/map',
{
headers: {"Authorization" : `${ADMIN_API_TOKEN}`},
params
}
)
return res.data;
}
async fetchMemberDataByToken(organizationMemberToken: string): Promise<AdminApiData> {
if (!ADMIN_API_URL) {
return Promise.reject('No admin backoffice set!');
}
//todo: this call can fail if the corresponding world is not activated or if the token is invalid. Handle that case.
const res = await Axios.get(ADMIN_API_URL+'/api/login-url/'+organizationMemberToken,
{ headers: {"Authorization" : `${ADMIN_API_TOKEN}`} }
)
return res.data;
}
async memberIsGrantedAccessToRoom(memberId: string, roomIdentifier: RoomIdentifier): Promise<boolean> {
if (!ADMIN_API_URL) {
return Promise.reject('No admin backoffice set!');
}
try {
//todo: send more specialized data instead of the whole id
const res = await Axios.get(ADMIN_API_URL+'/api/member/is-granted-access',
{ headers: {"Authorization" : `${ADMIN_API_TOKEN}`}, params: {memberId, roomIdentifier: roomIdentifier.id} }
)
return !!res.data;
} catch (e) {
console.log(e.message)
return false;
}
}
}
export const adminApi = new AdminApi();

View File

@ -0,0 +1,60 @@
import {ALLOW_ARTILLERY, SECRET_KEY} from "../Enum/EnvironmentVariable";
import {uuid} from "uuidv4";
import Jwt from "jsonwebtoken";
import {TokenInterface} from "../Controller/AuthenticateController";
class JWTTokenManager {
public createJWTToken(userUuid: string) {
return Jwt.sign({userUuid: userUuid}, SECRET_KEY, {expiresIn: '24h'});
}
public async getUserUuidFromToken(token: unknown): Promise<string> {
if (!token) {
throw new Error('An authentication error happened, a user tried to connect without a token.');
}
if (typeof(token) !== "string") {
throw new Error('Token is expected to be a string');
}
if(token === 'test') {
if (ALLOW_ARTILLERY) {
return uuid();
} else {
throw new Error("In order to perform a load-testing test on this environment, you must set the ALLOW_ARTILLERY environment variable to 'true'");
}
}
return new Promise<string>((resolve, reject) => {
Jwt.verify(token, SECRET_KEY, {},(err, tokenDecoded) => {
const tokenInterface = tokenDecoded as TokenInterface;
if (err) {
console.error('An authentication error happened, invalid JsonWebToken.', err);
reject(new Error('An authentication error happened, invalid JsonWebToken. '+err.message));
return;
}
if (tokenDecoded === undefined) {
console.error('Empty token found.');
reject(new Error('Empty token found.'));
return;
}
if (!this.isValidToken(tokenInterface)) {
reject(new Error('Authentication error, invalid token structure.'));
return;
}
resolve(tokenInterface.userUuid);
});
});
}
private isValidToken(token: object): token is TokenInterface {
return !(typeof((token as TokenInterface).userUuid) !== 'string');
}
}
export const jwtTokenManager = new JWTTokenManager();

View File

@ -1,5 +1,5 @@
import "jasmine"; import "jasmine";
import {World, ConnectCallback, DisconnectCallback } from "../src/Model/World"; import {GameRoom, ConnectCallback, DisconnectCallback } from "_Model/GameRoom";
import {Point} from "../src/Model/Websocket/MessageUserPosition"; import {Point} from "../src/Model/Websocket/MessageUserPosition";
import { Group } from "../src/Model/Group"; import { Group } from "../src/Model/Group";
import {PositionNotifier} from "../src/Model/PositionNotifier"; import {PositionNotifier} from "../src/Model/PositionNotifier";

View File

@ -1,5 +1,5 @@
import "jasmine"; import "jasmine";
import {World, ConnectCallback, DisconnectCallback } from "../src/Model/World"; import {GameRoom, ConnectCallback, DisconnectCallback } from "../src/Model/GameRoom";
import {Point} from "../src/Model/Websocket/MessageUserPosition"; import {Point} from "../src/Model/Websocket/MessageUserPosition";
import { Group } from "../src/Model/Group"; import { Group } from "../src/Model/Group";
import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface"; import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
@ -21,7 +21,7 @@ describe("World", () => {
} }
const world = new World(connect, disconnect, 160, 160, () => {}, () => {}, () => {}); const world = new GameRoom(connect, disconnect, 160, 160, () => {}, () => {}, () => {});
world.join(createMockUser(1), new Point(100, 100)); world.join(createMockUser(1), new Point(100, 100));
@ -48,7 +48,7 @@ describe("World", () => {
} }
const world = new World(connect, disconnect, 160, 160, () => {}, () => {}, () => {}); const world = new GameRoom(connect, disconnect, 160, 160, () => {}, () => {}, () => {});
world.join(createMockUser(1), new Point(100, 100)); world.join(createMockUser(1), new Point(100, 100));
@ -77,7 +77,7 @@ describe("World", () => {
disconnectCallNumber++; disconnectCallNumber++;
} }
const world = new World(connect, disconnect, 160, 160, () => {}, () => {}, () => {}); const world = new GameRoom(connect, disconnect, 160, 160, () => {}, () => {}, () => {});
world.join(createMockUser(1), new Point(100, 100)); world.join(createMockUser(1), new Point(100, 100));

View File

@ -6,12 +6,6 @@
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge"> <meta http-equiv="X-UA-Compatible" content="ie=edge">
<!-- Include stylesheet -->
<link href="https://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet">
<!-- Include the Quill library -->
<script src="https://cdn.quilljs.com/1.3.6/quill.js"></script>
<!-- Global site tag (gtag.js) - Google Analytics --> <!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-10196481-11"></script> <script async src="https://www.googletagmanager.com/gtag/js?id=UA-10196481-11"></script>
<script> <script>

View File

@ -6,6 +6,7 @@
"devDependencies": { "devDependencies": {
"@types/google-protobuf": "^3.7.3", "@types/google-protobuf": "^3.7.3",
"@types/jasmine": "^3.5.10", "@types/jasmine": "^3.5.10",
"@types/quill": "^1.3.7",
"@typescript-eslint/eslint-plugin": "^2.26.0", "@typescript-eslint/eslint-plugin": "^2.26.0",
"@typescript-eslint/parser": "^2.26.0", "@typescript-eslint/parser": "^2.26.0",
"eslint": "^6.8.0", "eslint": "^6.8.0",
@ -20,14 +21,14 @@
"webpack-merge": "^4.2.2" "webpack-merge": "^4.2.2"
}, },
"dependencies": { "dependencies": {
"@types/axios": "^0.14.0",
"@types/simple-peer": "^9.6.0", "@types/simple-peer": "^9.6.0",
"@types/socket.io-client": "^1.4.32", "@types/socket.io-client": "^1.4.32",
"axios": "^0.20.0",
"generic-type-guard": "^3.2.0", "generic-type-guard": "^3.2.0",
"google-protobuf": "^3.13.0", "google-protobuf": "^3.13.0",
"phaser": "^3.22.0", "phaser": "^3.22.0",
"queue-typescript": "^1.0.1", "queue-typescript": "^1.0.1",
"quill": "1.3.7", "quill": "^1.3.7",
"simple-peer": "^9.6.2", "simple-peer": "^9.6.2",
"socket.io-client": "^2.3.0", "socket.io-client": "^2.3.0",
"webpack-require-http": "^0.4.3" "webpack-require-http": "^0.4.3"

View File

@ -1,7 +1,3 @@
import {GameScene} from "../Phaser/Game/GameScene";
const Quill = require("quill");
import {HtmlUtils} from "../WebRtc/HtmlUtils"; import {HtmlUtils} from "../WebRtc/HtmlUtils";
import {UserInputManager} from "../Phaser/UserInput/UserInputManager"; import {UserInputManager} from "../Phaser/UserInput/UserInputManager";
import {RoomConnection} from "../Connexion/RoomConnection"; import {RoomConnection} from "../Connexion/RoomConnection";
@ -10,7 +6,6 @@ import {PlayGlobalMessageInterface} from "../Connexion/ConnexionModels";
export const CLASS_CONSOLE_MESSAGE = 'main-console'; export const CLASS_CONSOLE_MESSAGE = 'main-console';
export const INPUT_CONSOLE_MESSAGE = 'input-send-text'; export const INPUT_CONSOLE_MESSAGE = 'input-send-text';
export const UPLOAD_CONSOLE_MESSAGE = 'input-upload-music'; export const UPLOAD_CONSOLE_MESSAGE = 'input-upload-music';
export const BUTTON_CONSOLE_SEND = 'button-send';
export const INPUT_TYPE_CONSOLE = 'input-type'; export const INPUT_TYPE_CONSOLE = 'input-type';
export const AUDIO_TYPE = 'audio'; export const AUDIO_TYPE = 'audio';
@ -26,6 +21,7 @@ export class ConsoleGlobalMessageManager {
private buttonMainConsole: HTMLDivElement; private buttonMainConsole: HTMLDivElement;
private activeConsole: boolean = false; private activeConsole: boolean = false;
private userInputManager!: UserInputManager; private userInputManager!: UserInputManager;
private static cssLoaded: boolean = false;
constructor(private Connection: RoomConnection, userInputManager : UserInputManager) { constructor(private Connection: RoomConnection, userInputManager : UserInputManager) {
this.buttonMainConsole = document.createElement('div'); this.buttonMainConsole = document.createElement('div');
@ -123,24 +119,30 @@ export class ConsoleGlobalMessageManager {
section.appendChild(buttonDiv); section.appendChild(buttonDiv);
this.divMainConsole.appendChild(section); this.divMainConsole.appendChild(section);
//TODO refactor (async () => {
setTimeout(() => { // Start loading CSS
const cssPromise = ConsoleGlobalMessageManager.loadCss();
// Import quill
const Quill:any = await import("quill"); // eslint-disable-line @typescript-eslint/no-explicit-any
// Wait for CSS to be loaded
await cssPromise;
const toolbarOptions = [ const toolbarOptions = [
['bold', 'italic', 'underline', 'strike'], // toggled buttons ['bold', 'italic', 'underline', 'strike'], // toggled buttons
['blockquote', 'code-block'], ['blockquote', 'code-block'],
[{ 'header': 1 }, { 'header': 2 }], // custom button values [{'header': 1}, {'header': 2}], // custom button values
[{ 'list': 'ordered'}, { 'list': 'bullet' }], [{'list': 'ordered'}, {'list': 'bullet'}],
[{ 'script': 'sub'}, { 'script': 'super' }], // superscript/subscript [{'script': 'sub'}, {'script': 'super'}], // superscript/subscript
[{ 'indent': '-1'}, { 'indent': '+1' }], // outdent/indent [{'indent': '-1'}, {'indent': '+1'}], // outdent/indent
[{ 'direction': 'rtl' }], // text direction [{'direction': 'rtl'}], // text direction
[{ 'size': ['small', false, 'large', 'huge'] }], // custom dropdown [{'size': ['small', false, 'large', 'huge']}], // custom dropdown
[{ 'header': [1, 2, 3, 4, 5, 6, false] }], [{'header': [1, 2, 3, 4, 5, 6, false]}],
[{ 'color': [] }, { 'background': [] }], // dropdown with defaults from theme [{'color': []}, {'background': []}], // dropdown with defaults from theme
[{ 'font': [] }], [{'font': []}],
[{ 'align': [] }], [{'align': []}],
['clean'], ['clean'],
@ -154,8 +156,28 @@ export class ConsoleGlobalMessageManager {
toolbar: toolbarOptions toolbar: toolbarOptions
}, },
}); });
})();
}
}, 1000); private static loadCss(): Promise<void> {
return new Promise<void>((resolve, reject) => {
if (ConsoleGlobalMessageManager.cssLoaded) {
resolve();
return;
}
const fileref = document.createElement("link")
fileref.setAttribute("rel", "stylesheet")
fileref.setAttribute("type", "text/css")
fileref.setAttribute("href", "https://cdn.quilljs.com/1.3.7/quill.snow.css");
document.getElementsByTagName("head")[0].appendChild(fileref);
ConsoleGlobalMessageManager.cssLoaded = true;
fileref.onload = () => {
resolve();
}
fileref.onerror = () => {
reject();
}
});
} }
createUploadAudioPart(){ createUploadAudioPart(){

View File

@ -1,43 +1,78 @@
import Axios from "axios"; import Axios from "axios";
import {API_URL} from "../Enum/EnvironmentVariable"; import {API_URL} from "../Enum/EnvironmentVariable";
import {RoomConnection} from "./RoomConnection"; import {RoomConnection} from "./RoomConnection";
import {PositionInterface, ViewportInterface} from "./ConnexionModels";
import {GameConnexionTypes, urlManager} from "../Url/UrlManager";
import {localUserStore} from "./LocalUserStore";
import {LocalUser} from "./LocalUser";
import {Room} from "./Room";
interface LoginApiData { const URL_ROOM_STARTED = '/Floor0/floor0.json';
authToken: string
userUuid: string
mapUrlStart: string
newUrl: string
}
class ConnectionManager { class ConnectionManager {
private initPromise!: Promise<LoginApiData>; private localUser!:LocalUser;
private mapUrlStart: string|null = null;
private authToken:string|null = null; /**
private userUuid: string|null = null; * Tries to login to the node server and return the starting map url to be loaded
*/
public async initGameConnexion(): Promise<Room> {
public async init(): Promise<void> { const connexionType = urlManager.getGameConnexionType();
const match = /\/register\/(.+)/.exec(window.location.toString()); if(connexionType === GameConnexionTypes.register) {
const organizationMemberToken = match ? match[1] : null; const organizationMemberToken = urlManager.getOrganizationToken();
this.initPromise = Axios.post(`${API_URL}/login`, {organizationMemberToken}).then(res => res.data); const data = await Axios.post(`${API_URL}/register`, {organizationMemberToken}).then(res => res.data);
const data = await this.initPromise this.localUser = new LocalUser(data.userUuid, data.authToken);
this.authToken = data.authToken; localUserStore.saveUser(this.localUser);
this.userUuid = data.userUuid;
this.mapUrlStart = data.mapUrlStart;
const newUrl = data.newUrl;
if (newUrl) { const organizationSlug = data.organizationSlug;
history.pushState({}, '', newUrl); const worldSlug = data.worldSlug;
const roomSlug = data.roomSlug;
urlManager.editUrlForRoom(roomSlug, organizationSlug, worldSlug);
const room = new Room(window.location.pathname);
return Promise.resolve(room);
} else if (connexionType === GameConnexionTypes.anonymous || connexionType === GameConnexionTypes.empty) {
const localUser = localUserStore.getLocalUser();
if (localUser && localUser.jwtToken && localUser.uuid) {
this.localUser = localUser
} else {
const data = await Axios.post(`${API_URL}/anonymLogin`).then(res => res.data);
this.localUser = new LocalUser(data.userUuid, data.authToken);
localUserStore.saveUser(this.localUser);
}
let roomId: string
if (connexionType === GameConnexionTypes.empty) {
const defaultMapUrl = window.location.host.replace('play.', 'maps.') + URL_ROOM_STARTED;
roomId = urlManager.editUrlForRoom(defaultMapUrl, null, null);
} else {
roomId = window.location.pathname;
}
const room = new Room(roomId);
return Promise.resolve(room);
} else if (connexionType == GameConnexionTypes.organization) {
const localUser = localUserStore.getLocalUser();
if (localUser) {
this.localUser = localUser
const room = new Room(window.location.pathname);
return Promise.resolve(room);
} else {
//todo: find some kind of fallback?
return Promise.reject('Could not find a user in localstorage');
}
} }
return Promise.reject('Invalid URL');
} }
public initBenchmark(): void { public initBenchmark(): void {
this.authToken = 'test'; this.localUser = new LocalUser('', 'test');
} }
public connectToRoomSocket(): Promise<RoomConnection> { public connectToRoomSocket(roomId: string, name: string, characterLayers: string[], position: PositionInterface, viewport: ViewportInterface): Promise<RoomConnection> {
return new Promise<RoomConnection>((resolve, reject) => { return new Promise<RoomConnection>((resolve, reject) => {
const connection = new RoomConnection(this.authToken as string); const connection = new RoomConnection(this.localUser.jwtToken, roomId, name, characterLayers, position, viewport);
connection.onConnectError((error: object) => { connection.onConnectError((error: object) => {
console.log('An error occurred while connecting to socket server. Retrying'); console.log('An error occurred while connecting to socket server. Retrying');
reject(error); reject(error);
@ -50,20 +85,11 @@ class ConnectionManager {
return new Promise<RoomConnection>((resolve, reject) => { return new Promise<RoomConnection>((resolve, reject) => {
setTimeout(() => { setTimeout(() => {
//todo: allow a way to break recurrsion? //todo: allow a way to break recurrsion?
this.connectToRoomSocket().then((connection) => resolve(connection)); this.connectToRoomSocket(roomId, name, characterLayers, position, viewport).then((connection) => resolve(connection));
}, 4000 + Math.floor(Math.random() * 2000) ); }, 4000 + Math.floor(Math.random() * 2000) );
}); });
}); });
} }
public getMapUrlStart(): Promise<string> {
return this.initPromise.then(() => {
if (!this.mapUrlStart) {
throw new Error('No map url set!');
}
return this.mapUrlStart;
})
}
} }
export const connectionManager = new ConnectionManager(); export const connectionManager = new ConnectionManager();

View File

@ -6,6 +6,7 @@ export enum EventMessage{
WEBRTC_SIGNAL = "webrtc-signal", WEBRTC_SIGNAL = "webrtc-signal",
WEBRTC_SCREEN_SHARING_SIGNAL = "webrtc-screen-sharing-signal", WEBRTC_SCREEN_SHARING_SIGNAL = "webrtc-screen-sharing-signal",
WEBRTC_START = "webrtc-start", WEBRTC_START = "webrtc-start",
START_ROOM = "start-room", // From server to client: list of all room users/groups/items
JOIN_ROOM = "join-room", // bi-directional JOIN_ROOM = "join-room", // bi-directional
USER_POSITION = "user-position", // From client to server USER_POSITION = "user-position", // From client to server
USER_MOVED = "user-moved", // From server to client USER_MOVED = "user-moved", // From server to client

View File

@ -0,0 +1,9 @@
export class LocalUser {
public uuid: string;
public jwtToken: string;
constructor(uuid:string, jwtToken: string) {
this.uuid = uuid;
this.jwtToken = jwtToken;
}
}

View File

@ -0,0 +1,16 @@
import {LocalUser} from "./LocalUser";
class LocalUserStore {
saveUser(localUser: LocalUser) {
localStorage.setItem('localUser', JSON.stringify(localUser));
}
getLocalUser(): LocalUser|null {
const data = localStorage.getItem('localUser');
return data ? JSON.parse(data) : null;
}
}
export const localUserStore = new LocalUserStore();

View File

@ -0,0 +1,91 @@
import Axios from "axios";
import {API_URL} from "../Enum/EnvironmentVariable";
export class Room {
public readonly id: string;
public readonly isPublic: boolean;
private mapUrl: string|undefined;
private instance: string|undefined;
constructor(id: string) {
if (id.startsWith('/')) {
id = id.substr(1);
}
this.id = id;
if (id.startsWith('_/')) {
this.isPublic = true;
} else if (id.startsWith('@/')) {
this.isPublic = false;
} else {
throw new Error('Invalid room ID');
}
}
public async getMapUrl(): Promise<string> {
return new Promise<string>((resolve, reject) => {
if (this.mapUrl !== undefined) {
resolve(this.mapUrl);
return;
}
if (this.isPublic) {
const match = /_\/[^/]+\/(.+)/.exec(this.id);
if (!match) throw new Error('Could not extract url from "'+this.id+'"');
this.mapUrl = window.location.protocol+'//'+match[1];
resolve(this.mapUrl);
return;
} else {
// We have a private ID, we need to query the map URL from the server.
const urlParts = this.parsePrivateUrl(this.id);
Axios.get(`${API_URL}/map`, {
params: urlParts
}).then(({data}) => {
console.log('Map ', this.id, ' resolves to URL ', data.mapUrl);
resolve(data.mapUrl);
return;
});
}
});
}
/**
* Instance name is:
* - In a public URL: the second part of the URL ( _/[instance]/map.json)
* - In a private URL: [organizationId/worldId]
*/
public getInstance(): string {
if (this.instance !== undefined) {
return this.instance;
}
if (this.isPublic) {
const match = /_\/([^/]+)\/.+/.exec(this.id);
if (!match) throw new Error('Could not extract instance from "'+this.id+'"');
this.instance = match[1];
return this.instance;
} else {
const match = /_\/([^/]+)\/([^/]+)\/.+/.exec(this.id);
if (!match) throw new Error('Could not extract instance from "'+this.id+'"');
this.instance = match[1]+'/'+match[2];
return this.instance;
}
}
private parsePrivateUrl(url: string): { organizationSlug: string, worldSlug: string, roomSlug?: string } {
const regex = /@\/([^/]+)\/([^/]+)(?:\/([^/]*))?/gm;
const match = regex.exec(url);
if (!match) {
throw new Error('Invalid URL '+url);
}
const results: { organizationSlug: string, worldSlug: string, roomSlug?: string } = {
organizationSlug: match[1],
worldSlug: match[2],
}
if (match[3] !== undefined) {
results.roomSlug = match[3];
}
return results;
}
}

View File

@ -6,12 +6,11 @@ import {
GroupDeleteMessage, GroupDeleteMessage,
GroupUpdateMessage, GroupUpdateMessage,
ItemEventMessage, ItemEventMessage,
JoinRoomMessage, PlayGlobalMessage, PlayGlobalMessage,
PositionMessage, PositionMessage,
RoomJoinedMessage, RoomJoinedMessage,
ServerToClientMessage, ServerToClientMessage,
SetPlayerDetailsMessage, SetPlayerDetailsMessage,
SetUserIdMessage,
SilentMessage, StopGlobalMessage, SilentMessage, StopGlobalMessage,
UserJoinedMessage, UserJoinedMessage,
UserLeftMessage, UserLeftMessage,
@ -32,7 +31,7 @@ import {ProtobufClientUtils} from "../Network/ProtobufClientUtils";
import { import {
EventMessage, EventMessage,
GroupCreatedUpdatedMessageInterface, ItemEventMessageInterface, GroupCreatedUpdatedMessageInterface, ItemEventMessageInterface,
MessageUserJoined, PlayGlobalMessageInterface, MessageUserJoined, PlayGlobalMessageInterface, PositionInterface,
RoomJoinedMessageInterface, RoomJoinedMessageInterface,
ViewportInterface, WebRtcDisconnectMessageInterface, ViewportInterface, WebRtcDisconnectMessageInterface,
WebRtcSignalReceivedMessageInterface, WebRtcSignalReceivedMessageInterface,
@ -51,9 +50,26 @@ export class RoomConnection implements RoomConnection {
RoomConnection.websocketFactory = websocketFactory; RoomConnection.websocketFactory = websocketFactory;
} }
public constructor(token: string) { /**
*
* @param token A JWT token containing the UUID of the user
* @param roomId The ID of the room in the form "_/[instance]/[map_url]" or "@/[org]/[event]/[map]"
*/
public constructor(token: string|null, roomId: string, name: string, characterLayers: string[], position: PositionInterface, viewport: ViewportInterface) {
let url = API_URL.replace('http://', 'ws://').replace('https://', 'wss://'); let url = API_URL.replace('http://', 'ws://').replace('https://', 'wss://');
url += '?token='+token; url += '/room';
url += '?roomId='+(roomId ?encodeURIComponent(roomId):'');
url += '&token='+(token ?encodeURIComponent(token):'');
url += '&name='+encodeURIComponent(name);
for (const layer of characterLayers) {
url += '&characterLayers='+encodeURIComponent(layer);
}
url += '&x='+Math.floor(position.x);
url += '&y='+Math.floor(position.y);
url += '&top='+Math.floor(viewport.top);
url += '&bottom='+Math.floor(viewport.bottom);
url += '&left='+Math.floor(viewport.left);
url += '&right='+Math.floor(viewport.right);
if (RoomConnection.websocketFactory) { if (RoomConnection.websocketFactory) {
this.socket = RoomConnection.websocketFactory(url); this.socket = RoomConnection.websocketFactory(url);
@ -109,13 +125,13 @@ export class RoomConnection implements RoomConnection {
items[item.getItemid()] = JSON.parse(item.getStatejson()); items[item.getItemid()] = JSON.parse(item.getStatejson());
} }
this.resolveJoinRoom({ this.userId = roomJoinedMessage.getCurrentuserid();
this.dispatch(EventMessage.START_ROOM, {
users, users,
groups, groups,
items items
}) });
} else if (message.hasSetuseridmessage()) {
this.userId = (message.getSetuseridmessage() as SetUserIdMessage).getUserid();
} else if (message.hasErrormessage()) { } else if (message.hasErrormessage()) {
console.error(EventMessage.MESSAGE_ERROR, message.getErrormessage()?.getMessage()); console.error(EventMessage.MESSAGE_ERROR, message.getErrormessage()?.getMessage());
} else if (message.hasWebrtcsignaltoclientmessage()) { } else if (message.hasWebrtcsignaltoclientmessage()) {
@ -165,29 +181,6 @@ export class RoomConnection implements RoomConnection {
this.closed = true; this.closed = true;
} }
private resolveJoinRoom!: (value?: (RoomJoinedMessageInterface | PromiseLike<RoomJoinedMessageInterface> | undefined)) => void;
public joinARoom(roomId: string, startX: number, startY: number, direction: string, moving: boolean, viewport: ViewportInterface): Promise<RoomJoinedMessageInterface> {
const promise = new Promise<RoomJoinedMessageInterface>((resolve, reject) => {
this.resolveJoinRoom = resolve;
const positionMessage = this.toPositionMessage(startX, startY, direction, moving);
const viewportMessage = this.toViewportMessage(viewport);
const joinRoomMessage = new JoinRoomMessage();
joinRoomMessage.setRoomid(roomId);
joinRoomMessage.setPosition(positionMessage);
joinRoomMessage.setViewport(viewportMessage);
//console.log('Sending position ', positionMessage.getX(), positionMessage.getY());
const clientToServerMessage = new ClientToServerMessage();
clientToServerMessage.setJoinroommessage(joinRoomMessage);
this.socket.send(clientToServerMessage.serializeBinary().buffer);
})
return promise;
}
private toPositionMessage(x : number, y : number, direction : string, moving: boolean): PositionMessage { private toPositionMessage(x : number, y : number, direction : string, moving: boolean): PositionMessage {
const positionMessage = new PositionMessage(); const positionMessage = new PositionMessage();
positionMessage.setX(Math.floor(x)); positionMessage.setX(Math.floor(x));
@ -343,6 +336,13 @@ export class RoomConnection implements RoomConnection {
this.socket.addEventListener('open', callback) this.socket.addEventListener('open', callback)
} }
/**
* Triggered when we receive all the details of a room (users, groups, ...)
*/
public onStartRoom(callback: (event: RoomJoinedMessageInterface) => void): void {
this.onMessage(EventMessage.START_ROOM, callback);
}
public sendWebrtcSignal(signal: unknown, receiverId: number) { public sendWebrtcSignal(signal: unknown, receiverId: number) {
const webRtcSignal = new WebRtcSignalToServerMessage(); const webRtcSignal = new WebRtcSignalToServerMessage();
webRtcSignal.setReceiverid(receiverId); webRtcSignal.setReceiverid(receiverId);

View File

@ -1,10 +1,7 @@
import {GameScene} from "./GameScene"; import {GameScene} from "./GameScene";
import {
StartMapInterface
} from "../../Connexion/ConnexionModels";
import Axios from "axios";
import {API_URL} from "../../Enum/EnvironmentVariable";
import {connectionManager} from "../../Connexion/ConnectionManager"; import {connectionManager} from "../../Connexion/ConnectionManager";
import {Room} from "../../Connexion/Room";
import {FourOFourSceneName} from "../Reconnecting/FourOFourScene";
export interface HasMovedEvent { export interface HasMovedEvent {
direction: string; direction: string;
@ -16,6 +13,12 @@ export interface HasMovedEvent {
export class GameManager { export class GameManager {
private playerName!: string; private playerName!: string;
private characterLayers!: string[]; private characterLayers!: string[];
private startRoom!:Room;
public async init(scenePlugin: Phaser.Scenes.ScenePlugin) {
this.startRoom = await connectionManager.initGameConnexion();
await this.loadMap(this.startRoom, scenePlugin);
}
public setPlayerName(name: string): void { public setPlayerName(name: string): void {
this.playerName = name; this.playerName = name;
@ -29,15 +32,6 @@ export class GameManager {
this.characterLayers = layers; this.characterLayers = layers;
} }
loadStartMap() : Promise<StartMapInterface> {
return connectionManager.getMapUrlStart().then(mapUrlStart => {
return {
mapUrlStart: mapUrlStart,
startInstance: "global", //todo: is this property still usefull?
}
});
}
getPlayerName(): string { getPlayerName(): string {
return this.playerName; return this.playerName;
} }
@ -46,15 +40,31 @@ export class GameManager {
return this.characterLayers; return this.characterLayers;
} }
loadMap(mapUrl: string, scene: Phaser.Scenes.ScenePlugin, instance: string): string {
const sceneKey = GameScene.getMapKeyByUrl(mapUrl);
const gameIndex = scene.getIndex(sceneKey); public async loadMap(room: Room, scenePlugin: Phaser.Scenes.ScenePlugin): Promise<void> {
const roomID = room.id;
const mapUrl = await room.getMapUrl();
console.log('Loading map '+roomID+' at url '+mapUrl);
const gameIndex = scenePlugin.getIndex(mapUrl);
if(gameIndex === -1){ if(gameIndex === -1){
const game : Phaser.Scene = GameScene.createFromUrl(mapUrl, instance); const game : Phaser.Scene = GameScene.createFromUrl(room, mapUrl);
scene.add(sceneKey, game, false); console.log('Adding scene '+mapUrl);
scenePlugin.add(mapUrl, game, false);
} }
return sceneKey; }
public getMapKeyByUrl(mapUrlStart: string) : string {
// FIXME: the key should be computed from the full URL of the map.
const startPos = mapUrlStart.indexOf('://')+3;
const endPos = mapUrlStart.indexOf(".json");
return mapUrlStart.substring(startPos, endPos);
}
public async goToStartingMap(scenePlugin: Phaser.Scenes.ScenePlugin) {
const url = await this.startRoom.getMapUrl();
console.log('Starting scene '+url);
scenePlugin.start(url, {startLayerName: 'global'});
} }
} }

View File

@ -45,6 +45,7 @@ import {RoomConnection} from "../../Connexion/RoomConnection";
import {GlobalMessageManager} from "../../Administration/GlobalMessageManager"; import {GlobalMessageManager} from "../../Administration/GlobalMessageManager";
import {ConsoleGlobalMessageManager} from "../../Administration/ConsoleGlobalMessageManager"; import {ConsoleGlobalMessageManager} from "../../Administration/ConsoleGlobalMessageManager";
import {ResizableScene} from "../Login/ResizableScene"; import {ResizableScene} from "../Login/ResizableScene";
import {Room} from "../../Connexion/Room";
export enum Textures { export enum Textures {
@ -107,7 +108,6 @@ export class GameScene extends ResizableScene implements CenterListener {
private simplePeer!: SimplePeer; private simplePeer!: SimplePeer;
private GlobalMessageManager!: GlobalMessageManager; private GlobalMessageManager!: GlobalMessageManager;
private ConsoleGlobalMessageManager!: ConsoleGlobalMessageManager; private ConsoleGlobalMessageManager!: ConsoleGlobalMessageManager;
private connectionPromise!: Promise<RoomConnection>
private connectionAnswerPromise: Promise<RoomJoinedMessageInterface>; private connectionAnswerPromise: Promise<RoomJoinedMessageInterface>;
private connectionAnswerPromiseResolve!: (value?: RoomJoinedMessageInterface | PromiseLike<RoomJoinedMessageInterface>) => void; private connectionAnswerPromiseResolve!: (value?: RoomJoinedMessageInterface | PromiseLike<RoomJoinedMessageInterface>) => void;
// A promise that will resolve when the "create" method is called (signaling loading is ended) // A promise that will resolve when the "create" method is called (signaling loading is ended)
@ -138,27 +138,27 @@ export class GameScene extends ResizableScene implements CenterListener {
private outlinedItem: ActionableItem|null = null; private outlinedItem: ActionableItem|null = null;
private userInputManager!: UserInputManager; private userInputManager!: UserInputManager;
static createFromUrl(mapUrlFile: string, instance: string, key: string|null = null): GameScene { static createFromUrl(room: Room, mapUrlFile: string, gameSceneKey: string|null = null): GameScene {
const mapKey = GameScene.getMapKeyByUrl(mapUrlFile); // We use the map URL as a key
if (key === null) { if (gameSceneKey === null) {
key = mapKey; gameSceneKey = mapUrlFile;
} }
return new GameScene(mapKey, mapUrlFile, instance, key); return new GameScene(room, mapUrlFile, gameSceneKey);
} }
constructor(MapKey : string, MapUrlFile: string, instance: string, key: string) { constructor(private room: Room, MapUrlFile: string, gameSceneKey: string) {
super({ super({
key: key key: gameSceneKey
}); });
this.GameManager = gameManager; this.GameManager = gameManager;
this.Terrains = []; this.Terrains = [];
this.groups = new Map<number, Sprite>(); this.groups = new Map<number, Sprite>();
this.instance = instance; this.instance = room.getInstance();
this.MapKey = MapKey; this.MapKey = MapUrlFile;
this.MapUrlFile = MapUrlFile; this.MapUrlFile = MapUrlFile;
this.RoomId = this.instance + '__' + MapKey; this.RoomId = room.id;
this.createPromise = new Promise<void>((resolve, reject): void => { this.createPromise = new Promise<void>((resolve, reject): void => {
this.createPromiseResolve = resolve; this.createPromiseResolve = resolve;
@ -193,110 +193,6 @@ export class GameScene extends ResizableScene implements CenterListener {
loadObject(this.load); loadObject(this.load);
this.load.bitmapFont('main_font', 'resources/fonts/arcade.png', 'resources/fonts/arcade.xml'); this.load.bitmapFont('main_font', 'resources/fonts/arcade.png', 'resources/fonts/arcade.xml');
this.connectionPromise = connectionManager.connectToRoomSocket().then((connection : RoomConnection) => {
this.connection = connection;
this.connection.emitPlayerDetailsMessage(gameManager.getPlayerName(), gameManager.getCharacterSelected())
connection.onUserJoins((message: MessageUserJoined) => {
const userMessage: AddPlayerInterface = {
userId: message.userId,
characterLayers: message.characterLayers,
name: message.name,
position: message.position
}
this.addPlayer(userMessage);
});
connection.onUserMoved((message: UserMovedMessage) => {
const position = message.getPosition();
if (position === undefined) {
throw new Error('Position missing from UserMovedMessage');
}
//console.log('Received position ', position.getX(), position.getY(), "from user", message.getUserid());
const messageUserMoved: MessageUserMovedInterface = {
userId: message.getUserid(),
position: ProtobufClientUtils.toPointInterface(position)
}
this.updatePlayerPosition(messageUserMoved);
});
connection.onUserLeft((userId: number) => {
this.removePlayer(userId);
});
connection.onGroupUpdatedOrCreated((groupPositionMessage: GroupCreatedUpdatedMessageInterface) => {
this.shareGroupPosition(groupPositionMessage);
})
connection.onGroupDeleted((groupId: number) => {
try {
this.deleteGroup(groupId);
} catch (e) {
console.error(e);
}
})
connection.onServerDisconnected(() => {
console.log('Player disconnected from server. Reloading scene.');
this.simplePeer.closeAllConnections();
this.simplePeer.unregister();
const key = 'somekey'+Math.round(Math.random()*10000);
const game : Phaser.Scene = GameScene.createFromUrl(this.MapUrlFile, this.instance, key);
this.scene.add(key, game, true,
{
initPosition: {
x: this.CurrentPlayer.x,
y: this.CurrentPlayer.y
}
});
this.scene.stop(this.scene.key);
this.scene.remove(this.scene.key);
})
connection.onActionableEvent((message => {
const item = this.actionableItems.get(message.itemId);
if (item === undefined) {
console.warn('Received an event about object "'+message.itemId+'" but cannot find this item on the map.');
return;
}
item.fire(message.event, message.state, message.parameters);
}));
connection.receiveTeleportMessage((map: string) => {
//TODO
console.log('receiveTeleportMessage', map);
})
// When connection is performed, let's connect SimplePeer
this.simplePeer = new SimplePeer(this.connection);
this.GlobalMessageManager = new GlobalMessageManager(this.connection);
const self = this;
this.simplePeer.registerPeerConnectionListener({
onConnect(user: UserSimplePeerInterface) {
self.presentationModeSprite.setVisible(true);
self.chatModeSprite.setVisible(true);
},
onDisconnect(userId: number) {
if (self.simplePeer.getNbConnections() === 0) {
self.presentationModeSprite.setVisible(false);
self.chatModeSprite.setVisible(false);
}
}
})
this.scene.wake();
this.scene.sleep(ReconnectingSceneName);
return connection;
});
} }
// FIXME: we need to put a "unknown" instead of a "any" and validate the structure of the JSON we are receiving. // FIXME: we need to put a "unknown" instead of a "any" and validate the structure of the JSON we are receiving.
@ -422,7 +318,7 @@ export class GameScene extends ResizableScene implements CenterListener {
}); });
//permit to set bound collision //permit to set bound collision
this.physics.world.setBounds(0,0, this.Map.widthInPixels, this.Map.heightInPixels); this.physics.world.setBounds(0, 0, this.Map.widthInPixels, this.Map.heightInPixels);
//add layer on map //add layer on map
this.Layers = new Array<Phaser.Tilemaps.StaticTilemapLayer>(); this.Layers = new Array<Phaser.Tilemaps.StaticTilemapLayer>();
@ -483,7 +379,7 @@ export class GameScene extends ResizableScene implements CenterListener {
this.EventToClickOnTile(); this.EventToClickOnTile();
//initialise list of other player //initialise list of other player
this.MapPlayers = this.physics.add.group({ immovable: true }); this.MapPlayers = this.physics.add.group({immovable: true});
//create input to move //create input to move
this.userInputManager = new UserInputManager(this); this.userInputManager = new UserInputManager(this);
@ -499,7 +395,7 @@ export class GameScene extends ResizableScene implements CenterListener {
// Let's generate the circle for the group delimiter // Let's generate the circle for the group delimiter
const circleElement = Object.values(this.textures.list).find((object: Texture) => object.key === 'circleSprite'); const circleElement = Object.values(this.textures.list).find((object: Texture) => object.key === 'circleSprite');
if(circleElement) { if (circleElement) {
this.textures.remove('circleSprite'); this.textures.remove('circleSprite');
} }
this.circleTexture = this.textures.createCanvas('circleSprite', 96, 96); this.circleTexture = this.textures.createCanvas('circleSprite', 96, 96);
@ -511,14 +407,6 @@ export class GameScene extends ResizableScene implements CenterListener {
context.stroke(); context.stroke();
this.circleTexture.refresh(); this.circleTexture.refresh();
// Let's alter browser history
const url = new URL(this.MapUrlFile);
let path = '/_/'+this.instance+'/'+url.host+url.pathname;
if (this.startLayerName) {
path += '#'+this.startLayerName;
}
window.history.pushState({}, 'WorkAdventure', path);
// Let's pause the scene if the connection is not established yet // Let's pause the scene if the connection is not established yet
if (this.connection === undefined) { if (this.connection === undefined) {
// Let's wait 0.5 seconds before printing the "connecting" screen to avoid blinking // Let's wait 0.5 seconds before printing the "connecting" screen to avoid blinking
@ -532,7 +420,7 @@ export class GameScene extends ResizableScene implements CenterListener {
this.createPromiseResolve(); this.createPromiseResolve();
this.userInputManager.spaceEvent( () => { this.userInputManager.spaceEvent(() => {
this.outlinedItem?.activate(); this.outlinedItem?.activate();
}); });
@ -607,8 +495,131 @@ export class GameScene extends ResizableScene implements CenterListener {
} }
}); });
//lisen event to report user const camera = this.cameras.main;
this.onReportUser();
connectionManager.connectToRoomSocket(
this.RoomId,
gameManager.getPlayerName(),
gameManager.getCharacterSelected(),
{
x: this.startX,
y: this.startY
},
{
left: camera.scrollX,
top: camera.scrollY,
right: camera.scrollX + camera.width,
bottom: camera.scrollY + camera.height,
}).then((connection: RoomConnection) => {
this.connection = connection;
//this.connection.emitPlayerDetailsMessage(gameManager.getPlayerName(), gameManager.getCharacterSelected())
connection.onStartRoom((roomJoinedMessage: RoomJoinedMessageInterface) => {
this.initUsersPosition(roomJoinedMessage.users);
this.connectionAnswerPromiseResolve(roomJoinedMessage);
});
connection.onUserJoins((message: MessageUserJoined) => {
const userMessage: AddPlayerInterface = {
userId: message.userId,
characterLayers: message.characterLayers,
name: message.name,
position: message.position
}
this.addPlayer(userMessage);
});
connection.onUserMoved((message: UserMovedMessage) => {
const position = message.getPosition();
if (position === undefined) {
throw new Error('Position missing from UserMovedMessage');
}
//console.log('Received position ', position.getX(), position.getY(), "from user", message.getUserid());
const messageUserMoved: MessageUserMovedInterface = {
userId: message.getUserid(),
position: ProtobufClientUtils.toPointInterface(position)
}
this.updatePlayerPosition(messageUserMoved);
});
connection.onUserLeft((userId: number) => {
this.removePlayer(userId);
});
connection.onGroupUpdatedOrCreated((groupPositionMessage: GroupCreatedUpdatedMessageInterface) => {
this.shareGroupPosition(groupPositionMessage);
})
connection.onGroupDeleted((groupId: number) => {
try {
this.deleteGroup(groupId);
} catch (e) {
console.error(e);
}
})
connection.onServerDisconnected(() => {
console.log('Player disconnected from server. Reloading scene.');
this.simplePeer.closeAllConnections();
this.simplePeer.unregister();
const gameSceneKey = 'somekey' + Math.round(Math.random() * 10000);
const game: Phaser.Scene = GameScene.createFromUrl(this.room, this.MapUrlFile, gameSceneKey);
this.scene.add(gameSceneKey, game, true,
{
initPosition: {
x: this.CurrentPlayer.x,
y: this.CurrentPlayer.y
}
});
this.scene.stop(this.scene.key);
this.scene.remove(this.scene.key);
})
connection.onActionableEvent((message => {
const item = this.actionableItems.get(message.itemId);
if (item === undefined) {
console.warn('Received an event about object "' + message.itemId + '" but cannot find this item on the map.');
return;
}
item.fire(message.event, message.state, message.parameters);
}));
// When connection is performed, let's connect SimplePeer
this.simplePeer = new SimplePeer(this.connection);
this.GlobalMessageManager = new GlobalMessageManager(this.connection);
const self = this;
this.simplePeer.registerPeerConnectionListener({
onConnect(user: UserSimplePeerInterface) {
self.presentationModeSprite.setVisible(true);
self.chatModeSprite.setVisible(true);
},
onDisconnect(userId: number) {
if (self.simplePeer.getNbConnections() === 0) {
self.presentationModeSprite.setVisible(false);
self.chatModeSprite.setVisible(false);
}
}
})
//listen event to share position of user
this.CurrentPlayer.on(hasMovedEventName, this.pushPlayerPosition.bind(this))
this.CurrentPlayer.on(hasMovedEventName, this.outlineItem.bind(this))
this.CurrentPlayer.on(hasMovedEventName, (event: HasMovedEvent) => {
this.gameMap.setPosition(event.x, event.y);
})
this.scene.wake();
this.scene.sleep(ReconnectingSceneName);
return connection;
});
} }
private switchLayoutMode(): void { private switchLayoutMode(): void {
@ -655,6 +666,7 @@ export class GameScene extends ResizableScene implements CenterListener {
* @param tileWidth * @param tileWidth
* @param tileHeight * @param tileHeight
*/ */
//todo: push that into the gameManager
private loadNextGame(layer: ITiledMapLayer, mapWidth: number, tileWidth: number, tileHeight: number){ private loadNextGame(layer: ITiledMapLayer, mapWidth: number, tileWidth: number, tileHeight: number){
const exitSceneUrl = this.getExitSceneUrl(layer); const exitSceneUrl = this.getExitSceneUrl(layer);
if (exitSceneUrl === undefined) { if (exitSceneUrl === undefined) {
@ -665,9 +677,20 @@ export class GameScene extends ResizableScene implements CenterListener {
instance = this.instance; instance = this.instance;
} }
console.log('existSceneUrl', exitSceneUrl);
console.log('existSceneInstance', instance);
// TODO: eventually compute a relative URL // TODO: eventually compute a relative URL
// TODO: handle /@/ URL CASES!
const absoluteExitSceneUrl = new URL(exitSceneUrl, this.MapUrlFile).href; const absoluteExitSceneUrl = new URL(exitSceneUrl, this.MapUrlFile).href;
const exitSceneKey = gameManager.loadMap(absoluteExitSceneUrl, this.scene, instance); const absoluteExitSceneUrlWithoutProtocol = absoluteExitSceneUrl.toString().substr(absoluteExitSceneUrl.toString().indexOf('://')+3);
const roomId = '_/'+instance+'/'+absoluteExitSceneUrlWithoutProtocol;
console.log("Foo", instance, absoluteExitSceneUrlWithoutProtocol);
const room = new Room(roomId);
gameManager.loadMap(room, this.scene);
const exitSceneKey = roomId;
const tiles : number[] = layer.data as number[]; const tiles : number[] = layer.data as number[];
for (let key=0; key < tiles.length; key++) { for (let key=0; key < tiles.length; key++) {
@ -688,10 +711,12 @@ export class GameScene extends ResizableScene implements CenterListener {
if (this.PositionNextScene[y] === undefined) { if (this.PositionNextScene[y] === undefined) {
this.PositionNextScene[y] = new Array<{key: string, hash: string}>(); this.PositionNextScene[y] = new Array<{key: string, hash: string}>();
} }
this.PositionNextScene[y][x] = { room.getMapUrl().then((url: string) => {
key: exitSceneKey, this.PositionNextScene[y][x] = {
hash key: url,
} hash
}
})
} }
} }
@ -754,14 +779,6 @@ export class GameScene extends ResizableScene implements CenterListener {
}); });
} }
createCollisionObject(){
/*this.Objects.forEach((Object : Phaser.Physics.Arcade.Sprite) => {
this.physics.add.collider(this.CurrentPlayer, Object, (object1, object2) => {
this.CurrentPlayer.say("Collision with object : " + (object2 as Phaser.Physics.Arcade.Sprite).texture.key)
});
})*/
}
createCurrentPlayer(){ createCurrentPlayer(){
//initialise player //initialise player
//TODO create animation moving between exit and start //TODO create animation moving between exit and start
@ -778,33 +795,6 @@ export class GameScene extends ResizableScene implements CenterListener {
//create collision //create collision
this.createCollisionWithPlayer(); this.createCollisionWithPlayer();
this.createCollisionObject();
//join room
this.connectionPromise.then((connection: RoomConnection) => {
const camera = this.cameras.main;
connection.joinARoom(this.RoomId,
this.startX,
this.startY,
PlayerAnimationNames.WalkDown,
false, {
left: camera.scrollX,
top: camera.scrollY,
right: camera.scrollX + camera.width,
bottom: camera.scrollY + camera.height,
}).then((roomJoinedMessage: RoomJoinedMessageInterface) => {
this.initUsersPosition(roomJoinedMessage.users);
this.connectionAnswerPromiseResolve(roomJoinedMessage);
});
// FIXME: weirdly enough we don't use the result of joinARoom !!!!!!
//listen event to share position of user
this.CurrentPlayer.on(hasMovedEventName, this.pushPlayerPosition.bind(this))
this.CurrentPlayer.on(hasMovedEventName, this.outlineItem.bind(this))
this.CurrentPlayer.on(hasMovedEventName, (event: HasMovedEvent) => {
this.gameMap.setPosition(event.x, event.y);
})
});
} }
pushPlayerPosition(event: HasMovedEvent) { pushPlayerPosition(event: HasMovedEvent) {
@ -974,7 +964,6 @@ export class GameScene extends ResizableScene implements CenterListener {
type: "InitUserPositionEvent", type: "InitUserPositionEvent",
event: usersPosition event: usersPosition
}); });
} }
/** /**
@ -1128,12 +1117,7 @@ export class GameScene extends ResizableScene implements CenterListener {
this.groups.delete(groupId); this.groups.delete(groupId);
} }
public static getMapKeyByUrl(mapUrlStart: string) : string {
// FIXME: the key should be computed from the full URL of the map.
const startPos = mapUrlStart.indexOf('://')+3;
const endPos = mapUrlStart.indexOf(".json");
return mapUrlStart.substring(startPos, endPos);
}
/** /**
* Sends to the server an event emitted by one of the ActionableItems. * Sends to the server an event emitted by one of the ActionableItems.
@ -1189,10 +1173,4 @@ export class GameScene extends ResizableScene implements CenterListener {
public onCenterChange(): void { public onCenterChange(): void {
this.updateCameraOffset(); this.updateCameraOffset();
} }
public onReportUser(){
this.events.on('reportUser', (message: {reportedUserId: number, reportComment: string}) => {
this.connection.emitReportPlayerMessage(message.reportedUserId, message.reportComment);
});
}
} }

View File

@ -94,7 +94,7 @@ export class EnableCameraScene extends Phaser.Scene {
this.add.existing(this.logo); this.add.existing(this.logo);
this.input.keyboard.on('keyup-ENTER', () => { this.input.keyboard.on('keyup-ENTER', () => {
return this.login(); this.login();
}); });
this.getElementByIdOrFail<HTMLDivElement>('webRtcSetup').classList.add('active'); this.getElementByIdOrFail<HTMLDivElement>('webRtcSetup').classList.add('active');
@ -258,7 +258,7 @@ export class EnableCameraScene extends Phaser.Scene {
this.soundMeterSprite.setVolume(this.soundMeter.getVolume()); this.soundMeterSprite.setVolume(this.soundMeter.getVolume());
} }
private async login(): Promise<StartMapInterface> { private login(): void {
this.getElementByIdOrFail<HTMLDivElement>('webRtcSetup').style.display = 'none'; this.getElementByIdOrFail<HTMLDivElement>('webRtcSetup').style.display = 'none';
this.soundMeter.stop(); this.soundMeter.stop();
window.removeEventListener('resize', this.repositionCallback); window.removeEventListener('resize', this.repositionCallback);
@ -266,46 +266,7 @@ export class EnableCameraScene extends Phaser.Scene {
mediaManager.stopCamera(); mediaManager.stopCamera();
mediaManager.stopMicrophone(); mediaManager.stopMicrophone();
// Do we have a start URL in the address bar? If so, let's redirect to this address gameManager.goToStartingMap(this.scene);
const instanceAndMapUrl = this.findMapUrl();
if (instanceAndMapUrl !== null) {
const [mapUrl, instance] = instanceAndMapUrl;
const key = gameManager.loadMap(mapUrl, this.scene, instance);
this.scene.start(key, {
startLayerName: window.location.hash ? window.location.hash.substr(1) : undefined
} as GameSceneInitInterface);
return {
mapUrlStart: mapUrl,
startInstance: instance
};
} else {
// If we do not have a map address in the URL, let's ask the server for a start map.
return gameManager.loadStartMap().then((startMap: StartMapInterface) => {
const key = gameManager.loadMap(window.location.protocol + "//" + startMap.mapUrlStart, this.scene, startMap.startInstance);
this.scene.start(key);
return startMap;
}).catch((err) => {
console.error(err);
throw err;
});
}
}
/**
* Returns the map URL and the instance from the current URL
*/
private findMapUrl(): [string, string]|null {
const path = window.location.pathname;
if (!path.startsWith('/_/')) {
return null;
}
const instanceAndMap = path.substr(3);
const firstSlash = instanceAndMap.indexOf('/');
if (firstSlash === -1) {
return null;
}
const instance = instanceAndMap.substr(0, firstSlash);
return [window.location.protocol+'//'+instanceAndMap.substr(firstSlash+1), instance];
} }
private async getDevices() { private async getDevices() {

View File

@ -0,0 +1,40 @@
import {gameManager} from "../Game/GameManager";
import {TextField} from "../Components/TextField";
import {TextInput} from "../Components/TextInput";
import {ClickButton} from "../Components/ClickButton";
import Image = Phaser.GameObjects.Image;
import Rectangle = Phaser.GameObjects.Rectangle;
import {PLAYER_RESOURCES, PlayerResourceDescriptionInterface} from "../Entity/Character";
import {cypressAsserter} from "../../Cypress/CypressAsserter";
import {SelectCharacterSceneName} from "./SelectCharacterScene";
import {ResizableScene} from "./ResizableScene";
import {Scene} from "phaser";
import {LoginSceneName} from "./LoginScene";
import {FourOFourSceneName} from "../Reconnecting/FourOFourScene";
export const EntrySceneName = "EntryScene";
/**
* The EntryScene is not a real scene. It is the first scene loaded and is only used to initialize the gameManager
* and to route to the next correct scene.
*/
export class EntryScene extends Scene {
constructor() {
super({
key: EntrySceneName
});
}
preload() {
}
create() {
gameManager.init(this.scene).then(() => {
this.scene.start(LoginSceneName);
}).catch(() => {
this.scene.start(FourOFourSceneName, {
url: window.location.pathname.toString()
});
});
}
}

View File

@ -15,7 +15,8 @@ export class FourOFourScene extends Phaser.Scene {
private fileNameField!: Text; private fileNameField!: Text;
private logo!: Image; private logo!: Image;
private cat!: Sprite; private cat!: Sprite;
private file!: string; private file: string|undefined;
private url: string|undefined;
constructor() { constructor() {
super({ super({
@ -23,8 +24,9 @@ export class FourOFourScene extends Phaser.Scene {
}); });
} }
init({ file }: { file: string }) { init({ file, url }: { file?: string, url?: string }) {
this.file = file; this.file = file;
this.url = url;
} }
preload() { preload() {
@ -45,11 +47,22 @@ export class FourOFourScene extends Phaser.Scene {
this.mapNotFoundField = new TextField(this, this.game.renderer.width / 2, this.game.renderer.height / 2, "404 - File not found"); this.mapNotFoundField = new TextField(this, this.game.renderer.width / 2, this.game.renderer.height / 2, "404 - File not found");
this.mapNotFoundField.setOrigin(0.5, 0.5).setCenterAlign(); this.mapNotFoundField.setOrigin(0.5, 0.5).setCenterAlign();
this.couldNotFindField = new TextField(this, this.game.renderer.width / 2, this.game.renderer.height / 2 + 24, "Could not load file"); let text: string = '';
if (this.file !== undefined) {
text = "Could not load map"
}
if (this.url !== undefined) {
text = "Invalid URL"
}
this.couldNotFindField = new TextField(this, this.game.renderer.width / 2, this.game.renderer.height / 2 + 24, text);
this.couldNotFindField.setOrigin(0.5, 0.5).setCenterAlign(); this.couldNotFindField.setOrigin(0.5, 0.5).setCenterAlign();
this.fileNameField = this.add.text(this.game.renderer.width / 2, this.game.renderer.height / 2 + 38, this.file, { fontFamily: 'Georgia, "Goudy Bookletter 1911", Times, serif', fontSize: '10px' }); const url = this.file ? this.file : this.url;
this.fileNameField.setOrigin(0.5, 0.5); if (url !== undefined) {
this.fileNameField = this.add.text(this.game.renderer.width / 2, this.game.renderer.height / 2 + 38, url, { fontFamily: 'Georgia, "Goudy Bookletter 1911", Times, serif', fontSize: '10px' });
this.fileNameField.setOrigin(0.5, 0.5);
}
this.cat = this.physics.add.sprite(this.game.renderer.width / 2, this.game.renderer.height / 2 - 32, 'cat', 6); this.cat = this.physics.add.sprite(this.game.renderer.width / 2, this.game.renderer.height / 2 - 32, 'cat', 6);
this.cat.flipY=true; this.cat.flipY=true;

View File

@ -0,0 +1,50 @@
export enum GameConnexionTypes {
anonymous=1,
organization,
register,
empty,
unknown,
}
//this class is responsible with analysing and editing the game's url
class UrlManager {
//todo: use that to detect if we can find a token in localstorage
public getGameConnexionType(): GameConnexionTypes {
const url = window.location.pathname.toString();
if (url.includes('_/')) {
return GameConnexionTypes.anonymous;
} else if (url.includes('@/')) {
return GameConnexionTypes.organization;
} else if(url.includes('register/')) {
return GameConnexionTypes.register;
} else if(url === '/') {
return GameConnexionTypes.empty;
} else {
return GameConnexionTypes.unknown;
}
}
public getOrganizationToken(): string|null {
const match = /\/register\/(.+)/.exec(window.location.pathname.toString());
return match ? match [1] : null;
}
//todo: simply use the roomId
//todo: test this with cypress
public editUrlForRoom(roomSlug: string, organizationSlug: string|null, worldSlug: string |null): string {
let newUrl:string;
if (organizationSlug) {
newUrl = '/@/'+organizationSlug+'/'+worldSlug+'/'+roomSlug;
} else {
newUrl = '/_/global/'+roomSlug;
}
history.pushState({}, 'WorkAdventure', newUrl);
return newUrl;
}
}
export const urlManager = new UrlManager();

View File

@ -11,11 +11,11 @@ import WebGLRenderer = Phaser.Renderer.WebGL.WebGLRenderer;
import {OutlinePipeline} from "./Phaser/Shaders/OutlinePipeline"; import {OutlinePipeline} from "./Phaser/Shaders/OutlinePipeline";
import {CustomizeScene} from "./Phaser/Login/CustomizeScene"; import {CustomizeScene} from "./Phaser/Login/CustomizeScene";
import {CoWebsiteManager} from "./WebRtc/CoWebsiteManager"; import {CoWebsiteManager} from "./WebRtc/CoWebsiteManager";
import {connectionManager} from "./Connexion/ConnectionManager"; import {gameManager} from "./Phaser/Game/GameManager";
import {ResizableScene} from "./Phaser/Login/ResizableScene"; import {ResizableScene} from "./Phaser/Login/ResizableScene";
import {EntryScene} from "./Phaser/Login/EntryScene";
//CoWebsiteManager.loadCoWebsite('https://thecodingmachine.com'); //CoWebsiteManager.loadCoWebsite('https://thecodingmachine.com');
connectionManager.init();
// Load Jitsi if the environment variable is set. // Load Jitsi if the environment variable is set.
if (JITSI_URL) { if (JITSI_URL) {
@ -31,14 +31,7 @@ const config: GameConfig = {
width: width / RESOLUTION, width: width / RESOLUTION,
height: height / RESOLUTION, height: height / RESOLUTION,
parent: "game", parent: "game",
scene: [ scene: [EntryScene, LoginScene, SelectCharacterScene, EnableCameraScene, ReconnectingScene, FourOFourScene, CustomizeScene],
LoginScene,
SelectCharacterScene,
EnableCameraScene,
ReconnectingScene,
FourOFourScene,
CustomizeScene
],
zoom: RESOLUTION, zoom: RESOLUTION,
physics: { physics: {
default: "arcade", default: "arcade",

File diff suppressed because it is too large Load Diff

View File

@ -38,12 +38,6 @@ message SetPlayerDetailsMessage {
repeated string characterLayers = 2; repeated string characterLayers = 2;
} }
message JoinRoomMessage {
string roomId = 1;
PositionMessage position = 2;
ViewportMessage viewport = 3;
}
message UserMovesMessage { message UserMovesMessage {
PositionMessage position = 1; PositionMessage position = 1;
ViewportMessage viewport = 2; ViewportMessage viewport = 2;
@ -61,7 +55,6 @@ message ReportPlayerMessage {
message ClientToServerMessage { message ClientToServerMessage {
oneof message { oneof message {
JoinRoomMessage joinRoomMessage = 1;
UserMovesMessage userMovesMessage = 2; UserMovesMessage userMovesMessage = 2;
SilentMessage silentMessage = 3; SilentMessage silentMessage = 3;
ViewportMessage viewportMessage = 4; ViewportMessage viewportMessage = 4;
@ -142,10 +135,6 @@ message ErrorMessage {
string message = 1; string message = 1;
} }
message SetUserIdMessage {
int32 userId = 1;
}
message ItemStateMessage { message ItemStateMessage {
int32 itemId = 1; int32 itemId = 1;
string stateJson = 2; string stateJson = 2;
@ -155,6 +144,7 @@ message RoomJoinedMessage {
repeated UserJoinedMessage user = 1; repeated UserJoinedMessage user = 1;
repeated GroupUpdateMessage group = 2; repeated GroupUpdateMessage group = 2;
repeated ItemStateMessage item = 3; repeated ItemStateMessage item = 3;
int32 currentUserId = 4;
} }
message WebRtcStartMessage { message WebRtcStartMessage {
@ -181,13 +171,12 @@ message ServerToClientMessage {
BatchMessage batchMessage = 1; BatchMessage batchMessage = 1;
ErrorMessage errorMessage = 2; ErrorMessage errorMessage = 2;
RoomJoinedMessage roomJoinedMessage = 3; RoomJoinedMessage roomJoinedMessage = 3;
SetUserIdMessage setUserIdMessage = 4; // TODO: merge this with RoomJoinedMessage ? WebRtcStartMessage webRtcStartMessage = 4;
WebRtcStartMessage webRtcStartMessage = 5; WebRtcSignalToClientMessage webRtcSignalToClientMessage = 5;
WebRtcSignalToClientMessage webRtcSignalToClientMessage = 6; WebRtcSignalToClientMessage webRtcScreenSharingSignalToClientMessage = 6;
WebRtcSignalToClientMessage webRtcScreenSharingSignalToClientMessage = 7; WebRtcDisconnectMessage webRtcDisconnectMessage = 7;
WebRtcDisconnectMessage webRtcDisconnectMessage = 8; PlayGlobalMessage playGlobalMessage = 8;
PlayGlobalMessage playGlobalMessage = 9; StopGlobalMessage stopGlobalMessage = 9;
StopGlobalMessage stopGlobalMessage = 10; TeleportMessageMessage teleportMessageMessage = 10;
TeleportMessageMessage teleportMessageMessage = 11;
} }
} }