Zappy Server
C++ game server: world, rules, scheduler, network
Loading...
Searching...
No Matches
CommandDispatcher.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4#include <deque>
5#include <optional>
6#include <string>
7#include <unordered_map>
8#include <vector>
9
10#include "core/GameClock.hpp"
11#include "core/Scheduler.hpp"
12#include "core/World.hpp"
13#include "game/GuiNotifier.hpp"
16#include "protocol/AiParser.hpp"
17
26 public:
27 CommandDispatcher(ClientManager& clients, World& world, GuiNotifier& notifier,
28 const ServerConfig& config, Scheduler& scheduler);
29
31 void onNewConnection(int connectionId);
33 void dispatch(int connectionId, const std::string& line);
35 void onDisconnect(int connectionId);
37 std::vector<int> drainPendingDisconnects();
38
40 std::chrono::microseconds gameElapsed() const;
42 double gameTicks() const;
44 std::optional<GameClock::Stamp> teamJoin(const std::string& team) const;
45
46 private:
47 void _dispatchAi(int connectionId, const std::string& line);
48 void _dispatchGui(int connectionId, const std::string& line);
50 void _executeNext(int connectionId);
51
53 void _onAiJoined(int connectionId, int playerId);
54 void _startStarvationTimer(int connectionId, int playerId);
55
57 static constexpr int STARVATION_INTERVAL_MS = 126000;
58
59 // GUI command handlers
60 void _handleMsz(int connectionId);
61 void _handleBct(int connectionId, int x, int y);
62 void _handleMct(int connectionId);
63 void _handleTna(int connectionId);
64 void _handlePpo(int connectionId, int playerId);
65 void _handlePlv(int connectionId, int playerId);
66 void _handlePin(int connectionId, int playerId);
67 void _handleSgt(int connectionId);
68 void _handleSst(int freq);
69 void _handleStu(int connectionId);
70
71 // AI command handlers
72 void _handleForward(int connectionId);
73 void _handleRight(int connectionId);
74 void _handleLeft(int connectionId);
75 void _handleLook(int connectionId);
76 void _handleInventory(int connectionId);
77 void _handleBroadcast(int connectionId, const std::string& msg);
78 void _handleFork(int connectionId);
79 void _handleEject(int connectionId);
80 void _handleTake(int connectionId, ResourceType resource);
81 void _handleSet(int connectionId, ResourceType resource);
82 void _handleIncantation(int connectionId);
83 void _handleConnectNbr(int connectionId);
84
92
93 std::unordered_map<int, std::deque<Ai::Command>> _queues;
94 std::unordered_map<int, bool> _hasActive;
95 std::vector<int> _pendingDisconnects;
96};
ResourceType
Enumerate the different types of resources in the game.
Definition Resources.hpp:12
Owns every client socket and runs the poll() loop.
Sends each client line to the right handler.
void _handleMct(int connectionId)
void _handleFork(int connectionId)
std::chrono::microseconds gameElapsed() const
Wall-clock time since server start (single source for stu and the win banner).
void _handleSst(int freq)
void _handlePlv(int connectionId, int playerId)
void _dispatchGui(int connectionId, const std::string &line)
void _startStarvationTimer(int connectionId, int playerId)
HandshakeHandler _handshakeHandler
static constexpr int STARVATION_INTERVAL_MS
one food is consumed every 126 time units (1 unit = 1/freq seconds)
double gameTicks() const
Total game ticks elapsed (freq integrated over time). For the win banner.
void onNewConnection(int connectionId)
New socket connected: start its handshake.
void _onAiJoined(int connectionId, int playerId)
Fired on AI promotion: stamp team join, then start its food timer.
void _handleIncantation(int connectionId)
const ServerConfig & _config
void _handleTake(int connectionId, ResourceType resource)
void _handlePin(int connectionId, int playerId)
void _handleLeft(int connectionId)
void _handleLook(int connectionId)
void _handleStu(int connectionId)
void _handleSgt(int connectionId)
void _handleMsz(int connectionId)
void onDisconnect(int connectionId)
Socket dropped: clean up its queue and player.
void _executeNext(int connectionId)
Run the next queued AI command (or go idle if none left).
void dispatch(int connectionId, const std::string &line)
Route one line from a client.
ClientManager & _clients
void _handleBct(int connectionId, int x, int y)
std::unordered_map< int, bool > _hasActive
std::optional< GameClock::Stamp > teamJoin(const std::string &team) const
When team's first player joined, or nullopt if it never did.
void _handleForward(int connectionId)
void _dispatchAi(int connectionId, const std::string &line)
void _handleConnectNbr(int connectionId)
void _handleInventory(int connectionId)
std::vector< int > _pendingDisconnects
std::vector< int > drainPendingDisconnects()
Take the list of ids the server should disconnect this cycle.
std::unordered_map< int, std::deque< Ai::Command > > _queues
void _handleTna(int connectionId)
void _handleBroadcast(int connectionId, const std::string &msg)
void _handleRight(int connectionId)
void _handlePpo(int connectionId, int playerId)
void _handleSet(int connectionId, ResourceType resource)
void _handleEject(int connectionId)
Single owner of game frequency and elapsed game time.
Definition GameClock.hpp:22
Handles the first exchange with a new socket.
Priority queue of timed callbacks. Drives all game timing.
Definition Scheduler.hpp:23
Pure game state - no networking, no scheduling.
Definition World.hpp:47
Represents the configuration for the server application, described by:
Definition Args.hpp:17