Use user IDs instead of names

This commit is contained in:
PizZaKatZe 2021-12-15 14:48:45 +01:00
parent e528682403
commit 2bd71790b5
9 changed files with 48 additions and 57 deletions

View File

@ -96,11 +96,6 @@ export class GameRoom {
return this.users;
}
public getUserByName(name: string): User | undefined {
let foundUsers = Array.from(this.users.values());
foundUsers = foundUsers.filter((user: User) => user.name === name);
return foundUsers[0];
}
public getUserByUuid(uuid: string): User | undefined {
return this.usersByUuid.get(uuid);
}
@ -237,10 +232,6 @@ export class GameRoom {
});
}
public sendToUserWithName(name: string, message: ServerToClientMessage): void {
this.getUserByName(name)?.socket.write(message);
}
setSilent(user: User, silent: boolean) {
if (user.silent === silent) {
return;

View File

@ -25,7 +25,7 @@ export class User implements Movable {
public readonly IPAddress: string,
private position: PointInterface,
public silent: boolean,
public following: string[],
public following: number[],
private positionNotifier: PositionNotifier,
public readonly socket: UserSocket,
public readonly tags: string[],
@ -49,15 +49,15 @@ export class User implements Movable {
this.positionNotifier.updatePosition(this, position, oldPosition);
}
public addFollower(name: string): void {
if (this.following.includes(name)) {
public addFollower(userId: number): void {
if (this.following.includes(userId)) {
return;
}
this.following.push(name);
this.following.push(userId);
}
public delFollower(name: string): void {
const idx = this.following.indexOf(name);
public delFollower(userId: number): void {
const idx = this.following.indexOf(userId);
if (idx === -1) {
return;
}

View File

@ -846,16 +846,17 @@ export class SocketManager {
handleFollowConfirmationMessage(room: GameRoom, user: User, message: FollowConfirmationMessage) {
const clientMessage = new ServerToClientMessage();
clientMessage.setFollowconfirmationmessage(message);
room.sendToUserWithName(message.getLeader(), clientMessage);
const leader = room.getUserById(message.getLeader());
leader?.socket.write(clientMessage);
room.getUserByName(message.getLeader())?.addFollower(user.name);
leader?.addFollower(user.id);
user.addFollower(message.getLeader());
}
handleFollowAbortMessage(room: GameRoom, user: User, message: FollowAbortMessage) {
const clientMessage = new ServerToClientMessage();
clientMessage.setFollowabortmessage(message);
if (user.name === message.getLeader()) {
if (user.id === message.getLeader()) {
// Forward message
room.sendToOthersInGroupIncludingUser(user, clientMessage);
@ -865,10 +866,11 @@ export class SocketManager {
});
} else {
// Forward message
room.sendToUserWithName(message.getLeader(), clientMessage);
const leader = room.getUserById(message.getLeader());
leader?.socket.write(clientMessage);
// Update followers
room.getUserByName(message.getLeader())?.delFollower(user.name);
leader?.delFollower(user.id);
user.following = [];
}
}

View File

@ -19,7 +19,7 @@ vim: ft=typescript
let followState: string;
let followRole: string;
let followUsers: string[];
let followUsers: number[];
let stateUnsubscriber: Unsubscriber;
let roleUnsubscriber: Unsubscriber;
let nameUnsubscriber: Unsubscriber;
@ -51,14 +51,18 @@ vim: ft=typescript
}
});
function name(userId: number): string | undefined {
return gameScene.MapPlayersByKey.get(userId)?.PlayerValue;
}
function sendFollowRequest() {
gameScene.connection?.emitFollowRequest(gameManager.getPlayerName());
gameScene.connection?.emitFollowRequest();
followStateStore.set(followStates.active);
}
function acceptFollowRequest() {
gameScene.CurrentPlayer.enableFollowing();
gameScene.connection?.emitFollowConfirmation(followUsers[0], gameManager.getPlayerName());
gameScene.connection?.emitFollowConfirmation();
}
function abortEnding() {
@ -66,11 +70,7 @@ vim: ft=typescript
}
function reset() {
if (followRole === followRoles.leader && followUsers.length > 0) {
gameScene.connection?.emitFollowAbort(gameManager.getPlayerName(), "*");
} else {
gameScene.connection?.emitFollowAbort(followUsers[0], gameManager.getPlayerName());
}
gameScene.connection?.emitFollowAbort();
followStateStore.set(followStates.off);
followRoleStore.set(followRoles.leader);
followUsersStore.set([]);
@ -92,7 +92,7 @@ vim: ft=typescript
</section>
{#if followRole === followRoles.follower}
<section class="interact-menu-question">
<p>Do you want to follow {followUsers[0]}?</p>
<p>Do you want to follow {name(followUsers[0])}?</p>
</section>
<section class="interact-menu-action">
<button type="button" class="accept" on:click|preventDefault={acceptFollowRequest}>Yes</button>
@ -117,7 +117,7 @@ vim: ft=typescript
</section>
{#if followRole === followRoles.follower}
<section class="interact-menu-question">
<p>Do you want to stop following {followUsers[0]}?</p>
<p>Do you want to stop following {name(followUsers[0])}?</p>
</section>
{:else if followRole === followRoles.leader}
<section class="interact-menu-question">
@ -135,15 +135,15 @@ vim: ft=typescript
<div class="interact-status nes-container is-rounded">
<section class="interact-status">
{#if followRole === followRoles.follower}
<p>Following {followUsers[0]}</p>
<p>Following {name(followUsers[0])}</p>
{:else if followUsers.length === 0}
<p>Waiting for followers' confirmation</p>
{:else if followUsers.length === 1}
<p>{followUsers[0]} is following you</p>
<p>{name(followUsers[0])} is following you</p>
{:else if followUsers.length === 2}
<p>{followUsers[0]} and {followUsers[1]} are following you</p>
<p>{name(followUsers[0])} and {name(followUsers[1])} are following you</p>
{:else}
<p>{followUsers[0]}, {followUsers[1]} and {followUsers[2]} are following you</p>
<p>{name(followUsers[0])}, {name(followUsers[1])} and {name(followUsers[2])} are following you</p>
{/if}
</section>
</div>

View File

@ -750,39 +750,41 @@ export class RoomConnection implements RoomConnection {
this.socket.send(clientToServerMessage.serializeBinary().buffer);
}
public emitFollowRequest(user: string | null): void {
if (!user) {
public emitFollowRequest(): void {
if (!this.userId) {
return;
}
console.log("Emitting follow request");
const message = new FollowRequestMessage();
message.setLeader(user);
message.setLeader(this.userId);
const clientToServerMessage = new ClientToServerMessage();
clientToServerMessage.setFollowrequestmessage(message);
this.socket.send(clientToServerMessage.serializeBinary().buffer);
}
public emitFollowConfirmation(leader: string | null, follower: string | null): void {
if (!leader || !follower) {
public emitFollowConfirmation(): void {
if (!this.userId) {
return;
}
console.log("Emitting follow confirmation");
const message = new FollowConfirmationMessage();
message.setLeader(leader);
message.setFollower(follower);
message.setLeader(get(followUsersStore)[0]);
message.setFollower(this.userId);
const clientToServerMessage = new ClientToServerMessage();
clientToServerMessage.setFollowconfirmationmessage(message);
this.socket.send(clientToServerMessage.serializeBinary().buffer);
}
public emitFollowAbort(leader: string | null, follower: string | null): void {
if (!leader || !follower) {
public emitFollowAbort(): void {
const isLeader = get(followRoleStore) === followRoles.leader;
const hasFollowers = get(followUsersStore).length > 0;
if (!this.userId || (isLeader && !hasFollowers)) {
return;
}
console.log("Emitting follow abort");
const message = new FollowAbortMessage();
message.setLeader(leader);
message.setFollower(follower);
message.setLeader(isLeader ? this.userId : get(followUsersStore)[0]);
message.setFollower(isLeader ? 0 : this.userId);
const clientToServerMessage = new ClientToServerMessage();
clientToServerMessage.setFollowabortmessage(message);
this.socket.send(clientToServerMessage.serializeBinary().buffer);

View File

@ -1715,10 +1715,6 @@ ${escapedMessage}
});
}
public findPlayer(testFunction: (player: RemotePlayer) => boolean): RemotePlayer | undefined {
return Array.from(this.MapPlayersByKey.values()).find(testFunction);
}
/**
* Called by the connexion when a new player arrives on a map
*/

View File

@ -86,9 +86,9 @@ export class Player extends Character {
private computeFollowMovement(): number[] {
// Find followed WOKA and abort following if we lost it
const player = this.scene.findPlayer((p) => p.PlayerValue === get(followUsersStore)[0]);
const player = this.scene.MapPlayersByKey.get(get(followUsersStore)[0]);
if (!player) {
this.scene.connection?.emitFollowAbort(get(followUsersStore)[0], this.PlayerValue);
this.scene.connection?.emitFollowAbort();
followStateStore.set(followStates.off);
return [0, 0];
}

View File

@ -14,4 +14,4 @@ export const followRoles = {
export const followStateStore = writable(followStates.off);
export const followRoleStore = writable(followRoles.leader);
export const followUsersStore = writable<string[]>([]);
export const followUsersStore = writable<number[]>([]);

View File

@ -81,17 +81,17 @@ message QueryJitsiJwtMessage {
}
message FollowRequestMessage {
string leader = 1;
int32 leader = 1;
}
message FollowConfirmationMessage {
string leader = 1;
string follower = 2;
int32 leader = 1;
int32 follower = 2;
}
message FollowAbortMessage {
string leader = 1;
string follower = 2;
int32 leader = 1;
int32 follower = 2;
}
message ClientToServerMessage {