Use user IDs instead of names
This commit is contained in:
parent
e528682403
commit
2bd71790b5
9 changed files with 48 additions and 57 deletions
|
@ -96,11 +96,6 @@ export class GameRoom {
|
||||||
return this.users;
|
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 {
|
public getUserByUuid(uuid: string): User | undefined {
|
||||||
return this.usersByUuid.get(uuid);
|
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) {
|
setSilent(user: User, silent: boolean) {
|
||||||
if (user.silent === silent) {
|
if (user.silent === silent) {
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -25,7 +25,7 @@ export class User implements Movable {
|
||||||
public readonly IPAddress: string,
|
public readonly IPAddress: string,
|
||||||
private position: PointInterface,
|
private position: PointInterface,
|
||||||
public silent: boolean,
|
public silent: boolean,
|
||||||
public following: string[],
|
public following: number[],
|
||||||
private positionNotifier: PositionNotifier,
|
private positionNotifier: PositionNotifier,
|
||||||
public readonly socket: UserSocket,
|
public readonly socket: UserSocket,
|
||||||
public readonly tags: string[],
|
public readonly tags: string[],
|
||||||
|
@ -49,15 +49,15 @@ export class User implements Movable {
|
||||||
this.positionNotifier.updatePosition(this, position, oldPosition);
|
this.positionNotifier.updatePosition(this, position, oldPosition);
|
||||||
}
|
}
|
||||||
|
|
||||||
public addFollower(name: string): void {
|
public addFollower(userId: number): void {
|
||||||
if (this.following.includes(name)) {
|
if (this.following.includes(userId)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.following.push(name);
|
this.following.push(userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public delFollower(name: string): void {
|
public delFollower(userId: number): void {
|
||||||
const idx = this.following.indexOf(name);
|
const idx = this.following.indexOf(userId);
|
||||||
if (idx === -1) {
|
if (idx === -1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -846,16 +846,17 @@ export class SocketManager {
|
||||||
handleFollowConfirmationMessage(room: GameRoom, user: User, message: FollowConfirmationMessage) {
|
handleFollowConfirmationMessage(room: GameRoom, user: User, message: FollowConfirmationMessage) {
|
||||||
const clientMessage = new ServerToClientMessage();
|
const clientMessage = new ServerToClientMessage();
|
||||||
clientMessage.setFollowconfirmationmessage(message);
|
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());
|
user.addFollower(message.getLeader());
|
||||||
}
|
}
|
||||||
|
|
||||||
handleFollowAbortMessage(room: GameRoom, user: User, message: FollowAbortMessage) {
|
handleFollowAbortMessage(room: GameRoom, user: User, message: FollowAbortMessage) {
|
||||||
const clientMessage = new ServerToClientMessage();
|
const clientMessage = new ServerToClientMessage();
|
||||||
clientMessage.setFollowabortmessage(message);
|
clientMessage.setFollowabortmessage(message);
|
||||||
if (user.name === message.getLeader()) {
|
if (user.id === message.getLeader()) {
|
||||||
// Forward message
|
// Forward message
|
||||||
room.sendToOthersInGroupIncludingUser(user, clientMessage);
|
room.sendToOthersInGroupIncludingUser(user, clientMessage);
|
||||||
|
|
||||||
|
@ -865,10 +866,11 @@ export class SocketManager {
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// Forward message
|
// Forward message
|
||||||
room.sendToUserWithName(message.getLeader(), clientMessage);
|
const leader = room.getUserById(message.getLeader());
|
||||||
|
leader?.socket.write(clientMessage);
|
||||||
|
|
||||||
// Update followers
|
// Update followers
|
||||||
room.getUserByName(message.getLeader())?.delFollower(user.name);
|
leader?.delFollower(user.id);
|
||||||
user.following = [];
|
user.following = [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ vim: ft=typescript
|
||||||
|
|
||||||
let followState: string;
|
let followState: string;
|
||||||
let followRole: string;
|
let followRole: string;
|
||||||
let followUsers: string[];
|
let followUsers: number[];
|
||||||
let stateUnsubscriber: Unsubscriber;
|
let stateUnsubscriber: Unsubscriber;
|
||||||
let roleUnsubscriber: Unsubscriber;
|
let roleUnsubscriber: Unsubscriber;
|
||||||
let nameUnsubscriber: 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() {
|
function sendFollowRequest() {
|
||||||
gameScene.connection?.emitFollowRequest(gameManager.getPlayerName());
|
gameScene.connection?.emitFollowRequest();
|
||||||
followStateStore.set(followStates.active);
|
followStateStore.set(followStates.active);
|
||||||
}
|
}
|
||||||
|
|
||||||
function acceptFollowRequest() {
|
function acceptFollowRequest() {
|
||||||
gameScene.CurrentPlayer.enableFollowing();
|
gameScene.CurrentPlayer.enableFollowing();
|
||||||
gameScene.connection?.emitFollowConfirmation(followUsers[0], gameManager.getPlayerName());
|
gameScene.connection?.emitFollowConfirmation();
|
||||||
}
|
}
|
||||||
|
|
||||||
function abortEnding() {
|
function abortEnding() {
|
||||||
|
@ -66,11 +70,7 @@ vim: ft=typescript
|
||||||
}
|
}
|
||||||
|
|
||||||
function reset() {
|
function reset() {
|
||||||
if (followRole === followRoles.leader && followUsers.length > 0) {
|
gameScene.connection?.emitFollowAbort();
|
||||||
gameScene.connection?.emitFollowAbort(gameManager.getPlayerName(), "*");
|
|
||||||
} else {
|
|
||||||
gameScene.connection?.emitFollowAbort(followUsers[0], gameManager.getPlayerName());
|
|
||||||
}
|
|
||||||
followStateStore.set(followStates.off);
|
followStateStore.set(followStates.off);
|
||||||
followRoleStore.set(followRoles.leader);
|
followRoleStore.set(followRoles.leader);
|
||||||
followUsersStore.set([]);
|
followUsersStore.set([]);
|
||||||
|
@ -92,7 +92,7 @@ vim: ft=typescript
|
||||||
</section>
|
</section>
|
||||||
{#if followRole === followRoles.follower}
|
{#if followRole === followRoles.follower}
|
||||||
<section class="interact-menu-question">
|
<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>
|
||||||
<section class="interact-menu-action">
|
<section class="interact-menu-action">
|
||||||
<button type="button" class="accept" on:click|preventDefault={acceptFollowRequest}>Yes</button>
|
<button type="button" class="accept" on:click|preventDefault={acceptFollowRequest}>Yes</button>
|
||||||
|
@ -117,7 +117,7 @@ vim: ft=typescript
|
||||||
</section>
|
</section>
|
||||||
{#if followRole === followRoles.follower}
|
{#if followRole === followRoles.follower}
|
||||||
<section class="interact-menu-question">
|
<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>
|
</section>
|
||||||
{:else if followRole === followRoles.leader}
|
{:else if followRole === followRoles.leader}
|
||||||
<section class="interact-menu-question">
|
<section class="interact-menu-question">
|
||||||
|
@ -135,15 +135,15 @@ vim: ft=typescript
|
||||||
<div class="interact-status nes-container is-rounded">
|
<div class="interact-status nes-container is-rounded">
|
||||||
<section class="interact-status">
|
<section class="interact-status">
|
||||||
{#if followRole === followRoles.follower}
|
{#if followRole === followRoles.follower}
|
||||||
<p>Following {followUsers[0]}</p>
|
<p>Following {name(followUsers[0])}</p>
|
||||||
{:else if followUsers.length === 0}
|
{:else if followUsers.length === 0}
|
||||||
<p>Waiting for followers' confirmation</p>
|
<p>Waiting for followers' confirmation</p>
|
||||||
{:else if followUsers.length === 1}
|
{: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}
|
{: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}
|
{: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}
|
{/if}
|
||||||
</section>
|
</section>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -750,39 +750,41 @@ export class RoomConnection implements RoomConnection {
|
||||||
this.socket.send(clientToServerMessage.serializeBinary().buffer);
|
this.socket.send(clientToServerMessage.serializeBinary().buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public emitFollowRequest(user: string | null): void {
|
public emitFollowRequest(): void {
|
||||||
if (!user) {
|
if (!this.userId) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log("Emitting follow request");
|
console.log("Emitting follow request");
|
||||||
const message = new FollowRequestMessage();
|
const message = new FollowRequestMessage();
|
||||||
message.setLeader(user);
|
message.setLeader(this.userId);
|
||||||
const clientToServerMessage = new ClientToServerMessage();
|
const clientToServerMessage = new ClientToServerMessage();
|
||||||
clientToServerMessage.setFollowrequestmessage(message);
|
clientToServerMessage.setFollowrequestmessage(message);
|
||||||
this.socket.send(clientToServerMessage.serializeBinary().buffer);
|
this.socket.send(clientToServerMessage.serializeBinary().buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public emitFollowConfirmation(leader: string | null, follower: string | null): void {
|
public emitFollowConfirmation(): void {
|
||||||
if (!leader || !follower) {
|
if (!this.userId) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log("Emitting follow confirmation");
|
console.log("Emitting follow confirmation");
|
||||||
const message = new FollowConfirmationMessage();
|
const message = new FollowConfirmationMessage();
|
||||||
message.setLeader(leader);
|
message.setLeader(get(followUsersStore)[0]);
|
||||||
message.setFollower(follower);
|
message.setFollower(this.userId);
|
||||||
const clientToServerMessage = new ClientToServerMessage();
|
const clientToServerMessage = new ClientToServerMessage();
|
||||||
clientToServerMessage.setFollowconfirmationmessage(message);
|
clientToServerMessage.setFollowconfirmationmessage(message);
|
||||||
this.socket.send(clientToServerMessage.serializeBinary().buffer);
|
this.socket.send(clientToServerMessage.serializeBinary().buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public emitFollowAbort(leader: string | null, follower: string | null): void {
|
public emitFollowAbort(): void {
|
||||||
if (!leader || !follower) {
|
const isLeader = get(followRoleStore) === followRoles.leader;
|
||||||
|
const hasFollowers = get(followUsersStore).length > 0;
|
||||||
|
if (!this.userId || (isLeader && !hasFollowers)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log("Emitting follow abort");
|
console.log("Emitting follow abort");
|
||||||
const message = new FollowAbortMessage();
|
const message = new FollowAbortMessage();
|
||||||
message.setLeader(leader);
|
message.setLeader(isLeader ? this.userId : get(followUsersStore)[0]);
|
||||||
message.setFollower(follower);
|
message.setFollower(isLeader ? 0 : this.userId);
|
||||||
const clientToServerMessage = new ClientToServerMessage();
|
const clientToServerMessage = new ClientToServerMessage();
|
||||||
clientToServerMessage.setFollowabortmessage(message);
|
clientToServerMessage.setFollowabortmessage(message);
|
||||||
this.socket.send(clientToServerMessage.serializeBinary().buffer);
|
this.socket.send(clientToServerMessage.serializeBinary().buffer);
|
||||||
|
|
|
@ -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
|
* Called by the connexion when a new player arrives on a map
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -86,9 +86,9 @@ export class Player extends Character {
|
||||||
|
|
||||||
private computeFollowMovement(): number[] {
|
private computeFollowMovement(): number[] {
|
||||||
// Find followed WOKA and abort following if we lost it
|
// 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) {
|
if (!player) {
|
||||||
this.scene.connection?.emitFollowAbort(get(followUsersStore)[0], this.PlayerValue);
|
this.scene.connection?.emitFollowAbort();
|
||||||
followStateStore.set(followStates.off);
|
followStateStore.set(followStates.off);
|
||||||
return [0, 0];
|
return [0, 0];
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,4 +14,4 @@ export const followRoles = {
|
||||||
|
|
||||||
export const followStateStore = writable(followStates.off);
|
export const followStateStore = writable(followStates.off);
|
||||||
export const followRoleStore = writable(followRoles.leader);
|
export const followRoleStore = writable(followRoles.leader);
|
||||||
export const followUsersStore = writable<string[]>([]);
|
export const followUsersStore = writable<number[]>([]);
|
||||||
|
|
|
@ -81,17 +81,17 @@ message QueryJitsiJwtMessage {
|
||||||
}
|
}
|
||||||
|
|
||||||
message FollowRequestMessage {
|
message FollowRequestMessage {
|
||||||
string leader = 1;
|
int32 leader = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
message FollowConfirmationMessage {
|
message FollowConfirmationMessage {
|
||||||
string leader = 1;
|
int32 leader = 1;
|
||||||
string follower = 2;
|
int32 follower = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
message FollowAbortMessage {
|
message FollowAbortMessage {
|
||||||
string leader = 1;
|
int32 leader = 1;
|
||||||
string follower = 2;
|
int32 follower = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
message ClientToServerMessage {
|
message ClientToServerMessage {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue