R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
KickCommand.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** Created on 14/01/2026.
4** File description:
5** KickCommand.cpp
6*/
7
10#include "server/Rooms/Room.hpp"
12
13namespace server {
14
15 std::string KickCommand::execute(const std::vector<std::string> &args, const CommandContext &context) {
16 // Check if player is host
17 if (context.room->getHost() != context.playerId) {
18 return "Error: Only the room host can kick players.";
19 }
20
21 // Check arguments
22 if (args.empty()) {
23 return "Error: Missing player id. Usage: /kick <player_id>";
24 }
25
26 // Get all players in the room
27 std::vector<uint32_t> players = context.room->getPlayers();
28
29 try {
30 if (args[0].empty() || std::stoul(args[0]) == 0) {
31 return "Error: Invalid player id.";
32 }
33 } catch (std::invalid_argument &) {
34 return "Error: Invalid player id.";
35 }
36
37 // Get target id from args[0] and cast it to uint32_t
38 uint32_t targetId = std::stoul(args[0]);
39
40 // Find target player by id
41 uint32_t targetPlayerId = 0;
42 for (uint32_t playerId : players) {
43 if (playerId == targetId) {
44 targetPlayerId = playerId;
45 break;
46 }
47 }
48
49 if (targetPlayerId == 0) {
50 return "Error: Player not found in this room.";
51 }
52
53 // Check if trying to kick self
54 if (targetPlayerId == context.playerId) {
55 return "Error: You cannot kick yourself. Use /leave to exit the room.";
56 }
57
58 // Check if target is a spectator (cannot kick spectators)
59 if (context.room->hasSpectator(targetPlayerId)) {
60 return "Error: Cannot kick spectators.";
61 }
62
63 // Kick the player
64 LOG_INFO("Player ", context.playerName, " (", context.playerId, ") kicked ", targetId, " (",
65 targetPlayerId, ") from room ", context.room->getId());
66
67 // Use server's kickPlayer method which handles all cleanup and notifications
68 bool success = context.server->kickPlayer(targetPlayerId);
69
70 if (!success) {
71 return "Error: Failed to kick player from the room.";
72 }
73
74 return "Player has been kicked from the room.";
75 }
76
77} // namespace server
#define LOG_INFO(...)
Definition Logger.hpp:181
bool kickPlayer(uint32_t playerId)
Kick a player from their current room (for commands)
Definition Server.cpp:1200
std::string execute(const std::vector< std::string > &args, const CommandContext &context) override
Execute the command.
Context information for command execution.
uint32_t playerId
ID of the player executing the command.
std::string playerName
Name of the player executing the command.
std::shared_ptr< Room > room
Room where the command is executed.
::Server * server
Server instance for operations (global namespace)