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

Sends each client line to the right handler. More...

#include <CommandDispatcher.hpp>

+ Collaboration diagram for CommandDispatcher:

Public Member Functions

 CommandDispatcher (ClientManager &clients, World &world, GuiNotifier &notifier, const ServerConfig &config, Scheduler &scheduler)
 
void onNewConnection (int connectionId)
 New socket connected: start its handshake.
 
void dispatch (int connectionId, const std::string &line)
 Route one line from a client.
 
void onDisconnect (int connectionId)
 Socket dropped: clean up its queue and player.
 
std::vector< int > drainPendingDisconnects ()
 Take the list of ids the server should disconnect this cycle.
 
std::chrono::microseconds gameElapsed () const
 Wall-clock time since server start (single source for stu and the win banner).
 
double gameTicks () const
 Total game ticks elapsed (freq integrated over time). For the win banner.
 
std::optional< GameClock::StampteamJoin (const std::string &team) const
 When team's first player joined, or nullopt if it never did.
 

Private Member Functions

void _dispatchAi (int connectionId, const std::string &line)
 
void _dispatchGui (int connectionId, const std::string &line)
 
void _executeNext (int connectionId)
 Run the next queued AI command (or go idle if none left).
 
void _onAiJoined (int connectionId, int playerId)
 Fired on AI promotion: stamp team join, then start its food timer.
 
void _startStarvationTimer (int connectionId, int playerId)
 
void _handleMsz (int connectionId)
 
void _handleBct (int connectionId, int x, int y)
 
void _handleMct (int connectionId)
 
void _handleTna (int connectionId)
 
void _handlePpo (int connectionId, int playerId)
 
void _handlePlv (int connectionId, int playerId)
 
void _handlePin (int connectionId, int playerId)
 
void _handleSgt (int connectionId)
 
void _handleSst (int freq)
 
void _handleStu (int connectionId)
 
void _handleForward (int connectionId)
 
void _handleRight (int connectionId)
 
void _handleLeft (int connectionId)
 
void _handleLook (int connectionId)
 
void _handleInventory (int connectionId)
 
void _handleBroadcast (int connectionId, const std::string &msg)
 
void _handleFork (int connectionId)
 
void _handleEject (int connectionId)
 
void _handleTake (int connectionId, ResourceType resource)
 
void _handleSet (int connectionId, ResourceType resource)
 
void _handleIncantation (int connectionId)
 
void _handleConnectNbr (int connectionId)
 

Private Attributes

ClientManager_clients
 
World_world
 
GuiNotifier_notifier
 
Scheduler_scheduler
 
const ServerConfig_config
 
GameClock _clock
 
HandshakeHandler _handshakeHandler
 
std::unordered_map< int, std::deque< Ai::Command > > _queues
 
std::unordered_map< int, bool > _hasActive
 
std::vector< int > _pendingDisconnects
 

Static Private Attributes

static constexpr int STARVATION_INTERVAL_MS = 126000
 one food is consumed every 126 time units (1 unit = 1/freq seconds)
 

Detailed Description

Sends each client line to the right handler.

GUI lines are answered right away. AI lines get queued (max 10) and run one at a time, each taking some time units before the next starts. Also handles the handshake for new sockets and the food/starvation timer per player.

Definition at line 25 of file CommandDispatcher.hpp.

Constructor & Destructor Documentation

◆ CommandDispatcher()

CommandDispatcher::CommandDispatcher ( ClientManager clients,
World world,
GuiNotifier notifier,
const ServerConfig config,
Scheduler scheduler 
)

Definition at line 10 of file CommandDispatcher.cpp.

References _onAiJoined().

+ Here is the call graph for this function:

Member Function Documentation

◆ _dispatchAi()

void CommandDispatcher::_dispatchAi ( int  connectionId,
const std::string &  line 
)
private

Definition at line 94 of file CommandDispatcher.cpp.

References _clients, _executeNext(), _hasActive, _queues, _world, World::isGameEnded(), AiParser::parse(), and ClientManager::send().

Referenced by dispatch().

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

◆ _dispatchGui()

void CommandDispatcher::_dispatchGui ( int  connectionId,
const std::string &  line 
)
private

Definition at line 70 of file CommandDispatcher.cpp.

References _clients, _handleBct(), _handleMct(), _handleMsz(), _handlePin(), _handlePlv(), _handlePpo(), _handleSgt(), _handleSst(), _handleStu(), _handleTna(), GuiParser::parse(), and ClientManager::send().

Referenced by dispatch().

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

◆ _executeNext()

void CommandDispatcher::_executeNext ( int  connectionId)
private

Run the next queued AI command (or go idle if none left).

Definition at line 111 of file CommandDispatcher.cpp.

References _handleBroadcast(), _handleConnectNbr(), _handleEject(), _handleFork(), _handleForward(), _handleIncantation(), _handleInventory(), _handleLeft(), _handleLook(), _handleRight(), _handleSet(), _handleTake(), _hasActive, and _queues.

Referenced by _dispatchAi(), _handleBroadcast(), _handleConnectNbr(), _handleEject(), _handleFork(), _handleForward(), _handleIncantation(), _handleInventory(), _handleLeft(), _handleLook(), _handleRight(), _handleSet(), and _handleTake().

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

◆ _handleBct()

void CommandDispatcher::_handleBct ( int  connectionId,
int  x,
int  y 
)
private

Definition at line 13 of file GuiHandlers.cpp.

References _clients, _world, World::at(), Serializer::bct(), Tile::resources, and ClientManager::send().

Referenced by _dispatchGui().

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

◆ _handleBroadcast()

void CommandDispatcher::_handleBroadcast ( int  connectionId,
const std::string &  msg 
)
private

Definition at line 183 of file AiHandlers.cpp.

References _clients, _clock, _executeNext(), _scheduler, _world, broadcastDirection(), GameClock::freq(), ClientManager::getConnection(), World::getPlayer(), World::getPlayers(), World::height(), World::playerBroadcast(), Connection::playerId(), Scheduler::schedule(), ClientManager::send(), and World::width().

Referenced by _executeNext().

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

◆ _handleConnectNbr()

void CommandDispatcher::_handleConnectNbr ( int  connectionId)
private

Definition at line 140 of file CommandDispatcher.cpp.

References _clients, _executeNext(), _world, ClientManager::getConnection(), World::getPlayer(), World::getPlayers(), Connection::playerId(), ClientManager::send(), and World::teamEggCount().

Referenced by _executeNext().

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

◆ _handleEject()

void CommandDispatcher::_handleEject ( int  connectionId)
private

Definition at line 224 of file AiHandlers.cpp.

References _clients, _clock, _executeNext(), _scheduler, _world, broadcastDirection(), World::ejectPlayers(), GameClock::freq(), ClientManager::getConnection(), World::getPlayer(), World::height(), Connection::playerId(), Scheduler::schedule(), ClientManager::send(), and World::width().

Referenced by _executeNext().

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

◆ _handleFork()

void CommandDispatcher::_handleFork ( int  connectionId)
private

Definition at line 207 of file AiHandlers.cpp.

References _clients, _clock, _executeNext(), _scheduler, _world, World::addEgg(), GameClock::freq(), ClientManager::getConnection(), Connection::playerId(), Scheduler::schedule(), and ClientManager::send().

Referenced by _executeNext().

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

◆ _handleForward()

void CommandDispatcher::_handleForward ( int  connectionId)
private

Definition at line 51 of file AiHandlers.cpp.

References _clients, _clock, _executeNext(), _scheduler, _world, GameClock::freq(), ClientManager::getConnection(), World::getPlayer(), World::movePlayer(), Connection::playerId(), Scheduler::schedule(), and ClientManager::send().

Referenced by _executeNext().

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

◆ _handleIncantation()

void CommandDispatcher::_handleIncantation ( int  connectionId)
private

Definition at line 295 of file AiHandlers.cpp.

References _clients, _clock, _executeNext(), _scheduler, _world, Player::connectionId, World::finalizeIncantation(), GameClock::freq(), ClientManager::getConnection(), World::getPlayer(), World::getPlayers(), Connection::playerId(), Scheduler::schedule(), ClientManager::send(), World::startIncantation(), and Player::x.

Referenced by _executeNext().

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

◆ _handleInventory()

void CommandDispatcher::_handleInventory ( int  connectionId)
private

Definition at line 157 of file AiHandlers.cpp.

References _clients, _clock, _executeNext(), _scheduler, _world, GameClock::freq(), ClientManager::getConnection(), World::getPlayer(), Connection::playerId(), Scheduler::schedule(), and ClientManager::send().

Referenced by _executeNext().

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

◆ _handleLeft()

void CommandDispatcher::_handleLeft ( int  connectionId)
private

Definition at line 105 of file AiHandlers.cpp.

References _clients, _clock, _executeNext(), _scheduler, _world, GameClock::freq(), ClientManager::getConnection(), World::getPlayer(), Connection::playerId(), Scheduler::schedule(), ClientManager::send(), turnLeft(), and World::turnPlayer().

Referenced by _executeNext().

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

◆ _handleLook()

void CommandDispatcher::_handleLook ( int  connectionId)
private

Definition at line 123 of file AiHandlers.cpp.

References _clients, _clock, _executeNext(), _lookForwardRight(), _scheduler, _tileContent(), _world, World::at(), GameClock::freq(), ClientManager::getConnection(), World::getPlayer(), Connection::playerId(), Scheduler::schedule(), and ClientManager::send().

Referenced by _executeNext().

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

◆ _handleMct()

void CommandDispatcher::_handleMct ( int  connectionId)
private

Definition at line 18 of file GuiHandlers.cpp.

References _clients, _world, World::at(), Serializer::bct(), World::height(), Tile::resources, ClientManager::send(), and World::width().

Referenced by _dispatchGui().

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

◆ _handleMsz()

void CommandDispatcher::_handleMsz ( int  connectionId)
private

Definition at line 8 of file GuiHandlers.cpp.

References _clients, _world, World::height(), Serializer::msz(), ClientManager::send(), and World::width().

Referenced by _dispatchGui().

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

◆ _handlePin()

void CommandDispatcher::_handlePin ( int  connectionId,
int  playerId 
)
private

Definition at line 50 of file GuiHandlers.cpp.

References _clients, _world, World::getPlayer(), Serializer::pin(), and ClientManager::send().

Referenced by _dispatchGui().

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

◆ _handlePlv()

void CommandDispatcher::_handlePlv ( int  connectionId,
int  playerId 
)
private

Definition at line 40 of file GuiHandlers.cpp.

References _clients, _world, World::getPlayer(), Serializer::plv(), and ClientManager::send().

Referenced by _dispatchGui().

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

◆ _handlePpo()

void CommandDispatcher::_handlePpo ( int  connectionId,
int  playerId 
)
private

Definition at line 30 of file GuiHandlers.cpp.

References _clients, _world, World::getPlayer(), Serializer::ppo(), and ClientManager::send().

Referenced by _dispatchGui().

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

◆ _handleRight()

void CommandDispatcher::_handleRight ( int  connectionId)
private

Definition at line 86 of file AiHandlers.cpp.

References _clients, _clock, _executeNext(), _scheduler, _world, GameClock::freq(), ClientManager::getConnection(), World::getPlayer(), Connection::playerId(), Scheduler::schedule(), ClientManager::send(), World::turnPlayer(), and turnRight().

Referenced by _executeNext().

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

◆ _handleSet()

void CommandDispatcher::_handleSet ( int  connectionId,
ResourceType  resource 
)
private

Definition at line 275 of file AiHandlers.cpp.

References _clients, _clock, _executeNext(), _scheduler, _world, GameClock::freq(), ClientManager::getConnection(), Connection::playerId(), Scheduler::schedule(), ClientManager::send(), and World::setResource().

Referenced by _executeNext().

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

◆ _handleSgt()

void CommandDispatcher::_handleSgt ( int  connectionId)
private

Definition at line 60 of file GuiHandlers.cpp.

References _clients, _clock, GameClock::freq(), ClientManager::send(), and Serializer::sgt().

Referenced by _dispatchGui().

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

◆ _handleSst()

void CommandDispatcher::_handleSst ( int  freq)
private

Definition at line 75 of file GuiHandlers.cpp.

References _clock, _notifier, _scheduler, GuiNotifier::broadcast(), GameClock::freq(), Scheduler::rescale(), GameClock::setFreq(), and Serializer::sst().

Referenced by _dispatchGui().

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

◆ _handleStu()

void CommandDispatcher::_handleStu ( int  connectionId)
private

Definition at line 65 of file GuiHandlers.cpp.

References _clients, _clock, gameElapsed(), ClientManager::send(), Serializer::stu(), and GameClock::ticks().

Referenced by _dispatchGui().

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

◆ _handleTake()

void CommandDispatcher::_handleTake ( int  connectionId,
ResourceType  resource 
)
private

Definition at line 256 of file AiHandlers.cpp.

References _clients, _clock, _executeNext(), _scheduler, _world, GameClock::freq(), ClientManager::getConnection(), Connection::playerId(), Scheduler::schedule(), ClientManager::send(), and World::takeResource().

Referenced by _executeNext().

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

◆ _handleTna()

void CommandDispatcher::_handleTna ( int  connectionId)
private

Definition at line 25 of file GuiHandlers.cpp.

References _clients, _config, ClientManager::send(), ServerConfig::teamNames, and Serializer::tna().

Referenced by _dispatchGui().

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

◆ _onAiJoined()

void CommandDispatcher::_onAiJoined ( int  connectionId,
int  playerId 
)
private

Fired on AI promotion: stamp team join, then start its food timer.

Definition at line 33 of file CommandDispatcher.cpp.

References _clock, _startStarvationTimer(), _world, World::getPlayer(), World::getPlayers(), GameClock::recordJoin(), and Player::teamName.

Referenced by CommandDispatcher().

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

◆ _startStarvationTimer()

void CommandDispatcher::_startStarvationTimer ( int  connectionId,
int  playerId 
)
private

Definition at line 151 of file CommandDispatcher.cpp.

References _clock, _scheduler, GameClock::freq(), Scheduler::schedule(), and STARVATION_INTERVAL_MS.

Referenced by _onAiJoined().

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

◆ dispatch()

void CommandDispatcher::dispatch ( int  connectionId,
const std::string &  line 
)

Route one line from a client.

Definition at line 44 of file CommandDispatcher.cpp.

References _clients, _dispatchAi(), _dispatchGui(), _handshakeHandler, AI, ClientManager::getConnection(), GUI, HandshakeHandler::onLine(), and PENDING.

Referenced by Server::run().

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

◆ drainPendingDisconnects()

std::vector< int > CommandDispatcher::drainPendingDisconnects ( )

Take the list of ids the server should disconnect this cycle.

Definition at line 166 of file CommandDispatcher.cpp.

References _pendingDisconnects.

Referenced by Server::run().

+ Here is the caller graph for this function:

◆ gameElapsed()

std::chrono::microseconds CommandDispatcher::gameElapsed ( ) const

Wall-clock time since server start (single source for stu and the win banner).

Definition at line 24 of file CommandDispatcher.cpp.

References _clock, and GameClock::elapsed().

Referenced by Server::_handleGameOver(), and _handleStu().

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

◆ gameTicks()

double CommandDispatcher::gameTicks ( ) const

Total game ticks elapsed (freq integrated over time). For the win banner.

Definition at line 26 of file CommandDispatcher.cpp.

References _clock, and GameClock::ticks().

Referenced by Server::_handleGameOver().

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

◆ onDisconnect()

void CommandDispatcher::onDisconnect ( int  connectionId)

Socket dropped: clean up its queue and player.

Definition at line 55 of file CommandDispatcher.cpp.

References _clients, _hasActive, _notifier, _queues, _world, AI, ClientManager::getConnection(), World::getPlayers(), GUI, GuiNotifier::removeGui(), and World::removePlayer().

Referenced by Server::run().

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

◆ onNewConnection()

void CommandDispatcher::onNewConnection ( int  connectionId)

New socket connected: start its handshake.

Definition at line 39 of file CommandDispatcher.cpp.

References _handshakeHandler, and HandshakeHandler::onNewConnection().

Referenced by Server::run().

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

◆ teamJoin()

std::optional< GameClock::Stamp > CommandDispatcher::teamJoin ( const std::string &  team) const

When team's first player joined, or nullopt if it never did.

Definition at line 28 of file CommandDispatcher.cpp.

References _clock, and GameClock::joinOf().

Referenced by Server::_handleGameOver().

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

Member Data Documentation

◆ _clients

◆ _clock

◆ _config

const ServerConfig& CommandDispatcher::_config
private

Definition at line 89 of file CommandDispatcher.hpp.

Referenced by _handleTna().

◆ _handshakeHandler

HandshakeHandler CommandDispatcher::_handshakeHandler
private

Definition at line 91 of file CommandDispatcher.hpp.

Referenced by dispatch(), and onNewConnection().

◆ _hasActive

std::unordered_map<int, bool> CommandDispatcher::_hasActive
private

Definition at line 94 of file CommandDispatcher.hpp.

Referenced by _dispatchAi(), _executeNext(), and onDisconnect().

◆ _notifier

GuiNotifier& CommandDispatcher::_notifier
private

Definition at line 87 of file CommandDispatcher.hpp.

Referenced by _handleSst(), and onDisconnect().

◆ _pendingDisconnects

std::vector<int> CommandDispatcher::_pendingDisconnects
private

Definition at line 95 of file CommandDispatcher.hpp.

Referenced by drainPendingDisconnects().

◆ _queues

std::unordered_map<int, std::deque<Ai::Command> > CommandDispatcher::_queues
private

Definition at line 93 of file CommandDispatcher.hpp.

Referenced by _dispatchAi(), _executeNext(), and onDisconnect().

◆ _scheduler

◆ _world

◆ STARVATION_INTERVAL_MS

constexpr int CommandDispatcher::STARVATION_INTERVAL_MS = 126000
staticconstexprprivate

one food is consumed every 126 time units (1 unit = 1/freq seconds)

Definition at line 57 of file CommandDispatcher.hpp.

Referenced by _startStarvationTimer().


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