Zappy Server
C++ game server: world, rules, scheduler, network
Loading...
Searching...
No Matches
GuiNotifier.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4
6
7GuiNotifier::GuiNotifier(ClientManager& clients) : _clients(clients) {}
8
9void GuiNotifier::addGui(int connectionId) { _guiIds.push_back(connectionId); }
10
11void GuiNotifier::removeGui(int connectionId)
12{
13 _guiIds.erase(std::remove(_guiIds.begin(), _guiIds.end(), connectionId), _guiIds.end());
14}
15
16void GuiNotifier::send(int connectionId, const std::string& msg)
17{
18 _clients.send(connectionId, msg);
19}
20
21void GuiNotifier::broadcast(const std::string& msg)
22{
23 for (int id : _guiIds) _clients.send(id, msg);
24}
25
26void GuiNotifier::onPlayerAdded(int playerId, int x, int y, Orientation orientation, int level,
27 const std::string& teamName)
28{
29 broadcast(Serializer::pnw(playerId, x, y, orientation, level, teamName));
30}
31
32void GuiNotifier::onPlayerMoved(int playerId, int nx, int ny, Orientation newOrientation)
33{
34 broadcast(Serializer::ppo(playerId, nx, ny, newOrientation));
35}
36
37void GuiNotifier::onPlayerInventoryChanged(int playerId, int x, int y, Resources inventory)
38{
39 broadcast(Serializer::pin(playerId, x, y, inventory));
40}
41
42void GuiNotifier::onPlayerRemoved(int playerId) { broadcast(Serializer::pdi(playerId)); }
43
44void GuiNotifier::onPlayerEjected(int playerId) { broadcast(Serializer::pex(playerId)); }
45
46void GuiNotifier::onBroadcast(int playerId, const std::string& message)
47{
48 broadcast(Serializer::pbc(playerId, message));
49}
50
51void GuiNotifier::onResourceTaken(int playerId, ResourceType resourceType, int tileX, int tileY,
52 Resources resources)
53{
54 broadcast(Serializer::pgt(playerId, static_cast<int>(resourceType)));
55 broadcast(Serializer::bct(tileX, tileY, resources));
56}
57
58void GuiNotifier::onResourceDropped(int playerId, ResourceType resourceType, int tileX, int tileY,
59 Resources resources)
60{
61 broadcast(Serializer::pdr(playerId, static_cast<int>(resourceType)));
62 broadcast(Serializer::bct(tileX, tileY, resources));
63}
64
65void GuiNotifier::onEggLaid(int eggId, int playerId, int x, int y)
66{
67 broadcast(Serializer::pfk(playerId));
68 broadcast(Serializer::enw(eggId, playerId, x, y));
69}
70
71void GuiNotifier::onInitialEggSpawned(int eggId, const std::string& teamName, int x, int y)
72{
73 broadcast(Serializer::sse(eggId, teamName, x, y));
74}
75
77
79
80void GuiNotifier::onIncantationStart(int x, int y, int level,
81 const std::vector<int>& participantIds)
82{
83 broadcast(Serializer::pic(x, y, level, participantIds));
84}
85
86void GuiNotifier::onIncantationEnd(int x, int y, bool success)
87{
88 broadcast(Serializer::pie(x, y, success));
89}
90
91void GuiNotifier::onPlayerLevelUp(int playerId, int newLevel)
92{
93 broadcast(Serializer::plv(playerId, newLevel));
94}
95
96void GuiNotifier::onGameEnd(const std::string& winningTeam)
97{
98 broadcast(Serializer::seg(winningTeam));
99}
100
101void GuiNotifier::onTileChanged(int x, int y, Resources resources)
102{
103 broadcast(Serializer::bct(x, y, resources));
104}
Orientation
ResourceType
Enumerate the different types of resources in the game.
Definition Resources.hpp:12
Owns every client socket and runs the poll() loop.
void send(int connectionId, const std::string &msg)
Queue msg to be sent to a client (flushed during poll()).
void addGui(int connectionId)
ClientManager & _clients
void onIncantationStart(int x, int y, int level, const std::vector< int > &participantIds) override
Incantation began on a tile. participantIds = players involved. -> pic.
void onInitialEggSpawned(int eggId, const std::string &teamName, int x, int y) override
An initial team egg placed at game start (custom). -> sse.
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 broadcast(const std::string &msg)
std::vector< int > _guiIds
void onEggLaid(int eggId, int playerId, int x, int y) override
A player laid an egg via Fork. -> enw.
void onEggHatched(int eggId) override
An egg hatched into a connectable slot. -> ebo.
void onGameEnd(const std::string &winningTeam) override
A team won (6 players at max level). -> seg.
void onPlayerEjected(int playerId) override
Player got pushed off its tile by an eject. -> pex.
void onPlayerMoved(int playerId, int nx, int ny, Orientation newOrientation) override
Player changed tile and/or facing. -> ppo.
void onPlayerRemoved(int playerId) override
Player left (death or disconnect). -> pdi.
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 onBroadcast(int playerId, const std::string &message) override
Player shouted a broadcast message. -> pbc.
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 onPlayerInventoryChanged(int playerId, int x, int y, Resources inventory) override
Player's carried resources changed. -> pin.
void onTileChanged(int x, int y, Resources resources) override
A tile's resource contents changed (e.g. respawn). -> bct.
GuiNotifier(ClientManager &clients)
void onIncantationEnd(int x, int y, bool success) override
Incantation finished, success = leveled up or not. -> pie.
void send(int connectionId, const std::string &msg)
void removeGui(int connectionId)
Represents the resources available in the game, including food and various minerals.
Definition Resources.hpp:17
std::string pdr(int id, int resourceIndex)
Player drops resource resourceIndex (0..6). Wire: pdr #N i\n
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 seg(const std::string &team)
End of game, team won. Wire: seg NAME\n
std::string pex(int id)
Player expelled (got ejected). Wire: pex #N\n
std::string edi(int eggId)
Egg died. Wire: edi #E\n
std::string ebo(int eggId)
Egg hatched, player connects from it. Wire: ebo #E\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 sse(int eggId, const std::string &teamName, int x, int y)
std::string pgt(int id, int resourceIndex)
Player takes resource resourceIndex (0..6). Wire: pgt #N i\n
std::string pfk(int id)
Player lays an egg (fork). Wire: pfk #N\n
std::string pie(int x, int y, bool success)
std::string pbc(int id, const std::string &msg)
Player broadcast. Wire: pbc #N MESSAGE\n
std::string ppo(int id, int x, int y, Orientation o)
Player position. Wire: ppo #N X Y O\n
std::string pnw(int id, int x, int y, Orientation o, int level, const std::string &team)
New player connected. Wire: pnw #N X Y O L NAME\n (L = level)
std::string pdi(int id)
Player death. Wire: pdi #N\n
std::string enw(int eggId, int playerId, int x, int y)
Egg laid by a player. Wire: enw #E #N X Y\n (E = egg, N = parent)
std::string plv(int id, int level)
Player level. Wire: plv #N L\n
std::string pic(int x, int y, int level, const std::vector< int > &ids)