diff --git a/src/BuddyManager.cpp b/src/BuddyManager.cpp index 525ec9e..35c9b28 100644 --- a/src/BuddyManager.cpp +++ b/src/BuddyManager.cpp @@ -203,6 +203,9 @@ void BuddyManager::reqAcceptBuddy(CNSocket* sock, CNPacketData* data) { 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; //std::cout << "Buddy's ID: " << plr->buddyIDs[slotB] << std::endl; + + // add record to db + Database::addBuddyship(plr->iID, otherPlr->iID); } else { @@ -282,6 +285,9 @@ void BuddyManager::reqFindNameBuddyAccept(CNSocket* sock, CNPacketData* data) { 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; //std::cout << "Buddy's ID: " << plr->buddyIDs[slotB] << std::endl; + + // add record to db + Database::addBuddyship(plr->iID, otherPlr->iID); } else { @@ -405,6 +411,9 @@ void BuddyManager::reqBuddyDelete(CNSocket* sock, CNPacketData* data) { plr->buddyIDs[resp.iBuddySlot] = 0; sock->sendPacket((void*)&resp, P_FE2CL_REP_REMOVE_BUDDY_SUCC, sizeof(sP_FE2CL_REP_REMOVE_BUDDY_SUCC)); + // remove record from db + Database::removeBuddyship(plr->PCStyle.iPC_UID, pkt->iBuddyPCUID); + // remove buddy on their side, reusing the struct CNSocket* otherSock = PlayerManager::getSockFromID(pkt->iBuddyPCUID); if (otherSock == nullptr) diff --git a/src/Database.cpp b/src/Database.cpp index b0d8c28..0741dd8 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -521,7 +521,7 @@ void Database::updatePlayer(Player *player) { updateInventory(player); updateNanos(player); updateQuests(player); - updateBuddies(player); + //updateBuddies(player); we add/remove buddies explicitly now } void Database::updateInventory(Player *player){ @@ -634,6 +634,7 @@ void Database::updateQuests(Player* player) { db.commit(); } +// note: do not use. explicitly add/remove records instead. void Database::updateBuddies(Player* player) { db.begin_transaction(); @@ -766,6 +767,37 @@ int Database::getNumBuddies(Player* player) { return buddies.size() > 50 ? 50 : buddies.size(); } +// buddies +void Database::addBuddyship(int playerA, int playerB) { + std::lock_guard lock(dbCrit); + + db.begin_transaction(); + + Buddyship record; + record.PlayerAId = playerA; + record.PlayerBId = playerB; + record.Status = 0; // blocking ??? + db.insert(record); + + db.commit(); +} + +void Database::removeBuddyship(int playerA, int playerB) { + std::lock_guard lock(dbCrit); + + db.begin_transaction(); + + db.remove_all( + where(c(&Buddyship::PlayerAId) == playerA && c(&Buddyship::PlayerBId) == playerB) + ); + + db.remove_all( // the pair could be in either position + where(c(&Buddyship::PlayerAId) == playerB && c(&Buddyship::PlayerBId) == playerA) + ); + + db.commit(); +} + // email int Database::getUnreadEmailCount(int playerID) { std::lock_guard lock(dbCrit); diff --git a/src/Database.hpp b/src/Database.hpp index 516c561..d58f3dc 100644 --- a/src/Database.hpp +++ b/src/Database.hpp @@ -162,6 +162,10 @@ namespace Database { void appendBlob(std::vector*blob, int64_t input); int64_t blobToInt64(std::vector::iterator it); + // buddies + void addBuddyship(int playerA, int playerB); + void removeBuddyship(int playerA, int playerB); + // email int getUnreadEmailCount(int playerID); std::vector getEmails(int playerID, int page);