Implemented the real server
This commit is contained in:
1 parent
931236bec0
commit
075750442f
22 files changed
+1130
-44
No files matched your search
+31
-2
@@ -105,6 +105,23 @@ endif()
|
||||
add_executable(GameServer src/main.cpp README.md)
|
||||
source_group("Server" FILES src/main.cpp README.md)
|
||||
target_link_libraries(GameServer PRIVATE game_server_core)
|
||||
if(WIN32)
|
||||
set(BOOST_BUNDLED_DIR "${GAME_ROOT}/thirdparty/boost_1_90_0")
|
||||
if(NOT EXISTS "${BOOST_BUNDLED_DIR}/boost/asio.hpp")
|
||||
message(FATAL_ERROR
|
||||
"Boost was not found at thirdparty/boost_1_90_0. Configure the "
|
||||
"graphical project once to populate it.")
|
||||
endif()
|
||||
target_sources(GameServer PRIVATE
|
||||
"${GAME_ROOT}/src/game_server/NetworkProtocol.cpp"
|
||||
"${GAME_ROOT}/src/game_server/NetworkProtocol.h"
|
||||
"${GAME_ROOT}/src/game_server/WebsocketGameServer.cpp"
|
||||
"${GAME_ROOT}/src/game_server/WebsocketGameServer.h")
|
||||
target_include_directories(GameServer PRIVATE "${BOOST_BUNDLED_DIR}")
|
||||
target_compile_definitions(
|
||||
GameServer PRIVATE NETWORK WIN32_LEAN_AND_MEAN _WIN32_WINNT=0x0A00)
|
||||
target_link_libraries(GameServer PRIVATE ws2_32 mswsock)
|
||||
endif()
|
||||
|
||||
include(CTest)
|
||||
if(BUILD_TESTING)
|
||||
@@ -116,14 +133,26 @@ if(BUILD_TESTING)
|
||||
add_test(
|
||||
NAME GameServer.LocationState
|
||||
COMMAND GameServerStateTests)
|
||||
if(WIN32)
|
||||
add_executable(
|
||||
GameServerNetworkProtocolTests
|
||||
tests/NetworkProtocolTests.cpp
|
||||
"${GAME_ROOT}/src/game_server/NetworkProtocol.cpp"
|
||||
"${GAME_ROOT}/src/game_server/NetworkProtocol.h")
|
||||
target_link_libraries(
|
||||
GameServerNetworkProtocolTests PRIVATE game_server_core)
|
||||
add_test(
|
||||
NAME GameServer.NetworkProtocol
|
||||
COMMAND GameServerNetworkProtocolTests)
|
||||
endif()
|
||||
add_test(
|
||||
NAME GameServer.IdleSmoke
|
||||
COMMAND GameServer --ticks 2 --no-sleep)
|
||||
add_test(
|
||||
NAME GameServer.BattleZoneZeroPlayers
|
||||
NAME GameServer.BattleZoneSharedPlayer
|
||||
COMMAND GameServer --campaign battle_zone --location uni_exterior
|
||||
--ticks 2 --no-sleep)
|
||||
set_tests_properties(
|
||||
GameServer.BattleZoneZeroPlayers
|
||||
GameServer.BattleZoneSharedPlayer
|
||||
PROPERTIES WORKING_DIRECTORY "${GAME_ROOT}")
|
||||
endif()
|
||||
+16
-7
@@ -1,8 +1,8 @@
|
||||
# Headless GameServer
|
||||
|
||||
This target runs the authoritative `InternalGameServer` without SDL, OpenGL,
|
||||
audio, Lua, or rendering dependencies. It can prepare campaign/location data
|
||||
headlessly; networking is not implemented yet.
|
||||
audio, Lua, or rendering dependencies. On Windows it exposes the state through
|
||||
a Boost.Beast WebSocket listener.
|
||||
|
||||
## Build
|
||||
|
||||
@@ -39,12 +39,18 @@ For a multi-configuration Windows build, the executable is normally
|
||||
For smoke tests, `--ticks N` limits execution and `--no-sleep` skips the
|
||||
real-time delay.
|
||||
|
||||
Load a location with no players:
|
||||
Start the first multiplayer transport iteration:
|
||||
|
||||
```bash
|
||||
./server/build/GameServer --campaign battle_zone --location uni_exterior
|
||||
```powershell
|
||||
.\server\build\Release\GameServer.exe --campaign battle_zone --location uni_exterior
|
||||
```
|
||||
|
||||
The listener defaults to `ws://0.0.0.0:8080`. Launch the Windows graphical game
|
||||
afterward and select `battle_zone`; that campaign connects to
|
||||
`ws://127.0.0.1:8080`. Other campaigns continue to use the in-process server.
|
||||
The server broadcasts an initial full state and then one every five seconds.
|
||||
Client commands are intentionally discarded in this iteration.
|
||||
|
||||
If `--location` is omitted, the campaign's configured start location is used.
|
||||
`--campaigns PATH` selects a different campaign registry. Content paths are
|
||||
resolved from the process working directory, so run from the repository root
|
||||
@@ -56,6 +62,8 @@ Available startup options:
|
||||
--campaign ID
|
||||
--location ID
|
||||
--campaigns PATH
|
||||
--address IP
|
||||
--port N
|
||||
--tick-ms N
|
||||
--ticks N
|
||||
--no-sleep
|
||||
@@ -64,8 +72,9 @@ Available startup options:
|
||||
The graphical client executes Lua/dialogue behavior after building its runtime
|
||||
`Location`; the headless content pass loads authoritative navigation, NPC,
|
||||
interactive-object, trigger, teleport, taxi, and animation metadata only.
|
||||
The standalone lifecycle intentionally creates neither the local `"player"` nor
|
||||
the battle-zone test bot. Those are opt-ins used by the graphical host.
|
||||
The standalone WebSocket lifecycle creates the existing shared `"player"` so
|
||||
the graphical client can render its local character. It does not create the
|
||||
battle-zone test bot.
|
||||
|
||||
## Tests
|
||||
|
||||
|
||||
+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";
|
||||
|
||||
@@ -86,5 +86,13 @@ int main()
|
||||
return fail("Battle Zone test bot animation metadata was not loaded");
|
||||
}
|
||||
|
||||
FRG::InternalGameServer networkServer;
|
||||
networkServer.startCampaign(battleZone, {}, true, false);
|
||||
const auto networkSnapshot = networkServer.snapshot();
|
||||
if (networkSnapshot->players.count("player") != 1)
|
||||
return fail("Network campaign shared player was not created");
|
||||
if (networkSnapshot->players.count("battle_zone_test_bot") != 0)
|
||||
return fail("Network campaign unexpectedly created the test bot");
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
#include "game_server/NetworkProtocol.h"
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
namespace {
|
||||
|
||||
void require(bool condition, const char* message)
|
||||
{
|
||||
if (condition) return;
|
||||
std::cerr << "NetworkProtocolTests: " << message << '\n';
|
||||
std::exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
const nlohmann::json original{
|
||||
{"schemaVersion", 1},
|
||||
{"campaignId", "battle_zone"},
|
||||
{"players", {{"player", {{"hp", 200.0}}}}}
|
||||
};
|
||||
const std::string message = FRG::makeStateMessage(original);
|
||||
nlohmann::json parsed;
|
||||
std::string error;
|
||||
require(
|
||||
FRG::parseStateMessage(message, parsed, error),
|
||||
"valid state envelope was rejected");
|
||||
require(parsed == original, "state payload did not round trip");
|
||||
|
||||
require(
|
||||
!FRG::parseStateMessage(
|
||||
R"({"protocolVersion":2,"type":"state","payload":{}})",
|
||||
parsed, error),
|
||||
"unknown protocol version was accepted");
|
||||
require(
|
||||
!FRG::parseStateMessage(
|
||||
R"({"protocolVersion":1,"type":"command","payload":{}})",
|
||||
parsed, error),
|
||||
"unknown message type was accepted");
|
||||
require(
|
||||
!FRG::parseStateMessage("not json", parsed, error),
|
||||
"malformed JSON was accepted");
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
Reference in new issue
Block a user