5const size_t GameManager::MAX_PLAYERS_PER_GAME;
8 : broadcastFunc_(
std::move(broadcastFunc))
13 auto it = clientToGame_.find(clientId);
14 if (it != clientToGame_.end()) {
15 auto &gInst = games_.at(it->second);
16 return gInst.game->getCurrentGameState();
20 uint32_t gid = findOrCreateGame();
21 auto &gInst = games_.at(gid);
23 gInst.clients.push_back(clientId);
24 clientToGame_[clientId] = gid;
26 std::cout <<
"[GameManager] Added client " << clientId
27 <<
" to game " << gid <<
"\n";
30 auto gameState = gInst.game->getCurrentGameState();
32 std::string evt =
"\x01" + std::to_string(clientId);
33 gInst.game->onServerEventReceived(evt);
41 uint32_t gid = findOrCreateGame();
42 auto &gInst = games_.at(gid);
44 gInst.clients.push_back(clientId);
45 clientToGame_[clientId] = gid;
47 std::cout <<
"[GameManager] Added client " << clientId
48 <<
" to game " << gid <<
"\n";
54 auto it = clientToGame_.find(clientId);
55 if (it == clientToGame_.end()) {
59 uint32_t gid = it->second;
60 auto &gInst = games_.at(gid);
63 auto &cList = gInst.clients;
64 cList.erase(std::remove(cList.begin(), cList.end(), clientId), cList.end());
67 std::string evt =
"\x02" + std::to_string(clientId);
68 gInst.game->onServerEventReceived(evt);
70 clientToGame_.erase(clientId);
72 std::cout <<
"[GameManager] Removed client " << clientId
73 <<
" from game " << gid <<
"\n";
77 std::cout <<
"[GameManager] Destroyed game " << gid
78 <<
" (no players left)\n";
83 auto it = clientToGame_.find(clientId);
84 if (it != clientToGame_.end()) {
91 for (
auto &pair : games_) {
92 auto &gInst = pair.second;
95 gInst.game->update(dt);
98 auto events = gInst.game->getGameEvents();
101 for (
const auto &evt : events) {
104 outMsg.
type = evt[0];
107 outMsg.
payload.assign(evt.begin() + 1, evt.end());
110 for (
auto cid : gInst.clients) {
111 broadcastFunc_(cid, outMsg);
118 auto it = games_.find(gameId);
119 if (it == games_.end())
return;
121 auto &gInst = it->second;
125 std::string eventString(
126 reinterpret_cast<const char*
>(msg.
payload.data()),
130 gInst.game->onServerEventReceived(eventString);
136uint32_t GameManager::findOrCreateGame() {
138 for (
auto &pair : games_) {
139 if (pair.second.clients.size() < MAX_PLAYERS_PER_GAME) {
144 uint32_t gid = nextGameId_++;
147 gInst.
game = std::make_unique<Game>();
148 games_[gid] = std::move(gInst);
150 std::cout <<
"[GameManager] Created new game " << gid <<
"\n";
uint32_t assignClientToGame(uint32_t clientId)
Assign a client to a new or existing game.
uint32_t getGameIdForClient(uint32_t clientId) const
Get the game ID for a client, or 0 if none.
void updateAllGames(float dt)
Update all games with the given deltaTime and broadcast events to clients.
std::forward_list< std::string > syncClientToGame(uint32_t clientId)
Gets the current game state for syncing.
void removeClientFromGame(uint32_t clientId)
Remove a client from whichever game they are in.
GameManager(BroadcastFunc broadcastFunc)
Constructor that takes a broadcast function (to send packets to clients).
std::function< void(uint32_t, const Message &)> BroadcastFunc
void handleGameMessage(uint32_t gameId, uint32_t clientId, const Message &msg)
Handle a message from a client in a specific game.
A single game session: a Game object plus the connected clients.
std::unique_ptr< Game > game
Minimal network message with a 4-byte header (type + length) and a payload.
uint8_t type
Could map to messageType in GameMessage.hpp (CONNECT, DISCONNECT, etc.)
std::vector< uint8_t > payload
Arbitrary payload bytes.