Skip to content
This repository was archived by the owner on Apr 8, 2026. It is now read-only.

Commit 29daa93

Browse files
author
BuildTools
committed
refactor(LobbyService): rename getFormattedLobby to getLobby and simplify user retrieval logic
1 parent cfe15eb commit 29daa93

7 files changed

Lines changed: 81 additions & 123 deletions

File tree

dist/controllers/LobbyController.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ let Lobbies = class Lobbies {
8484
}
8585
try {
8686
const lobbyId = req.params.lobbyId;
87-
const lobby = await this.lobbyService.getFormattedLobby(lobbyId);
87+
const lobby = await this.lobbyService.getLobby(lobbyId);
8888
if (!lobby) {
8989
await this.createLog(req, 'getLobby', 'lobbies', 404);
9090
return res.status(404).send({ message: "Lobby not found" });

dist/services/LobbyService.d.ts

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,27 @@
11
import { IDatabaseService } from "./DatabaseService";
22
import { Lobby } from "../interfaces/Lobbies";
33
import { IUserService } from "./UserService";
4-
interface LobbyUser {
5-
username: string;
6-
user_id: string;
7-
verified: boolean;
8-
steam_username?: string;
9-
steam_avatar_url?: string;
10-
steam_id?: string;
11-
}
124
export interface ILobbyService {
135
getLobby(lobbyId: string): Promise<Lobby | null>;
14-
getFormattedLobby(lobbyId: string): Promise<{
15-
lobbyId: string;
16-
users: LobbyUser[];
17-
} | null>;
186
joinLobby(lobbyId: string, userId: string): Promise<void>;
197
leaveLobby(lobbyId: string, userId: string): Promise<void>;
208
getUserLobby(userId: string): Promise<Lobby | null>;
21-
getFormattedLobbyUsers(userIds: string[]): Promise<LobbyUser[]>;
229
createLobby(lobbyId: string, users?: string[]): Promise<void>;
2310
deleteLobby(lobbyId: string): Promise<void>;
24-
getUserLobbies(userId: string): Promise<{
25-
lobbyId: string;
26-
users: string;
27-
}[]>;
11+
getUserLobbies(userId: string): Promise<Lobby[]>;
2812
leaveAllLobbies(userId: string): Promise<void>;
2913
}
3014
export declare class LobbyService implements ILobbyService {
3115
private databaseService;
3216
private userService;
3317
constructor(databaseService: IDatabaseService, userService: IUserService);
3418
getLobby(lobbyId: string): Promise<Lobby | null>;
35-
getFormattedLobby(lobbyId: string): Promise<{
36-
lobbyId: string;
37-
users: LobbyUser[];
38-
} | null>;
3919
joinLobby(lobbyId: string, userId: string): Promise<void>;
4020
leaveLobby(lobbyId: string, userId: string): Promise<void>;
4121
getUserLobby(userId: string): Promise<Lobby | null>;
42-
getFormattedLobbyUsers(userIds: string[]): Promise<LobbyUser[]>;
4322
createLobby(lobbyId: string, users?: string[]): Promise<void>;
4423
deleteLobby(lobbyId: string): Promise<void>;
45-
getUserLobbies(userId: string): Promise<{
46-
lobbyId: string;
47-
users: string;
48-
}[]>;
24+
getUserLobbies(userId: string): Promise<Lobby[]>;
4925
leaveAllLobbies(userId: string): Promise<void>;
26+
private getUsersByIds;
5027
}
51-
export {};

dist/services/LobbyService.js

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,12 @@ let LobbyService = class LobbyService {
2020
this.userService = userService;
2121
}
2222
async getLobby(lobbyId) {
23-
const rows = await this.databaseService.read("SELECT users FROM lobbies WHERE lobbyId = ?", [lobbyId]);
24-
return rows[0] || null;
25-
}
26-
async getFormattedLobby(lobbyId) {
27-
const lobby = await this.getLobby(lobbyId);
28-
if (!lobby)
23+
const lobby = await this.databaseService.read("SELECT lobbyId, users FROM lobbies WHERE lobbyId = ?", [lobbyId]);
24+
if (lobby.length === 0)
2925
return null;
30-
const users = lobby.users;
31-
return { lobbyId, users };
26+
const userIds = lobby[0].users;
27+
const users = await this.getUsersByIds(userIds);
28+
return { lobbyId: lobby[0].lobbyId, users };
3229
}
3330
async joinLobby(lobbyId, userId) {
3431
const lobby = await this.getLobby(lobbyId);
@@ -50,21 +47,13 @@ let LobbyService = class LobbyService {
5047
}
5148
}
5249
async getUserLobby(userId) {
53-
const rows = await this.databaseService.read("SELECT lobbyId, users FROM lobbies WHERE JSON_EXTRACT(users, '$') LIKE ?", [`%"${userId}"%`]);
54-
if (rows.length === 0)
50+
const lobbies = await this.databaseService.read("SELECT lobbyId, users FROM lobbies WHERE JSON_EXTRACT(users, '$') LIKE ?", [`%"${userId}"%`]);
51+
if (lobbies.length === 0)
5552
return null;
56-
const row = rows[0];
57-
const users = row.users;
58-
return { lobbyId: row.lobbyId, users };
59-
}
60-
async getFormattedLobbyUsers(userIds) {
61-
if (userIds.length === 0)
62-
return [];
63-
const placeholders = userIds.map(() => '?').join(',');
64-
const users = await this.databaseService.read(`SELECT user_id, username, verified, steam_username, steam_avatar_url, steam_id
65-
FROM users
66-
WHERE user_id IN (${placeholders})`, userIds);
67-
return users.map(mapLobbyUser);
53+
const lobby = lobbies[0];
54+
const userIds = lobby.users;
55+
const users = await this.getUsersByIds(userIds);
56+
return { lobbyId: lobby.lobbyId, users };
6857
}
6958
async createLobby(lobbyId, users = []) {
7059
await this.databaseService.update("INSERT INTO lobbies (lobbyId, users) VALUES (?, ?)", [lobbyId, JSON.stringify(users)]);
@@ -75,14 +64,26 @@ let LobbyService = class LobbyService {
7564
]);
7665
}
7766
async getUserLobbies(userId) {
78-
return await this.databaseService.read("SELECT lobbyId, users FROM lobbies WHERE JSON_EXTRACT(users, '$') LIKE ?", [`%"${userId}"%`]);
67+
const lobbies = await this.databaseService.read("SELECT lobbyId, users FROM lobbies WHERE JSON_EXTRACT(users, '$') LIKE ?", [`%"${userId}"%`]);
68+
return Promise.all(lobbies.map(async (lobby) => {
69+
const userIds = lobby.users;
70+
const users = await this.getUsersByIds(userIds);
71+
return { lobbyId: lobby.lobbyId, users };
72+
}));
7973
}
8074
async leaveAllLobbies(userId) {
8175
const lobbies = await this.getUserLobbies(userId);
8276
for (const lobby of lobbies) {
8377
await this.leaveLobby(lobby.lobbyId, userId);
8478
}
8579
}
80+
async getUsersByIds(userIds) {
81+
if (userIds.length === 0)
82+
return [];
83+
return await this.databaseService.read(`SELECT user_id, username, verified, admin FROM users WHERE user_id IN (${userIds
84+
.map(() => "?")
85+
.join(",")})`, userIds);
86+
}
8687
};
8788
exports.LobbyService = LobbyService;
8889
exports.LobbyService = LobbyService = __decorate([
@@ -91,13 +92,3 @@ exports.LobbyService = LobbyService = __decorate([
9192
__param(1, (0, inversify_1.inject)("UserService")),
9293
__metadata("design:paramtypes", [Object, Object])
9394
], LobbyService);
94-
function mapLobbyUser(user) {
95-
return {
96-
username: user.username,
97-
user_id: user.user_id,
98-
verified: user.verified,
99-
steam_username: user.steam_username,
100-
steam_avatar_url: user.steam_avatar_url,
101-
steam_id: user.steam_id,
102-
};
103-
}

dist/sockets/UserSockets.d.ts

Whitespace-only changes.

dist/sockets/UserSockets.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"use strict";

src/controllers/LobbyController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export class Lobbies {
113113
}
114114
try {
115115
const lobbyId = req.params.lobbyId;
116-
const lobby = await this.lobbyService.getFormattedLobby(lobbyId);
116+
const lobby = await this.lobbyService.getLobby(lobbyId);
117117
if (!lobby) {
118118
await this.createLog(req, 'getLobby', 'lobbies', 404);
119119
return res.status(404).send({ message: "Lobby not found" });

src/services/LobbyService.ts

Lines changed: 51 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,14 @@ import { Lobby } from "../interfaces/Lobbies";
44
import { User } from "interfaces/User";
55
import { IUserService } from "./UserService";
66

7-
interface LobbyUser {
8-
username: string;
9-
user_id: string;
10-
verified: boolean;
11-
steam_username?: string;
12-
steam_avatar_url?: string;
13-
steam_id?: string;
14-
}
15-
167
export interface ILobbyService {
178
getLobby(lobbyId: string): Promise<Lobby | null>;
18-
getFormattedLobby(lobbyId: string): Promise<{ lobbyId: string; users: LobbyUser[] } | null>;
199
joinLobby(lobbyId: string, userId: string): Promise<void>;
2010
leaveLobby(lobbyId: string, userId: string): Promise<void>;
21-
getUserLobby(
22-
userId: string
23-
): Promise<Lobby | null>;
24-
getFormattedLobbyUsers(userIds: string[]): Promise<LobbyUser[]>;
11+
getUserLobby(userId: string): Promise<Lobby | null>;
2512
createLobby(lobbyId: string, users?: string[]): Promise<void>;
2613
deleteLobby(lobbyId: string): Promise<void>;
27-
getUserLobbies(userId: string): Promise<{ lobbyId: string; users: string }[]>;
14+
getUserLobbies(userId: string): Promise<Lobby[]>;
2815
leaveAllLobbies(userId: string): Promise<void>;
2916
}
3017

@@ -36,21 +23,23 @@ export class LobbyService implements ILobbyService {
3623
) {}
3724

3825
async getLobby(lobbyId: string): Promise<Lobby | null> {
39-
const rows = await this.databaseService.read<Lobby>(
40-
"SELECT users FROM lobbies WHERE lobbyId = ?",
26+
const lobby = await this.databaseService.read<{
27+
lobbyId: string;
28+
users: string[];
29+
}>(
30+
"SELECT lobbyId, users FROM lobbies WHERE lobbyId = ?",
4131
[lobbyId]
4232
);
43-
return rows[0] || null;
44-
}
4533

46-
async getFormattedLobby(lobbyId: string): Promise<{ lobbyId: string; users: LobbyUser[] } | null> {
47-
const lobby = await this.getLobby(lobbyId);
48-
if (!lobby) return null;
34+
if (lobby.length === 0) return null;
35+
36+
const userIds = lobby[0].users;
37+
const users = await this.getUsersByIds(userIds);
4938

50-
const users = lobby.users;
51-
return { lobbyId, users };
39+
return { lobbyId: lobby[0].lobbyId, users };
5240
}
5341

42+
5443
async joinLobby(lobbyId: string, userId: string): Promise<void> {
5544
const lobby = await this.getLobby(lobbyId);
5645
if (!lobby) throw new Error("Lobby not found");
@@ -75,31 +64,21 @@ export class LobbyService implements ILobbyService {
7564
}
7665
}
7766

78-
async getUserLobby(
79-
userId: string
80-
): Promise<Lobby | null> {
81-
const rows = await this.databaseService.read<Lobby>("SELECT lobbyId, users FROM lobbies WHERE JSON_EXTRACT(users, '$') LIKE ?",
82-
[`%"${userId}"%`]);
83-
84-
if (rows.length === 0) return null;
85-
86-
const row = rows[0];
87-
const users = row.users
88-
return { lobbyId: row.lobbyId, users };
89-
}
90-
91-
async getFormattedLobbyUsers(userIds: string[]): Promise<LobbyUser[]> {
92-
if (userIds.length === 0) return [];
93-
94-
const placeholders = userIds.map(() => '?').join(',');
95-
const users = await this.databaseService.read<User>(
96-
`SELECT user_id, username, verified, steam_username, steam_avatar_url, steam_id
97-
FROM users
98-
WHERE user_id IN (${placeholders})`,
99-
userIds
67+
async getUserLobby(userId: string): Promise<Lobby | null> {
68+
const lobbies = await this.databaseService.read<{
69+
lobbyId: string;
70+
users: string[];
71+
}>(
72+
"SELECT lobbyId, users FROM lobbies WHERE JSON_EXTRACT(users, '$') LIKE ?",
73+
[`%"${userId}"%`]
10074
);
101-
102-
return users.map(mapLobbyUser);
75+
76+
if (lobbies.length === 0) return null;
77+
78+
const lobby = lobbies[0];
79+
const userIds = lobby.users;
80+
const users = await this.getUsersByIds(userIds);
81+
return { lobbyId: lobby.lobbyId, users };
10382
}
10483

10584
async createLobby(lobbyId: string, users: string[] = []): Promise<void> {
@@ -115,11 +94,22 @@ export class LobbyService implements ILobbyService {
11594
]);
11695
}
11796

118-
async getUserLobbies(userId: string): Promise<{ lobbyId: string; users: string }[]> {
119-
return await this.databaseService.read<{ lobbyId: string; users: string }>(
97+
async getUserLobbies(userId: string): Promise<Lobby[]> {
98+
const lobbies = await this.databaseService.read<{
99+
lobbyId: string;
100+
users: string[];
101+
}>(
120102
"SELECT lobbyId, users FROM lobbies WHERE JSON_EXTRACT(users, '$') LIKE ?",
121103
[`%"${userId}"%`]
122104
);
105+
106+
return Promise.all(
107+
lobbies.map(async (lobby) => {
108+
const userIds = lobby.users;
109+
const users = await this.getUsersByIds(userIds);
110+
return { lobbyId: lobby.lobbyId, users };
111+
})
112+
);
123113
}
124114

125115
async leaveAllLobbies(userId: string): Promise<void> {
@@ -128,15 +118,15 @@ export class LobbyService implements ILobbyService {
128118
await this.leaveLobby(lobby.lobbyId, userId);
129119
}
130120
}
131-
}
132121

133-
function mapLobbyUser(user: User): LobbyUser {
134-
return {
135-
username: user.username,
136-
user_id: user.user_id,
137-
verified: user.verified,
138-
steam_username: user.steam_username,
139-
steam_avatar_url: user.steam_avatar_url,
140-
steam_id: user.steam_id,
141-
};
142-
}
122+
private async getUsersByIds(userIds: string[]): Promise<User[]> {
123+
if (userIds.length === 0) return [];
124+
125+
return await this.databaseService.read<User>(
126+
`SELECT user_id, username, verified, admin FROM users WHERE user_id IN (${userIds
127+
.map(() => "?")
128+
.join(",")})`,
129+
userIds
130+
);
131+
}
132+
}

0 commit comments

Comments
 (0)