2020-04-08 20:40:44 +02:00
|
|
|
import {MessageUserPosition, Point} from "./Websocket/MessageUserPosition";
|
2020-04-07 10:08:04 +02:00
|
|
|
import {PointInterface} from "./Websocket/PointInterface";
|
|
|
|
import {Group} from "./Group";
|
2020-04-08 20:40:44 +02:00
|
|
|
import {Distance} from "./Distance";
|
2020-09-16 16:06:43 +02:00
|
|
|
import {User} from "./User";
|
2020-04-29 01:40:32 +02:00
|
|
|
import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
|
2020-04-29 22:41:48 +02:00
|
|
|
import {PositionInterface} from "_Model/PositionInterface";
|
2020-05-14 23:19:48 +02:00
|
|
|
import {Identificable} from "_Model/Websocket/Identificable";
|
2020-09-16 16:06:43 +02:00
|
|
|
import {EntersCallback, LeavesCallback, MovesCallback, Zone} from "_Model/Zone";
|
2020-09-15 16:21:41 +02:00
|
|
|
import {PositionNotifier} from "./PositionNotifier";
|
|
|
|
import {ViewportInterface} from "_Model/Websocket/ViewportMessage";
|
2020-09-16 16:06:43 +02:00
|
|
|
import {Movable} from "_Model/Movable";
|
2020-04-07 10:08:04 +02:00
|
|
|
|
2020-09-18 13:57:38 +02:00
|
|
|
export type ConnectCallback = (user: number, group: Group) => void;
|
|
|
|
export type DisconnectCallback = (user: number, group: Group) => void;
|
2020-05-03 16:08:04 +02:00
|
|
|
|
2020-04-07 10:08:04 +02:00
|
|
|
export class World {
|
2020-05-14 23:19:48 +02:00
|
|
|
private readonly minDistance: number;
|
|
|
|
private readonly groupRadius: number;
|
2020-04-08 20:40:44 +02:00
|
|
|
|
2020-04-07 10:08:04 +02:00
|
|
|
// Users, sorted by ID
|
2020-09-18 13:57:38 +02:00
|
|
|
private readonly users: Map<number, User>;
|
2020-06-29 22:13:07 +02:00
|
|
|
private readonly groups: Set<Group>;
|
2020-04-08 20:40:44 +02:00
|
|
|
|
2020-05-14 23:19:48 +02:00
|
|
|
private readonly connectCallback: ConnectCallback;
|
|
|
|
private readonly disconnectCallback: DisconnectCallback;
|
2020-04-07 10:08:04 +02:00
|
|
|
|
2020-07-27 22:36:07 +02:00
|
|
|
private itemsState: Map<number, unknown> = new Map<number, unknown>();
|
|
|
|
|
2020-09-15 16:21:41 +02:00
|
|
|
private readonly positionNotifier: PositionNotifier;
|
|
|
|
|
2020-05-03 17:47:54 +02:00
|
|
|
constructor(connectCallback: ConnectCallback,
|
|
|
|
disconnectCallback: DisconnectCallback,
|
2020-05-03 16:56:19 +02:00
|
|
|
minDistance: number,
|
2020-05-08 00:35:36 +02:00
|
|
|
groupRadius: number,
|
2020-09-16 16:06:43 +02:00
|
|
|
onEnters: EntersCallback,
|
|
|
|
onMoves: MovesCallback,
|
|
|
|
onLeaves: LeavesCallback)
|
2020-04-07 10:08:04 +02:00
|
|
|
{
|
2020-09-18 13:57:38 +02:00
|
|
|
this.users = new Map<number, User>();
|
2020-06-29 22:13:07 +02:00
|
|
|
this.groups = new Set<Group>();
|
2020-04-07 10:08:04 +02:00
|
|
|
this.connectCallback = connectCallback;
|
|
|
|
this.disconnectCallback = disconnectCallback;
|
2020-05-03 16:56:19 +02:00
|
|
|
this.minDistance = minDistance;
|
|
|
|
this.groupRadius = groupRadius;
|
2020-09-15 16:21:41 +02:00
|
|
|
// A zone is 10 sprites wide.
|
2020-09-16 16:06:43 +02:00
|
|
|
this.positionNotifier = new PositionNotifier(320, 320, onEnters, onMoves, onLeaves);
|
2020-04-28 23:23:50 +02:00
|
|
|
}
|
2020-04-07 10:08:04 +02:00
|
|
|
|
2020-05-13 23:11:10 +02:00
|
|
|
public getGroups(): Group[] {
|
2020-06-29 22:10:23 +02:00
|
|
|
return Array.from(this.groups.values());
|
2020-05-13 23:11:10 +02:00
|
|
|
}
|
|
|
|
|
2020-09-18 13:57:38 +02:00
|
|
|
public getUsers(): Map<number, User> {
|
2020-05-19 19:11:12 +02:00
|
|
|
return this.users;
|
|
|
|
}
|
|
|
|
|
2020-05-15 23:24:04 +02:00
|
|
|
public join(socket : Identificable, userPosition: PointInterface): void {
|
2020-09-16 16:06:43 +02:00
|
|
|
this.users.set(socket.userId, new User(socket.userId, userPosition, false));
|
2020-04-29 23:12:55 +02:00
|
|
|
// Let's call update position to trigger the join / leave room
|
2020-05-14 23:19:48 +02:00
|
|
|
this.updatePosition(socket, userPosition);
|
2020-04-07 10:08:04 +02:00
|
|
|
}
|
|
|
|
|
2020-05-14 23:19:48 +02:00
|
|
|
public leave(user : Identificable){
|
2020-06-09 15:54:54 +02:00
|
|
|
const userObj = this.users.get(user.userId);
|
2020-05-14 23:19:48 +02:00
|
|
|
if (userObj === undefined) {
|
2020-05-23 15:04:25 +02:00
|
|
|
console.warn('User ', user.userId, 'does not belong to world! It should!');
|
2020-05-14 23:19:48 +02:00
|
|
|
}
|
2020-04-29 23:18:42 +02:00
|
|
|
if (userObj !== undefined && typeof userObj.group !== 'undefined') {
|
2020-05-14 23:19:48 +02:00
|
|
|
this.leaveGroup(userObj);
|
2020-04-29 23:18:42 +02:00
|
|
|
}
|
2020-05-23 15:04:25 +02:00
|
|
|
this.users.delete(user.userId);
|
2020-09-15 16:21:41 +02:00
|
|
|
|
|
|
|
if (userObj !== undefined) {
|
|
|
|
this.positionNotifier.leave(userObj);
|
2020-09-16 16:06:43 +02:00
|
|
|
this.positionNotifier.removeViewport(userObj);
|
2020-09-15 16:21:41 +02:00
|
|
|
}
|
2020-04-29 01:40:32 +02:00
|
|
|
}
|
|
|
|
|
2020-06-29 19:14:54 +02:00
|
|
|
public isEmpty(): boolean {
|
|
|
|
return this.users.size === 0;
|
|
|
|
}
|
|
|
|
|
2020-05-15 23:24:04 +02:00
|
|
|
public updatePosition(socket : Identificable, userPosition: PointInterface): void {
|
2020-06-09 15:54:54 +02:00
|
|
|
const user = this.users.get(socket.userId);
|
2020-04-09 23:26:19 +02:00
|
|
|
if(typeof user === 'undefined') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-16 16:06:43 +02:00
|
|
|
this.positionNotifier.updatePosition(user, userPosition, user.position);
|
|
|
|
|
|
|
|
const oldGroupPosition = user.group?.getPosition();
|
2020-09-21 11:24:03 +02:00
|
|
|
user.group?.updatePosition();
|
2020-09-15 16:21:41 +02:00
|
|
|
|
2020-05-19 19:11:12 +02:00
|
|
|
user.position = userPosition;
|
2020-04-09 23:26:19 +02:00
|
|
|
|
2020-08-31 14:03:40 +02:00
|
|
|
if (user.silent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-16 16:06:43 +02:00
|
|
|
if (user.group === undefined) {
|
2020-04-09 23:26:19 +02:00
|
|
|
// If the user is not part of a group:
|
|
|
|
// should he join a group?
|
2020-09-16 16:06:43 +02:00
|
|
|
const closestItem: User|Group|null = this.searchClosestAvailableUserOrGroup(user);
|
2020-04-09 23:26:19 +02:00
|
|
|
|
2020-04-29 22:41:48 +02:00
|
|
|
if (closestItem !== null) {
|
|
|
|
if (closestItem instanceof Group) {
|
|
|
|
// Let's join the group!
|
|
|
|
closestItem.join(user);
|
|
|
|
} else {
|
2020-09-16 16:06:43 +02:00
|
|
|
const closestUser : User = closestItem;
|
2020-06-09 15:54:54 +02:00
|
|
|
const group: Group = new Group([
|
2020-04-09 23:26:19 +02:00
|
|
|
user,
|
|
|
|
closestUser
|
|
|
|
], this.connectCallback, this.disconnectCallback);
|
2020-06-29 22:13:07 +02:00
|
|
|
this.groups.add(group);
|
2020-04-09 23:26:19 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-28 23:23:50 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// If the user is part of a group:
|
2020-04-29 23:12:55 +02:00
|
|
|
// should he leave the group?
|
2020-06-09 15:54:54 +02:00
|
|
|
const distance = World.computeDistanceBetweenPositions(user.position, user.group.getPosition());
|
2020-05-03 16:56:19 +02:00
|
|
|
if (distance > this.groupRadius) {
|
2020-04-29 23:12:55 +02:00
|
|
|
this.leaveGroup(user);
|
|
|
|
}
|
|
|
|
}
|
2020-05-08 00:35:36 +02:00
|
|
|
|
|
|
|
// At the very end, if the user is part of a group, let's call the callback to update group position
|
2020-09-21 11:24:03 +02:00
|
|
|
if (user.group !== undefined) {
|
2020-09-16 16:06:43 +02:00
|
|
|
this.positionNotifier.updatePosition(user.group, user.group.getPosition(), oldGroupPosition ? oldGroupPosition : user.group.getPosition());
|
2020-05-08 00:35:36 +02:00
|
|
|
}
|
2020-04-29 23:12:55 +02:00
|
|
|
}
|
2020-04-28 23:23:50 +02:00
|
|
|
|
2020-08-31 14:03:40 +02:00
|
|
|
setSilent(socket: Identificable, silent: boolean) {
|
|
|
|
const user = this.users.get(socket.userId);
|
|
|
|
if(typeof user === 'undefined') {
|
|
|
|
console.warn('In setSilent, could not find user with ID "'+socket.userId+'" in world.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (user.silent === silent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
user.silent = silent;
|
|
|
|
if (silent && user.group !== undefined) {
|
|
|
|
this.leaveGroup(user);
|
|
|
|
}
|
|
|
|
if (!silent) {
|
|
|
|
// If we are back to life, let's trigger a position update to see if we can join some group.
|
|
|
|
this.updatePosition(socket, user.position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 23:12:55 +02:00
|
|
|
/**
|
|
|
|
* Makes a user leave a group and closes and destroy the group if the group contains only one remaining person.
|
|
|
|
*
|
|
|
|
* @param user
|
|
|
|
*/
|
2020-09-16 16:06:43 +02:00
|
|
|
private leaveGroup(user: User): void {
|
2020-06-09 15:54:54 +02:00
|
|
|
const group = user.group;
|
2020-09-21 11:24:03 +02:00
|
|
|
if (group === undefined) {
|
2020-04-29 23:12:55 +02:00
|
|
|
throw new Error("The user is part of no group");
|
|
|
|
}
|
|
|
|
group.leave(user);
|
|
|
|
if (group.isEmpty()) {
|
2020-09-16 16:06:43 +02:00
|
|
|
this.positionNotifier.leave(group);
|
2020-04-29 23:12:55 +02:00
|
|
|
group.destroy();
|
2020-06-29 22:13:07 +02:00
|
|
|
if (!this.groups.has(group)) {
|
2020-06-29 22:10:23 +02:00
|
|
|
throw new Error("Could not find group "+group.getId()+" referenced by user "+user.id+" in World.");
|
2020-04-29 23:12:55 +02:00
|
|
|
}
|
2020-06-29 22:13:07 +02:00
|
|
|
this.groups.delete(group);
|
2020-05-08 00:35:36 +02:00
|
|
|
} else {
|
2020-09-16 16:06:43 +02:00
|
|
|
this.positionNotifier.updatePosition(group, group.getPosition(), group.getPosition());
|
2020-04-09 23:26:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Looks for the closest user that is:
|
2020-05-03 16:56:19 +02:00
|
|
|
* - close enough (distance <= minDistance)
|
|
|
|
* - not in a group
|
2020-08-31 14:03:40 +02:00
|
|
|
* - not silent
|
2020-05-03 16:56:19 +02:00
|
|
|
* OR
|
|
|
|
* - close enough to a group (distance <= groupRadius)
|
2020-04-09 23:26:19 +02:00
|
|
|
*/
|
2020-09-16 16:06:43 +02:00
|
|
|
private searchClosestAvailableUserOrGroup(user: User): User|Group|null
|
2020-04-09 23:26:19 +02:00
|
|
|
{
|
2020-05-03 16:56:19 +02:00
|
|
|
let minimumDistanceFound: number = Math.max(this.minDistance, this.groupRadius);
|
2020-09-16 16:06:43 +02:00
|
|
|
let matchingItem: User | Group | null = null;
|
2020-05-03 16:56:19 +02:00
|
|
|
this.users.forEach((currentUser, userId) => {
|
2020-04-29 22:41:48 +02:00
|
|
|
// Let's only check users that are not part of a group
|
|
|
|
if (typeof currentUser.group !== 'undefined') {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-09 23:26:19 +02:00
|
|
|
if(currentUser === user) {
|
|
|
|
return;
|
|
|
|
}
|
2020-08-31 14:03:40 +02:00
|
|
|
if (currentUser.silent) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-09 23:26:19 +02:00
|
|
|
|
2020-06-09 15:54:54 +02:00
|
|
|
const distance = World.computeDistance(user, currentUser); // compute distance between peers.
|
2020-04-28 23:23:50 +02:00
|
|
|
|
2020-05-03 16:56:19 +02:00
|
|
|
if(distance <= minimumDistanceFound && distance <= this.minDistance) {
|
2020-04-29 22:41:48 +02:00
|
|
|
minimumDistanceFound = distance;
|
|
|
|
matchingItem = currentUser;
|
|
|
|
}
|
|
|
|
/*if (typeof currentUser.group === 'undefined' || !currentUser.group.isFull()) {
|
2020-04-09 23:26:19 +02:00
|
|
|
// We found a user we can bind to.
|
|
|
|
return;
|
2020-04-29 22:41:48 +02:00
|
|
|
}*/
|
2020-04-09 23:26:19 +02:00
|
|
|
/*
|
2020-04-08 20:40:44 +02:00
|
|
|
if(context.groups.length > 0) {
|
2020-04-28 23:23:50 +02:00
|
|
|
|
2020-04-08 20:40:44 +02:00
|
|
|
context.groups.forEach(group => {
|
|
|
|
if(group.isPartOfGroup(userPosition)) { // Is the user in a group ?
|
|
|
|
if(group.isStillIn(userPosition)) { // Is the user leaving the group ? (is the user at more than max distance of each player)
|
2020-04-28 23:23:50 +02:00
|
|
|
|
2020-04-08 20:40:44 +02:00
|
|
|
// 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
|
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If the user is in no group
|
|
|
|
// Is there someone in a group close enough and with room in the group ?
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Aucun groupe n'existe donc je stock les users assez proches de moi
|
2020-04-09 23:26:19 +02:00
|
|
|
let dist: Distance = {
|
2020-04-08 20:40:44 +02:00
|
|
|
distance: distance,
|
|
|
|
first: userPosition,
|
|
|
|
second: user // TODO: convertir en messageUserPosition
|
|
|
|
}
|
|
|
|
usersToBeGroupedWith.push(dist);
|
|
|
|
}
|
2020-04-09 23:26:19 +02:00
|
|
|
*/
|
2020-04-29 22:41:48 +02:00
|
|
|
});
|
|
|
|
|
2020-05-03 16:56:19 +02:00
|
|
|
this.groups.forEach((group: Group) => {
|
2020-04-29 22:41:48 +02:00
|
|
|
if (group.isFull()) {
|
|
|
|
return;
|
2020-04-08 20:40:44 +02:00
|
|
|
}
|
2020-06-09 15:54:54 +02:00
|
|
|
const distance = World.computeDistanceBetweenPositions(user.position, group.getPosition());
|
2020-05-03 16:56:19 +02:00
|
|
|
if(distance <= minimumDistanceFound && distance <= this.groupRadius) {
|
2020-04-29 22:41:48 +02:00
|
|
|
minimumDistanceFound = distance;
|
|
|
|
matchingItem = group;
|
|
|
|
}
|
|
|
|
});
|
2020-04-08 20:40:44 +02:00
|
|
|
|
2020-04-29 22:41:48 +02:00
|
|
|
return matchingItem;
|
2020-04-08 20:40:44 +02:00
|
|
|
}
|
|
|
|
|
2020-09-16 16:06:43 +02:00
|
|
|
public static computeDistance(user1: User, user2: User): number
|
2020-04-08 20:40:44 +02:00
|
|
|
{
|
2020-04-09 23:26:19 +02:00
|
|
|
return Math.sqrt(Math.pow(user2.position.x - user1.position.x, 2) + Math.pow(user2.position.y - user1.position.y, 2));
|
2020-04-08 20:40:44 +02:00
|
|
|
}
|
2020-04-07 10:08:04 +02:00
|
|
|
|
2020-04-29 22:41:48 +02:00
|
|
|
public static computeDistanceBetweenPositions(position1: PositionInterface, position2: PositionInterface): number
|
|
|
|
{
|
|
|
|
return Math.sqrt(Math.pow(position2.x - position1.x, 2) + Math.pow(position2.y - position1.y, 2));
|
|
|
|
}
|
|
|
|
|
2020-07-27 22:36:07 +02:00
|
|
|
public setItemState(itemId: number, state: unknown) {
|
|
|
|
this.itemsState.set(itemId, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
public getItemsState(): Map<number, unknown> {
|
|
|
|
return this.itemsState;
|
|
|
|
}
|
|
|
|
|
2020-04-09 23:26:19 +02:00
|
|
|
/*getDistancesBetweenGroupUsers(group: Group): Distance[]
|
2020-04-08 20:40:44 +02:00
|
|
|
{
|
|
|
|
let i = 0;
|
|
|
|
let users = group.getUsers();
|
|
|
|
let distances: Distance[] = [];
|
|
|
|
users.forEach(function(user1, key1) {
|
|
|
|
users.forEach(function(user2, key2) {
|
|
|
|
if(key1 < key2) {
|
|
|
|
distances[i] = {
|
2020-04-09 23:26:19 +02:00
|
|
|
distance: World.computeDistance(user1, user2),
|
2020-04-08 20:40:44 +02:00
|
|
|
first: user1,
|
|
|
|
second: user2
|
|
|
|
};
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2020-04-28 23:23:50 +02:00
|
|
|
|
2020-04-08 20:40:44 +02:00
|
|
|
distances.sort(World.compareDistances);
|
2020-04-07 10:08:04 +02:00
|
|
|
|
2020-04-08 20:40:44 +02:00
|
|
|
return distances;
|
|
|
|
}
|
|
|
|
|
|
|
|
filterGroup(distances: Distance[], group: Group): void
|
|
|
|
{
|
|
|
|
let users = group.getUsers();
|
|
|
|
let usersToRemove = false;
|
|
|
|
let groupTmp: MessageUserPosition[] = [];
|
|
|
|
distances.forEach(dist => {
|
|
|
|
if(dist.distance <= World.MIN_DISTANCE) {
|
|
|
|
let users = [dist.first];
|
|
|
|
let usersbis = [dist.second]
|
|
|
|
groupTmp.push(dist.first);
|
|
|
|
groupTmp.push(dist.second);
|
|
|
|
} else {
|
|
|
|
usersToRemove = true;
|
|
|
|
}
|
|
|
|
});
|
2020-04-07 10:08:04 +02:00
|
|
|
|
2020-04-08 20:40:44 +02:00
|
|
|
if(usersToRemove) {
|
|
|
|
// Detecte le ou les users qui se sont fait sortir du groupe
|
|
|
|
let difference = users.filter(x => !groupTmp.includes(x));
|
2020-04-07 10:08:04 +02:00
|
|
|
|
2020-04-28 23:23:50 +02:00
|
|
|
// TODO : Notify users un difference that they have left the group
|
2020-04-08 20:40:44 +02:00
|
|
|
}
|
2020-04-07 10:08:04 +02:00
|
|
|
|
2020-04-08 20:40:44 +02:00
|
|
|
let newgroup = new Group(groupTmp);
|
|
|
|
this.groups.push(newgroup);
|
2020-04-07 10:08:04 +02:00
|
|
|
}
|
|
|
|
|
2020-04-08 20:40:44 +02:00
|
|
|
private static compareDistances(distA: Distance, distB: Distance): number
|
|
|
|
{
|
|
|
|
if (distA.distance < distB.distance) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (distA.distance > distB.distance) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2020-04-09 23:26:19 +02:00
|
|
|
}*/
|
2020-09-16 16:06:43 +02:00
|
|
|
setViewport(socket : Identificable, viewport: ViewportInterface): Movable[] {
|
2020-09-15 16:21:41 +02:00
|
|
|
const user = this.users.get(socket.userId);
|
|
|
|
if(typeof user === 'undefined') {
|
|
|
|
console.warn('In setViewport, could not find user with ID "'+socket.userId+'" in world.');
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return this.positionNotifier.setViewport(user, viewport);
|
|
|
|
}
|
2020-04-28 23:23:50 +02:00
|
|
|
}
|