Merge branch 'master' of https://github.com/thecodingmachine/workadventure into feature/back-players-proximity

This commit is contained in:
David MAECHLER 2020-04-06 17:35:58 +02:00
commit 60c0188e9e
16 changed files with 560 additions and 20 deletions

View file

@ -1,7 +1,7 @@
import {Application, Request, Response} from "express";
import Jwt, {JsonWebTokenError} from "jsonwebtoken";
import Jwt from "jsonwebtoken";
import {BAD_REQUEST, OK} from "http-status-codes";
import {SECRET_KEY} from "../Enum/EnvironmentVariable";
import {SECRET_KEY, ROOM} from "../Enum/EnvironmentVariable"; //TODO fix import by "_Enum/..."
export class AuthenticateController{
App : Application;
@ -21,8 +21,8 @@ export class AuthenticateController{
});
}
//TODO check user email for The Coding Machine game
let token = Jwt.sign({email: param.email}, SECRET_KEY, {expiresIn: '24h'});
return res.status(OK).send({token: token});
let token = Jwt.sign({email: param.email, roomId: ROOM}, SECRET_KEY, {expiresIn: '24h'});
return res.status(OK).send({token: token, roomId: ROOM});
});
}
}

View file

@ -5,6 +5,8 @@ import {MessageUserPosition} from "../Model/Websocket/MessageUserPosition"; //TO
import {ExSocketInterface} from "../Model/Websocket/ExSocketInterface"; //TODO fix import by "_Model/.."
import Jwt, {JsonWebTokenError} from "jsonwebtoken";
import {SECRET_KEY} from "../Enum/EnvironmentVariable"; //TODO fix import by "_Enum/..."
import {ExtRooms, RefreshUserPositionFunction} from "../Model/Websocket/ExtRoom";
import {ExtRoomsInterface} from "_Model/Websocket/ExtRoomsInterface";
export class IoSocketController{
Io: socketIO.Server;
@ -26,6 +28,7 @@ export class IoSocketController{
});
this.ioConnection();
this.shareUsersPosition();
}
ioConnection() {
@ -38,34 +41,47 @@ export class IoSocketController{
x: user x position on map
y: user y position on map
*/
socket.on('join-room', (message : string) => {
let messageUserPosition = this.hydrateMessageReceive(message);
if(messageUserPosition instanceof Error){
return socket.emit("message-error", JSON.stringify({message: messageUserPosition.message}))
}
//join user in room
socket.join(messageUserPosition.roomId);
// sending to all clients in room except sender
this.saveUserPosition((socket as ExSocketInterface), messageUserPosition);
this.saveUserInformation((socket as ExSocketInterface), messageUserPosition);
//add function to refresh position user in real time.
let rooms = (this.Io.sockets.adapter.rooms as ExtRoomsInterface);
rooms.refreshUserPosition = RefreshUserPositionFunction;
rooms.refreshUserPosition(rooms, this.Io);
socket.to(messageUserPosition.roomId).emit('join-room', messageUserPosition.toString());
});
socket.on('user-position', (message : string) => {
let messageUserPosition = this.hydrateMessageReceive(message);
if(messageUserPosition instanceof Error){
if (messageUserPosition instanceof Error) {
return socket.emit("message-error", JSON.stringify({message: messageUserPosition.message}));
}
// sending to all clients in room except sender
this.saveUserPosition((socket as ExSocketInterface), messageUserPosition);
socket.to(messageUserPosition.roomId).emit('join-room', messageUserPosition.toString());
this.saveUserInformation((socket as ExSocketInterface), messageUserPosition);
//refresh position of all user in all rooms in real time
let rooms = (this.Io.sockets.adapter.rooms as ExtRoomsInterface)
rooms.refreshUserPosition(rooms, this.Io);
});
});
}
//permit to save user position in socket
saveUserPosition(socket : ExSocketInterface, message : MessageUserPosition){
saveUserInformation(socket : ExSocketInterface, message : MessageUserPosition){
socket.position = message.position;
socket.roomId = message.roomId;
socket.userId = message.userId;
}
//Hydrate and manage error
@ -77,4 +93,42 @@ export class IoSocketController{
return new Error(err);
}
}
/** permit to share user position
** users position will send in event 'user-position'
** The data sent is an array with information for each user :
[
{
userId: <string>,
roomId: <string>,
position: {
x : <number>,
y : <number>
}
},
...
]
**/
seTimeOutInProgress : any = null;
shareUsersPosition(){
if(this.seTimeOutInProgress){
clearTimeout(this.seTimeOutInProgress);
}
//send for each room, all data of position user
let arrayMap = (this.Io.sockets.adapter.rooms as ExtRooms).userPositionMapByRoom;
if(!arrayMap){
this.seTimeOutInProgress = setTimeout(() => {
this.shareUsersPosition();
}, 10);
return;
}
arrayMap.forEach((value : any) => {
let roomId = value[0];
let data = value[1];
this.Io.in(roomId).emit('user-position', JSON.stringify(data));
});
this.seTimeOutInProgress = setTimeout(() => {
this.shareUsersPosition();
}, 10);
}
}