Skip to content

Commit 754e396

Browse files
committed
feat: allow registering handlers to any ui event
1 parent fadd857 commit 754e396

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

GhostServer/networkmanager.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ static void file_log(std::string str) {
2323
#ifdef GHOST_GUI
2424
# include <QVector>
2525
# define GHOST_LOG(x) (file_log(x), emit this->OnNewEvent(QString::fromStdString(x)))
26-
# define UI_EVENT(x) (emit this->UIEvent(x))
2726
#else
2827
# include <stdio.h>
2928
# define GHOST_LOG(x) (file_log(x), printf("[LOG] %s\n", std::string(x).c_str()))
30-
# define UI_EVENT(x) ((void)0)
3129
#endif
3230

3331
#define HEARTBEAT_RATE 5000
@@ -235,7 +233,6 @@ void NetworkManager::DisconnectPlayer(Client& c, const char *reason)
235233
if (toErase != -1) {
236234
this->clients.erase(this->clients.begin() + toErase);
237235
UI_EVENT("client_change");
238-
OnConnectedClientsChanged();
239236
}
240237
}
241238

@@ -373,11 +370,11 @@ void NetworkManager::CheckConnection()
373370
c.tcpSocket->send(packet_notify_all);
374371
}
375372

376-
UI_EVENT("client_change");
377373
GHOST_LOG("Connection: " + client.name + " (" + (client.spectator ? "spectator" : "player") + ") @ " + client.IP.toString() + ":" + std::to_string(client.port));
378-
OnConnectedClientsChanged();
379374

380375
this->clients.push_back(std::move(client));
376+
377+
UI_EVENT("client_change");
381378
}
382379

383380
void NetworkManager::ReceiveUDPUpdates(std::vector<std::tuple<sf::Packet, sf::IpAddress, unsigned short>>& buffer)
@@ -661,13 +658,18 @@ bool NetworkManager::IsOnWhitelist(std::string name, sf::IpAddress IP) {
661658
return index != whitelist.end();
662659
}
663660

664-
void NetworkManager::OnConnectedClientsChanged() {
665-
for (auto const& [id, callback] : connectedClientsChangedCallbacks) {
666-
callback();
661+
662+
void NetworkManager::UI_EVENT(std::string event) {
663+
for (auto item : uiEventCallbacks) {
664+
auto [type, callback] = item.second;
665+
if (type == event) callback();
667666
}
667+
#ifdef GHOST_GUI
668+
emit this->UIEvent(event);
669+
#endif
668670
}
669671

670-
int NetworkManager::RegisterConnectedClientsChangedCallback(std::function<void()> callback) {
671-
connectedClientsChangedCallbacks.insert({connectedClientsChangedCallbackId++, callback});
672-
return connectedClientsChangedCallbackId - 1;
673-
}
672+
int NetworkManager::RegisterEventCallback(std::string type, std::function<void()> callback) {
673+
uiEventCallbacks.insert({uiEventCallbackId++, {type, callback}});
674+
return uiEventCallbackId - 1;
675+
}

GhostServer/networkmanager.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,11 @@ class NetworkManager
105105

106106
sf::Clock clock;
107107

108-
std::map<int, std::function<void()>> connectedClientsChangedCallbacks;
109-
int connectedClientsChangedCallbackId = 1;
108+
std::map<int, std::tuple<std::string, std::function<void()>>> uiEventCallbacks;
109+
int uiEventCallbackId = 1;
110110

111111
void DoHeartbeats();
112112

113-
void OnConnectedClientsChanged();
114-
115113
public:
116114
NetworkManager(const char *logfile = "ghost_log.log");
117115
~NetworkManager();
@@ -150,8 +148,9 @@ class NetworkManager
150148

151149
bool IsOnWhitelist(std::string name, sf::IpAddress IP);
152150

153-
int RegisterConnectedClientsChangedCallback(std::function<void()> callback);
154-
void UnregisterConnectedClientsChangedCallback(int id) { connectedClientsChangedCallbacks.erase(id); }
151+
void UI_EVENT(std::string event);
152+
int RegisterEventCallback(std::string type, std::function<void()> callback);
153+
void UnregisterEventCallback(int id) { uiEventCallbacks.erase(id); }
155154

156155
#ifdef GHOST_GUI
157156
signals:

0 commit comments

Comments
 (0)