Skip to content

Commit

Permalink
fix: fixed build
Browse files Browse the repository at this point in the history
  • Loading branch information
engsr6982 committed Oct 8, 2024
1 parent 1f69b66 commit bc91450
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 59 deletions.
60 changes: 15 additions & 45 deletions src/tpa/core/TpaRequestPool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ void TpaRequestPool::_initTask() {
});
thread.detach();
}
void TpaRequestPool::_initReceiver(const string& receiver) {
if (mPool.find(receiver) == mPool.end()) {
mPool.emplace(string(receiver), std::unordered_map<string, std::unique_ptr<TpaRequest>>());
}
}


TpaRequestPool& TpaRequestPool::getInstance() {
Expand All @@ -67,17 +62,10 @@ TpaRequestPool& TpaRequestPool::getInstance() {


bool TpaRequestPool::hasRequest(const string& receiver, const string& sender) const {
auto senderPool = mPool.find(receiver); // receiver => sender => request
if (senderPool == mPool.end()) {
return false;
if (mPool.contains(receiver)) {
return mPool.at(receiver).contains(sender); // receiver => sender => request
}

auto request = senderPool->second.find(sender);
if (request == senderPool->second.end()) {
return false;
}

return true;
return false;
}

bool TpaRequestPool::addRequest(std::unique_ptr<TpaRequest> request) {
Expand All @@ -86,7 +74,10 @@ bool TpaRequestPool::addRequest(std::unique_ptr<TpaRequest> request) {
string const& receiver = request->getReceiver();
string const& sender = request->getSender();

_initReceiver(receiver); // 初始化接收者
// 初始化接收者池
if (!mPool.contains(receiver)) {
mPool[string(receiver)] = std::unordered_map<string, std::unique_ptr<TpaRequest>>{};
}

if (hasRequest(receiver, sender)) {
deleteRequest(receiver, sender); // 同玩家重复请求,删除旧请求
Expand All @@ -98,35 +89,22 @@ bool TpaRequestPool::addRequest(std::unique_ptr<TpaRequest> request) {
return false; // 接收者池不存在
}

senderPool->second.emplace(string(sender), request);
senderPool->second.emplace(string(sender), std::move(request));
return true;
}

bool TpaRequestPool::deleteRequest(const string& receiver, const string& sender) {
auto senderPool = mPool.find(receiver); // receiver => sender => request

if (senderPool == mPool.end()) {
return false;
}

auto request = senderPool->second.find(sender); // sender => request
if (request == senderPool->second.end()) {
return false;
if (mPool.contains(receiver)) {
auto& senders = mPool.at(receiver);
if (senders.contains(sender)) {
senders.erase(sender);
return true;
}
}

senderPool->second.erase(request); // 删除请求
return true;
return false;
}


std::vector<string> TpaRequestPool::getReceiverList() const {
std::vector<string> receiverList{};
for (const auto& [receiver, _] : mPool) {
receiverList.push_back(receiver);
}
return receiverList;
}

std::vector<string> TpaRequestPool::getSenderList(const string& receiver) const {
auto senderPool = mPool.find(receiver); // receiver => sender => request
if (senderPool == mPool.end()) {
Expand All @@ -153,14 +131,6 @@ TpaRequest* TpaRequestPool::getRequest(const string& receiver, const string& sen
return request->second.get();
}

std::unordered_map<string, std::unique_ptr<TpaRequest>>* TpaRequestPool::getSenderPool(const string& receiver) {
auto senderPool = mPool.find(receiver); // receiver => sender => request
if (senderPool == mPool.end()) {
return nullptr;
}
return &senderPool->second;
}

TpaRequest* TpaRequestPool::makeRequest(Player& sender, Player& receiver, TpaType type) {
addRequest(std::make_unique<TpaRequest>(sender, receiver, type));
return getRequest(receiver.getRealName(), sender.getRealName());
Expand Down
22 changes: 8 additions & 14 deletions src/tpa/core/TpaRequestPool.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "TpaRequest.h"
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
Expand All @@ -12,33 +13,26 @@ namespace tps::tpa {

class TpaRequestPool {
public:
// 接收者 => 发起者池 => 发起者 => 请求实例
// Query: 接收者 => 发起者池 => 发起者 => 请求实例
std::unordered_map<string, std::unordered_map<string, std::unique_ptr<TpaRequest>>> mPool;

TpaRequestPool() = default;
TpaRequestPool(const TpaRequestPool&) = delete;
TpaRequestPool& operator=(const TpaRequestPool&) = delete;

static TpaRequestPool& getInstance();

bool hasRequest(const string& receiver, const string& sender) const;

bool addRequest(std::unique_ptr<TpaRequest> request); // 添加请求实例 (需移交所有权)

bool deleteRequest(const string& receiver, const string& sender);

std::vector<string> getReceiverList() const; // 获取所有接收者
static TpaRequestPool& getInstance();

std::vector<string> getSenderList(const string& receiver) const; // 获取所有发起者
TpaRequest* makeRequest(Player& sender, Player& receiver, TpaType type); // 创建请求实例

bool hasRequest(const string& receiver, const string& sender) const;
bool addRequest(std::unique_ptr<TpaRequest> request); // 添加请求实例 (需移交所有权)
bool deleteRequest(const string& receiver, const string& sender);
TpaRequest* getRequest(const string& receiver, const string& sender) const; // 获取请求实例

std::unordered_map<string, std::unique_ptr<TpaRequest>>* getSenderPool(const string& receiver); // 获取发起者池

TpaRequest* makeRequest(Player& sender, Player& receiver, TpaType type); // 创建请求实例
std::vector<string> getSenderList(const string& receiver) const; // 获取所有发起者

// private:
void _initReceiver(const string& receiver);
void _initTask();
};

Expand Down

0 comments on commit bc91450

Please sign in to comment.