R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
ListCommand.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** Created on 14/01/2026.
4** File description:
5** ListCommand.cpp
6*/
7
9#include <sstream>
10#include "server/Rooms/Room.hpp"
12
13namespace server {
14
15 std::string ListCommand::execute(const std::vector<std::string> &args, const CommandContext &context) {
16 (void)args;
17
18 std::ostringstream oss;
19 uint32_t hostId = context.room->getHost();
20
21 // List players
22 std::vector<uint32_t> players = context.room->getPlayers();
23 oss << "=== Room " << context.room->getId() << " ===";
24 oss << "\nPlayers (" << players.size() << "/" << context.room->getMaxPlayers() << "): ";
25
26 if (players.empty()) {
27 oss << "(none)";
28 } else {
29 for (uint32_t playerId : players) {
30
31 const LobbyPlayer *player = context.server->getLobby()->getPlayer(playerId);
32 std::string playerName = player ? player->playerName : ("Player" + std::to_string(playerId));
33
34 oss << "\n - " << playerName << " (id: " << playerId << ")";
35 if (playerId == hostId) {
36 oss << "*"; // * indicates host
37 }
38 }
39 }
40
41 // List spectators
42 std::vector<uint32_t> spectators = context.room->getSpectators();
43 oss << "\nSpectators (" << spectators.size() << "): ";
44
45 if (spectators.empty()) {
46 oss << "(none)";
47 } else {
48 bool first = true;
49 for (uint32_t spectatorId : spectators) {
50 if (!first)
51 oss << ", ";
52 first = false;
53
54 const server::LobbyPlayer *spectator = context.server->getLobby()->getPlayer(spectatorId);
55 std::string spectatorName =
56 spectator ? spectator->playerName : ("Spectator" + std::to_string(spectatorId));
57
58 oss << spectatorName;
59 }
60 }
61
62 return oss.str();
63 }
64
65} // namespace server
std::shared_ptr< server::Lobby > getLobby() const
Get the lobby instance (for commands)
Definition Server.hpp:299
std::string execute(const std::vector< std::string > &args, const CommandContext &context) override
Execute the command.
Context information for command execution.
std::shared_ptr< Room > room
Room where the command is executed.
::Server * server
Server instance for operations (global namespace)
Information about a player in the lobby.
Definition Lobby.hpp:26
std::string playerName
Definition Lobby.hpp:28