From 08660d83dc47f872ad7a67ad73b2798c390030c9 Mon Sep 17 00:00:00 2001 From: Anonymous-275 Date: Wed, 31 Mar 2021 19:18:32 +0300 Subject: [PATCH] Unicycle bypass vehicle limit --- include/Client.h | 3 +++ include/TServer.h | 1 + src/TServer.cpp | 34 +++++++++++++++++++++++++++++----- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/include/Client.h b/include/Client.h index ad55b42..8f3febc 100644 --- a/include/Client.h +++ b/include/Client.h @@ -44,12 +44,14 @@ public: [[nodiscard]] SOCKET GetTCPSock() const { return mSocket[0]; } [[nodiscard]] std::string GetRoles() const { return mRole; } [[nodiscard]] std::string GetName() const { return mName; } + void SetUnicycleID(int ID) { mUnicycleID = ID; } void SetID(int ID) { mID = ID; } [[nodiscard]] int GetOpenCarID() const; [[nodiscard]] int GetCarCount() const; void ClearCars(); [[nodiscard]] int GetStatus() const { return mStatus; } [[nodiscard]] int GetID() const { return mID; } + [[nodiscard]] int GetUnicycleID() const { return mUnicycleID; } [[nodiscard]] bool IsConnected() const { return mIsConnected; } [[nodiscard]] bool IsSynced() const { return mIsSynced; } [[nodiscard]] bool IsSyncing() const { return mIsSyncing; } @@ -83,6 +85,7 @@ private: std::string mName = "Unknown Client"; SOCKET mSocket[2] { SOCKET(-1) }; sockaddr_in mUDPAddress {}; // is this initialization OK? yes it is + int mUnicycleID = -1; std::string mRole; std::string mDID; int mStatus = 0; diff --git a/include/TServer.h b/include/TServer.h index 426ec33..8c5747d 100644 --- a/include/TServer.h +++ b/include/TServer.h @@ -31,5 +31,6 @@ private: TClientSet mClients; 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 void Apply(TClient& c, int VID, const std::string& pckt); }; diff --git a/src/TServer.cpp b/src/TServer.cpp index a25ccf5..64be37e 100644 --- a/src/TServer.cpp +++ b/src/TServer.cpp @@ -165,6 +165,24 @@ void TServer::HandleEvent(TClient& c, const std::string& Data) { } } +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") { + c.SetUnicycleID(ID); + return true; + } + + return c.GetCarCount() < Application::Settings.MaxCars; +} + void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Network) { if (Pckt.length() < 4) return; @@ -181,9 +199,15 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ if (Data.at(0) == '0') { int CarID = c.GetOpenCarID(); debug(c.GetName() + (" created a car with ID ") + std::to_string(CarID)); - Packet = "Os:" + c.GetRoles() + ":" + c.GetName() + ":" + std::to_string(c.GetID()) + "-" + std::to_string(CarID) + Packet.substr(4); + + std::string CarJson = Packet.substr(5); + Packet = "Os:" + c.GetRoles() + ":" + c.GetName() + ":" + std::to_string(c.GetID()) + "-" + std::to_string(CarID) + ":" + CarJson; auto Res = TriggerLuaEvent(("onVehicleSpawn"), false, nullptr, std::make_unique(TLuaArg { { c.GetID(), CarID, Packet.substr(3) } }), true); - if (c.GetCarCount() >= Application::Settings.MaxCars || std::any_cast(Res)) { + + if (ShouldSpawn(c, CarJson, CarID) && std::any_cast(Res) == 0) { + c.AddNewCar(CarID, Packet); + Network.SendToAll(nullptr, Packet, true, true); + } else { if (!Network.Respond(c, Packet, true)) { // TODO: handle } @@ -192,9 +216,6 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ // TODO: handle } debug(c.GetName() + (" (force : car limit/lua) removed ID ") + std::to_string(CarID)); - } else { - c.AddNewCar(CarID, Packet); - Network.SendToAll(nullptr, Packet, true, true); } } return; @@ -209,6 +230,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); + } auto Res = TriggerLuaEvent(("onVehicleEdited"), false, nullptr, std::make_unique(TLuaArg { { c.GetID(), VID, Packet.substr(3) } }), true);