BIG WIP of refactoring my work in TS

This commit is contained in:
David MAECHLER 2020-04-07 10:08:04 +02:00
parent af5d2a5a97
commit fbfc208129
12 changed files with 274 additions and 11 deletions

View file

@ -87,7 +87,8 @@ export class IoSocketController{
//Hydrate and manage error
hydrateMessageReceive(message : string) : MessageUserPosition | Error{
try {
return new MessageUserPosition(message);
let data = JSON.parse(message);
return new MessageUserPosition(data);
}catch (err) {
//TODO log error
return new Error(err);

View file

@ -1 +1,130 @@
// TODO
import {MessageUserPosition} from "_Model/Websocket/MessageUserPosition";
import {Distance} from "_Model/Distance";
export class PositionController {
static readonly MIN_DISTANCE = 12;
static readonly MAX_PER_GROUP = 3;
constructor ()
{
// Injecter socket ?
}
getDistancesBetweenAllUsers(users: MessageUserPosition[]): Distance[]
{
let i = 0;
let distances: Distance[] = [];
users.forEach(function(user1, key1) {
users.forEach(function(user2, key2) {
if(key1 < key2) {
distances[i] = {
distance: PositionController.computeDistance(user1, user2),
user1: user1,
user2: user2
};
i++;
}
});
});
distances.sort(PositionController.compareDistances);
return distances;
}
createGroups(distances: Distance[], nbOfUsers: number, Oldgroups: MessageUserPosition[][]): MessageUserPosition[][]
{
// TODO : detect in existing groups if a user must be removed from the group
let alreadyInAGroup: any[string] = [];
let groups: MessageUserPosition[][] = [];
let roomId = 0;
for(let i = 0; i < distances.length; i++) {
let dist = distances[i];
if(dist.distance <= PositionController.MIN_DISTANCE) {
if(typeof groups[roomId] === 'undefined') {
groups[roomId] = [];
}
if(groups[roomId].indexOf(dist.user1) === -1 && typeof alreadyInAGroup[dist.user1.userId] === 'undefined') {
if(groups[roomId].length > 1) {
// if group is not empty we check current user can be added in the group according to its distance to the others already in it
for(let j = 0; j < groups[roomId].length; j++) {
let userTotest = groups[roomId][j];
if(PositionController.computeDistance(dist.user1, userTotest) <= PositionController.MIN_DISTANCE) {
groups[roomId].push(dist.user1);
alreadyInAGroup[dist.user1.userId] = true;
break;
}
}
} else {
groups[roomId].push(dist.user1);
alreadyInAGroup[dist.user1.userId] = true;
}
}
if(groups[roomId].length === PositionController.MAX_PER_GROUP) {
roomId++; // on créé un nouveau groupe
if(roomId > (nbOfUsers / PositionController.MAX_PER_GROUP)) {
console.log('There is no room left for user ID : ' + dist.user2.userId + ' !');
break;
}
continue;
}
if(groups[roomId].indexOf(dist.user2) === -1 && typeof alreadyInAGroup[dist.user2.userId] === 'undefined') {
if(groups[roomId].length > 1) {
// if group is not empty we check current user can be added in the group according to its distance to the others already in it
for(let j = 0; j < groups[roomId].length; j++) {
let userTotest = groups[roomId][j];
if(PositionController.computeDistance(dist.user2, userTotest) <= PositionController.MIN_DISTANCE) {
groups[roomId].push(dist.user2);
alreadyInAGroup[dist.user2.userId] = true;
break;
}
}
} else {
groups[roomId].push(dist.user2);
alreadyInAGroup[dist.user2.userId] = true;
}
}
}
}
return groups;
}
// FIXME
checkGroupDistance (groups: MessageUserPosition[][])
{
for(let i = 0; i < groups.length; i++) {
let group = groups[i];
group.forEach((user1, key1) => {
group.forEach((user2, key2) => {
if(key1 < key2) {
let distance = PositionController.computeDistance(user1, user2);
if(distance > PositionController.MIN_DISTANCE) {
// TODO : message a user1 et user2
}
}
});
});
}
}
private static computeDistance(user1: MessageUserPosition, user2: MessageUserPosition): number
{
return Math.sqrt(Math.pow(user2.position.x - user1.position.x, 2) + Math.pow(user2.position.y - user1.position.y, 2));
}
private static compareDistances(distA: Distance, distB: Distance): number
{
if (distA.distance < distB.distance) {
return -1;
}
if (distA.distance > distB.distance) {
return 1;
}
return 0;
}
}

View file

@ -1,148 +0,0 @@
// Constants
let MIN_DISTANCE = 12;
let MAX_PER_GROUP = 3;
let NB_USERS = 10;
// Utils
let rand = function(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
};
let compareDistances = function(distA, distB) {
if (distA.distance < distB.distance) {
return -1;
}
if (distA.distance > distB.distance) {
return 1;
}
return 0;
};
let computeDistance = function (user1, user2) {
return Math.sqrt(Math.pow(user2.X - user1.X, 2) + Math.pow(user2.Y - user1.Y, 2));
};
// Test Data
let users = [];
for(let i = 1; i <= NB_USERS; i++) {
let user = {};
user.id = rand(0,99999);
user.X = rand(0, 40);
user.Y = rand(0, 40);
users.push(user);
}
// Compute distance between each user
let getDistanceOfEachUser = function(users) {
let i = 0;
let distances = [];
users.forEach(function(user1, key1) {
users.forEach(function(user2, key2) {
if(key1 < key2) {
let distanceObj = {};
distanceObj.distance = computeDistance(user1, user2);
distanceObj.first = user1;
distanceObj.second = user2;
distances[i] = distanceObj;
i++;
}
});
});
return distances;
};
// Organise groups
let createGroups = function(distances) {
let i = 0;
let groups = [];
let alreadyInAGroup = [];
for(let j = 0; j < distances.length; j++) {
let dist = distances[j];
if(dist.distance <= MIN_DISTANCE) {
if(typeof groups[i] === 'undefined') {
groups[i] = [];
}
if(groups[i].indexOf(dist.first) === -1 && typeof alreadyInAGroup[dist.first.id] === 'undefined') {
if(groups[i].length > 1) {
// if group is not empty we check current user can be added in the group according to its distance to the others already in it
for(let l = 0; l < groups[i].length; l++) {
let userTotest = groups[i][l];
if(computeDistance(dist.first, userTotest) <= MIN_DISTANCE) {
groups[i].push(dist.first);
alreadyInAGroup[dist.first.id] = true;
break;
}
}
} else {
groups[i].push(dist.first);
alreadyInAGroup[dist.first.id] = true;
}
}
if(groups[i].length === MAX_PER_GROUP) {
i++; // on créé un nouveau groupe
if(i > (NB_USERS / MAX_PER_GROUP)) {
console.log('There is no room left for user ID : ' + dist.second.id + ' !');
break;
}
continue;
}
if(groups[i].indexOf(dist.second) === -1 && typeof alreadyInAGroup[dist.second.id] === 'undefined') {
if(groups[i].length > 1) {
// if group is not empty we check current user can be added in the group according to its distance to the others already in it
for(let l = 0; l < groups[i].length; l++) {
let userTotest = groups[i][l];
if(computeDistance(dist.second, userTotest) <= MIN_DISTANCE) {
groups[i].push(dist.second);
alreadyInAGroup[dist.second.id] = true;
break;
}
}
} else {
groups[i].push(dist.second);
alreadyInAGroup[dist.second.id] = true;
}
}
}
}
return groups;
};
let distances = getDistanceOfEachUser(users);
// ordonner par distance pour prioriser l'association en groupe des utilisateurs les plus proches
distances.sort(compareDistances);
let groups = createGroups(distances);
// Compute distance between each user of a already existing group
let checkGroupDistance = function(groups) {
for(let i = 0; i < groups.length; i++) {
let group = groups[i];
group.forEach(function(user1, key1) {
group.forEach(function(user2, key2) {
if(key1 < key2) {
let distance = computeDistance(user1, user2);
if(distance > MIN_DISTANCE) {
// TODO : message a user1 et user2
}
}
});
});
}
};
console.log(users);
console.log(distances);
console.log(groups);

View file

@ -0,0 +1,7 @@
import {MessageUserPosition} from "../Model/Websocket/MessageUserPosition";
export interface Distance {
distance: number,
user1: MessageUserPosition,
user2: MessageUserPosition,
}

18
back/src/Model/Group.ts Normal file
View file

@ -0,0 +1,18 @@
import {MessageUserPosition} from "./Websocket/MessageUserPosition";
export class Group {
static readonly MAX_PER_GROUP = 4;
users: MessageUserPosition[];
constructor(users: MessageUserPosition[]) {
this.users = users;
}
getUsers(): MessageUserPosition[] {
return this.users;
}
isFull(): boolean {
return this.users.length >= Group.MAX_PER_GROUP;
}
}

View file

@ -0,0 +1,7 @@
import { Group } from "./Group";
import { PointInterface } from "./Websocket/PointInterface";
export interface Userinteface {
group: Group,
pointInterface: PointInterface
}

View file

@ -0,0 +1,18 @@
import {MessageUserPosition} from "./MessageUserPosition";
export class Group {
static readonly MAX_PER_GROUP = 4;
users: MessageUserPosition[];
constructor(users: MessageUserPosition[]) {
this.users = users;
}
getUsers(): MessageUserPosition[] {
return this.users;
}
isFull(): boolean {
return this.users.length >= Group.MAX_PER_GROUP;
}
}

View file

@ -2,8 +2,7 @@ export class Message {
userId: string;
roomId: string;
constructor(message: string) {
let data = JSON.parse(message);
constructor(data: any) {
if(!data.userId || !data.roomId){
throw Error("userId or roomId cannot be null");
}

View file

@ -24,9 +24,8 @@ export class Point implements PointInterface{
export class MessageUserPosition extends Message{
position: PointInterface;
constructor(message: string) {
super(message);
let data = JSON.parse(message);
constructor(data: any) {
super(data);
this.position = new Point(data.position.x, data.position.y);
}

46
back/src/Model/World.ts Normal file
View file

@ -0,0 +1,46 @@
import {MessageUserPosition} from "./Websocket/MessageUserPosition";
import {PointInterface} from "./Websocket/PointInterface";
import {Group} from "./Group";
export class World {
// Users, sorted by ID
private users: Map<string, PointInterface>;
private groups: Group[]
private connectCallback: (user1: string, user2: string) => void;
private disconnectCallback: (user1: string, user2: string) => void;
constructor(connectCallback: (user1: string, user2: string) => void, disconnectCallback: (user1: string, user2: string) => void)
{
this.users = new Map<string, PointInterface>();
this.groups = [];
this.connectCallback = connectCallback;
this.disconnectCallback = disconnectCallback;
}
public join(userPosition: MessageUserPosition): void {
this.users.set(userPosition.userId, userPosition.position);
}
public updatePosition(userPosition: MessageUserPosition): void {
if(typeof userPosition.userId === 'undefined') {
throw new Error('unkown id');
}
//this.users.get(userPosition.userId).x;
// TODO: compute distance between peers.
// Is the user in a group?
// 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
// If the user is in no group
// is there someone in a group close enough and with room in the group?
}
}