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)
This commit is contained in:
David Négrier 2020-05-03 16:56:19 +02:00
parent dcaf9a8e46
commit 3b27f8b000
5 changed files with 28 additions and 35 deletions

View File

@ -4,7 +4,7 @@ import * as http from "http";
import {MessageUserPosition} from "../Model/Websocket/MessageUserPosition"; //TODO fix import by "_Model/.." import {MessageUserPosition} 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 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 {ExtRooms, RefreshUserPositionFunction} from "../Model/Websocket/ExtRoom";
import {ExtRoomsInterface} from "../Model/Websocket/ExtRoomsInterface"; import {ExtRoomsInterface} from "../Model/Websocket/ExtRoomsInterface";
import {World} from "../Model/World"; import {World} from "../Model/World";
@ -48,7 +48,7 @@ export class IoSocketController{
this.connectedUser(user1, user2); this.connectedUser(user1, user2);
}, (user1 : string, user2 : string) => { }, (user1 : string, user2 : string) => {
this.disConnectedUser(user1, user2); this.disConnectedUser(user1, user2);
}); }, MINIMUM_DISTANCE, GROUP_RADIUS);
} }
ioConnection() { ioConnection() {

View File

@ -1,7 +1,11 @@
const SECRET_KEY = process.env.SECRET_KEY || "THECODINGMACHINE_SECRET_KEY"; const SECRET_KEY = process.env.SECRET_KEY || "THECODINGMACHINE_SECRET_KEY";
const ROOM = process.env.ROOM || "THECODINGMACHINE"; 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 { export {
SECRET_KEY, SECRET_KEY,
ROOM ROOM,
MINIMUM_DISTANCE,
GROUP_RADIUS
} }

View File

@ -66,23 +66,6 @@ export class Group {
return this.users.indexOf(user) !== -1; 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 /*removeFromGroup(users: UserInterface[]): void
{ {
for(let i = 0; i < users.length; i++){ for(let i = 0; i < users.length; i++){

View File

@ -7,7 +7,8 @@ import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
import {PositionInterface} from "_Model/PositionInterface"; import {PositionInterface} from "_Model/PositionInterface";
export class World { export class World {
static readonly MIN_DISTANCE = 160; private minDistance: number;
private groupRadius: number;
// Users, sorted by ID // Users, sorted by ID
private users: Map<string, UserInterface>; private users: Map<string, UserInterface>;
@ -16,12 +17,16 @@ export class World {
private connectCallback: (user1: string, user2: string) => void; private connectCallback: (user1: string, user2: string) => void;
private disconnectCallback: (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<string, UserInterface>(); this.users = new Map<string, UserInterface>();
this.groups = []; this.groups = [];
this.connectCallback = connectCallback; this.connectCallback = connectCallback;
this.disconnectCallback = disconnectCallback; this.disconnectCallback = disconnectCallback;
this.minDistance = minDistance;
this.groupRadius = groupRadius;
} }
public join(userPosition: MessageUserPosition): void { public join(userPosition: MessageUserPosition): void {
@ -73,7 +78,7 @@ export class World {
// 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?
let distance = World.computeDistanceBetweenPositions(user.position, user.group.getPosition()); let distance = World.computeDistanceBetweenPositions(user.position, user.group.getPosition());
if (distance > World.MIN_DISTANCE) { if (distance > this.groupRadius) {
this.leaveGroup(user); this.leaveGroup(user);
} }
} }
@ -103,15 +108,16 @@ export class World {
/** /**
* Looks for the closest user that is: * Looks for the closest user that is:
* - close enough (distance <= MIN_DISTANCE) * - close enough (distance <= minDistance)
* - not in a group OR in a group that is not full * - not in a group
* OR
* - close enough to a group (distance <= groupRadius)
*/ */
private searchClosestAvailableUserOrGroup(user: UserInterface): UserInterface|Group|null private searchClosestAvailableUserOrGroup(user: UserInterface): UserInterface|Group|null
{ {
let usersToBeGroupedWith: Distance[] = []; let minimumDistanceFound: number = Math.max(this.minDistance, this.groupRadius);
let minimumDistanceFound: number = World.MIN_DISTANCE;
let matchingItem: UserInterface | Group | null = null; 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 // Let's only check users that are not part of a group
if (typeof currentUser.group !== 'undefined') { if (typeof currentUser.group !== 'undefined') {
return; return;
@ -122,7 +128,7 @@ export class World {
let distance = World.computeDistance(user, currentUser); // compute distance between peers. let distance = World.computeDistance(user, currentUser); // compute distance between peers.
if(distance <= minimumDistanceFound) { if(distance <= minimumDistanceFound && distance <= this.minDistance) {
minimumDistanceFound = distance; minimumDistanceFound = distance;
matchingItem = currentUser; matchingItem = currentUser;
} }
@ -162,12 +168,12 @@ export class World {
*/ */
}); });
this.groups.forEach(function(group: Group) { this.groups.forEach((group: Group) => {
if (group.isFull()) { if (group.isFull()) {
return; return;
} }
let distance = World.computeDistanceBetweenPositions(user.position, group.getPosition()); let distance = World.computeDistanceBetweenPositions(user.position, group.getPosition());
if(distance <= minimumDistanceFound) { if(distance <= minimumDistanceFound && distance <= this.groupRadius) {
minimumDistanceFound = distance; minimumDistanceFound = distance;
matchingItem = group; matchingItem = group;
} }

View File

@ -15,7 +15,7 @@ describe("World", () => {
} }
let world = new World(connect, disconnect); let world = new World(connect, disconnect, 160, 160);
world.join(new MessageUserPosition({ world.join(new MessageUserPosition({
userId: "foo", 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({ world.join(new MessageUserPosition({
userId: "foo", userId: "foo",
@ -108,7 +108,7 @@ describe("World", () => {
disconnectCalled = true; disconnectCalled = true;
} }
let world = new World(connect, disconnect); let world = new World(connect, disconnect, 160, 160);
world.join(new MessageUserPosition({ world.join(new MessageUserPosition({
userId: "foo", userId: "foo",