Zappy Server
C++ game server: world, rules, scheduler, network
Loading...
Searching...
No Matches
LogObserver.cpp
Go to the documentation of this file.
2
3#include <string>
4
5namespace {
6
7 constexpr const char* NET = "NET";
8 constexpr const char* WORLD = "WORLD";
9
11 std::string trim(const std::string& s)
12 {
13 if (!s.empty() && s.back() == '\n') return s.substr(0, s.size() - 1);
14 return s;
15 }
16
17 std::string resStr(const Resources& r)
18 {
19 return "food=" + std::to_string(r.food) + " lin=" + std::to_string(r.linemate) +
20 " der=" + std::to_string(r.deraumere) + " sib=" + std::to_string(r.sibur) +
21 " men=" + std::to_string(r.mendiane) + " phi=" + std::to_string(r.phiras) +
22 " thy=" + std::to_string(r.thystame);
23 }
24
25} // namespace
26
27LogObserver::LogObserver(Logger& logger) : _logger(logger) {}
28
29void LogObserver::onClientConnected(int connectionId)
30{
31 _logger.info(NET, "client connected (conn=" + std::to_string(connectionId) + ")");
32}
33
35{
36 _logger.info(NET, "client disconnected (conn=" + std::to_string(connectionId) + ")");
37}
38
39void LogObserver::onLineReceived(int connectionId, const std::string& line)
40{
41 _logger.debug(NET, "<- conn=" + std::to_string(connectionId) + " : " + trim(line));
42}
43
44void LogObserver::onLineSent(int connectionId, const std::string& line)
45{
46 _logger.debug(NET, "-> conn=" + std::to_string(connectionId) + " : " + trim(line));
47}
48
49void LogObserver::onPlayerAdded(int playerId, int x, int y, Orientation, int level,
50 const std::string& teamName)
51{
52 _logger.info(WORLD, "player #" + std::to_string(playerId) + " added team=" + teamName +
53 " lvl=" + std::to_string(level) + " at (" + std::to_string(x) + "," +
54 std::to_string(y) + ")");
55}
56
57void LogObserver::onPlayerMoved(int playerId, int nx, int ny, Orientation)
58{
59 _logger.debug(WORLD, "player #" + std::to_string(playerId) + " moved to (" +
60 std::to_string(nx) + "," + std::to_string(ny) + ")");
61}
62
63void LogObserver::onPlayerInventoryChanged(int playerId, int x, int y, Resources inventory)
64{
65 _logger.debug(WORLD, "player #" + std::to_string(playerId) + " inventory at (" +
66 std::to_string(x) + "," + std::to_string(y) + ") [" +
67 resStr(inventory) + "]");
68}
69
71{
72 _logger.info(WORLD, "player #" + std::to_string(playerId) + " removed");
73}
74
76{
77 _logger.info(WORLD, "player #" + std::to_string(playerId) + " ejected");
78}
79
80void LogObserver::onBroadcast(int playerId, const std::string& message)
81{
82 _logger.debug(WORLD, "player #" + std::to_string(playerId) + " broadcast: " + message);
83}
84
85void LogObserver::onResourceTaken(int playerId, ResourceType resourceType, int tileX, int tileY,
86 Resources resources)
87{
88 _logger.debug(WORLD, "player #" + std::to_string(playerId) + " took " +
89 Resources::get_name(resourceType) + " at (" + std::to_string(tileX) +
90 "," + std::to_string(tileY) + ") tile now [" + resStr(resources) +
91 "]");
92}
93
94void LogObserver::onResourceDropped(int playerId, ResourceType resourceType, int tileX, int tileY,
95 Resources resources)
96{
97 _logger.debug(WORLD, "player #" + std::to_string(playerId) + " dropped " +
98 Resources::get_name(resourceType) + " at (" + std::to_string(tileX) +
99 "," + std::to_string(tileY) + ") tile now [" + resStr(resources) +
100 "]");
101}
102
103void LogObserver::onEggLaid(int eggId, int playerId, int x, int y)
104{
105 _logger.info(WORLD, "egg #" + std::to_string(eggId) + " laid by #" + std::to_string(playerId) +
106 " at (" + std::to_string(x) + "," + std::to_string(y) + ")");
107}
108
109void LogObserver::onInitialEggSpawned(int eggId, const std::string& teamName, int x, int y)
110{
111 _logger.info(WORLD, "initial egg #" + std::to_string(eggId) + " for team " + teamName +
112 " at (" + std::to_string(x) + "," + std::to_string(y) + ")");
113}
114
116{
117 _logger.info(WORLD, "egg #" + std::to_string(eggId) + " hatched");
118}
119
121{
122 _logger.info(WORLD, "egg #" + std::to_string(eggId) + " died");
123}
124
125void LogObserver::onIncantationStart(int x, int y, int level,
126 const std::vector<int>& participantIds)
127{
128 _logger.info(WORLD, "incantation start lvl=" + std::to_string(level) + " at (" +
129 std::to_string(x) + "," + std::to_string(y) +
130 ") participants=" + std::to_string(participantIds.size()));
131}
132
133void LogObserver::onIncantationEnd(int x, int y, bool success)
134{
135 _logger.info(WORLD, std::string("incantation end ") + (success ? "success" : "fail") + " at (" +
136 std::to_string(x) + "," + std::to_string(y) + ")");
137}
138
139void LogObserver::onPlayerLevelUp(int playerId, int newLevel)
140{
141 _logger.info(WORLD,
142 "player #" + std::to_string(playerId) + " -> lvl " + std::to_string(newLevel));
143}
144
145void LogObserver::onGameEnd(const std::string& winningTeam)
146{
147 _logger.info(WORLD, "game end, winner=" + winningTeam);
148}
149
150void LogObserver::onTileChanged(int x, int y, Resources resources)
151{
152 _logger.debug(WORLD, "tile (" + std::to_string(x) + "," + std::to_string(y) + ") -> [" +
153 resStr(resources) + "]");
154}
Orientation
ResourceType
Enumerate the different types of resources in the game.
Definition Resources.hpp:12
void onGameEnd(const std::string &winningTeam) override
A team won (6 players at max level). -> seg.
void onPlayerInventoryChanged(int playerId, int x, int y, Resources inventory) override
Player's carried resources changed. -> pin.
void onResourceTaken(int playerId, ResourceType resourceType, int tileX, int tileY, Resources resources) override
Player picked a resource up off the tile. -> pgt (+ bct/pin)
void onPlayerEjected(int playerId) override
Player got pushed off its tile by an eject. -> pex.
void onLineSent(int connectionId, const std::string &line) override
One line was queued to be sent to the client.
void onEggLaid(int eggId, int playerId, int x, int y) override
A player laid an egg via Fork. -> enw.
void onClientConnected(int connectionId) override
A new client socket was accepted (assigned connectionId).
void onPlayerMoved(int playerId, int nx, int ny, Orientation newOrientation) override
Player changed tile and/or facing. -> ppo.
void onBroadcast(int playerId, const std::string &message) override
Player shouted a broadcast message. -> pbc.
void onClientDisconnected(int connectionId) override
A client socket closed or errored.
void onInitialEggSpawned(int eggId, const std::string &teamName, int x, int y) override
An initial team egg placed at game start (custom). -> sse.
LogObserver(Logger &logger)
void onIncantationStart(int x, int y, int level, const std::vector< int > &participantIds) override
Incantation began on a tile. participantIds = players involved. -> pic.
void onPlayerRemoved(int playerId) override
Player left (death or disconnect). -> pdi.
void onPlayerAdded(int playerId, int x, int y, Orientation orientation, int level, const std::string &teamName) override
Player spawned (egg hatched or initial connection). -> pnw.
void onTileChanged(int x, int y, Resources resources) override
A tile's resource contents changed (e.g. respawn). -> bct.
void onEggHatched(int eggId) override
An egg hatched into a connectable slot. -> ebo.
void onResourceDropped(int playerId, ResourceType resourceType, int tileX, int tileY, Resources resources) override
Player dropped a resource onto the tile. -> pdr (+ bct/pin)
void onEggDied(int eggId) override
An egg was destroyed (e.g. crushed by an eject). -> edi.
void onPlayerLevelUp(int playerId, int newLevel) override
Player reached a new level. -> plv.
void onIncantationEnd(int x, int y, bool success) override
Incantation finished, success = leveled up or not. -> pie.
void onLineReceived(int connectionId, const std::string &line) override
One complete ' '-terminated line arrived from the client.
Logger & _logger
Logging facade: formats [time] [LEVEL] [component] message, filters by level, and forwards to a singl...
Definition Logger.hpp:17
void info(std::string_view component, std::string_view msg)
Definition Logger.cpp:55
void debug(std::string_view component, std::string_view msg)
Definition Logger.cpp:50
Represents the resources available in the game, including food and various minerals.
Definition Resources.hpp:17
static std::string get_name(int index)
Definition Resources.hpp:33
int deraumere
Definition Resources.hpp:21
std::string trim(const std::string &s)
Strip a single trailing ' ' so protocol lines log on one row.
std::string resStr(const Resources &r)