Merge pull request #73 from thecodingmachine/groupradius

Making the group radius distinct from the minimum distance to connect 2 players
This commit is contained in:
David Négrier 2020-05-03 17:52:46 +02:00 committed by GitHub
commit 5f320102bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 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 {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";
@ -51,7 +51,7 @@ export class IoSocketController {
this.connectedUser(user1, group);
}, (user1: string, group: Group) => {
this.disConnectedUser(user1, group);
});
}, MINIMUM_DISTANCE, GROUP_RADIUS);
}
ioConnection() {

View File

@ -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) : 64;
const GROUP_RADIUS = process.env.GROUP_RADIUS ? Number(process.env.GROUP_RADIUS) : 48;
export {
SECRET_KEY,
ROOM
}
ROOM,
MINIMUM_DISTANCE,
GROUP_RADIUS
}

View File

@ -71,23 +71,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++){

View File

@ -10,7 +10,8 @@ export type ConnectCallback = (user: string, group: Group) => void;
export type DisconnectCallback = (user: string, group: Group) => void;
export class World {
static readonly MIN_DISTANCE = 160;
private minDistance: number;
private groupRadius: number;
// Users, sorted by ID
private users: Map<string, UserInterface>;
@ -19,12 +20,17 @@ export class World {
private connectCallback: ConnectCallback;
private disconnectCallback: DisconnectCallback;
constructor(connectCallback: ConnectCallback, disconnectCallback: DisconnectCallback)
constructor(connectCallback: ConnectCallback,
disconnectCallback: DisconnectCallback,
minDistance: number,
groupRadius: number)
{
this.users = new Map<string, UserInterface>();
this.groups = [];
this.connectCallback = connectCallback;
this.disconnectCallback = disconnectCallback;
this.minDistance = minDistance;
this.groupRadius = groupRadius;
}
public join(userPosition: MessageUserPosition): void {
@ -76,7 +82,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);
}
}
@ -106,15 +112,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;
@ -125,7 +132,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;
}
@ -165,12 +172,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;
}

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({
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",