-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
89 lines (75 loc) · 3.1 KB
/
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
import socket
import threading
import json
from queue import Queue
from game_logic import TicTacToe
class GameServer:
def __init__(self, host='0.0.0.0', port=5555):
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.bind((host, port))
self.server.listen()
self.waiting_players = Queue()
self.games = {}
print(f"Server started on port {port}")
def start(self):
while True:
client, addr = self.server.accept()
print(f"Connected to {addr}")
threading.Thread(target=self.handle_queue, args=(client,)).start()
def handle_queue(self, client):
if self.waiting_players.empty():
self.waiting_players.put(client)
self.send_message(client, {"type": "wait", "message": "Waiting for opponent..."})
else:
player1 = self.waiting_players.get()
player2 = client
game_id = len(self.games)
self.games[game_id] = TicTacToe()
threading.Thread(target=self.handle_game,
args=(game_id, player1, player2)).start()
def handle_game(self, game_id, player1, player2):
players = {player1: "X", player2: "O"}
current_player = player1
game = self.games[game_id]
# Inform players about their symbols
self.send_message(player1, {"type": "start", "symbol": "X"})
self.send_message(player2, {"type": "start", "symbol": "O"})
while True:
try:
# Send game state to both players
state = {
"type": "state",
"board": game.board,
"current_player": game.current_player
}
self.send_message(player1, state)
self.send_message(player2, state)
if current_player == player1:
data = self.receive_message(player1)
else:
data = self.receive_message(player2)
if data["type"] == "move":
position = data["position"]
if game.make_move(position):
winner = game.check_winner()
if winner:
end_state = {
"type": "end",
"board": game.board,
"winner": winner
}
self.send_message(player1, end_state)
self.send_message(player2, end_state)
break
current_player = player2 if current_player == player1 else player1
except:
print(f"Game {game_id} ended unexpectedly")
break
del self.games[game_id]
def send_message(self, client, message):
client.send(json.dumps(message).encode())
def receive_message(self, client):
return json.loads(client.recv(1024).decode())
if __name__ == "__main__":
server = GameServer()
server.start()