fix(chat): add validation for mp commands

This commit is contained in:
MingxuanGame
2025-08-18 13:51:52 +00:00
parent 8d5f71f7d8
commit 68f47c5a24

View File

@@ -22,7 +22,7 @@ from app.models.multiplayer_hub import (
ServerMultiplayerRoom, ServerMultiplayerRoom,
StartMatchCountdownRequest, StartMatchCountdownRequest,
) )
from app.models.room import MatchType, QueueMode from app.models.room import MatchType, QueueMode, RoomStatus
from app.models.score import GameMode from app.models.score import GameMode
from app.signalr.hub import MultiplayerHubs from app.signalr.hub import MultiplayerHubs
from app.signalr.hub.hub import Client from app.signalr.hub.hub import Client
@@ -187,14 +187,14 @@ def _roll(
async def _stats( async def _stats(
user: User, args: list[str], session: AsyncSession, channel: ChatChannel user: User, args: list[str], session: AsyncSession, channel: ChatChannel
) -> str: ) -> str:
if len(args) < 1: if len(args) >= 1:
return "Usage: !stats <username> [gamemode]" target_user = (
await session.exec(select(User).where(User.username == args[0]))
target_user = ( ).first()
await session.exec(select(User).where(User.username == args[0])) if not target_user:
).first() return f"User '{args[0]}' not found."
if not target_user: else:
return f"User '{args[0]}' not found." target_user = user
gamemode = None gamemode = None
if len(args) >= 2: if len(args) >= 2:
@@ -368,6 +368,11 @@ async def _mp_team(
user_client = MultiplayerHubs.get_client_by_id(str(user_id)) user_client = MultiplayerHubs.get_client_by_id(str(user_id))
if not user_client: if not user_client:
return f"User '{username}' is not in the room." return f"User '{username}' is not in the room."
if (
user_client.user_id != signalr_client.user_id
and room.room.host.user_id != signalr_client.user_id
):
return "You are not allowed to change other users' teams."
try: try:
await MultiplayerHubs.SendMatchRequest( await MultiplayerHubs.SendMatchRequest(
@@ -429,6 +434,9 @@ async def _mp_map(
if len(args) < 1: if len(args) < 1:
return "Usage: !mp map <mapid> [<playmode>]" return "Usage: !mp map <mapid> [<playmode>]"
if room.status != RoomStatus.IDLE:
return "Cannot change map while the game is running."
map_id = args[0] map_id = args[0]
if not map_id.isdigit(): if not map_id.isdigit():
return "Invalid map ID." return "Invalid map ID."
@@ -485,15 +493,19 @@ async def _mp_mods(
if len(args) < 1: if len(args) < 1:
return "Usage: !mp mods <mod1> [<mod2> ...]" return "Usage: !mp mods <mod1> [<mod2> ...]"
if room.status != RoomStatus.IDLE:
return "Cannot change mods while the game is running."
required_mods = [] required_mods = []
allowed_mods = [] allowed_mods = []
freestyle = False freestyle = False
for arg in args: for arg in args:
if arg == "None": arg = arg.upper()
if arg == "NONE":
required_mods.clear() required_mods.clear()
allowed_mods.clear() allowed_mods.clear()
break break
elif arg == "Freestyle": elif arg == "FREESTYLE":
freestyle = True freestyle = True
elif arg.startswith("+"): elif arg.startswith("+"):
mod = arg.removeprefix("+") mod = arg.removeprefix("+")