Skip to content

Commit c0d5144

Browse files
committed
feat: basis for more UI functionality
1 parent 71a5db2 commit c0d5144

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

GhostServer/mainwindow.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ MainWindow::MainWindow(QWidget* parent)
4545
network = new NetworkManager("ghost_log");
4646

4747
connect(network, &NetworkManager::OnNewEvent, this, &MainWindow::AddEventLog);
48+
connect(network, &NetworkManager::UIEvent, this, &MainWindow::UIEvent);
4849
connect(ui.serverButton, &QPushButton::clicked, this, &MainWindow::StartServer);
4950
connect(ui.startCountdown, &QPushButton::clicked, this, &MainWindow::StartCountdown);
5051
connect(ui.resetButton, &QPushButton::clicked, this, &MainWindow::ResetServer);
@@ -130,3 +131,7 @@ void MainWindow::OnPresetChanged(int index)
130131
ui.preCommandList->setPlainText(preset.preCommands);
131132
ui.postCommandList->setPlainText(preset.postCommands);
132133
}
134+
135+
void MainWindow::UIEvent(std::string event)
136+
{
137+
}

GhostServer/mainwindow.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ class MainWindow : public QWidget
3232
void StartServer();
3333
void StopServer();
3434
void ResetServer();
35+
void StartCountdown();
3536
void SubmitCommand();
3637
void OnPresetChanged(int index);
3738

3839
public slots:
3940
void AddEventLog(QString log) {ui.textBrowser->append(log);}
40-
void StartCountdown();
41+
void UIEvent(std::string event);
4142

4243

4344
};

GhostServer/networkmanager.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ 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))
2627
#else
2728
# include <stdio.h>
2829
# define GHOST_LOG(x) (file_log(x), printf("[LOG] %s\n", std::string(x).c_str()))
30+
# define UI_EVENT(x) ((void)0)
2931
#endif
3032

3133
#define HEARTBEAT_RATE 5000
@@ -36,7 +38,7 @@ static void file_log(std::string str) {
3638
#ifdef _WIN32
3739
# define strcasecmp _stricmp
3840
#else
39-
# include <strings.h>
41+
# include <strings.h>
4042
#endif
4143

4244
static std::chrono::time_point<std::chrono::steady_clock> lastHeartbeat;
@@ -180,6 +182,8 @@ bool NetworkManager::StartServer(const int port)
180182
this->serverThread = std::thread(&NetworkManager::RunServer, this);
181183
this->serverThread.detach();
182184

185+
UI_EVENT("server_start");
186+
UI_EVENT("client_change");
183187
GHOST_LOG("Server started on " + this->serverIP.toString() + " (public IP: " + sf::IpAddress::getPublicAddress().toString() + ") on port " + std::to_string(this->serverPort));
184188
GHOST_LOG("Enter 'help' for a list of commands.");
185189

@@ -202,6 +206,8 @@ void NetworkManager::StopServer()
202206
this->udpSocket.unbind();
203207
this->listener.close();
204208

209+
UI_EVENT("client_change");
210+
UI_EVENT("server_stop");
205211
GHOST_LOG("Server stopped!");
206212
}
207213

@@ -225,6 +231,7 @@ void NetworkManager::DisconnectPlayer(Client& c, const char *reason)
225231

226232
if (toErase != -1) {
227233
this->clients.erase(this->clients.begin() + toErase);
234+
UI_EVENT("client_change");
228235
}
229236
}
230237

@@ -258,6 +265,7 @@ void NetworkManager::SetAccept(bool players, bool allow)
258265
this->acceptingSpectators = allow;
259266
}
260267
if (!changed) return;
268+
UI_EVENT("accept_refuse");
261269
GHOST_LOG(std::string("Now ") + (allow ? "accepting" : "refusing") + " connections from " + (players ? "players" : "spectators"));
262270

263271
}
@@ -357,6 +365,7 @@ void NetworkManager::CheckConnection()
357365
c.tcpSocket->send(packet_notify_all);
358366
}
359367

368+
UI_EVENT("client_change");
360369
GHOST_LOG("Connection: " + client.name + " (" + (client.spectator ? "spectator" : "player") + ") @ " + client.IP.toString() + ":" + std::to_string(client.port));
361370

362371
this->clients.push_back(std::move(client));
@@ -430,6 +439,7 @@ void NetworkManager::Treat(sf::Packet& packet, sf::IpAddress ip, unsigned short
430439
std::string map;
431440
packet >> map;
432441
client->currentMap = map;
442+
UI_EVENT("client_change");
433443
GHOST_LOG(client->name + " is now on " + map);
434444
SEND_TO_OTHERS(packet);
435445
break;

GhostServer/networkmanager.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ class NetworkManager
146146

147147
#ifdef GHOST_GUI
148148
signals:
149-
void OnNewEvent(QString event);
149+
void OnNewEvent(QString log);
150+
void UIEvent(std::string event);
150151
#endif
151152

152153

0 commit comments

Comments
 (0)