From 3b27f8b000520af808ce2f1e04badc7af3a877e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Sun, 3 May 2020 16:56:19 +0200 Subject: [PATCH 1/2] Making the group radius distinct from the minimum distance to connect 2 players Also, changed default settings from 160px for Group Radius to 120px (minimum distance to connect 2 players remains 160px) --- back/src/Controller/IoSocketController.ts | 4 ++-- back/src/Enum/EnvironmentVariable.ts | 8 +++++-- back/src/Model/Group.ts | 17 -------------- back/src/Model/World.ts | 28 ++++++++++++++--------- back/tests/WorldTest.ts | 6 ++--- 5 files changed, 28 insertions(+), 35 deletions(-) diff --git a/back/src/Controller/IoSocketController.ts b/back/src/Controller/IoSocketController.ts index 23a238e6..04dea735 100644 --- a/back/src/Controller/IoSocketController.ts +++ b/back/src/Controller/IoSocketController.ts @@ -4,7 +4,7 @@ import * as http from "http"; import {MessageUserPosition} from "../Model/Websocket/MessageUserPosition"; //TODO fix import by "_Model/.." 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 {SECRET_KEY, MINIMUM_DISTANCE, GROUP_RADIUS} from "../Enum/EnvironmentVariable"; //TODO fix import by "_Enum/..." import {ExtRooms, RefreshUserPositionFunction} from "../Model/Websocket/ExtRoom"; import {ExtRoomsInterface} from "../Model/Websocket/ExtRoomsInterface"; import {World} from "../Model/World"; @@ -48,7 +48,7 @@ export class IoSocketController{ this.connectedUser(user1, user2); }, (user1 : string, user2 : string) => { this.disConnectedUser(user1, user2); - }); + }, MINIMUM_DISTANCE, GROUP_RADIUS); } ioConnection() { diff --git a/back/src/Enum/EnvironmentVariable.ts b/back/src/Enum/EnvironmentVariable.ts index f795bf90..96d87e98 100644 --- a/back/src/Enum/EnvironmentVariable.ts +++ b/back/src/Enum/EnvironmentVariable.ts @@ -1,7 +1,11 @@ const SECRET_KEY = process.env.SECRET_KEY || "THECODINGMACHINE_SECRET_KEY"; const ROOM = process.env.ROOM || "THECODINGMACHINE"; +const MINIMUM_DISTANCE = process.env.MINIMUM_DISTANCE ? Number(process.env.MINIMUM_DISTANCE) : 160; +const GROUP_RADIUS = process.env.GROUP_RADIUS ? Number(process.env.GROUP_RADIUS) : 128; export { SECRET_KEY, - ROOM -} \ No newline at end of file + ROOM, + MINIMUM_DISTANCE, + GROUP_RADIUS +} diff --git a/back/src/Model/Group.ts b/back/src/Model/Group.ts index caf9b926..259a0379 100644 --- a/back/src/Model/Group.ts +++ b/back/src/Model/Group.ts @@ -66,23 +66,6 @@ export class Group { return this.users.indexOf(user) !== -1; } - isStillIn(user: UserInterface): boolean - { - if(!this.isPartOfGroup(user)) { - return false; - } - let stillIn = true; - for(let i = 0; i <= this.users.length; i++) { - let userInGroup = this.users[i]; - let distance = World.computeDistance(user, userInGroup); - if(distance > World.MIN_DISTANCE) { - stillIn = false; - break; - } - } - return stillIn; - } - /*removeFromGroup(users: UserInterface[]): void { for(let i = 0; i < users.length; i++){ diff --git a/back/src/Model/World.ts b/back/src/Model/World.ts index 795cc8be..e05879c1 100644 --- a/back/src/Model/World.ts +++ b/back/src/Model/World.ts @@ -7,7 +7,8 @@ import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface"; import {PositionInterface} from "_Model/PositionInterface"; export class World { - static readonly MIN_DISTANCE = 160; + private minDistance: number; + private groupRadius: number; // Users, sorted by ID private users: Map; @@ -16,12 +17,16 @@ export class World { 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) + constructor(connectCallback: (user1: string, user2: string) => void, disconnectCallback: (user1: string, user2: string) => void, + minDistance: number, + groupRadius: number) { this.users = new Map(); this.groups = []; this.connectCallback = connectCallback; this.disconnectCallback = disconnectCallback; + this.minDistance = minDistance; + this.groupRadius = groupRadius; } public join(userPosition: MessageUserPosition): void { @@ -73,7 +78,7 @@ export class World { // If the user is part of a group: // should he leave the group? let distance = World.computeDistanceBetweenPositions(user.position, user.group.getPosition()); - if (distance > World.MIN_DISTANCE) { + if (distance > this.groupRadius) { this.leaveGroup(user); } } @@ -103,15 +108,16 @@ export class World { /** * Looks for the closest user that is: - * - close enough (distance <= MIN_DISTANCE) - * - not in a group OR in a group that is not full + * - close enough (distance <= minDistance) + * - not in a group + * OR + * - close enough to a group (distance <= groupRadius) */ private searchClosestAvailableUserOrGroup(user: UserInterface): UserInterface|Group|null { - let usersToBeGroupedWith: Distance[] = []; - let minimumDistanceFound: number = World.MIN_DISTANCE; + let minimumDistanceFound: number = Math.max(this.minDistance, this.groupRadius); let matchingItem: UserInterface | Group | null = null; - this.users.forEach(function(currentUser, userId) { + this.users.forEach((currentUser, userId) => { // Let's only check users that are not part of a group if (typeof currentUser.group !== 'undefined') { return; @@ -122,7 +128,7 @@ export class World { let distance = World.computeDistance(user, currentUser); // compute distance between peers. - if(distance <= minimumDistanceFound) { + if(distance <= minimumDistanceFound && distance <= this.minDistance) { minimumDistanceFound = distance; matchingItem = currentUser; } @@ -162,12 +168,12 @@ export class World { */ }); - this.groups.forEach(function(group: Group) { + this.groups.forEach((group: Group) => { if (group.isFull()) { return; } let distance = World.computeDistanceBetweenPositions(user.position, group.getPosition()); - if(distance <= minimumDistanceFound) { + if(distance <= minimumDistanceFound && distance <= this.groupRadius) { minimumDistanceFound = distance; matchingItem = group; } diff --git a/back/tests/WorldTest.ts b/back/tests/WorldTest.ts index 1d499727..994f1a4b 100644 --- a/back/tests/WorldTest.ts +++ b/back/tests/WorldTest.ts @@ -15,7 +15,7 @@ describe("World", () => { } - let world = new World(connect, disconnect); + let world = new World(connect, disconnect, 160, 160); world.join(new MessageUserPosition({ userId: "foo", @@ -63,7 +63,7 @@ describe("World", () => { } - let world = new World(connect, disconnect); + let world = new World(connect, disconnect, 160, 160); world.join(new MessageUserPosition({ userId: "foo", @@ -108,7 +108,7 @@ describe("World", () => { disconnectCalled = true; } - let world = new World(connect, disconnect); + let world = new World(connect, disconnect, 160, 160); world.join(new MessageUserPosition({ userId: "foo", From bf723b2e3012b8c7a7cc8804e98fe7cca828bb8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Sun, 3 May 2020 17:46:26 +0200 Subject: [PATCH 2/2] Reducing connection distance and group radius --- back/src/Enum/EnvironmentVariable.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/back/src/Enum/EnvironmentVariable.ts b/back/src/Enum/EnvironmentVariable.ts index 96d87e98..cfd98fcd 100644 --- a/back/src/Enum/EnvironmentVariable.ts +++ b/back/src/Enum/EnvironmentVariable.ts @@ -1,7 +1,7 @@ const SECRET_KEY = process.env.SECRET_KEY || "THECODINGMACHINE_SECRET_KEY"; const ROOM = process.env.ROOM || "THECODINGMACHINE"; -const MINIMUM_DISTANCE = process.env.MINIMUM_DISTANCE ? Number(process.env.MINIMUM_DISTANCE) : 160; -const GROUP_RADIUS = process.env.GROUP_RADIUS ? Number(process.env.GROUP_RADIUS) : 128; +const MINIMUM_DISTANCE = process.env.MINIMUM_DISTANCE ? Number(process.env.MINIMUM_DISTANCE) : 64; +const GROUP_RADIUS = process.env.GROUP_RADIUS ? Number(process.env.GROUP_RADIUS) : 48; export { SECRET_KEY,