temp
This commit is contained in:
parent
61c6b9dacb
commit
60606947ab
18 changed files with 284 additions and 384 deletions
|
@ -3,6 +3,7 @@ import Jwt from "jsonwebtoken";
|
|||
import {BAD_REQUEST, OK} from "http-status-codes";
|
||||
import {SECRET_KEY, ROOM} from "../Enum/EnvironmentVariable"; //TODO fix import by "_Enum/..."
|
||||
import { uuid } from 'uuidv4';
|
||||
import {userManager} from "_Model/Users/UserManager";
|
||||
|
||||
export class AuthenticateController{
|
||||
App : Application;
|
||||
|
@ -10,6 +11,7 @@ export class AuthenticateController{
|
|||
constructor(App : Application) {
|
||||
this.App = App;
|
||||
this.login();
|
||||
this.getAllUsers();
|
||||
}
|
||||
|
||||
//permit to login on application. Return token to connect on Websocket IO.
|
||||
|
@ -22,13 +24,16 @@ export class AuthenticateController{
|
|||
});
|
||||
}
|
||||
//TODO check user email for The Coding Machine game
|
||||
let userId = uuid();
|
||||
let token = Jwt.sign({email: param.email, roomId: ROOM, userId: userId}, SECRET_KEY, {expiresIn: '24h'});
|
||||
return res.status(OK).send({
|
||||
token: token,
|
||||
roomId: ROOM,
|
||||
userId: userId
|
||||
});
|
||||
let user = userManager.createUser(param.email);
|
||||
let token = Jwt.sign({email: user.email, userId: user.id}, SECRET_KEY, {expiresIn: '24h'});
|
||||
return res.status(OK).send(user);
|
||||
});
|
||||
}
|
||||
|
||||
getAllUsers(){
|
||||
this.App.get("/users", (req: Request, res: Response) => {
|
||||
let users = userManager.getAllUsers();
|
||||
return res.status(OK).send(users);
|
||||
});
|
||||
}
|
||||
}
|
|
@ -7,6 +7,7 @@ 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";
|
||||
import {userManager} from "_Model/Users/UserManager";
|
||||
|
||||
export class IoSocketController{
|
||||
Io: socketIO.Server;
|
||||
|
@ -112,23 +113,12 @@ export class IoSocketController{
|
|||
**/
|
||||
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];
|
||||
this.Io.in(roomId).emit('user-position', JSON.stringify(arrayMap));
|
||||
});
|
||||
this.seTimeOutInProgress = setTimeout(() => {
|
||||
this.shareUsersPosition();
|
||||
}, 10);
|
||||
//every 1/10 of seconds, emit the current list of events
|
||||
setInterval(() => {
|
||||
let userEvents = userManager.getEventList();
|
||||
if (userEvents.length) {
|
||||
this.Io.emit('user-position', JSON.stringify(userEvents));
|
||||
}
|
||||
}, 100)
|
||||
}
|
||||
}
|
||||
|
|
86
back/src/Model/Users/UserManager.ts
Normal file
86
back/src/Model/Users/UserManager.ts
Normal file
|
@ -0,0 +1,86 @@
|
|||
import {uuid} from "uuidv4";
|
||||
import {BehaviorSubject} from "rxjs";
|
||||
|
||||
export interface UserPosition {
|
||||
x: number;
|
||||
y: number;
|
||||
}
|
||||
|
||||
export class User {
|
||||
name: string;
|
||||
email: string;
|
||||
position: UserPosition;
|
||||
id: string;
|
||||
|
||||
constructor(id: string, name: string, email: string, position: UserPosition) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
this.position = position
|
||||
}
|
||||
}
|
||||
|
||||
export interface UserPositionChangeEvent {
|
||||
user: User;
|
||||
deleted: boolean
|
||||
added: boolean
|
||||
}
|
||||
|
||||
class UserManager {
|
||||
|
||||
private usersList: Map<string, User> = new Map();
|
||||
|
||||
private eventsList: UserPositionChangeEvent[] = [];
|
||||
|
||||
//todo add more parameters
|
||||
createUser(email: string): User {
|
||||
let userId = uuid();
|
||||
let user = new User(userId, "toto", email, {x: 0, y: 0});
|
||||
|
||||
this.usersList.set(userId, user);
|
||||
this.eventsList.push({added: true, deleted: false, user})
|
||||
return user;
|
||||
}
|
||||
|
||||
deleteUser(id: string): User {
|
||||
let user = this.usersList.get(id);
|
||||
if (!user) {
|
||||
throw "Could not delete user with id "+id;
|
||||
}
|
||||
this.usersList.delete(id);
|
||||
|
||||
this.eventsList.push({added: false, deleted: true, user});
|
||||
return user;
|
||||
}
|
||||
|
||||
updateUserPosition(id: string, userPosition: UserPosition): User {
|
||||
let user = this.usersList.get(id);
|
||||
if (!user) {
|
||||
throw "Could not find user with id "+id;
|
||||
}
|
||||
user.position = userPosition;
|
||||
this.usersList.set(id, user);
|
||||
|
||||
this.eventsList.push({added: false, deleted: false, user});
|
||||
return user;
|
||||
}
|
||||
|
||||
getAllUsers(): User[] {
|
||||
let array = [];
|
||||
for (const value of this.usersList.values()) {
|
||||
array.push(value);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
//flush the list of events
|
||||
getEventList(): UserPositionChangeEvent[] {
|
||||
let events = this.eventsList;
|
||||
this.eventsList = [];
|
||||
return events;
|
||||
}
|
||||
}
|
||||
|
||||
export const userManager = new UserManager();
|
||||
|
||||
|
|
@ -66,6 +66,7 @@
|
|||
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
|
||||
|
||||
/* Advanced Options */
|
||||
"downlevelIteration": true,
|
||||
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue