Refactoring following code to make a distinction beween followed and following users.
Also, moving sending messages to the User class.
This commit is contained in:
parent
fd9cb09de6
commit
cd805fab31
@ -125,7 +125,6 @@ export class GameRoom {
|
||||
joinRoomMessage.getIpaddress(),
|
||||
position,
|
||||
false,
|
||||
[],
|
||||
this.positionNotifier,
|
||||
socket,
|
||||
joinRoomMessage.getTagList(),
|
||||
@ -228,7 +227,7 @@ export class GameRoom {
|
||||
this.leaveGroup(user);
|
||||
}
|
||||
};
|
||||
const users = user.group.getUsers().filter((u) => u.following.length === 0);
|
||||
const users = user.group.getUsers().filter((u) => !u.hasFollowers() && !u.following);
|
||||
users.forEach((foreignUser) => leaveIfOutOfRadius(foreignUser));
|
||||
}
|
||||
}
|
||||
|
@ -152,4 +152,16 @@ export class Group implements Movable {
|
||||
get getSize() {
|
||||
return this.users.size;
|
||||
}
|
||||
|
||||
/**
|
||||
* A group can have at most one person leading the way in it.
|
||||
*/
|
||||
get leader(): User|undefined {
|
||||
for (const user of this.users) {
|
||||
if (user.hasFollowers()) {
|
||||
return user;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ import { PositionNotifier } from "_Model/PositionNotifier";
|
||||
import { ServerDuplexStream } from "grpc";
|
||||
import {
|
||||
BatchMessage,
|
||||
CompanionMessage,
|
||||
CompanionMessage, FollowAbortMessage, FollowConfirmationMessage,
|
||||
PusherToBackMessage,
|
||||
ServerToClientMessage,
|
||||
SubMessage,
|
||||
@ -18,6 +18,8 @@ export type UserSocket = ServerDuplexStream<PusherToBackMessage, ServerToClientM
|
||||
export class User implements Movable {
|
||||
public listenedZones: Set<Zone>;
|
||||
public group?: Group;
|
||||
private _following: User|undefined;
|
||||
private followedBy: Set<User> = new Set<User>();
|
||||
|
||||
public constructor(
|
||||
public id: number,
|
||||
@ -25,7 +27,6 @@ export class User implements Movable {
|
||||
public readonly IPAddress: string,
|
||||
private position: PointInterface,
|
||||
public silent: boolean,
|
||||
public following: number[],
|
||||
private positionNotifier: PositionNotifier,
|
||||
public readonly socket: UserSocket,
|
||||
public readonly tags: string[],
|
||||
@ -49,19 +50,43 @@ export class User implements Movable {
|
||||
this.positionNotifier.updatePosition(this, position, oldPosition);
|
||||
}
|
||||
|
||||
public addFollower(userId: number): void {
|
||||
if (this.following.includes(userId)) {
|
||||
return;
|
||||
}
|
||||
this.following.push(userId);
|
||||
public addFollower(follower: User): void {
|
||||
this.followedBy.add(follower);
|
||||
follower._following = this;
|
||||
|
||||
const message = new FollowConfirmationMessage();
|
||||
message.setFollower(follower.id);
|
||||
message.setLeader(this.id);
|
||||
const clientMessage = new ServerToClientMessage();
|
||||
clientMessage.setFollowconfirmationmessage(message);
|
||||
this.socket.write(clientMessage);
|
||||
}
|
||||
|
||||
public delFollower(userId: number): void {
|
||||
const idx = this.following.indexOf(userId);
|
||||
if (idx === -1) {
|
||||
return;
|
||||
public delFollower(follower: User): void {
|
||||
this.followedBy.delete(follower);
|
||||
follower._following = undefined;
|
||||
|
||||
const message = new FollowAbortMessage();
|
||||
message.setFollower(follower.id);
|
||||
message.setLeader(this.id);
|
||||
const clientMessage = new ServerToClientMessage();
|
||||
clientMessage.setFollowabortmessage(message);
|
||||
this.socket.write(clientMessage);
|
||||
follower.socket.write(clientMessage);
|
||||
}
|
||||
|
||||
public hasFollowers(): boolean {
|
||||
return this.followedBy.size !== 0;
|
||||
}
|
||||
|
||||
get following(): User | undefined {
|
||||
return this._following;
|
||||
}
|
||||
|
||||
public stopLeading(): void {
|
||||
for (const follower of this.followedBy) {
|
||||
this.delFollower(follower);
|
||||
}
|
||||
this.following.splice(idx, 1);
|
||||
}
|
||||
|
||||
private batchedMessages: BatchMessage = new BatchMessage();
|
||||
|
@ -844,34 +844,28 @@ export class SocketManager {
|
||||
}
|
||||
|
||||
handleFollowConfirmationMessage(room: GameRoom, user: User, message: FollowConfirmationMessage) {
|
||||
const clientMessage = new ServerToClientMessage();
|
||||
clientMessage.setFollowconfirmationmessage(message);
|
||||
const leader = room.getUserById(message.getLeader());
|
||||
leader?.socket.write(clientMessage);
|
||||
if (!leader) {
|
||||
console.info('Could not find user "', message.getLeader(), '" while handling a follow confirmation in room "', room.roomUrl,'". Maybe the user just left.');
|
||||
return;
|
||||
}
|
||||
|
||||
leader?.addFollower(user.id);
|
||||
user.addFollower(message.getLeader());
|
||||
// By security, we look at the group leader. If the group leader is NOT the leader in the message, everybody should
|
||||
// stop following the group leader (to avoid having 2 group leaders)
|
||||
if (user?.group?.leader && user?.group?.leader !== leader) {
|
||||
user?.group?.leader?.stopLeading();
|
||||
}
|
||||
|
||||
leader.addFollower(user);
|
||||
}
|
||||
|
||||
handleFollowAbortMessage(room: GameRoom, user: User, message: FollowAbortMessage) {
|
||||
const clientMessage = new ServerToClientMessage();
|
||||
clientMessage.setFollowabortmessage(message);
|
||||
if (user.id === message.getLeader()) {
|
||||
// Forward message
|
||||
room.sendToOthersInGroupIncludingUser(user, clientMessage);
|
||||
|
||||
// Update followers
|
||||
user.group?.getUsers().forEach((user) => {
|
||||
user.following = [];
|
||||
});
|
||||
user?.group?.leader?.stopLeading();
|
||||
} else {
|
||||
// Forward message
|
||||
const leader = room.getUserById(message.getLeader());
|
||||
leader?.socket.write(clientMessage);
|
||||
|
||||
// Update followers
|
||||
leader?.delFollower(user.id);
|
||||
user.following = [];
|
||||
leader?.delFollower(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user