Merge pull request #81 from thecodingmachine/display_groups

Adding the display of a circle around the group
This commit is contained in:
David Négrier 2020-05-08 16:20:56 +02:00 committed by GitHub
commit e4824fe34d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 161 additions and 13 deletions

View file

@ -9,6 +9,7 @@ import {ExtRooms, RefreshUserPositionFunction} from "../Model/Websocket/ExtRoom"
import {ExtRoomsInterface} from "../Model/Websocket/ExtRoomsInterface";
import {World} from "../Model/World";
import {Group} from "_Model/Group";
import {UserInterface} from "_Model/UserInterface";
enum SockerIoEvent {
CONNECTION = "connection",
@ -20,6 +21,8 @@ enum SockerIoEvent {
WEBRTC_START = "webrtc-start",
WEBRTC_DISCONNECT = "webrtc-disconect",
MESSAGE_ERROR = "message-error",
GROUP_CREATE_UPDATE = "group-create-update",
GROUP_DELETE = "group-delete",
}
export class IoSocketController {
@ -51,7 +54,38 @@ export class IoSocketController {
this.connectedUser(user1, group);
}, (user1: string, group: Group) => {
this.disConnectedUser(user1, group);
}, MINIMUM_DISTANCE, GROUP_RADIUS);
}, MINIMUM_DISTANCE, GROUP_RADIUS, (group: Group) => {
this.sendUpdateGroupEvent(group);
}, (groupUuid: string, lastUser: UserInterface) => {
this.sendDeleteGroupEvent(groupUuid, lastUser);
});
}
private sendUpdateGroupEvent(group: Group): void {
// Let's get the room of the group. To do this, let's get anyone in the group and find its room.
// Note: this is suboptimal
let userId = group.getUsers()[0].id;
let client: ExSocketInterface|null = this.searchClientById(userId);
if (client === null) {
return;
}
let roomId = client.roomId;
this.Io.in(roomId).emit(SockerIoEvent.GROUP_CREATE_UPDATE, {
position: group.getPosition(),
groupId: group.getId()
});
}
private sendDeleteGroupEvent(uuid: string, lastUser: UserInterface): void {
// Let's get the room of the group. To do this, let's get anyone in the group and find its room.
// Note: this is suboptimal
let userId = lastUser.id;
let client: ExSocketInterface|null = this.searchClientById(userId);
if (client === null) {
return;
}
let roomId = client.roomId;
this.Io.in(roomId).emit(SockerIoEvent.GROUP_DELETE, uuid);
}
ioConnection() {
@ -146,7 +180,7 @@ export class IoSocketController {
}
/**
*
* TODO: each call to this method is suboptimal. It means that instead of passing an ID, we should pass a client object.
* @param userId
*/
searchClientById(userId: string): ExSocketInterface | null {
@ -286,7 +320,7 @@ export class IoSocketController {
this.joinWebRtcRoom(Client, group.getId());
}
//connected user
//disconnect user
disConnectedUser(userId: string, group: Group) {
let Client = this.searchClientById(userId);
if (!Client) {

View file

@ -9,6 +9,10 @@ import {PositionInterface} from "_Model/PositionInterface";
export type ConnectCallback = (user: string, group: Group) => void;
export type DisconnectCallback = (user: string, group: Group) => void;
// callback called when a group is created or moved or changes users
export type GroupUpdatedCallback = (group: Group) => void;
export type GroupDeletedCallback = (uuid: string, lastUser: UserInterface) => void;
export class World {
private minDistance: number;
private groupRadius: number;
@ -19,11 +23,15 @@ export class World {
private connectCallback: ConnectCallback;
private disconnectCallback: DisconnectCallback;
private groupUpdatedCallback: GroupUpdatedCallback;
private groupDeletedCallback: GroupDeletedCallback;
constructor(connectCallback: ConnectCallback,
disconnectCallback: DisconnectCallback,
minDistance: number,
groupRadius: number)
groupRadius: number,
groupUpdatedCallback: GroupUpdatedCallback,
groupDeletedCallback: GroupDeletedCallback)
{
this.users = new Map<string, UserInterface>();
this.groups = [];
@ -31,6 +39,8 @@ export class World {
this.disconnectCallback = disconnectCallback;
this.minDistance = minDistance;
this.groupRadius = groupRadius;
this.groupUpdatedCallback = groupUpdatedCallback;
this.groupDeletedCallback = groupDeletedCallback;
}
public join(userPosition: MessageUserPosition): void {
@ -86,6 +96,11 @@ export class World {
this.leaveGroup(user);
}
}
// At the very end, if the user is part of a group, let's call the callback to update group position
if (typeof user.group !== 'undefined') {
this.groupUpdatedCallback(user.group);
}
}
/**
@ -101,12 +116,15 @@ export class World {
group.leave(user);
if (group.isEmpty()) {
this.groupDeletedCallback(group.getId(), user);
group.destroy();
const index = this.groups.indexOf(group, 0);
if (index === -1) {
throw new Error("Could not find group");
}
this.groups.splice(index, 1);
} else {
this.groupUpdatedCallback(group);
}
}

View file

@ -15,7 +15,7 @@ describe("World", () => {
}
let world = new World(connect, disconnect, 160, 160);
let world = new World(connect, disconnect, 160, 160, () => {}, () => {});
world.join(new MessageUserPosition({
userId: "foo",
@ -62,7 +62,7 @@ describe("World", () => {
}
let world = new World(connect, disconnect, 160, 160);
let world = new World(connect, disconnect, 160, 160, () => {}, () => {});
world.join(new MessageUserPosition({
userId: "foo",
@ -107,7 +107,7 @@ describe("World", () => {
disconnectCallNumber++;
}
let world = new World(connect, disconnect, 160, 160);
let world = new World(connect, disconnect, 160, 160, () => {}, () => {});
world.join(new MessageUserPosition({
userId: "foo",