This commit is contained in:
EmosewaMC 2023-07-14 21:43:00 -07:00
parent 9375c36c7b
commit 04a97e618b
2 changed files with 35 additions and 66 deletions

View File

@ -1,27 +1,22 @@
#define _VARIADIC_MAX 10
#include "ZoneInstanceManager.h" #include "ZoneInstanceManager.h"
// Custom Classes
#include "MasterPackets.h" #include "MasterPackets.h"
#include "PacketUtils.h" #include "PacketUtils.h"
#include "dServer.h" #include "dServer.h"
// C++ #include <algorithm>
#include <future> #include <future>
// Static Variables
ZoneInstanceManager* ZoneInstanceManager::m_Address = nullptr;
//! Requests a zone transfer //! Requests a zone transfer
void ZoneInstanceManager::RequestZoneTransfer(dServer* server, uint32_t zoneID, uint32_t zoneClone, bool mythranShift, std::function<void(bool, uint32_t, uint32_t, uint32_t, std::string, uint16_t)> callback) { void ZoneInstanceManager::RequestZoneTransfer(dServer* server, uint32_t zoneID, uint32_t zoneClone, bool mythranShift, ZoneTransferCallback callback) {
ZoneTransferRequest* request = new ZoneTransferRequest(); ZoneTransferRequest request{};
request->requestID = ++currentRequestID; request.requestID = ++currentRequestID;
request->callback = callback; request.callback = callback;
this->requests.push_back(request); this->requests.push_back(request);
MasterPackets::SendZoneTransferRequest(server, request->requestID, mythranShift, zoneID, zoneClone); MasterPackets::SendZoneTransferRequest(server, request.requestID, mythranShift, zoneID, zoneClone);
} }
//! Handles a zone transfer response //! Handles a zone transfer response
@ -34,18 +29,15 @@ void ZoneInstanceManager::HandleRequestZoneTransferResponse(uint64_t requestID,
uint16_t serverPort = PacketUtils::ReadPacketU16(29, packet); uint16_t serverPort = PacketUtils::ReadPacketU16(29, packet);
std::string serverIP = PacketUtils::ReadString(31, packet, false); std::string serverIP = PacketUtils::ReadString(31, packet, false);
for (uint32_t i = 0; i < this->requests.size(); ++i) { auto transferRequest = std::find(requests.begin(), requests.end(), [requestID](const ZoneTransferRequest& transfer) {
if (this->requests[i]->requestID == requestID) { return requestID == transfer.requestID;
});
if (transferRequest == requests.end()) return;
// Call the request callback // Now begin the callback since we confirmed we have found the matching request.
this->requests[i]->callback(mythranShift, zoneID, zoneInstance, zoneClone, serverIP, serverPort); transferRequest->callback(mythranShift, zoneID, zoneInstance, zoneClone, serverIP, serverPort);
delete this->requests[i];
this->requests.erase(this->requests.begin() + i);
return;
}
}
requests.erase(transferRequest);
} }
void ZoneInstanceManager::CreatePrivateZone(dServer* server, uint32_t zoneID, uint32_t zoneClone, const std::string& password) { void ZoneInstanceManager::CreatePrivateZone(dServer* server, uint32_t zoneID, uint32_t zoneClone, const std::string& password) {
@ -56,12 +48,12 @@ void ZoneInstanceManager::RequestPrivateZone(
dServer* server, dServer* server,
bool mythranShift, bool mythranShift,
const std::string& password, const std::string& password,
std::function<void(bool, uint32_t, uint32_t, uint32_t, std::string, uint16_t)> callback) { ZoneTransferCallback callback) {
ZoneTransferRequest* request = new ZoneTransferRequest(); ZoneTransferRequest request;
request->requestID = ++currentRequestID; request.requestID = ++currentRequestID;
request->callback = callback; request.callback = callback;
this->requests.push_back(request); requests.push_back(request);
MasterPackets::SendZoneRequestPrivate(server, request->requestID, mythranShift, password); MasterPackets::SendZoneRequestPrivate(server, request.requestID, mythranShift, password);
} }

View File

@ -1,65 +1,42 @@
#pragma once #pragma once
// C++
#define _VARIADIC_MAX 10
#include <functional> #include <functional>
#include <vector> #include <vector>
#include <string> #include <string>
// RakNet #include "RakNetTypes.h"
#include <RakNetTypes.h>
class dServer; class dServer;
using ZoneTransferCallback = std::function<void(bool, uint32_t, uint32_t, uint32_t, std::string, uint16_t)>;
/*! // The zone request
\file ZoneInstanceManager.hpp
\brief A class for handling zone transfers and zone-related functions
*/
//! The zone request
struct ZoneTransferRequest { struct ZoneTransferRequest {
uint64_t requestID; uint64_t requestID = 0;
std::function<void(bool, uint32_t, uint32_t, uint32_t, std::string, uint16_t)> callback; ZoneTransferCallback callback;
}; };
//! The zone manager //! The zone manager
class ZoneInstanceManager { class ZoneInstanceManager {
private: private:
static ZoneInstanceManager* m_Address; //!< The singleton instance std::vector<ZoneTransferRequest> requests; //!< The zone transfer requests
std::vector<ZoneTransferRequest*> requests; //!< The zone transfer requests
uint64_t currentRequestID; //!< The current request ID uint64_t currentRequestID; //!< The current request ID
public: public:
/**
//! The singleton method * @param zoneID The zone ID
static ZoneInstanceManager* Instance() { * @param zoneClone The zone clone
if (m_Address == 0) { * @param mythranShift Whether or not this is a mythran shift
m_Address = new ZoneInstanceManager; * @param callback The callback function
m_Address->currentRequestID = 0;
}
return m_Address;
}
//! Requests a zone transfer
/*!
\param zoneID The zone ID
\param zoneClone The zone clone
\param mythranShift Whether or not this is a mythran shift
\param callback The callback function
*/ */
void RequestZoneTransfer(dServer* server, uint32_t zoneID, uint32_t zoneClone, bool mythranShift, std::function<void(bool, uint32_t, uint32_t, uint32_t, std::string, uint16_t)> callback); void RequestZoneTransfer(dServer* server, uint32_t zoneID, uint32_t zoneClone, bool mythranShift, ZoneTransferCallback callback);
//! Handles a zone transfer response /**
/*! * @param requestID The request ID
\param requestID The request ID * @param packet The packet
\param packet The packet
*/ */
void HandleRequestZoneTransferResponse(uint64_t requestID, Packet* packet); void HandleRequestZoneTransferResponse(uint64_t requestID, Packet* packet);
void CreatePrivateZone(dServer* server, uint32_t zoneID, uint32_t zoneClone, const std::string& password); void CreatePrivateZone(dServer* server, uint32_t zoneID, uint32_t zoneClone, const std::string& password);
void RequestPrivateZone(dServer* server, bool mythranShift, const std::string& password, std::function<void(bool, uint32_t, uint32_t, uint32_t, std::string, uint16_t)> callback); void RequestPrivateZone(dServer* server, bool mythranShift, const std::string& password, ZoneTransferCallback callback);
}; };