Merge pull request #44 from ErodeesFleurs/sendPacket

Improving universe.sendPacket
This commit is contained in:
Kae 2024-03-19 21:01:53 +11:00 committed by GitHub
commit 58a346e563
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 12 deletions

View File

@ -167,17 +167,20 @@ PacketPtr createPacket(PacketType type) {
} }
} }
PacketPtr createPacket(PacketType type, JsonArray const& args) { PacketPtr createPacket(PacketType type, Maybe<Json> const& args) {
if (!args)
return createPacket(type);
switch (type) { switch (type) {
case PacketType::Pause: return make_shared<PausePacket>(args[0].toBool()); case PacketType::Pause: return make_shared<PausePacket>(args->getBool("pause"), args->getFloat("timescale", 1.0f));
case PacketType::ServerInfo: return make_shared<ServerInfoPacket>(args[0].toUInt(), args[1].toUInt()); case PacketType::ServerInfo: return make_shared<ServerInfoPacket>(args->getUInt("players"), args->getUInt("maxPlayers"));
case PacketType::GiveItem: return make_shared<GiveItemPacket>(ItemDescriptor(args[0])); case PacketType::GiveItem: return make_shared<GiveItemPacket>(ItemDescriptor(args->getObject("ItemDescriptor")));
case PacketType::UpdateTileProtection: return make_shared<UpdateTileProtectionPacket>(args[0].toUInt(), args[1].toBool()); case PacketType::UpdateTileProtection: return make_shared<UpdateTileProtectionPacket>(args->getUInt("dungeonId"), args->getBool("protected"));
case PacketType::SetDungeonGravity: return make_shared<SetDungeonGravityPacket>(args[0].toUInt(), args[0].toFloat()); case PacketType::SetDungeonGravity: return make_shared<SetDungeonGravityPacket>(args->getUInt("dungeonId"), args->getFloat("gravity"));
case PacketType::SetDungeonBreathable: return make_shared<SetDungeonBreathablePacket>(args[0].toUInt(), args[0].toBool()); case PacketType::SetDungeonBreathable: return make_shared<SetDungeonBreathablePacket>(args->getUInt("dungeonId"), args->getBool("breathable"));
case PacketType::SetPlayerStart: return make_shared<SetPlayerStartPacket>(Vec2F{args[0].toArray()[0].toFloat(), args[0].toArray()[0].toFloat()}, args[1].toBool()); case PacketType::SetPlayerStart: return make_shared<SetPlayerStartPacket>(Vec2F{args->getArray("position")[0].toFloat(), args->getArray("position")[1].toFloat()}, args->getBool("respawnInWorld"));
case PacketType::EntityMessage: return make_shared<EntityMessagePacket>(EntityId(args[0].toUInt()), args[1].toString(), args[2].toArray(), Uuid(args[3].toString())); case PacketType::EntityMessage: return make_shared<EntityMessagePacket>(EntityId(args->getInt("entityId")), args->getString("message"), args->get("JsonArray").toArray(), Uuid(args->getString("Uuid")));
case PacketType::UpdateWorldProperties: return make_shared<UpdateWorldPropertiesPacket>(args[0].toObject()); case PacketType::UpdateWorldProperties: return make_shared<UpdateWorldPropertiesPacket>(args->getObject("updatedProperties"));
default: default:
throw StarPacketException(strf("Unrecognized packet type {}", (unsigned int)type)); throw StarPacketException(strf("Unrecognized packet type {}", (unsigned int)type));
} }

View File

@ -139,7 +139,7 @@ struct Packet {
}; };
PacketPtr createPacket(PacketType type); PacketPtr createPacket(PacketType type);
PacketPtr createPacket(PacketType type, JsonArray const& args); PacketPtr createPacket(PacketType type, Maybe<Json> const& args);
template <PacketType PacketT> template <PacketType PacketT>
struct PacketBase : public Packet { struct PacketBase : public Packet {

View File

@ -129,7 +129,7 @@ RpcThreadPromise<Json> LuaBindings::UniverseServerCallbacks::sendWorldMessage(Un
void LuaBindings::UniverseServerCallbacks::sendPacket(UniverseServer* universe, ConnectionId clientId, String const& packetTypeName, Json const& args) { void LuaBindings::UniverseServerCallbacks::sendPacket(UniverseServer* universe, ConnectionId clientId, String const& packetTypeName, Json const& args) {
auto packetType = PacketTypeNames.getLeft(packetTypeName); auto packetType = PacketTypeNames.getLeft(packetTypeName);
auto packet = createPacket(packetType, args.toArray()); auto packet = createPacket(packetType, args);
universe->sendPacket(clientId, packet); universe->sendPacket(clientId, packet);
} }