Handle world creation error when sending world messages, add active world callbacks

This commit is contained in:
Kae 2023-07-24 18:42:55 +10:00
parent a2d901bd66
commit af31bde032
8 changed files with 48 additions and 18 deletions

View File

@ -1,3 +1,5 @@
-- Small helper to organize code for the same context into different Lua scripts without having to "hook" previously defined.
modules = setmetatable({}, {__call = function(this, path, names)
for i, name in pairs(names) do
require(path .. name .. ".lua")

View File

@ -0,0 +1,12 @@
local module = {}
modules.messages = module
function module.init()
message.setHandler("keepAlive", function(_, _, time)
return world.setExpiryTime(tonumber(time) or 0)
end)
end
function module.update()
end

View File

@ -1,12 +0,0 @@
local module = {}
modules.test = module
function module.init()
message.setHandler("type", function()
return world.type()
end)
end
function module.update()
end

View File

@ -1,2 +1,2 @@
require "/scripts/opensb/util/modules.lua"
modules("/scripts/opensb/worldserver/", {"test"})
modules("/scripts/opensb/worldserver/", {"messages"})

View File

@ -151,6 +151,11 @@ List<WorldId> UniverseServer::activeWorlds() const {
return m_worlds.keys();
}
bool UniverseServer::isWorldActive(WorldId const& worldId) const {
RecursiveMutexLocker locker(m_mainLock);
return m_worlds.contains(worldId);
}
List<ConnectionId> UniverseServer::clientIds() const {
ReadLocker clientsLocker(m_clientsLock);
return m_clients.keys();
@ -1045,8 +1050,14 @@ void UniverseServer::handleWorldMessages() {
auto it = m_pendingWorldMessages.begin();
while (it != m_pendingWorldMessages.end()) {
auto& worldId = it->first;
if (auto worldPtr = triggerWorldCreation(worldId).value()) {
worldPtr->passMessages(move(it->second));
if (auto worldResult = triggerWorldCreation(worldId)) {
auto& world = *worldResult;
if (world)
world->passMessages(move(it->second));
else for (auto& message : it->second)
message.promise.fail("Error creating world");
it = m_pendingWorldMessages.erase(it);
}
else

View File

@ -51,6 +51,7 @@ public:
void setPause(bool pause);
List<WorldId> activeWorlds() const;
bool isWorldActive(WorldId const& worldId) const;
List<ConnectionId> clientIds() const;
size_t numberOfClients() const;

View File

@ -18,7 +18,9 @@ LuaCallbacks LuaBindings::makeUniverseServerCallbacks(UniverseServer* universe)
callbacks.registerCallbackWithSignature<bool, ConnectionId>("isAdmin", bind(UniverseServerCallbacks::isAdmin, universe, _1));
callbacks.registerCallbackWithSignature<bool, ConnectionId>("isPvp", bind(UniverseServerCallbacks::isPvp, universe, _1));
callbacks.registerCallbackWithSignature<void, ConnectionId, bool>("setPvp", bind(UniverseServerCallbacks::setPvp, universe, _1, _2));
callbacks.registerCallbackWithSignature<RpcThreadPromise<Json>, LuaEngine&, String, String, LuaVariadic<Json>>("sendWorldMessage", bind(UniverseServerCallbacks::sendWorldMessage, universe, _1, _2, _3, _4));
callbacks.registerCallbackWithSignature<bool, String>("isWorldActive", bind(UniverseServerCallbacks::isWorldActive, universe, _1));
callbacks.registerCallbackWithSignature<bool, StringList>("activeWorlds", bind(UniverseServerCallbacks::activeWorlds, universe));
callbacks.registerCallbackWithSignature<RpcThreadPromise<Json>, String, String, LuaVariadic<Json>>("sendWorldMessage", bind(UniverseServerCallbacks::sendWorldMessage, universe, _1, _2, _3));
return callbacks;
}
@ -108,7 +110,19 @@ void LuaBindings::UniverseServerCallbacks::setPvp(UniverseServer* universe, Conn
universe->setPvp(client, setPvpTo);
}
RpcThreadPromise<Json> LuaBindings::UniverseServerCallbacks::sendWorldMessage(UniverseServer* universe, LuaEngine& engine, String const& worldId, String const& message, LuaVariadic<Json> args) {
bool LuaBindings::UniverseServerCallbacks::isWorldActive(UniverseServer* universe, String const& worldId) {
return universe->isWorldActive(parseWorldId(worldId));
}
StringList LuaBindings::UniverseServerCallbacks::activeWorlds(UniverseServer* universe) {
StringList worlds;
for (WorldId& world : universe->activeWorlds())
worlds.append(printWorldId(world));
return worlds;
}
RpcThreadPromise<Json> LuaBindings::UniverseServerCallbacks::sendWorldMessage(UniverseServer* universe, String const& worldId, String const& message, LuaVariadic<Json> args) {
return universe->sendWorldMessage(parseWorldId(worldId), message, JsonArray::from(move(args)));
}

View File

@ -23,7 +23,9 @@ namespace LuaBindings {
bool isAdmin(UniverseServer* universe, ConnectionId arg1);
bool isPvp(UniverseServer* universe, ConnectionId arg1);
void setPvp(UniverseServer* universe, ConnectionId arg1, Maybe<bool> arg2);
RpcThreadPromise<Json> sendWorldMessage(UniverseServer* universe, LuaEngine& engine, String const& worldId, String const& message, LuaVariadic<Json> args);
bool isWorldActive(UniverseServer* universe, String const& worldId);
StringList activeWorlds(UniverseServer* universe);
RpcThreadPromise<Json> sendWorldMessage(UniverseServer* universe, String const& worldId, String const& message, LuaVariadic<Json> args);
}
}
}