multiplayer 2.0.3__tar.gz → 2.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {multiplayer-2.0.3 → multiplayer-2.2.0}/PKG-INFO +1 -1
- {multiplayer-2.0.3 → multiplayer-2.2.0}/pyproject.toml +1 -1
- {multiplayer-2.0.3 → multiplayer-2.2.0}/pyproject.toml.orig +1 -1
- {multiplayer-2.0.3 → multiplayer-2.2.0}/src/multiplayer/game.py +31 -1
- {multiplayer-2.0.3 → multiplayer-2.2.0}/src/multiplayer/save.py +3 -0
- {multiplayer-2.0.3 → multiplayer-2.2.0}/src/multiplayer/server.py +126 -9
- {multiplayer-2.0.3 → multiplayer-2.2.0}/README.md +0 -0
- {multiplayer-2.0.3 → multiplayer-2.2.0}/src/multiplayer/__init__.py +0 -0
- {multiplayer-2.0.3 → multiplayer-2.2.0}/src/multiplayer/client.py +0 -0
- {multiplayer-2.0.3 → multiplayer-2.2.0}/src/multiplayer/data/cities.csv +0 -0
- {multiplayer-2.0.3 → multiplayer-2.2.0}/src/multiplayer/data/countries.csv +0 -0
- {multiplayer-2.0.3 → multiplayer-2.2.0}/src/multiplayer/data/egyptian_gods.csv +0 -0
- {multiplayer-2.0.3 → multiplayer-2.2.0}/src/multiplayer/data/european_kings.csv +0 -0
- {multiplayer-2.0.3 → multiplayer-2.2.0}/src/multiplayer/data/european_queens.csv +0 -0
- {multiplayer-2.0.3 → multiplayer-2.2.0}/src/multiplayer/data/greek_gods.csv +0 -0
- {multiplayer-2.0.3 → multiplayer-2.2.0}/src/multiplayer/data/planets_moons.csv +0 -0
- {multiplayer-2.0.3 → multiplayer-2.2.0}/src/multiplayer/data/rivers.csv +0 -0
- {multiplayer-2.0.3 → multiplayer-2.2.0}/src/multiplayer/data/roman_gods.csv +0 -0
- {multiplayer-2.0.3 → multiplayer-2.2.0}/src/multiplayer/data/seas_oceans.csv +0 -0
- {multiplayer-2.0.3 → multiplayer-2.2.0}/src/multiplayer/exceptions.py +0 -0
- {multiplayer-2.0.3 → multiplayer-2.2.0}/src/multiplayer/py.typed +0 -0
- {multiplayer-2.0.3 → multiplayer-2.2.0}/src/multiplayer/utils.py +0 -0
|
@@ -524,17 +524,22 @@ class GameGroup:
|
|
|
524
524
|
parameters (dict): Custom parameters for the group.
|
|
525
525
|
"""
|
|
526
526
|
|
|
527
|
-
def __init__(self, name: str, **kwargs: Any):
|
|
527
|
+
def __init__(self, name: str, password: str | None = None, **kwargs: Any):
|
|
528
528
|
"""Initializes a new game group.
|
|
529
529
|
|
|
530
530
|
Args:
|
|
531
531
|
name (str): The name of the group.
|
|
532
|
+
password: Optional password required to access the group.
|
|
532
533
|
**kwargs: Optional custom parameters for the group.
|
|
533
534
|
"""
|
|
534
535
|
self._id: str = str(uuid.uuid4())
|
|
535
536
|
self.name: str = name
|
|
536
537
|
self._games: List[Game] = []
|
|
537
538
|
self.parameters: Dict[str, Any] = kwargs
|
|
539
|
+
self._hash: str | None = (
|
|
540
|
+
bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
|
|
541
|
+
if password else None
|
|
542
|
+
)
|
|
538
543
|
|
|
539
544
|
@property
|
|
540
545
|
def ID(self) -> str:
|
|
@@ -554,6 +559,31 @@ class GameGroup:
|
|
|
554
559
|
"""
|
|
555
560
|
return tuple(self._games)
|
|
556
561
|
|
|
562
|
+
@property
|
|
563
|
+
def hash(self) -> str | None:
|
|
564
|
+
"""The bcrypt hash of the group access password, if configured."""
|
|
565
|
+
return self._hash
|
|
566
|
+
|
|
567
|
+
@property
|
|
568
|
+
def password_required(self) -> bool:
|
|
569
|
+
"""Whether access to the group requires a password."""
|
|
570
|
+
return self._hash is not None
|
|
571
|
+
|
|
572
|
+
def check_password(self, password: str | None) -> bool:
|
|
573
|
+
"""Checks a plain-text group password."""
|
|
574
|
+
return bool(
|
|
575
|
+
self._hash
|
|
576
|
+
and isinstance(password, str)
|
|
577
|
+
and bcrypt.checkpw(password.encode(), self._hash.encode())
|
|
578
|
+
)
|
|
579
|
+
|
|
580
|
+
def change_password(self, new_password: str | None) -> None:
|
|
581
|
+
"""Sets a password or removes protection when ``None`` is supplied."""
|
|
582
|
+
self._hash = (
|
|
583
|
+
bcrypt.hashpw(new_password.encode(), bcrypt.gensalt()).decode()
|
|
584
|
+
if new_password else None
|
|
585
|
+
)
|
|
586
|
+
|
|
557
587
|
def add_game(self, game: Game | str) -> None:
|
|
558
588
|
"""Adds a game session to the group.
|
|
559
589
|
|
|
@@ -171,6 +171,7 @@ def _group_to_dict(group: GameGroup) -> Dict[str, Any]:
|
|
|
171
171
|
return {
|
|
172
172
|
"id": group.ID,
|
|
173
173
|
"name": group.name,
|
|
174
|
+
"hash": group.hash,
|
|
174
175
|
"games": [_game_to_dict(g) for g in group.games],
|
|
175
176
|
"parameters": group.parameters,
|
|
176
177
|
}
|
|
@@ -188,6 +189,8 @@ def _group_from_dict(data: Dict[str, Any]) -> GameGroup:
|
|
|
188
189
|
group = GameGroup.__new__(GameGroup)
|
|
189
190
|
group._id = data["id"]
|
|
190
191
|
group.name = data["name"]
|
|
192
|
+
# ``hash`` is optional for backward compatibility with older save files.
|
|
193
|
+
group._hash = data.get("hash")
|
|
191
194
|
group._games = [_game_from_dict(g) for g in data.get("games", [])]
|
|
192
195
|
group.parameters = data.get("parameters", {})
|
|
193
196
|
return group
|
|
@@ -151,6 +151,7 @@ class ClientSession:
|
|
|
151
151
|
self._writer: "asyncio.StreamWriter" = writer
|
|
152
152
|
self._guest_default_player_id: Optional[str] = None
|
|
153
153
|
self.subscribed_groups: Set[str] = set()
|
|
154
|
+
self.authorized_groups: Set[str] = set()
|
|
154
155
|
self.joined_games: Set[str] = set()
|
|
155
156
|
|
|
156
157
|
@property
|
|
@@ -280,10 +281,11 @@ class GameServer:
|
|
|
280
281
|
"GROUP_LIST": AccessLevel.BASE,
|
|
281
282
|
"GROUP_SUBSCRIBE": AccessLevel.BASE,
|
|
282
283
|
"GROUP_UNSUBSCRIBE": AccessLevel.BASE,
|
|
283
|
-
"GROUP_ADD_GAME": AccessLevel.
|
|
284
|
+
"GROUP_ADD_GAME": AccessLevel.BASE,
|
|
284
285
|
"GROUP_REMOVE_GAME": AccessLevel.GROUP_ADMIN,
|
|
285
286
|
"GROUP_DELETE": AccessLevel.ADMIN,
|
|
286
287
|
"GROUP_GAME_LIST_ALL": AccessLevel.GROUP_ADMIN,
|
|
288
|
+
"GROUP_PASSWORD_SET": AccessLevel.GROUP_ADMIN,
|
|
287
289
|
"SERVER_INFO_GET": AccessLevel.ADMIN,
|
|
288
290
|
"SERVER_CONFIG_GET": AccessLevel.ADMIN,
|
|
289
291
|
"SERVER_CONFIG_SET": AccessLevel.ADMIN,
|
|
@@ -326,6 +328,7 @@ class GameServer:
|
|
|
326
328
|
"GROUP_REMOVE_GAME": "GROUP_REMOVE_GAME_RESPONSE",
|
|
327
329
|
"GROUP_DELETE": "GROUP_DELETE_RESPONSE",
|
|
328
330
|
"GROUP_GAME_LIST_ALL": "GROUP_GAME_LIST_ALL_RESPONSE",
|
|
331
|
+
"GROUP_PASSWORD_SET": "GROUP_PASSWORD_SET_RESPONSE",
|
|
329
332
|
"SERVER_INFO_GET": "SERVER_INFO_GET_RESPONSE",
|
|
330
333
|
"SERVER_CONFIG_GET": "SERVER_CONFIG_GET_RESPONSE",
|
|
331
334
|
"SERVER_CONFIG_SET": "SERVER_CONFIG_SET_RESPONSE",
|
|
@@ -1641,6 +1644,20 @@ class GameServer:
|
|
|
1641
1644
|
"error_code": "INVALID_DATA",
|
|
1642
1645
|
"message": "A game name is required.",
|
|
1643
1646
|
}
|
|
1647
|
+
group_id = payload.get("group_id")
|
|
1648
|
+
group = None
|
|
1649
|
+
if group_id is not None:
|
|
1650
|
+
group = self._groups.get(group_id)
|
|
1651
|
+
if group is None:
|
|
1652
|
+
return "GAME_CREATE_RESPONSE", {
|
|
1653
|
+
"success": False, "error_code": "GROUP_NOT_FOUND",
|
|
1654
|
+
"message": "Group not found.",
|
|
1655
|
+
}
|
|
1656
|
+
if not self._authorize_group(session, group, payload.get("group_password")):
|
|
1657
|
+
return "GAME_CREATE_RESPONSE", {
|
|
1658
|
+
"success": False, "error_code": "INVALID_GROUP_PASSWORD",
|
|
1659
|
+
"message": "Invalid group password.",
|
|
1660
|
+
}
|
|
1644
1661
|
game = Game(
|
|
1645
1662
|
name=name,
|
|
1646
1663
|
max_players=payload.get("max_players"),
|
|
@@ -1655,6 +1672,8 @@ class GameServer:
|
|
|
1655
1672
|
self._game_roles[game.ID] = {}
|
|
1656
1673
|
self._game_custom_state[game.ID] = {}
|
|
1657
1674
|
self._game_creator_session[game.ID] = session.session_id
|
|
1675
|
+
if group is not None:
|
|
1676
|
+
group.add_game(game)
|
|
1658
1677
|
return "GAME_CREATE_RESPONSE", {
|
|
1659
1678
|
"success": True,
|
|
1660
1679
|
"game_id": game.ID,
|
|
@@ -1682,6 +1701,11 @@ class GameServer:
|
|
|
1682
1701
|
"error_code": "GROUP_NOT_FOUND",
|
|
1683
1702
|
"message": "Group not found.",
|
|
1684
1703
|
}
|
|
1704
|
+
if not self._authorize_group(session, group, payload.get("group_password")):
|
|
1705
|
+
return "GAME_LIST_RESPONSE", {
|
|
1706
|
+
"success": False, "error_code": "INVALID_GROUP_PASSWORD",
|
|
1707
|
+
"message": "Invalid group password.",
|
|
1708
|
+
}
|
|
1685
1709
|
games = [self._game_summary(game) for game in group.games]
|
|
1686
1710
|
else:
|
|
1687
1711
|
games = [self._game_summary(game) for game in self._games.values()]
|
|
@@ -1706,6 +1730,15 @@ class GameServer:
|
|
|
1706
1730
|
"error_code": "GAME_NOT_FOUND",
|
|
1707
1731
|
"message": "Game not found.",
|
|
1708
1732
|
}
|
|
1733
|
+
group_password = payload.get("group_password")
|
|
1734
|
+
for group in self._groups.values():
|
|
1735
|
+
if any(group_game.ID == game.ID for group_game in group.games):
|
|
1736
|
+
if not self._authorize_group(session, group, group_password):
|
|
1737
|
+
return "GAME_JOIN_RESPONSE", {
|
|
1738
|
+
"success": False,
|
|
1739
|
+
"error_code": "INVALID_GROUP_PASSWORD",
|
|
1740
|
+
"message": "Invalid group password.",
|
|
1741
|
+
}
|
|
1709
1742
|
role = payload.get("role")
|
|
1710
1743
|
if role not in ("PLAYER", "OBSERVER"):
|
|
1711
1744
|
return "GAME_JOIN_RESPONSE", {
|
|
@@ -2217,7 +2250,20 @@ class GameServer:
|
|
|
2217
2250
|
"message": "A group name is required.",
|
|
2218
2251
|
}
|
|
2219
2252
|
attributes = payload.get("attributes")
|
|
2220
|
-
|
|
2253
|
+
password = payload.get("password")
|
|
2254
|
+
if password is not None and (not isinstance(password, str) or not password):
|
|
2255
|
+
return "GROUP_CREATE_RESPONSE", {
|
|
2256
|
+
"success": False,
|
|
2257
|
+
"error_code": "INVALID_DATA",
|
|
2258
|
+
"message": "Password must be a non-empty string.",
|
|
2259
|
+
}
|
|
2260
|
+
group_attributes = dict(attributes) if isinstance(attributes, dict) else {}
|
|
2261
|
+
group_attributes.pop("password", None)
|
|
2262
|
+
group = GameGroup(
|
|
2263
|
+
name=name,
|
|
2264
|
+
password=password,
|
|
2265
|
+
**group_attributes,
|
|
2266
|
+
)
|
|
2221
2267
|
self._groups[group.ID] = group
|
|
2222
2268
|
self._audit("GROUP_CREATE", session, group.ID)
|
|
2223
2269
|
return "GROUP_CREATE_RESPONSE", {
|
|
@@ -2239,7 +2285,12 @@ class GameServer:
|
|
|
2239
2285
|
Tuple[str, Dict[str, Any]]: The response type and payload.
|
|
2240
2286
|
"""
|
|
2241
2287
|
groups = [
|
|
2242
|
-
{
|
|
2288
|
+
{
|
|
2289
|
+
"group_id": group.ID,
|
|
2290
|
+
"name": group.name,
|
|
2291
|
+
"games_count": len(group.games),
|
|
2292
|
+
"requires_password": group.password_required,
|
|
2293
|
+
}
|
|
2243
2294
|
for group in self._groups.values()
|
|
2244
2295
|
]
|
|
2245
2296
|
return "GROUP_LIST_RESPONSE", {"success": True, "groups": groups}
|
|
@@ -2263,6 +2314,13 @@ class GameServer:
|
|
|
2263
2314
|
"error_code": "GROUP_NOT_FOUND",
|
|
2264
2315
|
"message": "Group not found.",
|
|
2265
2316
|
}
|
|
2317
|
+
group = self._groups[group_id]
|
|
2318
|
+
if not self._authorize_group(session, group, payload.get("password")):
|
|
2319
|
+
return "GROUP_SUBSCRIBE_RESPONSE", {
|
|
2320
|
+
"success": False,
|
|
2321
|
+
"error_code": "INVALID_GROUP_PASSWORD",
|
|
2322
|
+
"message": "Invalid group password.",
|
|
2323
|
+
}
|
|
2266
2324
|
if group_id in session.subscribed_groups:
|
|
2267
2325
|
return "GROUP_SUBSCRIBE_RESPONSE", {
|
|
2268
2326
|
"success": False,
|
|
@@ -2308,6 +2366,20 @@ class GameServer:
|
|
|
2308
2366
|
"message": "Unsubscribed from group successfully",
|
|
2309
2367
|
}
|
|
2310
2368
|
|
|
2369
|
+
def _authorize_group(
|
|
2370
|
+
self, session: ClientSession, group: GameGroup, password: Any
|
|
2371
|
+
) -> bool:
|
|
2372
|
+
"""Validates group access and remembers successful session authorization."""
|
|
2373
|
+
if not group.password_required:
|
|
2374
|
+
session.authorized_groups.add(group.ID)
|
|
2375
|
+
return True
|
|
2376
|
+
if group.ID in session.authorized_groups:
|
|
2377
|
+
return True
|
|
2378
|
+
if group.check_password(password):
|
|
2379
|
+
session.authorized_groups.add(group.ID)
|
|
2380
|
+
return True
|
|
2381
|
+
return False
|
|
2382
|
+
|
|
2311
2383
|
def _can_admin_group(self, session: ClientSession, group_id: str) -> bool:
|
|
2312
2384
|
"""Checks whether a session can administer a given group.
|
|
2313
2385
|
|
|
@@ -2344,12 +2416,6 @@ class GameServer:
|
|
|
2344
2416
|
"error_code": "GROUP_NOT_FOUND",
|
|
2345
2417
|
"message": "Group not found.",
|
|
2346
2418
|
}
|
|
2347
|
-
if not self._can_admin_group(session, group.ID):
|
|
2348
|
-
return "GROUP_ADD_GAME_RESPONSE", {
|
|
2349
|
-
"success": False,
|
|
2350
|
-
"error_code": "INSUFFICIENT_PERMISSIONS",
|
|
2351
|
-
"message": "Cannot modify this group.",
|
|
2352
|
-
}
|
|
2353
2419
|
game = self._games.get(payload.get("game_id"))
|
|
2354
2420
|
if game is None:
|
|
2355
2421
|
return "GROUP_ADD_GAME_RESPONSE", {
|
|
@@ -2357,6 +2423,19 @@ class GameServer:
|
|
|
2357
2423
|
"error_code": "GAME_NOT_FOUND",
|
|
2358
2424
|
"message": "Game not found.",
|
|
2359
2425
|
}
|
|
2426
|
+
is_creator = self._game_creator_session.get(game.ID) == session.session_id
|
|
2427
|
+
if not is_creator and not self._can_admin_group(session, group.ID):
|
|
2428
|
+
return "GROUP_ADD_GAME_RESPONSE", {
|
|
2429
|
+
"success": False,
|
|
2430
|
+
"error_code": "INSUFFICIENT_PERMISSIONS",
|
|
2431
|
+
"message": "Only the game creator or a group administrator can add this game.",
|
|
2432
|
+
}
|
|
2433
|
+
if not self._authorize_group(session, group, payload.get("group_password")):
|
|
2434
|
+
return "GROUP_ADD_GAME_RESPONSE", {
|
|
2435
|
+
"success": False,
|
|
2436
|
+
"error_code": "INVALID_GROUP_PASSWORD",
|
|
2437
|
+
"message": "Invalid group password.",
|
|
2438
|
+
}
|
|
2360
2439
|
group.add_game(game)
|
|
2361
2440
|
await self._broadcast_group(
|
|
2362
2441
|
group.ID,
|
|
@@ -2424,6 +2503,44 @@ class GameServer:
|
|
|
2424
2503
|
"message": "Game removed from group successfully",
|
|
2425
2504
|
}
|
|
2426
2505
|
|
|
2506
|
+
def _handle_group_password_set(
|
|
2507
|
+
self, session: ClientSession, payload: Dict[str, Any]
|
|
2508
|
+
) -> Tuple[str, Dict[str, Any]]:
|
|
2509
|
+
"""Sets, changes, or removes a group's access password."""
|
|
2510
|
+
group_id = payload.get("group_id")
|
|
2511
|
+
group = self._groups.get(group_id)
|
|
2512
|
+
if group is None:
|
|
2513
|
+
return "GROUP_PASSWORD_SET_RESPONSE", {
|
|
2514
|
+
"success": False, "error_code": "GROUP_NOT_FOUND",
|
|
2515
|
+
"message": "Group not found.",
|
|
2516
|
+
}
|
|
2517
|
+
if not self._can_admin_group(session, group.ID):
|
|
2518
|
+
return "GROUP_PASSWORD_SET_RESPONSE", {
|
|
2519
|
+
"success": False, "error_code": "INSUFFICIENT_PERMISSIONS",
|
|
2520
|
+
"message": "Cannot modify this group.",
|
|
2521
|
+
}
|
|
2522
|
+
password = payload.get("password")
|
|
2523
|
+
if password is not None and (not isinstance(password, str) or not password):
|
|
2524
|
+
return "GROUP_PASSWORD_SET_RESPONSE", {
|
|
2525
|
+
"success": False, "error_code": "INVALID_DATA",
|
|
2526
|
+
"message": "Password must be a non-empty string or null.",
|
|
2527
|
+
}
|
|
2528
|
+
group.change_password(password)
|
|
2529
|
+
if password is None:
|
|
2530
|
+
for client in self._sessions.values():
|
|
2531
|
+
client.authorized_groups.add(group.ID)
|
|
2532
|
+
else:
|
|
2533
|
+
for client in self._sessions.values():
|
|
2534
|
+
client.authorized_groups.discard(group.ID)
|
|
2535
|
+
session.authorized_groups.add(group.ID)
|
|
2536
|
+
self._audit("GROUP_PASSWORD_SET", session, group.ID)
|
|
2537
|
+
return "GROUP_PASSWORD_SET_RESPONSE", {
|
|
2538
|
+
"success": True,
|
|
2539
|
+
"group_id": group.ID,
|
|
2540
|
+
"requires_password": group.password_required,
|
|
2541
|
+
"message": "Group password updated successfully",
|
|
2542
|
+
}
|
|
2543
|
+
|
|
2427
2544
|
def _handle_group_delete(
|
|
2428
2545
|
self, session: ClientSession, payload: Dict[str, Any]
|
|
2429
2546
|
) -> Tuple[str, Dict[str, Any]]:
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|