1
+ import { Logger } from "./functions/logger" ;
1
2
import express from "express" ;
2
3
import { createServer } from "node:http" ;
3
4
import { Server , type Socket } from "socket.io" ;
@@ -9,6 +10,9 @@ import type {
9
10
StartGameData ,
10
11
UpdateCookiesData ,
11
12
} from "./types/rooms" ;
13
+ import colors from "colors" ;
14
+ import "dotenv/config"
15
+ const logger = Logger ( "Server" ) ;
12
16
13
17
/**
14
18
* Initializes the Express application.
@@ -18,12 +22,12 @@ const app = express();
18
22
/**
19
23
* Creates an HTTP server using the Express application.
20
24
*/
21
- const httpServer = createServer ( app ) ;
25
+ const HTTP = createServer ( app ) ;
22
26
23
27
/**
24
28
* Initializes Socket.IO with the HTTP server.
25
29
*/
26
- const io = new Server ( httpServer ) ;
30
+ const io = new Server ( HTTP ) ;
27
31
28
32
/**
29
33
* Stores active rooms on the server.
@@ -58,16 +62,14 @@ function generateCode(): string {
58
62
*/
59
63
app . get ( "/ping" , ( req , res ) => {
60
64
res . status ( 200 ) . send ( {
61
- message : "pong" ,
62
- date : new Date ( )
65
+ message : "pong!" ,
63
66
} ) ;
64
67
} ) ;
65
68
66
69
/**
67
70
* Handles client connections and sets up event listeners.
68
71
*/
69
72
io . on ( "connection" , ( socket : Socket ) => {
70
- console . log ( `Client connected: ${ socket . id } ` ) ;
71
73
72
74
/**
73
75
* Handles player joining or creating a room.
@@ -120,10 +122,8 @@ io.on("connection", (socket: Socket) => {
120
122
} ) ;
121
123
122
124
io . to ( room_code ) . emit ( "update_room" , { room_player, room } ) ;
123
- console . log (
124
- `Player "${ room_player } " joined room "${ room_code } ". Room state:` ,
125
- room ,
126
- ) ;
125
+
126
+ logger . info ( `Player "${ colors . bold . brightGreen . underline ( room_player ) } " joined room "${ colors . bold . brightGreen . underline ( room_code ) } ".` ) ;
127
127
} ) ;
128
128
129
129
/**
@@ -144,64 +144,61 @@ io.on("connection", (socket: Socket) => {
144
144
145
145
if ( room . players . length === 0 ) {
146
146
delete ROOMS [ room_code ] ;
147
- console . log ( `Room ${ room_code } has been deleted.` ) ;
147
+ logger . info ( `Room ${ room_code } has been deleted.` ) ;
148
148
} else if ( room . owner === room_player ) {
149
149
room . owner = room . players [ 0 ] ?. room_player || null ;
150
- console . log ( `New owner of room ${ room_code } : ${ room . owner } ` ) ;
150
+ logger . info ( `New owner of room ${ room_code } : ${ room . owner } ` ) ;
151
151
}
152
152
153
153
socket . leave ( room_code ) ;
154
154
io . to ( room_code ) . emit ( "update_room" , { room_player, room } ) ;
155
155
156
- console . log (
157
- `Player ${ room_player } (Socket ID: ${ socket . id } ) left room ${ room_code } ` ,
158
- ) ;
156
+ logger . info ( `Player ${ room_player } (Socket ID: ${ socket . id } ) left room ${ room_code } ` ) ;
159
157
} ) ;
160
158
161
159
/**
162
- * Handles joining a random public room.
163
- * Filters for available public rooms with space for more players.
164
- * If no public rooms are available, an error is emitted.
165
- * If the player is already in a room, an error is emitted.
166
- * Otherwise, the player is added to a random room and the room is updated.
167
- * @param {string } room_player - The name/identifier of the player.
168
- */
160
+ * Handles joining a random public room.
161
+ * Filters for available public rooms with space for more players.
162
+ * If no public rooms are available, an error is emitted.
163
+ * If the player is already in a room, an error is emitted.
164
+ * Otherwise, the player is added to a random room and the room is updated.
165
+ * @param {string } room_player - The name/identifier of the player.
166
+ */
169
167
socket . on ( "join_random_room" , ( { room_player } : { room_player : string } ) => {
170
- // Filters public rooms that are in "waiting" state and have less than 10 players
171
- const availableRooms = Object . values ( ROOMS ) . filter (
172
- ( room ) => room . public && room . state === "waiting" ) ;
173
-
174
- // If no available public rooms
175
- if ( availableRooms . length === 0 ) {
176
- socket . emit ( "err_socket" , { err_socket : "NO_PUBLIC_ROOMS_AVAILABLE" } ) ;
177
- return ;
178
- }
179
-
180
- // Randomly select a room from the available rooms
181
- const randomRoom = availableRooms [ Math . floor ( Math . random ( ) * availableRooms . length ) ] ;
182
-
183
- // Check if the player is already in the selected room
184
- if ( randomRoom . players . find ( ( player ) => player . room_player === room_player ) ) {
185
- socket . emit ( "err_socket" , { err_socket : "PLAYER_EXISTS_IN_ROOM" } ) ;
186
- return ;
187
- }
188
-
189
- // Add the player to the selected room
190
- socket . join ( randomRoom . code ) ;
191
-
192
- randomRoom . players . push ( {
193
- id : generateUuid ( ) ,
194
- date : new Date ( ) ,
195
- socket : socket . id ,
196
- player_data : { cookies : null } ,
197
- room_player,
198
- } ) ;
168
+ // Filters public rooms that are in "waiting" state and have less than 10 players
169
+ const availableRooms = Object . values ( ROOMS ) . filter (
170
+ ( room ) => room . public && room . state === "waiting" ) ;
199
171
200
- io . to ( randomRoom . code ) . emit ( "update_room" , { room_player, room : randomRoom } ) ;
201
-
202
- console . log ( `Player "${ room_player } " joined random room "${ randomRoom . code } ". Room state:` , randomRoom ) ;
203
-
204
- } ) ;
172
+ // If no available public rooms
173
+ if ( availableRooms . length === 0 ) {
174
+ socket . emit ( "err_socket" , { err_socket : "NO_PUBLIC_ROOMS_AVAILABLE" } ) ;
175
+ return ;
176
+ }
177
+
178
+ // Randomly select a room from the available rooms
179
+ const randomRoom = availableRooms [ Math . floor ( Math . random ( ) * availableRooms . length ) ] ;
180
+
181
+ // Check if the player is already in the selected room
182
+ if ( randomRoom . players . find ( ( player ) => player . room_player === room_player ) ) {
183
+ socket . emit ( "err_socket" , { err_socket : "PLAYER_EXISTS_IN_ROOM" } ) ;
184
+ return ;
185
+ }
186
+
187
+ // Add the player to the selected room
188
+ socket . join ( randomRoom . code ) ;
189
+
190
+ randomRoom . players . push ( {
191
+ id : generateUuid ( ) ,
192
+ date : new Date ( ) ,
193
+ socket : socket . id ,
194
+ player_data : { cookies : null } ,
195
+ room_player,
196
+ } ) ;
197
+
198
+ io . to ( randomRoom . code ) . emit ( "update_room" , { room_player, room : randomRoom } ) ;
199
+
200
+ logger . info ( `Player "${ colors . bold . brightGreen . underline ( room_player ) } " joined random room "${ randomRoom . code } ".` ) ;
201
+ } ) ;
205
202
206
203
/**
207
204
* Handles player rejoining a room.
@@ -219,7 +216,7 @@ io.on("connection", (socket: Socket) => {
219
216
player . socket = socket . id ;
220
217
socket . join ( room_code ) ;
221
218
io . to ( room_code ) . emit ( "update_room" , { room_player, room } ) ;
222
- console . log ( `Player "${ room_player } " rejoined room "${ room_code } ".` ) ;
219
+ logger . info ( `Player "${ room_player } " rejoined room "${ room_code } ".` ) ;
223
220
}
224
221
} ) ;
225
222
@@ -265,13 +262,10 @@ io.on("connection", (socket: Socket) => {
265
262
266
263
if ( room . state === "finished" ) {
267
264
delete ROOMS [ room_code ] ;
268
- console . log ( `Room ${ room_code } has been deleted.` ) ;
265
+ logger . info ( `Room ${ room_code } has been deleted.` ) ;
269
266
}
270
267
271
- console . log (
272
- `Game in room "${ room_code } " finished! Ranking:` ,
273
- ranking ,
274
- ) ;
268
+ logger . info ( `Game in room "${ room_code } " finished! Ranking: ${ JSON . stringify ( ranking ) } ` ) ;
275
269
return ;
276
270
}
277
271
@@ -305,8 +299,8 @@ io.on("connection", (socket: Socket) => {
305
299
306
300
if ( player ) {
307
301
player . player_data . cookies = cookies ;
308
- console . log (
309
- `Player "${ room_player } " in room "${ room_code } " updated cookies to ${ cookies } .` ,
302
+ logger . info (
303
+ `Player "${ room_player } " in room "${ room_code } " updated cookies to ${ cookies } .`
310
304
) ;
311
305
}
312
306
} ,
@@ -316,12 +310,12 @@ io.on("connection", (socket: Socket) => {
316
310
* Handles client disconnection.
317
311
*/
318
312
socket . on ( "disconnect" , ( ) => {
319
- console . log ( `Client disconnected: ${ socket . id } ` ) ;
313
+ logger . info ( `Client disconnected: ${ socket . id } ` ) ;
320
314
} ) ;
321
315
322
316
} ) ;
323
317
324
- // Starts the server on port 3000
325
- httpServer . listen ( 3000 , ( ) => {
326
- console . log ( "Server is running on http://localhost:3000" ) ;
318
+
319
+ HTTP . listen ( process . env . PORT , ( ) => {
320
+ logger . info ( `Socket running: ` + colors . bold . brightGreen . underline ( ` http://0.0.0.0: ${ process . env . PORT } ` ) ) ;
327
321
} ) ;
0 commit comments