mirror of
https://github.com/OpenFusionProject/OpenFusion.git
synced 2024-11-22 21:40:05 +00:00
Refactor buddy request-by-name
This commit is contained in:
parent
4454faffc3
commit
90754665a0
@ -133,20 +133,21 @@ void BuddyManager::reqBuddyByName(CNSocket* sock, CNPacketData* data) {
|
|||||||
|
|
||||||
INITSTRUCT(sP_FE2CL_REP_PC_FIND_NAME_MAKE_BUDDY_SUCC, resp);
|
INITSTRUCT(sP_FE2CL_REP_PC_FIND_NAME_MAKE_BUDDY_SUCC, resp);
|
||||||
|
|
||||||
CNSocket* otherSock = sock;
|
CNSocket* otherSock = nullptr;
|
||||||
|
|
||||||
int sizeOfRes = sizeof(pkt->szFirstName) / 9; // Maximum size of a player's first name
|
for (auto& pair : PlayerManager::players) {
|
||||||
int sizeOfLNRes = sizeof(pkt->szLastName) / 17; // Maximum size of a player's last name
|
Player* plr = pair.second.plr;
|
||||||
|
if (strcmp(U16toU8(plr->PCStyle.szFirstName).c_str(), U16toU8(pkt->szFirstName).c_str()) == 0
|
||||||
for (auto pair : PlayerManager::players) {
|
&& strcmp(U16toU8(plr->PCStyle.szLastName).c_str(), U16toU8(pkt->szLastName).c_str()) == 0
|
||||||
int sizeOfReq = sizeof(pair.second.plr->PCStyle.szFirstName) / 9;
|
&& !playerHasBuddyWithID(plrReq, plr->iID)) {
|
||||||
int sizeOfLNReq = sizeof(pair.second.plr->PCStyle.szLastName) / 17;
|
|
||||||
if (BuddyManager::NameCheck(pair.second.plr->PCStyle.szFirstName, pkt->szFirstName, sizeOfReq, sizeOfRes) == true && BuddyManager::NameCheck(pair.second.plr->PCStyle.szLastName, pkt->szLastName, sizeOfLNReq, sizeOfLNRes) == true) { // This long line of gorgeous parameters is to check if the player's name matches :eyes:
|
|
||||||
otherSock = pair.first;
|
otherSock = pair.first;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (otherSock == nullptr)
|
||||||
|
return; // no player found
|
||||||
|
|
||||||
resp.iPCUID = plrReq->PCStyle.iPC_UID;
|
resp.iPCUID = plrReq->PCStyle.iPC_UID;
|
||||||
resp.iNameCheckFlag = plrReq->PCStyle.iNameCheck;
|
resp.iNameCheckFlag = plrReq->PCStyle.iNameCheck;
|
||||||
|
|
||||||
@ -230,32 +231,23 @@ void BuddyManager::reqFindNameBuddyAccept(CNSocket* sock, CNPacketData* data) {
|
|||||||
|
|
||||||
sP_CL2FE_REQ_PC_FIND_NAME_ACCEPT_BUDDY* pkt = (sP_CL2FE_REQ_PC_FIND_NAME_ACCEPT_BUDDY*)data->buf;
|
sP_CL2FE_REQ_PC_FIND_NAME_ACCEPT_BUDDY* pkt = (sP_CL2FE_REQ_PC_FIND_NAME_ACCEPT_BUDDY*)data->buf;
|
||||||
|
|
||||||
Player* plr = PlayerManager::getPlayer(sock);
|
Player* plrReq = PlayerManager::getPlayer(sock);
|
||||||
|
|
||||||
INITSTRUCT(sP_FE2CL_REP_ACCEPT_MAKE_BUDDY_SUCC, resp);
|
INITSTRUCT(sP_FE2CL_REP_ACCEPT_MAKE_BUDDY_SUCC, resp);
|
||||||
|
|
||||||
CNSocket* otherSock = sock;
|
Player* otherPlr = PlayerManager::getPlayerFromID(pkt->iBuddyPCUID);
|
||||||
|
|
||||||
int sizeOfRes = sizeof(pkt->szFirstName) / 9;
|
if (otherPlr == nullptr)
|
||||||
int sizeOfLNRes = sizeof(pkt->szLastName) / 17;
|
return;
|
||||||
|
|
||||||
for (auto pair : PlayerManager::players) {
|
CNSocket* otherSock = PlayerManager::getSockFromID(pkt->iBuddyPCUID);
|
||||||
int sizeOfReq = sizeof(pair.second.plr->PCStyle.szFirstName) / 9;
|
|
||||||
int sizeOfLNReq = sizeof(pair.second.plr->PCStyle.szLastName) / 17;
|
|
||||||
if (BuddyManager::NameCheck(pair.second.plr->PCStyle.szFirstName, pkt->szFirstName, sizeOfReq, sizeOfRes) == true && BuddyManager::NameCheck(pair.second.plr->PCStyle.szLastName, pkt->szLastName, sizeOfLNReq, sizeOfLNRes) == true) {
|
|
||||||
otherSock = pair.first;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Player* otherPlr = PlayerManager::getPlayer(otherSock);
|
int slotA = getAvailableBuddySlot(plrReq);
|
||||||
|
|
||||||
int slotA = getAvailableBuddySlot(plr);
|
|
||||||
int slotB = getAvailableBuddySlot(otherPlr);
|
int slotB = getAvailableBuddySlot(otherPlr);
|
||||||
if (slotA == -1 || slotB == -1)
|
if (slotA == -1 || slotB == -1)
|
||||||
return; // sanity check
|
return; // sanity check
|
||||||
|
|
||||||
if (pkt->iAcceptFlag == 1 && plr->iID != otherPlr->iID && !playerHasBuddyWithID(plr, otherPlr->iID)) {
|
if (pkt->iAcceptFlag == 1 && plrReq->iID != otherPlr->iID && !playerHasBuddyWithID(plrReq, otherPlr->iID)) {
|
||||||
INITSTRUCT(sP_FE2CL_REP_ACCEPT_MAKE_BUDDY_SUCC, resp);
|
INITSTRUCT(sP_FE2CL_REP_ACCEPT_MAKE_BUDDY_SUCC, resp);
|
||||||
|
|
||||||
// A to B
|
// A to B
|
||||||
@ -270,26 +262,26 @@ void BuddyManager::reqFindNameBuddyAccept(CNSocket* sock, CNPacketData* data) {
|
|||||||
memcpy(resp.BuddyInfo.szFirstName, otherPlr->PCStyle.szFirstName, sizeof(resp.BuddyInfo.szFirstName));
|
memcpy(resp.BuddyInfo.szFirstName, otherPlr->PCStyle.szFirstName, sizeof(resp.BuddyInfo.szFirstName));
|
||||||
memcpy(resp.BuddyInfo.szLastName, otherPlr->PCStyle.szLastName, sizeof(resp.BuddyInfo.szLastName));
|
memcpy(resp.BuddyInfo.szLastName, otherPlr->PCStyle.szLastName, sizeof(resp.BuddyInfo.szLastName));
|
||||||
sock->sendPacket((void*)&resp, P_FE2CL_REP_ACCEPT_MAKE_BUDDY_SUCC, sizeof(sP_FE2CL_REP_ACCEPT_MAKE_BUDDY_SUCC));
|
sock->sendPacket((void*)&resp, P_FE2CL_REP_ACCEPT_MAKE_BUDDY_SUCC, sizeof(sP_FE2CL_REP_ACCEPT_MAKE_BUDDY_SUCC));
|
||||||
plr->buddyIDs[slotA] = otherPlr->PCStyle.iPC_UID;
|
plrReq->buddyIDs[slotA] = otherPlr->PCStyle.iPC_UID;
|
||||||
//std::cout << "Buddy's ID: " << plr->buddyIDs[slotA] << std::endl;
|
//std::cout << "Buddy's ID: " << plr->buddyIDs[slotA] << std::endl;
|
||||||
|
|
||||||
// B to A, using the same struct
|
// B to A, using the same struct
|
||||||
resp.iBuddySlot = slotB;
|
resp.iBuddySlot = slotB;
|
||||||
resp.BuddyInfo.iID = plr->iID;
|
resp.BuddyInfo.iID = plrReq->iID;
|
||||||
resp.BuddyInfo.iPCUID = plr->PCStyle.iPC_UID;
|
resp.BuddyInfo.iPCUID = plrReq->PCStyle.iPC_UID;
|
||||||
resp.BuddyInfo.iPCState = 1;
|
resp.BuddyInfo.iPCState = 1;
|
||||||
resp.BuddyInfo.bBlocked = 0;
|
resp.BuddyInfo.bBlocked = 0;
|
||||||
resp.BuddyInfo.iGender = plr->PCStyle.iGender;
|
resp.BuddyInfo.iGender = plrReq->PCStyle.iGender;
|
||||||
resp.BuddyInfo.bFreeChat = 1;
|
resp.BuddyInfo.bFreeChat = 1;
|
||||||
resp.BuddyInfo.iNameCheckFlag = plr->PCStyle.iNameCheck;
|
resp.BuddyInfo.iNameCheckFlag = plrReq->PCStyle.iNameCheck;
|
||||||
memcpy(resp.BuddyInfo.szFirstName, plr->PCStyle.szFirstName, sizeof(resp.BuddyInfo.szFirstName));
|
memcpy(resp.BuddyInfo.szFirstName, plrReq->PCStyle.szFirstName, sizeof(resp.BuddyInfo.szFirstName));
|
||||||
memcpy(resp.BuddyInfo.szLastName, plr->PCStyle.szLastName, sizeof(resp.BuddyInfo.szLastName));
|
memcpy(resp.BuddyInfo.szLastName, plrReq->PCStyle.szLastName, sizeof(resp.BuddyInfo.szLastName));
|
||||||
otherSock->sendPacket((void*)&resp, P_FE2CL_REP_ACCEPT_MAKE_BUDDY_SUCC, sizeof(sP_FE2CL_REP_ACCEPT_MAKE_BUDDY_SUCC));
|
otherSock->sendPacket((void*)&resp, P_FE2CL_REP_ACCEPT_MAKE_BUDDY_SUCC, sizeof(sP_FE2CL_REP_ACCEPT_MAKE_BUDDY_SUCC));
|
||||||
otherPlr->buddyIDs[slotB] = plr->PCStyle.iPC_UID;
|
otherPlr->buddyIDs[slotB] = plrReq->PCStyle.iPC_UID;
|
||||||
//std::cout << "Buddy's ID: " << plr->buddyIDs[slotB] << std::endl;
|
//std::cout << "Buddy's ID: " << plr->buddyIDs[slotB] << std::endl;
|
||||||
|
|
||||||
// add record to db
|
// add record to db
|
||||||
Database::addBuddyship(plr->iID, otherPlr->iID);
|
Database::addBuddyship(plrReq->iID, otherPlr->iID);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -707,25 +699,6 @@ int BuddyManager::getAvailableBuddySlot(Player* plr) {
|
|||||||
return slot;
|
return slot;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BuddyManager::NameCheck(char16_t reqName[], char16_t resName[], int sizeOfLNReq, int sizeOfLNRes) {
|
|
||||||
// If lengths of array are not equal means
|
|
||||||
// array are not equal
|
|
||||||
if (sizeOfLNReq != sizeOfLNRes)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// Sort both arrays
|
|
||||||
std::sort(reqName, reqName + sizeOfLNReq);
|
|
||||||
std::sort(resName, resName + sizeOfLNRes);
|
|
||||||
|
|
||||||
// Linearly compare elements
|
|
||||||
for (int i = 0; i < sizeOfLNReq; i++)
|
|
||||||
if (reqName[i] != resName[i])
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// If all elements were same.
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool BuddyManager::playerHasBuddyWithID(Player* plr, int buddyID) {
|
bool BuddyManager::playerHasBuddyWithID(Player* plr, int buddyID) {
|
||||||
for (int i = 0; i < 50; i++) {
|
for (int i = 0; i < 50; i++) {
|
||||||
if (plr->buddyIDs[i] == buddyID)
|
if (plr->buddyIDs[i] == buddyID)
|
||||||
|
@ -50,6 +50,5 @@ namespace BuddyManager {
|
|||||||
|
|
||||||
// Name checks
|
// Name checks
|
||||||
int getAvailableBuddySlot(Player* plr);
|
int getAvailableBuddySlot(Player* plr);
|
||||||
bool NameCheck(char16_t reqName[], char16_t resName[], int sizeOfReq, int sizeOfRes); // checks if the request and requested player's names match
|
|
||||||
bool playerHasBuddyWithID(Player* plr, int buddyID);
|
bool playerHasBuddyWithID(Player* plr, int buddyID);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user