From 7da8a6138f1e684b11f613901461fc157557100b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Tue, 28 Apr 2020 23:01:56 +0200 Subject: [PATCH 1/2] Adding a (failing test) for disconnecting users --- back/tests/WorldTest.ts | 52 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/back/tests/WorldTest.ts b/back/tests/WorldTest.ts index 1f5affc8..4511efc3 100644 --- a/back/tests/WorldTest.ts +++ b/back/tests/WorldTest.ts @@ -12,7 +12,7 @@ describe("World", () => { connectCalled = true; } let disconnect = (user1: string, user2: string): void => { - + } let world = new World(connect, disconnect); @@ -53,14 +53,58 @@ describe("World", () => { })); expect(connectCalled).toBe(false); }); - /** + + it("should disconnect user1 and user2", () => { + let connectCalled: boolean = false; + let disconnectCalled: boolean = false; + let connect = (user1: string, user2: string): void => { + connectCalled = true; + } + let disconnect = (user1: string, user2: string): void => { + disconnectCalled = true; + } + + let world = new World(connect, disconnect); + + world.join(new MessageUserPosition({ + userId: "foo", + roomId: 1, + position: new Point(100, 100) + })); + + world.join(new MessageUserPosition({ + userId: "bar", + roomId: 1, + position: new Point(259, 100) + })); + + expect(connectCalled).toBe(false); + + world.updatePosition(new MessageUserPosition({ + userId: "bar", + roomId: 1, + position: new Point(261, 100) + })); + + expect(disconnectCalled).toBe(true); + + disconnectCalled = false; + world.updatePosition(new MessageUserPosition({ + userId: "bar", + roomId: 1, + position: new Point(262, 100) + })); + expect(disconnectCalled).toBe(false); + }); + + /** it('Should return the distances between all users', () => { let connectCalled: boolean = false; let connect = (user1: string, user2: string): void => { connectCalled = true; } let disconnect = (user1: string, user2: string): void => { - + } let world = new World(connect, disconnect); @@ -100,4 +144,4 @@ describe("World", () => { //expect(distances).toBe([]); }) **/ -}) \ No newline at end of file +}) From abeac558fdaa7e16d27c52e8eaf0daecad3c5ad5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Tue, 28 Apr 2020 23:23:50 +0200 Subject: [PATCH 2/2] Adding TODO comment --- back/src/Model/World.ts | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/back/src/Model/World.ts b/back/src/Model/World.ts index 804a176b..d1fe3709 100644 --- a/back/src/Model/World.ts +++ b/back/src/Model/World.ts @@ -14,13 +14,13 @@ 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) { this.users = new Map(); this.groups = []; this.connectCallback = connectCallback; this.disconnectCallback = disconnectCallback; - } + } public join(userPosition: MessageUserPosition): void { this.users.set(userPosition.userId, { @@ -55,9 +55,17 @@ export class World { closestUser.group.join(user); } } - + + } else { + // If the user is part of a group: + // should we split the group? + + // TODO: analyze the group of the user: + // => take one user. "walk the tree of users, each branch being a link 0) { - + 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) - + // Should we split the group? (is each player reachable from the current player?) // This is needed if // A <==> B <==> C <===> D @@ -136,7 +144,7 @@ export class World { } */ } - + }, this.users); return matchingUser; @@ -164,7 +172,7 @@ export class World { } }); }); - + distances.sort(World.compareDistances); return distances; @@ -190,7 +198,7 @@ export class World { // Detecte le ou les users qui se sont fait sortir du groupe let difference = users.filter(x => !groupTmp.includes(x)); - // TODO : Notify users un difference that they have left the group + // TODO : Notify users un difference that they have left the group } let newgroup = new Group(groupTmp); @@ -207,4 +215,4 @@ export class World { } return 0; }*/ -} \ No newline at end of file +}