From 15f7a6ba8525bf65805b4e62d040cdc162830312 Mon Sep 17 00:00:00 2001 From: Anonymous-275 Date: Wed, 31 Mar 2021 19:49:52 +0300 Subject: [PATCH] Unicycle edit check --- include/TServer.h | 1 + src/TServer.cpp | 27 +++++++++++++++++---------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/include/TServer.h b/include/TServer.h index 8c5747d..472bfce 100644 --- a/include/TServer.h +++ b/include/TServer.h @@ -32,5 +32,6 @@ private: mutable RWMutex mClientsMutex; static void ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Network); static bool ShouldSpawn(TClient& c, const std::string& CarJson, int ID); + static bool IsUnicycle(TClient& c, const std::string& CarJson); static void Apply(TClient& c, int VID, const std::string& pckt); }; diff --git a/src/TServer.cpp b/src/TServer.cpp index d782f6e..b93fc0a 100644 --- a/src/TServer.cpp +++ b/src/TServer.cpp @@ -164,18 +164,23 @@ void TServer::HandleEvent(TClient& c, const std::string& Data) { a++; } } - +bool TServer::IsUnicycle(TClient &c, const std::string &CarJson) { + rapidjson::Document Car; + Car.Parse(CarJson.c_str(), CarJson.size()); + if(Car.HasParseError()){ + error("Failed to parse vehicle data -> " + CarJson); + } else if (Car["jbm"].IsString() && std::string(Car["jbm"].GetString()) == "unicycle") { + return true; + } + return false; +} bool TServer::ShouldSpawn(TClient& c, const std::string& CarJson, int ID) { if(c.GetUnicycleID() > -1 && (c.GetCarCount() - 1) < Application::Settings.MaxCars){ return true; } - rapidjson::Document Car; - Car.Parse(CarJson.c_str(), CarJson.size()); - if(Car.HasParseError()){ - error("Failed to parse vehicle data -> " + CarJson); - } else if (Car["jbm"].IsString() && std::string(Car["jbm"].GetString()) == "unicycle") { + if(IsUnicycle(c,CarJson)){ c.SetUnicycleID(ID); return true; } @@ -230,13 +235,12 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ VID = stoi(vid); } if (PID != -1 && VID != -1 && PID == c.GetID()) { - if(c.GetUnicycleID() == VID){ - c.SetUnicycleID(-1); - } auto Res = TriggerLuaEvent(("onVehicleEdited"), false, nullptr, std::make_unique(TLuaArg { { c.GetID(), VID, Packet.substr(3) } }), true); - if (!std::any_cast(Res)) { + + if ((c.GetUnicycleID() != VID || IsUnicycle(c,Packet.substr(Packet.find('{')))) + && std::any_cast(Res) == 0) { Network.SendToAll(&c, Packet, false, true); Apply(c, VID, Packet); } else { @@ -259,6 +263,9 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ VID = stoi(vid); } if (PID != -1 && VID != -1 && PID == c.GetID()) { + if(c.GetUnicycleID() == VID){ + c.SetUnicycleID(-1); + } Network.SendToAll(nullptr, Packet, true, true); TriggerLuaEvent(("onVehicleDeleted"), false, nullptr, std::make_unique(TLuaArg { { c.GetID(), VID } }), false);