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"
// Custom Classes
#include "MasterPackets.h"
#include "PacketUtils.h"
#include "dServer.h"
// C++
#include <algorithm>
#include <future>
// Static Variables
ZoneInstanceManager* ZoneInstanceManager::m_Address = nullptr;
//! 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();
request->requestID = ++currentRequestID;
request->callback = callback;
ZoneTransferRequest request{};
request.requestID = ++currentRequestID;
request.callback = callback;
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
@ -34,18 +29,15 @@ void ZoneInstanceManager::HandleRequestZoneTransferResponse(uint64_t requestID,
uint16_t serverPort = PacketUtils::ReadPacketU16(29, packet);
std::string serverIP = PacketUtils::ReadString(31, packet, false);
for (uint32_t i = 0; i < this->requests.size(); ++i) {
if (this->requests[i]->requestID == requestID) {
auto transferRequest = std::find(requests.begin(), requests.end(), [requestID](const ZoneTransferRequest& transfer) {
return requestID == transfer.requestID;
});
if (transferRequest == requests.end()) return;
// Call the request callback
this->requests[i]->callback(mythranShift, zoneID, zoneInstance, zoneClone, serverIP, serverPort);
delete this->requests[i];
this->requests.erase(this->requests.begin() + i);
return;
}
}
// Now begin the callback since we confirmed we have found the matching request.
transferRequest->callback(mythranShift, zoneID, zoneInstance, zoneClone, serverIP, serverPort);
requests.erase(transferRequest);
}
void ZoneInstanceManager::CreatePrivateZone(dServer* server, uint32_t zoneID, uint32_t zoneClone, const std::string& password) {
@ -56,12 +48,12 @@ void ZoneInstanceManager::RequestPrivateZone(
dServer* server,
bool mythranShift,
const std::string& password,
std::function<void(bool, uint32_t, uint32_t, uint32_t, std::string, uint16_t)> callback) {
ZoneTransferRequest* request = new ZoneTransferRequest();
request->requestID = ++currentRequestID;
request->callback = callback;
ZoneTransferCallback callback) {
ZoneTransferRequest request;
request.requestID = ++currentRequestID;
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
// C++
#define _VARIADIC_MAX 10
#include <functional>
#include <vector>
#include <string>
// RakNet
#include <RakNetTypes.h>
#include "RakNetTypes.h"
class dServer;
using ZoneTransferCallback = std::function<void(bool, uint32_t, uint32_t, uint32_t, std::string, uint16_t)>;
/*!
\file ZoneInstanceManager.hpp
\brief A class for handling zone transfers and zone-related functions
*/
//! The zone request
// The zone request
struct ZoneTransferRequest {
uint64_t requestID;
std::function<void(bool, uint32_t, uint32_t, uint32_t, std::string, uint16_t)> callback;
uint64_t requestID = 0;
ZoneTransferCallback callback;
};
//! The zone manager
class ZoneInstanceManager {
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
public:
//! The singleton method
static ZoneInstanceManager* Instance() {
if (m_Address == 0) {
m_Address = new ZoneInstanceManager;
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
/**
* @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 packet The packet
/**
* @param requestID The request ID
* @param packet The packet
*/
void HandleRequestZoneTransferResponse(uint64_t requestID, Packet* packet);
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);
};