|
Zappy Server
C++ game server: world, rules, scheduler, network
|
Pure game state - no networking, no scheduling. More...
#include <World.hpp>
Collaboration diagram for World:Public Member Functions | |
| World (int width, int height, const std::vector< std::string > &teamNames, unsigned int seed) | |
| Tile & | at (int x, int y) |
| Access a tile by position. Map is toroidal, coordinates wrap. | |
| const Tile & | at (int x, int y) const |
| std::vector< std::pair< int, int > > | spawnResources () |
| int | addPlayer (int connectionId, const std::string &teamName, int x, int y, Orientation orientation) |
| void | removePlayer (int id) |
| void | movePlayer (int id, int x, int y) |
| void | turnPlayer (int id, Orientation orientation) |
| bool | takeResource (int playerId, ResourceType type) |
| bool | setResource (int playerId, ResourceType type) |
| bool | consumeFood (int playerId) |
Consume one unit of food from playerId (starvation tick). Fires onPlayerInventoryChanged so observers stay in sync. | |
| Player & | getPlayer (int id) |
| void | playerBroadcast (int playerId, const std::string &message) |
| Fire onBroadcast to observers (GUI animation, logging). No state change. | |
| EjectResult | ejectPlayers (int ejectorId) |
| void | spawnInitialEggs (int countPerTeam) |
Spawn countPerTeam eggs for every team at random tiles (server startup). | |
| int | addEgg (int playerId) |
| bool | hatchEgg (int eggId) |
| std::optional< Egg > | popEggForTeam (const std::string &teamName) |
| int | teamEggCount (const std::string &team) const |
| std::optional< std::vector< int > > | startIncantation (int playerId) |
Validate and start an incantation for playerId. Returns the list of participant IDs on success, nullopt if prerequisites fail. Prerequisites are checked again in finalizeIncantation() after the ritual delay. | |
| bool | finalizeIncantation (int x, int y, const std::vector< int > &participantIds) |
| Finalize an incantation after the 300/f second delay. Re-checks prerequisites. On success, levels up all participants and consumes stones. Returns false if any participant died or moved off the tile. | |
| std::optional< std::string > | checkWin () const |
| bool | isGameEnded () const |
| True once a team has won. Game logic stops reacting to AI commands. | |
| const std::optional< std::string > & | winner () const |
| Winning team name, set when isGameEnded() becomes true. | |
| int | teamPlayerCount (const std::string &team) const |
| int | width () const |
| int | height () const |
| const std::unordered_map< int, Player > & | getPlayers () const |
| const std::unordered_map< int, Egg > & | getEggs () const |
| void | addWorldObserver (IWorldObserver *observer) |
Private Member Functions | |
| int | _spawnEgg (const std::string &teamName, int x, int y, int parentPlayerId) |
Private Attributes | |
| int | _width |
| int | _height |
| std::vector< Tile > | _tiles |
| std::unordered_map< int, Player > | _players |
| std::unordered_map< int, Egg > | _eggs |
| std::vector< std::string > | _teamNames |
| std::mt19937 | _rng |
| int | _nextPlayerId = 0 |
| int | _nextEggId = 0 |
| bool | _gameEnded = false |
| std::optional< std::string > | _winner |
| std::vector< IWorldObserver * > | _observers |
Pure game state - no networking, no scheduling.
Owns the map, players, eggs, and teams. The server layer calls mutation methods here after dispatching commands, then notifies GUI clients via GuiNotifier. Keeping World network-free makes it fully unit-testable.
| World::World | ( | int | width, |
| int | height, | ||
| const std::vector< std::string > & | teamNames, | ||
| unsigned int | seed | ||
| ) |
|
private |
Definition at line 194 of file World.cpp.
References _eggs, _nextEggId, _observers, at(), and Tile::eggIds.
Referenced by addEgg(), and spawnInitialEggs().
Here is the call graph for this function:
Here is the caller graph for this function:| int World::addEgg | ( | int | playerId | ) |
Definition at line 216 of file World.cpp.
References _players, and _spawnEgg().
Referenced by CommandDispatcher::_handleFork().
Here is the call graph for this function:
Here is the caller graph for this function:| int World::addPlayer | ( | int | connectionId, |
| const std::string & | teamName, | ||
| int | x, | ||
| int | y, | ||
| Orientation | orientation | ||
| ) |
Definition at line 58 of file World.cpp.
References _nextPlayerId, _observers, _players, at(), Player::connectionId, Resources::food, Player::id, id(), Player::inventory, Player::level, Player::orientation, Tile::playerIds, Player::teamName, Player::x, and Player::y.
Referenced by HandshakeHandler::_promoteToAi().
Here is the call graph for this function:
Here is the caller graph for this function:| void World::addWorldObserver | ( | IWorldObserver * | observer | ) |
Definition at line 420 of file World.cpp.
References _observers.
Referenced by Server::Server().
Here is the caller graph for this function:| Tile & World::at | ( | int | x, |
| int | y | ||
| ) |
Access a tile by position. Map is toroidal, coordinates wrap.
Definition at line 12 of file World.cpp.
References _height, _tiles, and _width.
Referenced by CommandDispatcher::_handleBct(), CommandDispatcher::_handleLook(), CommandDispatcher::_handleMct(), HandshakeHandler::_promoteToGui(), _spawnEgg(), addPlayer(), ejectPlayers(), finalizeIncantation(), hatchEgg(), movePlayer(), popEggForTeam(), removePlayer(), setResource(), spawnResources(), startIncantation(), and takeResource().
Here is the caller graph for this function:| const Tile & World::at | ( | int | x, |
| int | y | ||
| ) | const |
| std::optional< std::string > World::checkWin | ( | ) | const |
Definition at line 405 of file World.cpp.
References _players.
Referenced by finalizeIncantation().
Here is the caller graph for this function:| bool World::consumeFood | ( | int | playerId | ) |
Consume one unit of food from playerId (starvation tick). Fires onPlayerInventoryChanged so observers stay in sync.
Definition at line 135 of file World.cpp.
References _observers, _players, and Resources::food.
| EjectResult World::ejectPlayers | ( | int | ejectorId | ) |
Definition at line 154 of file World.cpp.
References _eggs, _observers, _players, at(), and movePlayer().
Referenced by CommandDispatcher::_handleEject().
Here is the call graph for this function:
Here is the caller graph for this function:| bool World::finalizeIncantation | ( | int | x, |
| int | y, | ||
| const std::vector< int > & | participantIds | ||
| ) |
Finalize an incantation after the 300/f second delay. Re-checks prerequisites. On success, levels up all participants and consumes stones. Returns false if any participant died or moved off the tile.
Definition at line 331 of file World.cpp.
References _checkReqs(), _gameEnded, _observers, _players, _winner, at(), checkWin(), DERAUMERE, INCANTATION_REQS, LINEMATE, IncantationReq::linemate, MENDIANE, PHIRAS, SIBUR, THYSTAME, and winner().
Referenced by CommandDispatcher::_handleIncantation().
Here is the call graph for this function:
Here is the caller graph for this function:| const std::unordered_map< int, Egg > & World::getEggs | ( | ) | const |
Definition at line 403 of file World.cpp.
References _eggs.
Referenced by HandshakeHandler::_promoteToGui().
Here is the caller graph for this function:| Player & World::getPlayer | ( | int | id | ) |
Definition at line 147 of file World.cpp.
References _players.
Referenced by CommandDispatcher::_handleBroadcast(), CommandDispatcher::_handleConnectNbr(), CommandDispatcher::_handleEject(), CommandDispatcher::_handleForward(), CommandDispatcher::_handleIncantation(), CommandDispatcher::_handleInventory(), CommandDispatcher::_handleLeft(), CommandDispatcher::_handleLook(), CommandDispatcher::_handlePin(), CommandDispatcher::_handlePlv(), CommandDispatcher::_handlePpo(), CommandDispatcher::_handleRight(), and CommandDispatcher::_onAiJoined().
Here is the caller graph for this function:| const std::unordered_map< int, Player > & World::getPlayers | ( | ) | const |
Definition at line 402 of file World.cpp.
References _players.
Referenced by CommandDispatcher::_handleBroadcast(), CommandDispatcher::_handleConnectNbr(), Server::_handleGameOver(), CommandDispatcher::_handleIncantation(), CommandDispatcher::_onAiJoined(), HandshakeHandler::_promoteToGui(), and CommandDispatcher::onDisconnect().
Here is the caller graph for this function:| bool World::hatchEgg | ( | int | eggId | ) |
Definition at line 230 of file World.cpp.
References _eggs, _observers, at(), and Tile::eggIds.
Here is the call graph for this function:| int World::height | ( | ) | const |
Definition at line 401 of file World.cpp.
References _height.
Referenced by CommandDispatcher::_handleBroadcast(), CommandDispatcher::_handleEject(), CommandDispatcher::_handleMct(), CommandDispatcher::_handleMsz(), and World().
Here is the caller graph for this function:| bool World::isGameEnded | ( | ) | const |
True once a team has won. Game logic stops reacting to AI commands.
Definition at line 417 of file World.cpp.
References _gameEnded.
Referenced by CommandDispatcher::_dispatchAi(), HandshakeHandler::onLine(), and Server::run().
Here is the caller graph for this function:| void World::movePlayer | ( | int | id, |
| int | x, | ||
| int | y | ||
| ) |
Definition at line 91 of file World.cpp.
References _height, _observers, _players, _width, at(), and Tile::playerIds.
Referenced by CommandDispatcher::_handleForward(), and ejectPlayers().
Here is the call graph for this function:
Here is the caller graph for this function:| void World::playerBroadcast | ( | int | playerId, |
| const std::string & | message | ||
| ) |
Fire onBroadcast to observers (GUI animation, logging). No state change.
Definition at line 149 of file World.cpp.
References _observers.
Referenced by CommandDispatcher::_handleBroadcast().
Here is the caller graph for this function:| std::optional< Egg > World::popEggForTeam | ( | const std::string & | teamName | ) |
Definition at line 244 of file World.cpp.
References _eggs, _observers, at(), Tile::eggIds, Egg::id, Egg::x, and Egg::y.
Referenced by HandshakeHandler::_promoteToAi().
Here is the call graph for this function:
Here is the caller graph for this function:| void World::removePlayer | ( | int | id | ) |
Definition at line 78 of file World.cpp.
References _observers, _players, at(), and Tile::playerIds.
Referenced by CommandDispatcher::onDisconnect().
Here is the call graph for this function:
Here is the caller graph for this function:| bool World::setResource | ( | int | playerId, |
| ResourceType | type | ||
| ) |
Definition at line 123 of file World.cpp.
References _observers, _players, at(), and Tile::resources.
Referenced by CommandDispatcher::_handleSet().
Here is the call graph for this function:
Here is the caller graph for this function:| void World::spawnInitialEggs | ( | int | countPerTeam | ) |
Spawn countPerTeam eggs for every team at random tiles (server startup).
Definition at line 208 of file World.cpp.
References _height, _rng, _spawnEgg(), _teamNames, and _width.
Referenced by Server::Server().
Here is the call graph for this function:
Here is the caller graph for this function:| std::vector< std::pair< int, int > > World::spawnResources | ( | ) |
Definition at line 28 of file World.cpp.
References _height, _observers, _rng, _tiles, _width, at(), Resources::density(), Tile::resources, and Resources::TYPE_COUNT.
Referenced by Server::Server().
Here is the call graph for this function:
Here is the caller graph for this function:| std::optional< std::vector< int > > World::startIncantation | ( | int | playerId | ) |
Validate and start an incantation for playerId. Returns the list of participant IDs on success, nullopt if prerequisites fail. Prerequisites are checked again in finalizeIncantation() after the ritual delay.
Definition at line 304 of file World.cpp.
References _checkReqs(), _observers, _players, at(), and INCANTATION_REQS.
Referenced by CommandDispatcher::_handleIncantation().
Here is the call graph for this function:
Here is the caller graph for this function:| bool World::takeResource | ( | int | playerId, |
| ResourceType | type | ||
| ) |
Definition at line 110 of file World.cpp.
References _observers, _players, and at().
Referenced by CommandDispatcher::_handleTake().
Here is the call graph for this function:
Here is the caller graph for this function:| int World::teamEggCount | ( | const std::string & | team | ) | const |
Definition at line 222 of file World.cpp.
References _eggs.
Referenced by CommandDispatcher::_handleConnectNbr(), HandshakeHandler::_promoteToAi(), and HandshakeHandler::onLine().
Here is the caller graph for this function:| int World::teamPlayerCount | ( | const std::string & | team | ) | const |
| void World::turnPlayer | ( | int | id, |
| Orientation | orientation | ||
| ) |
Definition at line 103 of file World.cpp.
References _observers, and _players.
Referenced by CommandDispatcher::_handleLeft(), and CommandDispatcher::_handleRight().
Here is the caller graph for this function:| int World::width | ( | ) | const |
Definition at line 400 of file World.cpp.
References _width.
Referenced by CommandDispatcher::_handleBroadcast(), CommandDispatcher::_handleEject(), CommandDispatcher::_handleMct(), CommandDispatcher::_handleMsz(), and World().
Here is the caller graph for this function:| const std::optional< std::string > & World::winner | ( | ) | const |
Winning team name, set when isGameEnded() becomes true.
Definition at line 418 of file World.cpp.
References _winner.
Referenced by Server::_handleGameOver(), and finalizeIncantation().
Here is the caller graph for this function:
|
private |
Definition at line 126 of file World.hpp.
Referenced by _spawnEgg(), ejectPlayers(), getEggs(), hatchEgg(), popEggForTeam(), and teamEggCount().
|
private |
Definition at line 131 of file World.hpp.
Referenced by finalizeIncantation(), and isGameEnded().
|
private |
Definition at line 123 of file World.hpp.
Referenced by at(), at(), height(), movePlayer(), spawnInitialEggs(), and spawnResources().
|
private |
Definition at line 130 of file World.hpp.
Referenced by _spawnEgg().
|
private |
Definition at line 129 of file World.hpp.
Referenced by addPlayer().
|
private |
Definition at line 133 of file World.hpp.
Referenced by _spawnEgg(), addPlayer(), addWorldObserver(), consumeFood(), ejectPlayers(), finalizeIncantation(), hatchEgg(), movePlayer(), playerBroadcast(), popEggForTeam(), removePlayer(), setResource(), spawnResources(), startIncantation(), takeResource(), and turnPlayer().
|
private |
Definition at line 125 of file World.hpp.
Referenced by addEgg(), addPlayer(), checkWin(), consumeFood(), ejectPlayers(), finalizeIncantation(), getPlayer(), getPlayers(), movePlayer(), removePlayer(), setResource(), startIncantation(), takeResource(), teamPlayerCount(), and turnPlayer().
|
private |
Definition at line 128 of file World.hpp.
Referenced by spawnInitialEggs(), and spawnResources().
|
private |
Definition at line 127 of file World.hpp.
Referenced by spawnInitialEggs().
|
private |
|
private |
Definition at line 122 of file World.hpp.
Referenced by at(), at(), movePlayer(), spawnInitialEggs(), spawnResources(), and width().
|
private |
Definition at line 132 of file World.hpp.
Referenced by finalizeIncantation(), and winner().