Advertisement
Koridev

Untitled

Apr 24th, 2020
550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.57 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "constants.h"
  3. #include "gm.h"
  4. #include "messenger_manager.h"
  5. #include "buffer_manager.h"
  6. #include "desc_client.h"
  7. #include "log.h"
  8. #include "config.h"
  9. #include "p2p.h"
  10. #include "crc32.h"
  11. #include "char.h"
  12. #include "char_manager.h"
  13. #include "questmanager.h"
  14. static char __account[CHARACTER_NAME_MAX_LEN * 2 + 1];
  15. static char __companion[CHARACTER_NAME_MAX_LEN * 2 + 1];
  16. MessengerManager::MessengerManager()
  17. {
  18. }
  19.  
  20. MessengerManager::~MessengerManager()
  21. {
  22. }
  23.  
  24. void MessengerManager::Initialize()
  25. {
  26. }
  27.  
  28. void MessengerManager::Destroy()
  29. {
  30. }
  31.  
  32. void MessengerManager::P2PLogin(MessengerManager::keyA account)
  33. {
  34.     Login(account);
  35. }
  36.  
  37. void MessengerManager::P2PLogout(MessengerManager::keyA account)
  38. {
  39.     Logout(account);
  40. }
  41.  
  42. void MessengerManager::Login(MessengerManager::keyA account)
  43. {
  44.     if (m_set_loginAccount.find(account) != m_set_loginAccount.end())
  45.         return;
  46.  
  47.     DBManager::instance().EscapeString(__account, sizeof(__account), account.c_str(), account.size());
  48.     if (account.compare(__account))
  49.         return;
  50.  
  51.  
  52.     DBManager::instance().FuncQuery(std::bind1st(std::mem_fun(&MessengerManager::LoadList), this),
  53.         "SELECT account, companion FROM messenger_list%s WHERE account='%s'", get_table_postfix(), __account);
  54.  
  55.     m_set_loginAccount.insert(account);
  56. }
  57.  
  58. void MessengerManager::LoadList(SQLMsg * msg)
  59. {
  60.     if (NULL == msg)
  61.         return;
  62.  
  63.     if (NULL == msg->Get())
  64.         return;
  65.  
  66.     if (msg->Get()->uiNumRows == 0)
  67.         return;
  68.  
  69.     std::string account;
  70.  
  71.     sys_log(1, "Messenger::LoadList");
  72.  
  73.     for (uint i = 0; i < msg->Get()->uiNumRows; ++i)
  74.     {
  75.         MYSQL_ROW row = mysql_fetch_row(msg->Get()->pSQLResult);
  76.  
  77.         if (row[0] && row[1])
  78.         {
  79.             if (account.length() == 0)
  80.                 account = row[0];
  81.  
  82.             m_Relation[row[0]].insert(row[1]);
  83.             m_InverseRelation[row[1]].insert(row[0]);
  84.         }
  85.     }
  86.  
  87.     SendList(account);
  88.  
  89.     std::set<MessengerManager::keyT>::iterator it;
  90.  
  91.     for (it = m_InverseRelation[account].begin(); it != m_InverseRelation[account].end(); ++it)
  92.         SendLogin(*it, account);
  93. }
  94.  
  95. void MessengerManager::Logout(MessengerManager::keyA account)
  96. {
  97.     if (m_set_loginAccount.find(account) == m_set_loginAccount.end())
  98.         return;
  99.  
  100.     m_set_loginAccount.erase(account);
  101.  
  102.     std::set<MessengerManager::keyT>::iterator it;
  103.  
  104.     for (it = m_InverseRelation[account].begin(); it != m_InverseRelation[account].end(); ++it)
  105.     {
  106.         SendLogout(*it, account);
  107.     }
  108.  
  109.     std::map<keyT, std::set<keyT> >::iterator it2 = m_Relation.begin();
  110.  
  111.     while (it2 != m_Relation.end())
  112.     {
  113.         it2->second.erase(account);
  114.         ++it2;
  115.     }
  116.  
  117.     m_Relation.erase(account);
  118.     //m_map_stMobile.erase(account);
  119. }
  120.  
  121. void MessengerManager::RequestToAdd(LPCHARACTER ch, LPCHARACTER target)
  122. {
  123.     if (!ch->IsPC() || !target->IsPC())
  124.         return;
  125.  
  126.     if (quest::CQuestManager::instance().GetPCForce(ch->GetPlayerID())->IsRunning() == true)
  127.     {
  128.         ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("상대방이 친구 추가를 받을 수 없는 상태입니다."));
  129.         return;
  130.     }
  131.  
  132.     if (quest::CQuestManager::instance().GetPCForce(target->GetPlayerID())->IsRunning() == true)
  133.         return;
  134.  
  135.     DWORD dw1 = GetCRC32(ch->GetName(), strlen(ch->GetName()));
  136.     DWORD dw2 = GetCRC32(target->GetName(), strlen(target->GetName()));
  137.  
  138.     char buf[64];
  139.     snprintf(buf, sizeof(buf), "%u:%u", dw1, dw2);
  140.     DWORD dwComplex = GetCRC32(buf, strlen(buf));
  141.  
  142.     m_set_requestToAdd.insert(dwComplex);
  143.  
  144.     target->ChatPacket(CHAT_TYPE_COMMAND, "messenger_auth %s", ch->GetName());
  145. }
  146.  
  147. bool MessengerManager::AuthToAdd(MessengerManager::keyA account, MessengerManager::keyA companion, bool bDeny)
  148. {
  149.     DWORD dw1 = GetCRC32(companion.c_str(), companion.length());
  150.     DWORD dw2 = GetCRC32(account.c_str(), account.length());
  151.  
  152.     char buf[64];
  153.     snprintf(buf, sizeof(buf), "%u:%u", dw1, dw2);
  154.     DWORD dwComplex = GetCRC32(buf, strlen(buf));
  155.  
  156.     if (m_set_requestToAdd.find(dwComplex) == m_set_requestToAdd.end())
  157.     {
  158.         sys_log(0, "MessengerManager::AuthToAdd : request not exist %s -> %s", companion.c_str(), account.c_str());
  159.         return false;
  160.     }
  161.  
  162.     m_set_requestToAdd.erase(dwComplex);
  163.  
  164.     if (!bDeny)
  165.     {
  166.         AddToList(companion, account);
  167.         AddToList(account, companion);
  168.     }
  169.     return true;
  170. }
  171.  
  172. void MessengerManager::__AddToList(MessengerManager::keyA account, MessengerManager::keyA companion)
  173. {
  174.     m_Relation[account].insert(companion);
  175.     m_InverseRelation[companion].insert(account);
  176.  
  177.     LPCHARACTER ch = CHARACTER_MANAGER::instance().FindPC(account.c_str());
  178.     LPDESC d = ch ? ch->GetDesc() : NULL;
  179.  
  180.     if (d)
  181.     {
  182.         ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("<메신져> %s 님을 친구로 추가하였습니다."), companion.c_str());
  183.     }
  184.  
  185.     LPCHARACTER tch = CHARACTER_MANAGER::instance().FindPC(companion.c_str());
  186.  
  187.     if (tch){
  188.         SendLogin(account, companion);
  189.         ch->ChatPacket(CHAT_TYPE_INFO, ("<Debug> SendLogin(account, companion) = work"));
  190.     }
  191.     else {
  192.         SendLogout(account, companion);
  193.         ch->ChatPacket(CHAT_TYPE_INFO, ("<Debug> SendLogout(account, companion) = work"));
  194.     }
  195. }
  196.  
  197. void MessengerManager::AddToList(MessengerManager::keyA account, MessengerManager::keyA companion)
  198. {
  199.     if (companion.size() == 0)
  200.         return;
  201.  
  202.     if (m_Relation[account].find(companion) != m_Relation[account].end())
  203.         return;
  204.     LPCHARACTER ch = CHARACTER_MANAGER::instance().FindPC(account.c_str());
  205.     LPDESC d = ch ? ch->GetDesc() : NULL;
  206.     LPCHARACTER tch = CHARACTER_MANAGER::instance().FindPC(companion.c_str());
  207.  
  208.     DBManager::instance().EscapeString(__account, sizeof(__account), account.c_str(), account.size());
  209.     DBManager::instance().EscapeString(__companion, sizeof(__companion), companion.c_str(), companion.size());
  210.     if (account.compare(__account) || companion.compare(__companion))
  211.         return;
  212.  
  213.  
  214.     sys_log(0, "Messenger Add %s %s", account.c_str(), companion.c_str());
  215.     ch->ChatPacket(CHAT_TYPE_INFO, ("Messenger Add %s %s", account.c_str(), companion.c_str()));
  216.     DBManager::instance().Query("INSERT INTO messenger_list%s VALUES ('%s', '%s')",
  217.         get_table_postfix(), __account, __companion);
  218.  
  219.     __AddToList(account, companion);
  220.  
  221.     TPacketGGMessenger p2ppck;
  222.  
  223.     p2ppck.bHeader = HEADER_GG_MESSENGER_ADD;
  224.     if (d)
  225.     {
  226.         ch->ChatPacket(CHAT_TYPE_INFO, "companion = ok");
  227.     }
  228.     else
  229.     {
  230.         ch->ChatPacket(CHAT_TYPE_INFO, "companion = not work :<");
  231.     }
  232.     if (tch)
  233.     {
  234.         ch->ChatPacket(CHAT_TYPE_INFO, "test HEADER = ok ID: %d", p2ppck.bHeader);
  235.     }
  236.     else
  237.     {
  238.         ch->ChatPacket(CHAT_TYPE_INFO, "test HEADER = not ok ID: %d", p2ppck.bHeader);
  239.     }
  240.     strlcpy(p2ppck.szAccount, account.c_str(), sizeof(p2ppck.szAccount));
  241.     strlcpy(p2ppck.szCompanion, companion.c_str(), sizeof(p2ppck.szCompanion));
  242.     P2P_MANAGER::instance().Send(&p2ppck, sizeof(TPacketGGMessenger));
  243.     {
  244.         ch->ChatPacket(CHAT_TYPE_INFO, "test P2P_MANAGER::instance().Send(&p2ppck, sizeof(TPacketGGMessenger) = ok");
  245.     }
  246. }
  247.  
  248. void MessengerManager::__RemoveFromList(MessengerManager::keyA account, MessengerManager::keyA companion)
  249. {
  250.     m_Relation[account].erase(companion);
  251.     m_InverseRelation[companion].erase(account);
  252.  
  253.     LPCHARACTER ch = CHARACTER_MANAGER::instance().FindPC(account.c_str());
  254.     LPDESC d = ch ? ch->GetDesc() : NULL;
  255.  
  256.     if (d)
  257.         ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("<메신져> %s 님을 메신저에서 삭제하였습니다."), companion.c_str());
  258. }
  259.  
  260. void MessengerManager::RemoveFromList(MessengerManager::keyA account, MessengerManager::keyA companion)
  261. {
  262.     if (companion.size() == 0)
  263.         return;
  264.  
  265.     DBManager::instance().EscapeString(__account, sizeof(__account), account.c_str(), account.size());
  266.     DBManager::instance().EscapeString(__companion, sizeof(__companion), companion.c_str(), companion.size());
  267.     if (account.compare(__account) || companion.compare(__companion))
  268.         return;
  269.  
  270.     sys_log(1, "Messenger Remove %s %s", account.c_str(), companion.c_str());
  271.     DBManager::instance().Query("DELETE FROM messenger_list%s WHERE account='%s' AND companion = '%s'",
  272.         get_table_postfix(), __account, __companion);
  273.  
  274.     __RemoveFromList(account, companion);
  275.  
  276.     TPacketGGMessenger p2ppck;
  277.  
  278.     p2ppck.bHeader = HEADER_GG_MESSENGER_REMOVE;
  279.     strlcpy(p2ppck.szAccount, account.c_str(), sizeof(p2ppck.szAccount));
  280.     strlcpy(p2ppck.szCompanion, companion.c_str(), sizeof(p2ppck.szCompanion));
  281.     P2P_MANAGER::instance().Send(&p2ppck, sizeof(TPacketGGMessenger));
  282. }
  283.  
  284. void MessengerManager::RemoveAllList(keyA account)
  285. {
  286.     std::set<keyT>  company(m_Relation[account]);
  287.  
  288.     DBManager::instance().EscapeString(__account, sizeof(__account), account.c_str(), account.size());
  289.     if (account.compare(__account))
  290.         return;
  291.  
  292.     /* SQL Data 삭제 */
  293.     DBManager::instance().Query("DELETE FROM messenger_list%s WHERE account='%s' OR companion='%s'",
  294.         get_table_postfix(), __account, __account);
  295.  
  296.     /* 내가 가지고있는 리스트 삭제 */
  297.     for (std::set<keyT>::iterator iter = company.begin();
  298.         iter != company.end();
  299.         iter++)
  300.     {
  301.         this->RemoveFromList(account, *iter);
  302.     }
  303.  
  304.     /* 복사한 데이타 삭제 */
  305.     for (std::set<keyT>::iterator iter = company.begin();
  306.         iter != company.end();
  307.         )
  308.     {
  309.         company.erase(iter++);
  310.     }
  311.  
  312.     company.clear();
  313. }
  314.  
  315.  
  316. void MessengerManager::SendList(MessengerManager::keyA account)
  317. {
  318.     LPCHARACTER ch = CHARACTER_MANAGER::instance().FindPC(account.c_str());
  319.  
  320.     if (!ch)
  321.         return;
  322.  
  323.     LPDESC d = ch->GetDesc();
  324.  
  325.     if (!d)
  326.         return;
  327.  
  328.     if (m_Relation.find(account) == m_Relation.end())
  329.         return;
  330.  
  331.     if (m_Relation[account].empty())
  332.         return;
  333.  
  334.     TPacketGCMessenger pack;
  335.  
  336.     pack.header = HEADER_GC_MESSENGER;
  337.     pack.subheader = MESSENGER_SUBHEADER_GC_LIST;
  338.     pack.size = sizeof(TPacketGCMessenger);
  339.  
  340.     TPacketGCMessengerListOffline pack_offline;
  341.     TPacketGCMessengerListOnline pack_online;
  342.  
  343.     TEMP_BUFFER buf(128 * 1024); // 128k
  344.  
  345.     itertype(m_Relation[account]) it = m_Relation[account].begin(), eit = m_Relation[account].end();
  346.  
  347.     while (it != eit)
  348.     {
  349.         if (m_set_loginAccount.find(*it) != m_set_loginAccount.end())
  350.         {
  351.             pack_online.connected = 1;
  352.  
  353.             // Online
  354.             pack_online.length = it->size();
  355.  
  356.             buf.write(&pack_online, sizeof(TPacketGCMessengerListOnline));
  357.             buf.write(it->c_str(), it->size());
  358.         }
  359.         else
  360.         {
  361.             pack_offline.connected = 0;
  362.  
  363.             // Offline
  364.             pack_offline.length = it->size();
  365.  
  366.             buf.write(&pack_offline, sizeof(TPacketGCMessengerListOffline));
  367.             buf.write(it->c_str(), it->size());
  368.         }
  369.  
  370.         ++it;
  371.     }
  372.  
  373.     pack.size += buf.size();
  374.  
  375.     d->BufferedPacket(&pack, sizeof(TPacketGCMessenger));
  376.     d->Packet(buf.read_peek(), buf.size());
  377. }
  378.  
  379. void MessengerManager::SendLogin(MessengerManager::keyA account, MessengerManager::keyA companion)
  380. {
  381.     LPCHARACTER ch = CHARACTER_MANAGER::instance().FindPC(account.c_str());
  382.     LPDESC d = ch ? ch->GetDesc() : NULL;
  383.  
  384.     if (!d)
  385.         return;
  386.  
  387.     if (!d->GetCharacter())
  388.         return;
  389.  
  390.     if (ch->GetGMLevel() == GM_PLAYER && gm_get_level(companion.c_str()) != GM_PLAYER)
  391.         return;
  392.  
  393.     BYTE bLen = companion.size();
  394.  
  395.     TPacketGCMessenger pack;
  396.  
  397.     pack.header = HEADER_GC_MESSENGER;
  398.     pack.subheader = MESSENGER_SUBHEADER_GC_LOGIN;
  399.     pack.size = sizeof(TPacketGCMessenger)+sizeof(BYTE)+bLen;
  400.  
  401.     d->BufferedPacket(&pack, sizeof(TPacketGCMessenger));
  402.     d->BufferedPacket(&bLen, sizeof(BYTE));
  403.     d->Packet(companion.c_str(), companion.size());
  404. }
  405.  
  406. void MessengerManager::SendLogout(MessengerManager::keyA account, MessengerManager::keyA companion)
  407. {
  408.     if (!companion.size())
  409.         return;
  410.  
  411.     LPCHARACTER ch = CHARACTER_MANAGER::instance().FindPC(account.c_str());
  412.     LPDESC d = ch ? ch->GetDesc() : NULL;
  413.  
  414.     if (!d)
  415.         return;
  416.  
  417.     BYTE bLen = companion.size();
  418.  
  419.     TPacketGCMessenger pack;
  420.  
  421.     pack.header = HEADER_GC_MESSENGER;
  422.     pack.subheader = MESSENGER_SUBHEADER_GC_LOGOUT;
  423.     pack.size = sizeof(TPacketGCMessenger)+sizeof(BYTE)+bLen;
  424.  
  425.     d->BufferedPacket(&pack, sizeof(TPacketGCMessenger));
  426.     d->BufferedPacket(&bLen, sizeof(BYTE));
  427.     d->Packet(companion.c_str(), companion.size());
  428. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement