Separating state out of the location

This commit is contained in:
Vladislav Khorev committed 2026-10-30 14:56:24 +06:00
1 parent 5e9a663812
commit 297bd7377f
45 files changed
+2114 -778

No files matched your search

+123
View File
@@ -0,0 +1,123 @@
#include "game_server/InternalGameServer.h"
#include "Campaign.h"
#include <atomic>
#include <chrono>
#include <csignal>
#include <cstdlib>
#include <exception>
#include <iostream>
#include <limits>
#include <string>
#include <thread>
namespace {
std::atomic_bool stopRequested{false};
void requestStop(int)
{
stopRequested.store(true);
}
std::int64_t parsePositiveNumber(const char* value, const char* option)
{
try {
const std::string text(value);
std::size_t consumed = 0;
const long long parsed = std::stoll(text, &consumed);
if (consumed != text.size() || parsed <= 0)
throw std::invalid_argument("not positive");
return static_cast<std::int64_t>(parsed);
} catch (const std::exception&) {
throw std::runtime_error(
std::string("Invalid value for ") + option + ": " + value);
}
}
} // namespace
int main(int argc, char** argv)
{
std::int64_t tickMs = 50;
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";
try {
for (int index = 1; index < argc; ++index) {
const std::string option(argv[index]);
if (option == "--ticks" && index + 1 < argc) {
maxTicks = parsePositiveNumber(argv[++index], "--ticks");
} else if (option == "--tick-ms" && index + 1 < argc) {
tickMs = parsePositiveNumber(argv[++index], "--tick-ms");
} else if (option == "--no-sleep") {
sleepBetweenTicks = false;
} else if (option == "--campaign" && index + 1 < argc) {
campaignId = argv[++index];
} else if (option == "--location" && index + 1 < argc) {
locationId = argv[++index];
} else if (option == "--campaigns" && index + 1 < argc) {
campaignsPath = argv[++index];
} else if (option == "--help") {
std::cout
<< "Usage: GameServer [--ticks N] [--tick-ms N] "
"[--no-sleep] [--campaign ID] [--location ID] "
"[--campaigns PATH]\n";
return EXIT_SUCCESS;
} else {
throw std::runtime_error("Unknown or incomplete option: " + option);
}
}
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
return EXIT_FAILURE;
}
std::signal(SIGINT, requestStop);
std::signal(SIGTERM, requestStop);
FRG::InternalGameServer server;
if (!campaignId.empty()) {
try {
FRG::CampaignRegistry::instance().loadFromJson(campaignsPath);
const FRG::CampaignConfig* campaign =
FRG::CampaignRegistry::instance().find(campaignId);
if (!campaign)
throw std::runtime_error(
"Unknown campaign '" + campaignId + "'");
const std::string selectedLocation = locationId.empty()
? campaign->startLocation : locationId;
const FRG::LocationConfig* location =
campaign->findLocation(selectedLocation);
if (!location)
throw std::runtime_error(
"Campaign '" + campaignId +
"' has no location '" + selectedLocation + "'");
server.startCampaign(*campaign, std::string(), false, false);
server.prepareLocation(*location);
server.activateLocation(selectedLocation);
std::cout << "Loaded campaign '" << campaignId
<< "', location '" << selectedLocation
<< "' with zero players\n";
} catch (const std::exception& error) {
std::cerr << "Server content setup failed: "
<< error.what() << '\n';
return EXIT_FAILURE;
}
}
const auto tickDuration = std::chrono::milliseconds(tickMs);
auto nextTick = std::chrono::steady_clock::now();
std::cout << "GameServer started (idle headless mode, "
<< tickMs << " ms tick)\n";
for (std::int64_t tick = 0;
tick < maxTicks && !stopRequested.load(); ++tick) {
nextTick += tickDuration;
server.update(tickMs);
if (sleepBetweenTicks) std::this_thread::sleep_until(nextTick);
}
std::cout << "GameServer stopped\n";
return EXIT_SUCCESS;
}