Implemented the real server
This commit is contained in:
1 parent
931236bec0
commit
075750442f
22 files changed
+1130
-44
No files matched your search
+39
-5
@@ -1,4 +1,7 @@
|
||||
#include "game_server/InternalGameServer.h"
|
||||
#if defined(NETWORK)
|
||||
#include "game_server/WebsocketGameServer.h"
|
||||
#endif
|
||||
#include "Campaign.h"
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
@@ -7,6 +10,7 @@
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
@@ -38,12 +42,14 @@ std::int64_t parsePositiveNumber(const char* value, const char* option)
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
std::int64_t tickMs = 50;
|
||||
std::int64_t tickMs = 500;
|
||||
std::int64_t maxTicks = (std::numeric_limits<std::int64_t>::max)();
|
||||
bool sleepBetweenTicks = true;
|
||||
std::string campaignId;
|
||||
std::string locationId;
|
||||
std::string campaignsPath = "resources/config/campaigns.json";
|
||||
std::string listenAddress = "0.0.0.0";
|
||||
std::int64_t listenPort = 8080;
|
||||
|
||||
try {
|
||||
for (int index = 1; index < argc; ++index) {
|
||||
@@ -60,11 +66,17 @@ int main(int argc, char** argv)
|
||||
locationId = argv[++index];
|
||||
} else if (option == "--campaigns" && index + 1 < argc) {
|
||||
campaignsPath = argv[++index];
|
||||
} else if (option == "--address" && index + 1 < argc) {
|
||||
listenAddress = argv[++index];
|
||||
} else if (option == "--port" && index + 1 < argc) {
|
||||
listenPort = parsePositiveNumber(argv[++index], "--port");
|
||||
if (listenPort > 65535)
|
||||
throw std::runtime_error("Invalid value for --port");
|
||||
} else if (option == "--help") {
|
||||
std::cout
|
||||
<< "Usage: GameServer [--ticks N] [--tick-ms N] "
|
||||
"[--no-sleep] [--campaign ID] [--location ID] "
|
||||
"[--campaigns PATH]\n";
|
||||
"[--campaigns PATH] [--address IP] [--port N]\n";
|
||||
return EXIT_SUCCESS;
|
||||
} else {
|
||||
throw std::runtime_error("Unknown or incomplete option: " + option);
|
||||
@@ -75,6 +87,10 @@ int main(int argc, char** argv)
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
//Temporary override:
|
||||
campaignId = "battle_zone";
|
||||
locationId = "uni_exterior";
|
||||
|
||||
std::signal(SIGINT, requestStop);
|
||||
std::signal(SIGTERM, requestStop);
|
||||
|
||||
@@ -95,12 +111,12 @@ int main(int argc, char** argv)
|
||||
throw std::runtime_error(
|
||||
"Campaign '" + campaignId +
|
||||
"' has no location '" + selectedLocation + "'");
|
||||
server.startCampaign(*campaign, std::string(), false, false);
|
||||
server.startCampaign(*campaign, std::string(), true, false);
|
||||
server.prepareLocation(*location);
|
||||
server.activateLocation(selectedLocation);
|
||||
std::cout << "Loaded campaign '" << campaignId
|
||||
<< "', location '" << selectedLocation
|
||||
<< "' with zero players\n";
|
||||
<< "' with shared player '" << server.localPlayerId() << "'\n";
|
||||
} catch (const std::exception& error) {
|
||||
std::cerr << "Server content setup failed: "
|
||||
<< error.what() << '\n';
|
||||
@@ -110,12 +126,30 @@ int main(int argc, char** argv)
|
||||
const auto tickDuration = std::chrono::milliseconds(tickMs);
|
||||
auto nextTick = std::chrono::steady_clock::now();
|
||||
|
||||
std::cout << "GameServer started (idle headless mode, "
|
||||
#if defined(NETWORK)
|
||||
std::unique_ptr<FRG::WebsocketGameServer> websocketServer;
|
||||
try {
|
||||
websocketServer = std::make_unique<FRG::WebsocketGameServer>(
|
||||
server, listenAddress, static_cast<std::uint16_t>(listenPort));
|
||||
} catch (const std::exception& error) {
|
||||
std::cerr << "WebSocket listener setup failed: "
|
||||
<< error.what() << '\n';
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
std::cout << "GameServer listening on ws://" << listenAddress << ':'
|
||||
<< websocketServer->port() << " (" << tickMs << " ms tick)\n";
|
||||
#else
|
||||
std::cout << "GameServer started (network disabled, "
|
||||
<< tickMs << " ms tick)\n";
|
||||
#endif
|
||||
for (std::int64_t tick = 0;
|
||||
tick < maxTicks && !stopRequested.load(); ++tick) {
|
||||
nextTick += tickDuration;
|
||||
#if defined(NETWORK)
|
||||
websocketServer->update(tickMs);
|
||||
#else
|
||||
server.update(tickMs);
|
||||
#endif
|
||||
if (sleepBetweenTicks) std::this_thread::sleep_until(nextTick);
|
||||
}
|
||||
std::cout << "GameServer stopped\n";
|
||||
|
||||
Reference in new issue
Block a user