Cleanup; pretty

This commit is contained in:
PizZaKatZe 2021-12-18 11:30:58 +01:00
parent c96b65549f
commit 5c385c520a
5 changed files with 20 additions and 18 deletions

View File

@ -224,19 +224,18 @@ export class GameRoom {
} else { } else {
// 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?
const leaveIfOutOfRadius = (user: User) => { const users = user.group.getUsers().filter((u) => !u.hasFollowers() && !u.following);
if (user.group === undefined) { users.forEach((foreignUser: User) => {
if (foreignUser.group === undefined) {
return; return;
} }
const usrPos = user.getPosition(); const usrPos = foreignUser.getPosition();
const grpPos = user.group.getPosition(); const grpPos = foreignUser.group.getPosition();
const distance = GameRoom.computeDistanceBetweenPositions(usrPos, grpPos); const distance = GameRoom.computeDistanceBetweenPositions(usrPos, grpPos);
if (distance > this.groupRadius) { if (distance > this.groupRadius) {
this.leaveGroup(user); this.leaveGroup(foreignUser);
} }
}; });
const users = user.group.getUsers().filter((u) => !u.hasFollowers() && !u.following);
users.forEach((foreignUser) => leaveIfOutOfRadius(foreignUser));
} }
} }

View File

@ -156,7 +156,7 @@ export class Group implements Movable {
/** /**
* A group can have at most one person leading the way in it. * A group can have at most one person leading the way in it.
*/ */
get leader(): User|undefined { get leader(): User | undefined {
for (const user of this.users) { for (const user of this.users) {
if (user.hasFollowers()) { if (user.hasFollowers()) {
return user; return user;

View File

@ -6,7 +6,9 @@ import { PositionNotifier } from "_Model/PositionNotifier";
import { ServerDuplexStream } from "grpc"; import { ServerDuplexStream } from "grpc";
import { import {
BatchMessage, BatchMessage,
CompanionMessage, FollowAbortMessage, FollowConfirmationMessage, CompanionMessage,
FollowAbortMessage,
FollowConfirmationMessage,
PusherToBackMessage, PusherToBackMessage,
ServerToClientMessage, ServerToClientMessage,
SubMessage, SubMessage,
@ -18,7 +20,7 @@ export type UserSocket = ServerDuplexStream<PusherToBackMessage, ServerToClientM
export class User implements Movable { export class User implements Movable {
public listenedZones: Set<Zone>; public listenedZones: Set<Zone>;
public group?: Group; public group?: Group;
private _following: User|undefined; private _following: User | undefined;
private followedBy: Set<User> = new Set<User>(); private followedBy: Set<User> = new Set<User>();
public constructor( public constructor(

View File

@ -846,12 +846,13 @@ export class SocketManager {
handleFollowConfirmationMessage(room: GameRoom, user: User, message: FollowConfirmationMessage) { handleFollowConfirmationMessage(room: GameRoom, user: User, message: FollowConfirmationMessage) {
const leader = room.getUserById(message.getLeader()); const leader = room.getUserById(message.getLeader());
if (!leader) { if (!leader) {
console.info('Could not find user "', message.getLeader(), '" while handling a follow confirmation in room "', room.roomUrl,'". Maybe the user just left.'); const message = `Could not follow user "{message.getLeader()}" in room "{room.roomUrl}".`;
console.info(message, "Maybe the user just left.");
return; return;
} }
// By security, we look at the group leader. If the group leader is NOT the leader in the message, everybody should // By security, we look at the group leader. If the group leader is NOT the leader in the message,
// stop following the group leader (to avoid having 2 group leaders) // everybody should stop following the group leader (to avoid having 2 group leaders)
if (user?.group?.leader && user?.group?.leader !== leader) { if (user?.group?.leader && user?.group?.leader !== leader) {
user?.group?.leader?.stopLeading(); user?.group?.leader?.stopLeading();
} }

View File

@ -26,14 +26,14 @@ describe("PositionNotifier", () => {
y: 500, y: 500,
moving: false, moving: false,
direction: 'down' direction: 'down'
}, false, [], positionNotifier, {} as UserSocket, [], null, 'foo', []); }, false, positionNotifier, {} as UserSocket, [], null, 'foo', []);
const user2 = new User(2, 'test', '10.0.0.2', { const user2 = new User(2, 'test', '10.0.0.2', {
x: -9999, x: -9999,
y: -9999, y: -9999,
moving: false, moving: false,
direction: 'down' direction: 'down'
}, false, [], positionNotifier, {} as UserSocket, [], null, 'foo', []); }, false, positionNotifier, {} as UserSocket, [], null, 'foo', []);
positionNotifier.addZoneListener({} as ZoneSocket, 0, 0); positionNotifier.addZoneListener({} as ZoneSocket, 0, 0);
positionNotifier.addZoneListener({} as ZoneSocket, 0, 1); positionNotifier.addZoneListener({} as ZoneSocket, 0, 1);
@ -101,14 +101,14 @@ describe("PositionNotifier", () => {
y: 500, y: 500,
moving: false, moving: false,
direction: 'down' direction: 'down'
}, false, [], positionNotifier, {} as UserSocket, [], null, 'foo', []); }, false, positionNotifier, {} as UserSocket, [], null, 'foo', []);
const user2 = new User(2, 'test', '10.0.0.2', { const user2 = new User(2, 'test', '10.0.0.2', {
x: 0, x: 0,
y: 0, y: 0,
moving: false, moving: false,
direction: 'down' direction: 'down'
}, false, [], positionNotifier, {} as UserSocket, [], null, 'foo', []); }, false, positionNotifier, {} as UserSocket, [], null, 'foo', []);
const listener = {} as ZoneSocket; const listener = {} as ZoneSocket;
positionNotifier.addZoneListener(listener, 0, 0); positionNotifier.addZoneListener(listener, 0, 0);