-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
173 lines (166 loc) · 6.84 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
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
import asyncio
import websockets
import json
import uuid
import socket
# Store rooms: token -> room state
rooms = {}
# Map WebSocket connections to room tokens
player_rooms = {}
async def notify_players(token, message):
"""Send a message to both players in a room."""
for player in rooms[token]["players"]:
await player.send(json.dumps(message))
def determine_winner(choices):
"""Determine the winner of a round: R (Stone), P (Paper), S (Scissors)."""
if choices[0] == choices[1]:
return "tie"
choices_list = ["R", "P", "S"]
index1 = choices_list.index(choices[0])
index2 = choices_list.index(choices[1])
return 0 if (index1 - index2) % 3 == 1 else 1
async def run_game(token):
"""Run a 3-round game for a room."""
room = rooms[token]
for round_num in range(1, 4):
room["round"] = round_num
room["choices"] = [None, None]
usernames = room["player_usernames"]
await notify_players(token, {
"status": "round_start",
"round": round_num,
"message": f"Round {round_num}: Enter your choice (R for Stone, P for Paper, S for Scissors)"
})
while None in room["choices"]:
await asyncio.sleep(0.1)
winner = determine_winner(room["choices"])
if winner != "tie":
room["scores"][winner] += 1
winner_name = "Tie" if winner == "tie" else usernames[winner]
await notify_players(token, {
"status": "round_result",
"round": round_num,
"winner": winner_name,
"scores": room["scores"],
"choices": room["choices"]
})
scores = room["scores"]
usernames = room["player_usernames"]
overall_winner = usernames[0] if scores[0] > scores[1] else usernames[1] if scores[1] > scores[0] else "Tie"
await notify_players(token, {
"status": "game_over",
"winner": overall_winner,
"final_scores": scores
})
del rooms[token]
for ws in room["players"]:
if ws in player_rooms:
del player_rooms[ws]
async def handle_client(websocket, path):
"""Handle incoming client messages."""
player_rooms[websocket] = None
try:
async for message in websocket:
data = json.loads(message)
action = data.get("action")
if action == "create_room":
if len(rooms) >= 50:
await websocket.send(json.dumps({
"status": "error",
"message": "Server is full"
}))
continue
token = str(uuid.uuid4())
rooms[token] = {
"players": [websocket],
"choices": [None, None],
"scores": [0, 0],
"round": 0,
"player_usernames": [data.get("username")]
}
player_rooms[websocket] = token
print(f"Room created: {token} by {data.get('username')}")
await websocket.send(json.dumps({
"status": "room_created",
"token": token,
"message": f"Room created. Share this token: {token}"
}))
await websocket.send(json.dumps({
"status": "waiting_for_opponent",
"message": "Waiting for another player to join..."
}))
elif action == "join_room":
token = data.get("token")
if not isinstance(token, str) or token not in rooms:
print(f"Join failed: Invalid token {token} from {data.get('username')}")
await websocket.send(json.dumps({
"status": "error",
"message": "Room does not exist or invalid token"
}))
continue
if len(rooms[token]["players"]) >= 2:
await websocket.send(json.dumps({
"status": "error",
"message": "Room is full"
}))
continue
rooms[token]["players"].append(websocket)
rooms[token]["player_usernames"].append(data.get("username"))
player_rooms[websocket] = token
usernames = rooms[token]["player_usernames"]
print(f"{data.get('username')} joined room {token}")
await notify_players(token, {
"status": "game_start",
"opponents": usernames,
"message": f"Game starting! {usernames[0]} vs {usernames[1]}"
})
asyncio.create_task(run_game(token))
elif action == "play":
token = player_rooms.get(websocket)
if token and rooms[token]["round"] > 0:
player_index = rooms[token]["players"].index(websocket)
choice = data.get("choice").upper()
if choice in ["R", "P", "S"] and rooms[token]["choices"][player_index] is None:
rooms[token]["choices"][player_index] = choice
else:
await websocket.send(json.dumps({
"status": "error",
"message": "Invalid choice or already submitted"
}))
except websockets.ConnectionClosed:
token = player_rooms.get(websocket)
if token and token in rooms:
remaining_players = [p for p in rooms[token]["players"] if p != websocket]
if remaining_players:
await remaining_players[0].send(json.dumps({
"status": "error",
"message": "Opponent disconnected. Game over."
}))
print(f"Player disconnected from room {token}")
del rooms[token]
for ws in rooms[token]["players"]:
if ws in player_rooms:
del player_rooms[ws]
if websocket in player_rooms:
del player_rooms[websocket]
# Get the machine's IPv6 address
def get_ipv6_address():
"""Fetch the local IPv6 address using socket."""
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
try:
# Connect to a public IPv6 address (Google DNS) to get local IPv6
s.connect(('2001:4860:4860::8888', 80))
ip = s.getsockname()[0]
except Exception:
ip = '::1' # Fallback to IPv6 localhost
finally:
s.close()
return ip
# Start the server on IPv6
server_ip = get_ipv6_address()
port = 8765
# Bind to all IPv6 interfaces (::)
start_server = websockets.serve(handle_client, "::", port, family=socket.AF_INET6)
asyncio.get_event_loop().run_until_complete(start_server)
print(f"Server running on ws://[{server_ip}]:{port}")
asyncio.get_event_loop().run_forever()