Zappy Server
C++ game server: world, rules, scheduler, network
Loading...
Searching...
No Matches
GuiHandlers.cpp
Go to the documentation of this file.
1#include <chrono>
2#include <cmath>
3#include <stdexcept>
4
7
8void CommandDispatcher::_handleMsz(int connectionId)
9{
11}
12
13void CommandDispatcher::_handleBct(int connectionId, int x, int y)
14{
15 _clients.send(connectionId, Serializer::bct(x, y, _world.at(x, y).resources));
16}
17
18void CommandDispatcher::_handleMct(int connectionId)
19{
20 for (int y = 0; y < _world.height(); ++y)
21 for (int x = 0; x < _world.width(); ++x)
22 _clients.send(connectionId, Serializer::bct(x, y, _world.at(x, y).resources));
23}
24
25void CommandDispatcher::_handleTna(int connectionId)
26{
27 for (const auto& team : _config.teamNames) _clients.send(connectionId, Serializer::tna(team));
28}
29
30void CommandDispatcher::_handlePpo(int connectionId, int playerId)
31{
32 try {
33 auto& p = _world.getPlayer(playerId);
34 _clients.send(connectionId, Serializer::ppo(p.id, p.x, p.y, p.orientation));
35 } catch (const std::out_of_range&) {
36 _clients.send(connectionId, "suc\n");
37 }
38}
39
40void CommandDispatcher::_handlePlv(int connectionId, int playerId)
41{
42 try {
43 auto& p = _world.getPlayer(playerId);
44 _clients.send(connectionId, Serializer::plv(p.id, p.level));
45 } catch (const std::out_of_range&) {
46 _clients.send(connectionId, "suc\n");
47 }
48}
49
50void CommandDispatcher::_handlePin(int connectionId, int playerId)
51{
52 try {
53 auto& p = _world.getPlayer(playerId);
54 _clients.send(connectionId, Serializer::pin(p.id, p.x, p.y, p.inventory));
55 } catch (const std::out_of_range&) {
56 _clients.send(connectionId, "suc\n");
57 }
58}
59
60void CommandDispatcher::_handleSgt(int connectionId)
61{
62 _clients.send(connectionId, Serializer::sgt(_clock.freq()));
63}
64
65void CommandDispatcher::_handleStu(int connectionId)
66{
67 // GUI displays whole seconds: truncate the elapsed time to an integer second.
68 // Ticks come alongside so the GUI can also show freq-accurate game time.
69 int seconds =
70 static_cast<int>(std::chrono::duration_cast<std::chrono::seconds>(gameElapsed()).count());
71 long long ticks = std::llround(_clock.ticks());
72 _clients.send(connectionId, Serializer::stu(seconds, ticks));
73}
74
76{
77 float ratio = _clock.setFreq(freq);
78 _scheduler.rescale(ratio);
80}
void send(int connectionId, const std::string &msg)
Queue msg to be sent to a client (flushed during poll()).
void _handleMct(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)
const ServerConfig & _config
void _handlePin(int connectionId, int playerId)
void _handleStu(int connectionId)
void _handleSgt(int connectionId)
void _handleMsz(int connectionId)
ClientManager & _clients
void _handleBct(int connectionId, int x, int y)
void _handleTna(int connectionId)
void _handlePpo(int connectionId, int playerId)
double ticks() const
Total ticks elapsed: integral of freq over time. Fractional by nature.
Definition GameClock.cpp:20
float setFreq(int newFreq)
Change frequency, banking ticks accrued at the old rate first.
Definition GameClock.cpp:22
int freq() const
Definition GameClock.hpp:32
void broadcast(const std::string &msg)
void rescale(float ratio)
Rescale all pending fire times by ratio.
Definition Scheduler.cpp:32
Tile & at(int x, int y)
Access a tile by position. Map is toroidal, coordinates wrap.
Definition World.cpp:12
int height() const
Definition World.cpp:401
Player & getPlayer(int id)
Definition World.cpp:147
int width() const
Definition World.cpp:400
std::string pin(int id, int x, int y, const Resources &r)
Player inventory. Wire: pin #N X Y q q q q q q q\n
std::string msz(int x, int y)
Map size. Wire: msz X Y\n
std::string sst(int freq)
Reply to "sst": frequency was changed to F. Wire: sst F\n
std::string tna(const std::string &teamName)
Team name (one per team). Wire: tna NAME\n
std::string sgt(int freq)
Reply to "sgt": current time unit frequency. Wire: sgt F\n
std::string bct(int x, int y, const Resources &r)
Tile content (resources on it). Wire: bct X Y q q q q q q q\n
std::string stu(int seconds, long long ticks)
std::string ppo(int id, int x, int y, Orientation o)
Player position. Wire: ppo #N X Y O\n
std::string plv(int id, int level)
Player level. Wire: plv #N L\n
std::vector< std::string > teamNames
Definition Args.hpp:21
Resources resources
Definition Tile.hpp:11