Zappy Server
C++ game server: world, rules, scheduler, network
Loading...
Searching...
No Matches
Server Class Reference

Owns every subsystem and runs the main game loop. More...

#include <Server.hpp>

+ Collaboration diagram for Server:

Public Member Functions

 Server (const ServerConfig &config)
 
void run ()
 Run the game loop until the process is stopped.
 

Private Member Functions

void _scheduleRespawn ()
 (Re)schedule the periodic world resource respawn.
 
void _logStartup () const
 
void _handleGameOver ()
 Freeze the world and log the winner banner. Runs once when a team wins.
 

Private Attributes

ServerConfig _config
 
Listener _listener
 
ClientManager _clients
 
World _world
 
GuiNotifier _notifier
 
Logger _logger
 
LogObserver _logObserver
 
Scheduler _scheduler
 
CommandDispatcher _dispatcher
 
bool _gameOverHandled = false
 

Static Private Attributes

static constexpr int RESPAWN_INTERVAL_MS = 20000
 Resources respawn every 20 time units (subject spec).
 

Detailed Description

Owns every subsystem and runs the main game loop.

run() loops forever: poll the sockets, pass new connections / lines / disconnects to the CommandDispatcher, then let the Scheduler fire any timers that are due (respawn, starvation, incantations...). Single thread.

Definition at line 20 of file Server.hpp.

Constructor & Destructor Documentation

◆ Server()

Server::Server ( const ServerConfig config)

Definition at line 23 of file Server.cpp.

References _clients, _config, _logger, _logObserver, _notifier, _world, ClientManager::addNetworkObserver(), World::addWorldObserver(), ServerConfig::clientsNb, FileSink::forRun(), Info, ServerConfig::port, Logger::setSink(), World::spawnInitialEggs(), and World::spawnResources().

+ Here is the call graph for this function:

Member Function Documentation

◆ _handleGameOver()

void Server::_handleGameOver ( )
private

Freeze the world and log the winner banner. Runs once when a team wins.

Definition at line 63 of file Server.cpp.

References _clients, _config, _dispatcher, _gameOverHandled, _logger, _notifier, _scheduler, _world, GuiNotifier::broadcast(), Scheduler::clear(), ClientManager::disconnect(), CommandDispatcher::gameElapsed(), CommandDispatcher::gameTicks(), World::getPlayers(), Serializer::gwt(), Logger::info(), CommandDispatcher::teamJoin(), ServerConfig::teamNames, and World::winner().

Referenced by run().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _logStartup()

void Server::_logStartup ( ) const
private

Definition at line 53 of file Server.cpp.

References _config, ServerConfig::height, ServerConfig::port, ServerConfig::seed, ServerConfig::teamNames, and ServerConfig::width.

Referenced by run().

+ Here is the caller graph for this function:

◆ _scheduleRespawn()

void Server::_scheduleRespawn ( )
private

(Re)schedule the periodic world resource respawn.

Definition at line 45 of file Server.cpp.

References _config, _scheduler, ServerConfig::freq, RESPAWN_INTERVAL_MS, and Scheduler::schedule().

Referenced by run().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ run()

void Server::run ( )

Run the game loop until the process is stopped.

Definition at line 98 of file Server.cpp.

References _clients, _dispatcher, _gameOverHandled, _handleGameOver(), _logger, _logStartup(), _scheduler, _scheduleRespawn(), _world, ClientManager::disconnect(), PollResult::disconnectedIds, CommandDispatcher::dispatch(), CommandDispatcher::drainPendingDisconnects(), Logger::info(), World::isGameEnded(), PollResult::lines, Scheduler::msUntilNext(), PollResult::newConnections, CommandDispatcher::onDisconnect(), CommandDispatcher::onNewConnection(), ClientManager::poll(), and Scheduler::tick().

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ _clients

ClientManager Server::_clients
private

Definition at line 39 of file Server.hpp.

Referenced by _handleGameOver(), run(), and Server().

◆ _config

ServerConfig Server::_config
private

Definition at line 37 of file Server.hpp.

Referenced by _handleGameOver(), _logStartup(), _scheduleRespawn(), and Server().

◆ _dispatcher

CommandDispatcher Server::_dispatcher
private

Definition at line 45 of file Server.hpp.

Referenced by _handleGameOver(), and run().

◆ _gameOverHandled

bool Server::_gameOverHandled = false
private

Definition at line 46 of file Server.hpp.

Referenced by _handleGameOver(), and run().

◆ _listener

Listener Server::_listener
private

Definition at line 38 of file Server.hpp.

◆ _logger

Logger Server::_logger
private

Definition at line 42 of file Server.hpp.

Referenced by _handleGameOver(), run(), and Server().

◆ _logObserver

LogObserver Server::_logObserver
private

Definition at line 43 of file Server.hpp.

Referenced by Server().

◆ _notifier

GuiNotifier Server::_notifier
private

Definition at line 41 of file Server.hpp.

Referenced by _handleGameOver(), and Server().

◆ _scheduler

Scheduler Server::_scheduler
private

Definition at line 44 of file Server.hpp.

Referenced by _handleGameOver(), _scheduleRespawn(), and run().

◆ _world

World Server::_world
private

Definition at line 40 of file Server.hpp.

Referenced by _handleGameOver(), run(), and Server().

◆ RESPAWN_INTERVAL_MS

constexpr int Server::RESPAWN_INTERVAL_MS = 20000
staticconstexprprivate

Resources respawn every 20 time units (subject spec).

Definition at line 35 of file Server.hpp.

Referenced by _scheduleRespawn().


The documentation for this class was generated from the following files: