Skip to content

Commit 71a5db2

Browse files
committed
refactor: method for accept/refuse
1 parent c8f4533 commit 71a5db2

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

GhostServer/commands.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -340,20 +340,17 @@ void handle_cmd(NetworkManager *network, char *line) {
340340
std::string subcmd = argsL[1];
341341
if (subcmd == "players") {
342342
network->ScheduleServerThread([=]() {
343-
network->acceptingPlayers = true;
343+
network->SetAccept(true, true);
344344
});
345-
LINE("Now accepting connections from players");
346345
} else if (subcmd == "spectators") {
347346
network->ScheduleServerThread([=]() {
348-
network->acceptingSpectators = true;
347+
network->SetAccept(false, true);
349348
});
350-
LINE("Now accepting connections from spectators");
351349
} else if (subcmd == "all") {
352350
network->ScheduleServerThread([=]() {
353-
network->acceptingPlayers = true;
354-
network->acceptingSpectators = true;
351+
network->SetAccept(true, true);
352+
network->SetAccept(false, true);
355353
});
356-
LINE("Now accepting connections from players and spectators");
357354
} else {
358355
LINE("Usage: accept <players|spectators|all>");
359356
}
@@ -368,20 +365,17 @@ void handle_cmd(NetworkManager *network, char *line) {
368365
std::string subcmd = argsL[1];
369366
if (subcmd == "players") {
370367
network->ScheduleServerThread([=]() {
371-
network->acceptingPlayers = false;
368+
network->SetAccept(true, false);
372369
});
373-
LINE("Now refusing connections from players");
374370
} else if (subcmd == "spectators") {
375371
network->ScheduleServerThread([=]() {
376-
network->acceptingSpectators = false;
372+
network->SetAccept(false, false);
377373
});
378-
LINE("Now refusing connections from spectators");
379374
} else if (subcmd == "all") {
380375
network->ScheduleServerThread([=]() {
381-
network->acceptingPlayers = false;
382-
network->acceptingSpectators = false;
376+
network->SetAccept(true, false);
377+
network->SetAccept(false, false);
383378
});
384-
LINE("Now refusing connections from players and spectators");
385379
} else {
386380
LINE("Usage: refuse <players|spectators|all>");
387381
}

GhostServer/networkmanager.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,25 @@ void NetworkManager::StartCountdown(const std::string preCommands, const std::st
243243
client.tcpSocket->send(packet);
244244
}
245245
if (true) { // TODO: Make this a setting?
246-
this->acceptingPlayers = false;
247-
GHOST_LOG("Now refusing connections from players");
246+
this->SetAccept(true, false);
248247
}
249248
}
250249

250+
void NetworkManager::SetAccept(bool players, bool allow)
251+
{
252+
bool changed = false;
253+
if (players) {
254+
changed = this->acceptingPlayers != allow;
255+
this->acceptingPlayers = allow;
256+
} else {
257+
changed = this->acceptingSpectators != allow;
258+
this->acceptingSpectators = allow;
259+
}
260+
if (!changed) return;
261+
GHOST_LOG(std::string("Now ") + (allow ? "accepting" : "refusing") + " connections from " + (players ? "players" : "spectators"));
262+
263+
}
264+
251265
bool NetworkManager::ShouldBlockConnection(const sf::IpAddress& ip)
252266
{
253267
if (std::find_if(this->clients.begin(), this->clients.end(), [&ip](const Client& c) { return ip == c.IP; }) != this->clients.end()) {

GhostServer/networkmanager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class NetworkManager
133133
bool ShouldBlockConnection(const sf::IpAddress &ip);
134134
void DisconnectPlayer(Client &client, const char *reason);
135135
void StartCountdown(const std::string preCommands, const std::string postCommands, const int duration);
136+
void SetAccept(bool players, bool accept);
136137

137138
void CheckConnection();
138139
void ReceiveUDPUpdates(std::vector<std::tuple<sf::Packet, sf::IpAddress, unsigned short>>& buffer);

0 commit comments

Comments
 (0)