-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_server.py
505 lines (447 loc) · 21.4 KB
/
game_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
from enum import Enum
import socket
import threading
import logging
from time import sleep
from typing import List, override
from server import Client
from server import Server, Socket_address, UserInputHandler
logging.basicConfig(level=logging.INFO,
format='%(name)s: %(message)s',
)
class TooManyPlayersError(Exception):
pass
class NumberOfInvalidInputsExceeded(Exception):
pass
class InvalideInput(Exception):
pass
class RPSEnum(Enum):
NoChoice = 0
Rock = 1
Paper = 2
Scissors = 3
class Player(Client):
def __init__(self, client: Client, busy: bool = True):
super().__init__(client.getSocket(), client.getAddress(), client.getUsername())
self.__choice: RPSEnum = RPSEnum.NoChoice
self.__score: int = 0
self.__lock = threading.Lock()
def getScore(self):
return self.__score
def incrementScore(self):
self.__score += 1
def getChoice(self) -> RPSEnum:
return self.__choice
def setChoice(self, choice: RPSEnum):
self.__choice = choice
def getClient(self):
return Client(self.getSocket(), self.getAddress(), self.getUsername())
def do_lock(self):
self.__lock.acquire()
def do_unlock(self):
self.__lock.release()
def is_locked(self):
return self.__lock.locked()
class GameServer(Server):
def __init__(self, server_address, close_event):
super().__init__(server_address, close_event)
self.games: List[Game] = list()
self.clients: List[Player] = list()
self.logger = logging.getLogger("Game server")
self.chat_logger = logging.getLogger("Chat")
self.games_requests: list[tuple(str, str)] = []
self.games_requests_lock = threading.Lock()
@override
def start(self):
self.server_socket.setsockopt(
socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server_socket.bind((self.host, self.port))
self.server_socket.listen(4)
self.logger.info(f"Listening on {self.host}:{self.port}")
while not self.close_event.is_set():
try:
client_socket, client_address = self.server_socket.accept()
self.logger.info(
f"Connection from client {client_address[0]}:{client_address[1]}")
new_player = self._request_client_username(
client_socket, client_address, "Server << Enter your username:", 0)
if new_player:
# assign a thread for each new subscribed client
client_handler = threading.Thread(
target=self._handle_client, args=(new_player,))
client_handler.start()
except KeyboardInterrupt:
self._close_server()
except socket.error as e:
if 'bad file descriptor' in str(e):
self.logger.error(
"Server socket is closed or shutdown..!")
else:
self.logger.error(
f"Error accepting or handling new connections: {e}")
@override
def _request_client_username(self, client_socket, client_address, message, nbr_of_attempts):
max_nbr_of_attempts = 3
if nbr_of_attempts >= max_nbr_of_attempts:
# close client's connection
client_socket.send(
"close".encode())
else:
# request username
client_socket.send(message.encode())
# wait for client's response
username = client_socket.recv(2048).decode().strip()
if len(username) > 0:
# block access to self.clients variable
self.lock.acquire()
# check if username is not already taken
if username not in [client.getUsername() for client in self.clients]:
new_player = Player(
Client(
socket=client_socket,
address=Socket_address(
ip=client_address[0],
port=client_address[1]
),
username=username
)
)
# username accepted
self.clients.append(
new_player
)
# permet access to self.clients variable
self.lock.release()
client_socket.send(
f"Server << Welcome {username} :)".encode())
self.logger.info(f"{username} joined the chatroom")
# inform other clients
with self.lock:
players_list = [player.getUsername(
) for game in self.games for player in game.players]
self._broadcast(
f"{username} joined the chatroom".encode(),
[
client for client in self.clients
if client.getUsername()
not in players_list
and new_player.getUsername() not in players_list
]
)
return new_player
else:
# permet access to self.clients variable if first condition not satisfied.
# Preventing undefinete block of access to self.clients
self.lock.release()
# request another username
self._request_client_username(
client_socket, client_address, f"Server << Username is already taken. Connection will be closed on no attempts left. {3 - nbr_of_attempts - 1} attempts left. Enter a different username:", nbr_of_attempts + 1)
else:
# invalide username format
self._request_client_username(
client_socket, client_address, f"Server << Invalid username. Connection will be closed on no attempts left. {3 - nbr_of_attempts - 1} attempts left. Enter a different username:", nbr_of_attempts + 1)
# handle incoming messages for each client
@override
def _handle_client(self, player: Player):
try:
while not self.close_event.is_set():
data = player.getSocket().recv(1024)
if not data:
break
else:
message = data.decode().strip()
# client requesting closing connection
if message == "close":
self._disconnect_client(player)
# client requesting starting a match with an oppenent
elif message.split(" ")[0] == "play":
# check if client exists
oppenent_username = message.split(" ")[1]
oppenent_exists, opponent, error_message = self.__get_opponent(
oppenent_username)
if oppenent_username == player.getUsername():
player.getSocket().send(
f"Server << Please enter a valid oppenent's username".encode())
if player.is_locked():
player.do_unlock()
else:
if oppenent_exists:
self.games_requests.append(
(player.getUsername(), oppenent_username)
)
opponent.getSocket().send(f"Server << {player.getUsername(
)} is requesting to play Rock Paper Scissors with you. Do you accept ?\n(Enter --> accept) or (Press <Enter> to refuse)".encode()
)
# lock the requesting player here,
# until the game ends
player.do_lock()
else:
player.getSocket().send(
f"Server << {error_message}".encode()
)
if player.is_locked():
player.do_unlock()
elif message.split(" ")[0].startswith("accept"):
oppenent_exists, opponent, error_message = self.__get_game_request(
player
)
if oppenent_exists:
with self.games_requests_lock:
self.games_requests = [game_request for game_request in self.games_requests if game_request != (
opponent.getUsername(), player.getUsername())
]
new_game = Game(self)
with new_game.lock:
new_game.addPlayer(player)
new_game.addPlayer(opponent)
with self.lock:
self.games.append(new_game)
game_thread = threading.Thread(
target=new_game.start_game
)
game_thread.start()
# end of game
game_thread.join()
# release opponenet lock
# if player.is_locked():
# player.do_unlock()
# # inform opponent
# remove the game from the list of games
with self.lock:
self.games = [
game for game in self.games if game.id != new_game.id
]
else:
player.getSocket().send(
f"Server << {error_message}".encode())
else:
oppenent_exists, opponent, error_message = self.__get_game_request(
player
)
if oppenent_exists:
with self.games_requests_lock:
self.games_requests = [game_request for game_request in self.games_requests if game_request != (
opponent.getUsername(), player.getUsername())
]
self.chat_logger.info(
f"{player.getUsername()} refused game request from {opponent.getUsername()}")
opponent.getSocket().send(
f"Server << {player.getUsername()} refused your request".encode())
else:
client_username = player.getUsername()
self.chat_logger.info(
f"{client_username} << {message}"
)
with self.lock:
players_list = [player.getUsername(
) for game in self.games for player in game.players]
self._broadcast(
f"{client_username} << {message}".encode(),
[
client for client in self.clients
if client.getUsername()
not in players_list
and client_username != client.getUsername()
]
)
if player.is_locked():
player.do_unlock()
except KeyboardInterrupt:
self._close_server()
except socket.error as e:
# send a close signal to client's socket when ERROR
self.logger.warning(f"Error handling client: {e}")
# close connection with client on ERROR
with self.lock:
self.clients.remove(player)
def __get_game_request(self, oppenent: Player) -> (bool, Player | None):
with self.games_requests_lock:
error_message = ""
# check if the request exists
for games_request in self.games_requests:
if games_request[1] == oppenent.getUsername():
# check if the oppenent exists
oppenent_exists, oppenent, message = self.__get_opponent(
games_request[0]
)
error_message = message
if oppenent_exists:
return True, oppenent, None
return False, None, error_message
def __get_opponent(self, oppenent_username: str) -> (bool, Player | None, str | None):
with self.lock:
player_busy = [player for game in self.games for player in game.players if player.getUsername(
) == oppenent_username]
if len(player_busy) > 0:
return False, None, "Player is busy playing another match"
for client in self.clients:
if client.getUsername() == oppenent_username:
return True, client, None
return False, None, f"Player <{oppenent_username}> not found"
class Game():
id = 0
def __init__(self, gameServer: GameServer):
self.gameServer = gameServer
self.players: List[Player] = list()
self.lock = threading.Lock()
self.game_close_event = threading.Event()
self.logger = logging.getLogger("Game")
Game.id += 1
def start_game(self):
# Start the rock-paper-scissors game logic here
player1, player2 = self.players
while not self.game_close_event.is_set():
self.logger.info(
f"Game Started {player1.getUsername()} VS {
player2.getUsername()}"
)
player1.getSocket().send(
f"Server << You are playing against <{
player2.getUsername()}>".encode()
)
player2.getSocket().send(
f"Server << You are playing against <{
player1.getUsername()}>".encode()
)
self.gameServer._broadcast("Server << Make your choice...\n1- Rock\n2- Paper\n3- Scissors".encode(),
[player1.getClient(), player2.getClient()]
)
# assign a thread for each new subscribed client
player1_handler = threading.Thread(
target=self.__handle_client, args=(player1,), daemon=True)
player2_handler = threading.Thread(
target=self.__handle_client, args=(player2,), daemon=True)
player1_handler.start()
player2_handler.start()
player1_handler.join()
player2_handler.join()
if self.game_close_event.is_set():
# Reset players for the next round
with self.lock:
for player in self.players:
player.setChoice(RPSEnum.NoChoice)
break
result = self.__determine_winner(RPSEnum(player1.getChoice(
)), RPSEnum(player2.getChoice()))
if result == 0:
self.logger.info(
f"{player1.getUsername()} VS {player2.getUsername()} << It's a tie!")
self.server._broadcast("Server << It's a tie!".encode(), [
player1.getClient(), player2.getClient()])
elif result == 1:
self.logger.info(
f"{player1.getUsername()} VS {player2.getUsername()} << {player1.getUsername()} won!!")
player1.getSocket().send("Server << You win!!".encode())
self.gameServer._broadcast(f"Server << {player1.getUsername()} wins!".encode(), [
player2.getClient()])
player1.incrementScore()
else:
self.logger.info(
f"{player1.getUsername()} VS {player2.getUsername()} << {player2.getUsername()} won!!")
player2.getSocket().send("Server << You win!!".encode())
self.gameServer._broadcast(f"Server << You lost :((".encode(), [
player1.getClient()])
player2.incrementScore()
# Reset players for the next round
with self.lock:
for player in self.players:
player.setChoice(RPSEnum.NoChoice)
# handle incoming messages for each client
def __handle_client(self, player: Player):
while not self.game_close_event.is_set() and player.getChoice() == RPSEnum.NoChoice:
data = player.getSocket().recv(1024)
if not data:
player.setChoice(RPSEnum.NoChoice)
else:
message = data.decode().strip()
if message == "close":
self.server._disconnect_client(player.getClient())
else:
player_username = player.getClient().getUsername()
with self.lock:
opponent = [
other_player for other_player in self.players if other_player.getUsername() != player_username
][0]
if message in ["1", "2", "3"]:
player.setChoice(RPSEnum(int(message)))
self.gameServer.logger.info(
f"{player_username} VS {opponent.getUsername()} << {player_username} chose {RPSEnum(int(message)).name}")
elif message == "exit":
opponent = [opponent for oppenent in self.players if opponent.getUsername(
) != player_username
]
if len(opponent) > 0:
opponent[0].getSocket().send(f"Server << {player_username} quit the game. You win :)".encode()
)
while not self.game_close_event.is_set():
player.getSocket().send(
f"Server << Waiting for your oppenent to exit the game...".encode())
sleep(1)
player.getSocket().send(
f"Server << Welcome back to the chat :)".encode())
if player.is_locked():
player.do_unlock()
return
elif message == "quit":
self.game_close_event.set()
player.getSocket().send(f"Server << Welcome back to the chat :)".encode())
if player.is_locked():
player.do_unlock()
return
else:
if not self.game_close_event.is_set():
player.getSocket().send("Server << Please enter a valid choice!".encode())
def __determine_winner(self, choice1: RPSEnum, choice2: RPSEnum):
if choice1 == RPSEnum.Rock:
return self.rock(choice2)
elif choice1 == RPSEnum.Paper:
return self.paper(choice2)
elif choice1 == RPSEnum.Scissors:
return self.scissors(choice2)
else:
return 2
def addPlayer(self, player: Player):
if len(self.players) < 2:
self.players.append(player)
else:
raise TooManyPlayersError("Number of players exceeded")
def rock(self, other_choice):
if other_choice == 'rock':
return 0 # Tie
elif other_choice == 'paper':
return 2 # Player 2 wins
elif other_choice == 'scissors':
return 1 # Player 1 wins
def paper(self, other_choice):
if other_choice == 'rock':
return 1 # Player 1 wins
elif other_choice == 'paper':
return 0 # Tie
elif other_choice == 'scissors':
return 2 # Player 2 wins
def scissors(self, other_choice):
if other_choice == 'rock':
return 2 # Player 2 wins
elif other_choice == 'paper':
return 1 # Player 1 wins
elif other_choice == 'scissors':
return 0 # Tie
if __name__ == "__main__":
close_event = threading.Event()
server = GameServer(("127.0.0.1", 12345), close_event)
start_thread = threading.Thread(target=server.start)
user_input_handler = UserInputHandler(server, close_event)
user_input_thread = threading.Thread(target=user_input_handler.start)
try:
start_thread.start()
# send_thread.start()
user_input_thread.start()
# waiting for the closing flag
server.close_event.wait()
except KeyboardInterrupt:
close_event.set()
logging.warning("Server has shut down.")
finally:
user_input_handler.stop()
user_input_thread.join()
logging.info(f"Exiting...")