Implemented the real server
This commit is contained in:
1 parent
931236bec0
commit
075750442f
22 files changed
+1130
-44
No files matched your search
@@ -0,0 +1,109 @@
|
||||
#include "RemoteGameServerHost.h"
|
||||
#include "WebsocketGameClient.h"
|
||||
#include "../Campaign.h"
|
||||
#include "../Location.h"
|
||||
#include <stdexcept>
|
||||
|
||||
namespace FRG {
|
||||
|
||||
RemoteGameServerHost::RemoteGameServerHost(WebsocketGameClient& client)
|
||||
: client_(client)
|
||||
{
|
||||
}
|
||||
|
||||
void RemoteGameServerHost::initializeCampaign(
|
||||
const std::string& campaignId, const std::string&,
|
||||
const std::string& zipFile)
|
||||
{
|
||||
const CampaignConfig* campaign =
|
||||
CampaignRegistry::instance().find(campaignId);
|
||||
if (!campaign)
|
||||
throw std::runtime_error("Unknown remote campaign '" + campaignId + "'");
|
||||
client_.replica_.startCampaign(*campaign, zipFile, true, false);
|
||||
}
|
||||
|
||||
void RemoteGameServerHost::clearCampaign()
|
||||
{
|
||||
runtimeLocations_.clear();
|
||||
client_.replica_.clearCampaign();
|
||||
}
|
||||
|
||||
void RemoteGameServerHost::resetCampaign(
|
||||
const std::string&, const std::string&)
|
||||
{
|
||||
// The authoritative remote campaign cannot be reset by this client.
|
||||
}
|
||||
|
||||
bool RemoteGameServerHost::prepareLocation(const LocationConfig& config)
|
||||
{
|
||||
return client_.replica_.prepareLocation(config);
|
||||
}
|
||||
|
||||
void RemoteGameServerHost::setupRuntimeLocation(
|
||||
const std::string& id, const std::shared_ptr<Location>& location,
|
||||
LocationSetup params, AudioPlayerAsync* audioPlayer)
|
||||
{
|
||||
auto stored = client_.replica_.state_.locations.find(id);
|
||||
if (stored == client_.replica_.state_.locations.end())
|
||||
throw std::runtime_error("Remote location state was not prepared");
|
||||
params.definition = stored->second.definition;
|
||||
params.gameState = &stored->second;
|
||||
location->setup(params, audioPlayer);
|
||||
}
|
||||
|
||||
Location* RemoteGameServerHost::runtimeLocation(const std::string& id) const
|
||||
{
|
||||
auto found = runtimeLocations_.find(id);
|
||||
return found == runtimeLocations_.end() ? nullptr : found->second.get();
|
||||
}
|
||||
|
||||
bool RemoteGameServerHost::hasRuntimeLocation(const std::string& id) const
|
||||
{
|
||||
return runtimeLocations_.count(id) > 0;
|
||||
}
|
||||
|
||||
std::vector<std::string> RemoteGameServerHost::loadedRuntimeLocationIds() const
|
||||
{
|
||||
std::vector<std::string> result;
|
||||
result.reserve(runtimeLocations_.size());
|
||||
for (const auto& [id, location] : runtimeLocations_) result.push_back(id);
|
||||
return result;
|
||||
}
|
||||
|
||||
void RemoteGameServerHost::clearRuntimeLocations(bool)
|
||||
{
|
||||
runtimeLocations_.clear();
|
||||
}
|
||||
|
||||
void RemoteGameServerHost::bindRuntimeLocation(
|
||||
const std::string& id, std::shared_ptr<Location> location)
|
||||
{
|
||||
runtimeLocations_[id] = std::move(location);
|
||||
auto stored = client_.replica_.state_.locations.find(id);
|
||||
if (stored != client_.replica_.state_.locations.end() &&
|
||||
stored->second.hasPersistentState) {
|
||||
runtimeLocations_[id]->applyAuxiliaryState(stored->second);
|
||||
}
|
||||
}
|
||||
|
||||
void RemoteGameServerHost::update(std::int64_t deltaMs)
|
||||
{
|
||||
client_.update(deltaMs);
|
||||
const auto state = client_.snapshot();
|
||||
if (!state || state->currentLocationId.empty()) return;
|
||||
auto runtime = runtimeLocations_.find(state->currentLocationId);
|
||||
if (runtime == runtimeLocations_.end() || !runtime->second) return;
|
||||
runtime->second->update(deltaMs, LocationSimulationEvents{});
|
||||
}
|
||||
|
||||
void RemoteGameServerHost::save(nlohmann::json&)
|
||||
{
|
||||
// Persistence remains server-side.
|
||||
}
|
||||
|
||||
void RemoteGameServerHost::load(const nlohmann::json&)
|
||||
{
|
||||
// Persistence remains server-side.
|
||||
}
|
||||
|
||||
} // namespace FRG
|
||||
Reference in new issue
Block a user