346 lines
12 KiB
C++
346 lines
12 KiB
C++
#include "game_server/InternalGameServer.h"
|
|
#include "game_server/WebsocketGameClient.h"
|
|
#include "game_server/WebsocketGameServer.h"
|
|
#include <chrono>
|
|
#include <cstdlib>
|
|
#include <iostream>
|
|
#include <memory>
|
|
#include <thread>
|
|
|
|
namespace {
|
|
|
|
void require(bool condition, const char* message)
|
|
{
|
|
if (condition) return;
|
|
std::cerr << "WebsocketLockstepTests: " << message << '\n';
|
|
std::exit(EXIT_FAILURE);
|
|
}
|
|
|
|
FRG::Vec3fp point(float x, float y, float z)
|
|
{
|
|
return {FRG::fp(x), FRG::fp(y), FRG::fp(z)};
|
|
}
|
|
|
|
bool samePoint(const FRG::Vec3fp& left, const FRG::Vec3fp& right)
|
|
{
|
|
return left.x == right.x && left.y == right.y && left.z == right.z;
|
|
}
|
|
|
|
template <class Predicate>
|
|
bool waitFor(Predicate predicate)
|
|
{
|
|
const auto deadline =
|
|
std::chrono::steady_clock::now() + std::chrono::seconds(3);
|
|
while (std::chrono::steady_clock::now() < deadline) {
|
|
if (predicate()) return true;
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool sameMovement(
|
|
const FRG::CharacterState& left, const FRG::CharacterState& right)
|
|
{
|
|
return left.position.x.raw_value() == right.position.x.raw_value() &&
|
|
left.position.z.raw_value() == right.position.z.raw_value() &&
|
|
left.velocity.x.raw_value() == right.velocity.x.raw_value() &&
|
|
left.velocity.y.raw_value() == right.velocity.y.raw_value();
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main()
|
|
{
|
|
FRG::WebsocketEndpoint secureEndpoint;
|
|
require(
|
|
secureEndpoint.parse(
|
|
"wss://api.shadowoverbishkek.com/server001/"),
|
|
"secure WebSocket endpoint was rejected");
|
|
require(
|
|
secureEndpoint.host == "api.shadowoverbishkek.com" &&
|
|
secureEndpoint.port == "443" &&
|
|
secureEndpoint.path == "/server001/" &&
|
|
secureEndpoint.useTls,
|
|
"secure WebSocket endpoint was parsed incorrectly");
|
|
FRG::WebsocketEndpoint insecureEndpoint;
|
|
require(
|
|
insecureEndpoint.parse("ws://127.0.0.1:8080/"),
|
|
"insecure WebSocket endpoint was rejected");
|
|
require(
|
|
insecureEndpoint.host == "127.0.0.1" &&
|
|
insecureEndpoint.port == "8080" &&
|
|
!insecureEndpoint.useTls,
|
|
"insecure WebSocket endpoint was parsed incorrectly");
|
|
FRG::WebsocketEndpoint invalidEndpoint;
|
|
require(
|
|
!invalidEndpoint.parse("https://api.shadowoverbishkek.com/"),
|
|
"non-WebSocket endpoint was accepted");
|
|
|
|
FRG::CampaignConfig campaign;
|
|
campaign.id = "test_lockstep";
|
|
campaign.networkPlayerSlots = {"player1", "player2"};
|
|
|
|
FRG::InternalGameServer authoritative;
|
|
authoritative.startCampaign(campaign, {}, true, false);
|
|
nlohmann::json initialState;
|
|
authoritative.save(initialState);
|
|
initialState["players"]["player2"] = initialState["players"]["player1"];
|
|
FRG::LocationGameState testLocation;
|
|
auto hostileNpc = std::make_shared<FRG::CharacterState>();
|
|
hostileNpc->enabled = true;
|
|
hostileNpc->hp = 200.f;
|
|
hostileNpc->position = point(-1.f, 0.f, 0.f);
|
|
hostileNpc->walkTarget = point(4.f, 0.f, 0.f);
|
|
hostileNpc->requestedWalkTarget = hostileNpc->walkTarget;
|
|
hostileNpc->pathWaypoints = {hostileNpc->walkTarget};
|
|
hostileNpc->velocity = {FRG::fp(1.5f), FRG::fp(0.f)};
|
|
testLocation.npcs.emplace("hostile_npc", std::move(hostileNpc));
|
|
auto secondHostileNpc = std::make_shared<FRG::CharacterState>();
|
|
secondHostileNpc->enabled = true;
|
|
secondHostileNpc->hp = 200.f;
|
|
secondHostileNpc->position = point(1.f, 0.f, 0.f);
|
|
secondHostileNpc->walkTarget = point(-4.f, 0.f, 0.f);
|
|
secondHostileNpc->requestedWalkTarget = secondHostileNpc->walkTarget;
|
|
secondHostileNpc->pathWaypoints = {secondHostileNpc->walkTarget};
|
|
secondHostileNpc->velocity = {FRG::fp(-1.5f), FRG::fp(0.f)};
|
|
testLocation.npcs.emplace(
|
|
"hostile_npc_2", std::move(secondHostileNpc));
|
|
nlohmann::json testLocationState;
|
|
testLocation.save(testLocationState);
|
|
initialState["currentLocationId"] = "test_location";
|
|
initialState["locations"]["test_location"] = std::move(testLocationState);
|
|
authoritative.load(initialState);
|
|
FRG::WebsocketGameServer server(
|
|
authoritative, {"player1", "player2"}, "lockstep-v2:test_lockstep",
|
|
"127.0.0.1", 0);
|
|
auto client = std::make_unique<FRG::WebsocketGameClient>(
|
|
"127.0.0.1", std::to_string(server.port()), "/");
|
|
|
|
require(
|
|
waitFor([&]() { return client->connected(); }),
|
|
"client did not connect");
|
|
require(
|
|
waitFor([&]() {
|
|
client->update(0);
|
|
return client->initialized();
|
|
}),
|
|
"client did not receive initial state");
|
|
require(client->localPlayerId() == "player1", "wrong player slot assigned");
|
|
|
|
// Advance avoidance before the second client joins. Its initial snapshot must
|
|
// carry previous-tick velocity or its very next ORCA result will diverge.
|
|
server.update();
|
|
require(
|
|
waitFor([&]() {
|
|
client->update(0);
|
|
const auto first = client->snapshot();
|
|
const auto host = authoritative.snapshot();
|
|
return sameMovement(
|
|
first->locations.at("test_location").npcs.at(
|
|
"hostile_npc").state,
|
|
host->locations.at("test_location").npcs.at(
|
|
"hostile_npc").state);
|
|
}),
|
|
"first client did not apply the pre-join movement tick");
|
|
|
|
auto secondClient = std::make_unique<FRG::WebsocketGameClient>(
|
|
"127.0.0.1", std::to_string(server.port()), "/");
|
|
require(
|
|
waitFor([&]() {
|
|
secondClient->update(0);
|
|
return secondClient->initialized();
|
|
}),
|
|
"second client did not receive initial state");
|
|
require(
|
|
secondClient->localPlayerId() == "player2",
|
|
"second client received the wrong player slot");
|
|
|
|
FRG::WebsocketGameClient overflow(
|
|
"127.0.0.1", std::to_string(server.port()), "/");
|
|
require(
|
|
waitFor([&]() {
|
|
overflow.update(0);
|
|
return overflow.rejected();
|
|
}),
|
|
"connection beyond configured slots was not rejected");
|
|
|
|
server.update();
|
|
require(
|
|
waitFor([&]() {
|
|
client->update(0);
|
|
secondClient->update(0);
|
|
return client->snapshot()->players.at("player2").state.enabled &&
|
|
secondClient->snapshot()->players.at("player1").state.enabled;
|
|
}) &&
|
|
authoritative.snapshot()->players.at("player1").state.enabled &&
|
|
authoritative.snapshot()->players.at("player2").state.enabled,
|
|
"connection presence was not broadcast to all replicas");
|
|
|
|
for (int tick = 0; tick < 6; ++tick) server.update();
|
|
require(
|
|
waitFor([&]() {
|
|
client->update(0);
|
|
secondClient->update(0);
|
|
const auto first = client->snapshot();
|
|
const auto second = secondClient->snapshot();
|
|
const auto host = authoritative.snapshot();
|
|
for (const char* id : {"hostile_npc", "hostile_npc_2"}) {
|
|
const auto& hostState =
|
|
host->locations.at("test_location").npcs.at(id).state;
|
|
if (!sameMovement(
|
|
first->locations.at("test_location").npcs.at(id).state,
|
|
hostState) ||
|
|
!sameMovement(
|
|
second->locations.at("test_location").npcs.at(id).state,
|
|
hostState)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}),
|
|
"late-joining client diverged during character avoidance");
|
|
|
|
client->submit(FRG::SetCharacterDestinationCommand{
|
|
{}, "spoofed", point(1.234f, 0.f, 2.345f), "ignored"});
|
|
secondClient->submit(FRG::SetCharacterDestinationCommand{
|
|
{}, "spoofed", point(-1.234f, 0.f, 3.456f), "ignored"});
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
|
server.update();
|
|
server.update();
|
|
server.update();
|
|
const bool destinationsConverged = waitFor([&]() {
|
|
client->update(0);
|
|
secondClient->update(0);
|
|
const auto first = client->snapshot();
|
|
const auto second = secondClient->snapshot();
|
|
const auto host = authoritative.snapshot();
|
|
return samePoint(
|
|
first->players.at("player1").state.requestedWalkTarget,
|
|
point(1.234f, 0.f, 2.345f)) &&
|
|
samePoint(
|
|
second->players.at("player2").state.requestedWalkTarget,
|
|
point(-1.234f, 0.f, 3.456f)) &&
|
|
samePoint(
|
|
first->players.at("player2").state.requestedWalkTarget,
|
|
second->players.at("player2").state.requestedWalkTarget) &&
|
|
samePoint(
|
|
second->players.at("player1").state.requestedWalkTarget,
|
|
first->players.at("player1").state.requestedWalkTarget) &&
|
|
samePoint(
|
|
host->players.at("player1").state.requestedWalkTarget,
|
|
first->players.at("player1").state.requestedWalkTarget) &&
|
|
samePoint(
|
|
host->players.at("player2").state.requestedWalkTarget,
|
|
second->players.at("player2").state.requestedWalkTarget);
|
|
});
|
|
if (!destinationsConverged) {
|
|
const auto first = client->snapshot();
|
|
const auto second = secondClient->snapshot();
|
|
const auto host = authoritative.snapshot();
|
|
std::cerr << "destination revisions " << first->revision << ", "
|
|
<< second->revision << ", " << host->revision << "\n"
|
|
<< "player targets "
|
|
<< first->players.at("player1").state.requestedWalkTarget.toEigen().transpose()
|
|
<< " / "
|
|
<< host->players.at("player1").state.requestedWalkTarget.toEigen().transpose()
|
|
<< "\nplayer2 targets "
|
|
<< second->players.at("player2").state.requestedWalkTarget.toEigen().transpose()
|
|
<< " / "
|
|
<< host->players.at("player2").state.requestedWalkTarget.toEigen().transpose()
|
|
<< std::endl;
|
|
}
|
|
require(
|
|
destinationsConverged,
|
|
"simultaneous destinations did not converge across lockstep replicas");
|
|
|
|
client->submit(FRG::SetCharacterTargetCommand{
|
|
"stale_location", "spoofed_character", "hostile_npc"});
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
|
server.update();
|
|
server.update();
|
|
server.update();
|
|
require(
|
|
waitFor([&]() {
|
|
client->update(0);
|
|
secondClient->update(0);
|
|
return client->snapshot()->players.at("player1").state.attackTargetId ==
|
|
"hostile_npc" &&
|
|
secondClient->snapshot()->players.at("player1").state.attackTargetId ==
|
|
"hostile_npc" &&
|
|
authoritative.snapshot()->players.at("player1").state.attackTargetId ==
|
|
"hostile_npc";
|
|
}),
|
|
"target command was not delayed and rebroadcast");
|
|
|
|
client->submit(FRG::ClearCharacterAttackTargetCommand{
|
|
"stale_location", "spoofed_character"});
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
|
server.update();
|
|
server.update();
|
|
server.update();
|
|
require(
|
|
waitFor([&]() {
|
|
client->update(0);
|
|
secondClient->update(0);
|
|
return client->snapshot()->players.at("player1").state.attackTargetId.empty() &&
|
|
secondClient->snapshot()->players.at("player1").state.attackTargetId.empty() &&
|
|
authoritative.snapshot()->players.at("player1").state.attackTargetId.empty();
|
|
}),
|
|
"clear attack target command was not delayed and rebroadcast");
|
|
|
|
client->submit(FRG::TryCharacterAttackComboCommand{
|
|
"stale_location", "spoofed_character"});
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
|
server.update();
|
|
server.update();
|
|
server.update();
|
|
require(
|
|
waitFor([&]() {
|
|
client->update(0);
|
|
secondClient->update(0);
|
|
const auto host = authoritative.snapshot();
|
|
return sameMovement(
|
|
client->snapshot()->locations.at("test_location").npcs.at(
|
|
"hostile_npc").state,
|
|
host->locations.at("test_location").npcs.at(
|
|
"hostile_npc").state) &&
|
|
sameMovement(
|
|
secondClient->snapshot()->locations.at(
|
|
"test_location").npcs.at("hostile_npc").state,
|
|
host->locations.at("test_location").npcs.at(
|
|
"hostile_npc").state);
|
|
}),
|
|
"attack combo command was not rebroadcast in lockstep");
|
|
|
|
secondClient.reset();
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
|
server.update();
|
|
require(
|
|
waitFor([&]() {
|
|
client->update(0);
|
|
return !client->snapshot()->players.at("player2").state.enabled;
|
|
}) &&
|
|
!authoritative.snapshot()->players.at("player2").state.enabled,
|
|
"disconnect presence was not propagated");
|
|
|
|
FRG::WebsocketGameClient replacement(
|
|
"127.0.0.1", std::to_string(server.port()), "/");
|
|
require(
|
|
waitFor([&]() {
|
|
replacement.update(0);
|
|
return replacement.initialized();
|
|
}),
|
|
"disconnected player slot was not reusable");
|
|
server.update();
|
|
require(
|
|
waitFor([&]() {
|
|
client->update(0);
|
|
replacement.update(0);
|
|
return client->snapshot()->players.at("player2").state.enabled;
|
|
}) &&
|
|
authoritative.snapshot()->players.at("player2").state.enabled,
|
|
"reconnected player was not enabled");
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|