Guest User

Untitled

a guest
Oct 18th, 2017
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 38.54 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "../../libgame/include/grid.h"
  3. #include "constants.h"
  4. #include "utils.h"
  5. #include "config.h"
  6. #include "offline_shop.h"
  7. #include "desc.h"
  8. #include "desc_manager.h"
  9. #include "char.h"
  10. #include "char_manager.h"
  11. #include "item.h"
  12. #include "item_manager.h"
  13. #include "buffer_manager.h"
  14. #include "packet.h"
  15. #include "log.h"
  16. #include "db.h"
  17. #include "questmanager.h"
  18. #include "monarch.h"
  19. #include "mob_manager.h"
  20. #include "locale_service.h"
  21. #include "desc_client.h"
  22. #include "group_text_parse_tree.h"
  23. #include <boost/algorithm/string/predicate.hpp>
  24. #include <cctype>
  25. #include "offlineshop_manager.h"
  26. #include "p2p.h"
  27. #include "entity.h"
  28. #include "sectree_manager.h"
  29. #include "offlineshop_config.h"
  30.  
  31.  
  32. COfflineShopManager::COfflineShopManager()
  33. {
  34. }
  35.  
  36.  
  37. COfflineShopManager::~COfflineShopManager()
  38. {
  39. }
  40.  
  41. struct FFindOfflineShop
  42. {
  43. const char * szName;
  44.  
  45. DWORD dwVID, dwRealOwner;
  46. FFindOfflineShop(const char * c_szName) : szName(c_szName), dwVID(0), dwRealOwner(0) {};
  47.  
  48. void operator()(LPENTITY ent)
  49. {
  50. if (!ent)
  51. return;
  52.  
  53. if (ent->IsType(ENTITY_CHARACTER))
  54. {
  55. LPCHARACTER ch = (LPCHARACTER)ent;
  56. if (ch->IsOfflineShopNPC() && !strcmp(szName, ch->GetName()))
  57. {
  58. dwVID = ch->GetVID();
  59. dwRealOwner = ch->GetOfflineShopRealOwner();
  60. M2_DESTROY_CHARACTER(ch);
  61. }
  62. }
  63. }
  64. };
  65.  
  66. bool COfflineShopManager::StartShopping(LPCHARACTER pkChr, LPCHARACTER pkChrShopKeeper)
  67. {
  68. if (pkChr->GetOfflineShopOwner() == pkChrShopKeeper)
  69. return false;
  70.  
  71. if (pkChrShopKeeper->IsPC())
  72. return false;
  73.  
  74. sys_log(0, "OFFLINE_SHOP: START: %s", pkChr->GetName());
  75. return true;
  76. }
  77.  
  78. LPOFFLINESHOP COfflineShopManager::CreateOfflineShop(LPCHARACTER npc, DWORD dwOwnerPID)
  79. {
  80. if (FindOfflineShop(npc->GetVID()))
  81. return NULL;
  82.  
  83. LPOFFLINESHOP pkOfflineShop = M2_NEW COfflineShop;
  84. pkOfflineShop->SetOfflineShopNPC(npc);
  85.  
  86. m_map_pkOfflineShopByNPC.insert(TShopMap::value_type(npc->GetVID(), pkOfflineShop));
  87. m_Map_pkOfflineShopByNPC2.insert(TOfflineShopMap::value_type(dwOwnerPID, npc->GetVID()));
  88. return pkOfflineShop;
  89. }
  90.  
  91. LPOFFLINESHOP COfflineShopManager::FindOfflineShop(DWORD dwVID)
  92. {
  93. TShopMap::iterator it = m_map_pkOfflineShopByNPC.find(dwVID);
  94.  
  95. if (it == m_map_pkOfflineShopByNPC.end())
  96. return NULL;
  97.  
  98. return it->second;
  99. }
  100.  
  101. void COfflineShopManager::DestroyOfflineShop(LPCHARACTER ch, DWORD dwVID, bool bDestroyAll)
  102. {
  103. if (dwVID == 0 && ch)
  104. {
  105. std::auto_ptr<SQLMsg> pMsg(DBManager::instance().DirectQuery("SELECT mapIndex, mapIndex,channel FROM %soffline_shop_npc WHERE owner_id = %u", get_table_postfix(), ch->GetPlayerID()));
  106. if (pMsg->Get()->uiNumRows == 0)
  107. {
  108. ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("You can't use this option because you don't have offline shop npc!"));
  109. return;
  110. }
  111.  
  112. MYSQL_ROW row = mysql_fetch_row(pMsg->Get()->pSQLResult);
  113.  
  114. long lMapIndex = 0;
  115. str_to_number(lMapIndex, row[0]);
  116.  
  117. TPacketGGRemoveOfflineShop p;
  118. p.bHeader = HEADER_GG_REMOVE_OFFLINE_SHOP;
  119. p.lMapIndex = lMapIndex;
  120.  
  121. // Set offline shop name
  122. char szNpcName[CHARACTER_NAME_MAX_LEN + 1];
  123. snprintf(szNpcName, sizeof(szNpcName), "%s", ch->GetName());
  124. strlcpy(p.szNpcName, szNpcName, sizeof(p.szNpcName));
  125. // End Of Set offline shop name
  126.  
  127. P2P_MANAGER::instance().Send(&p, sizeof(TPacketGGRemoveOfflineShop));
  128. Giveback(ch);
  129. }
  130. else if (dwVID && ch)
  131. {
  132. LPCHARACTER npc;
  133. if (!ch)
  134. npc = CHARACTER_MANAGER::instance().Find(dwVID);
  135. else
  136. npc = CHARACTER_MANAGER::instance().Find(FindMyOfflineShop(ch->GetPlayerID()));
  137.  
  138. if (!npc)
  139. {
  140. std::auto_ptr<SQLMsg> pMsg(DBManager::instance().DirectQuery("SELECT mapIndex,channel FROM %soffline_shop_npc WHERE owner_id = %u", get_table_postfix(), ch->GetPlayerID()));
  141. if (pMsg->Get()->uiNumRows == 0)
  142. {
  143. ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("You can't use this option because you don't have offline shop npc!"));
  144. return;
  145. }
  146.  
  147. MYSQL_ROW row = mysql_fetch_row(pMsg->Get()->pSQLResult);
  148.  
  149. long lMapIndex = 0;
  150. str_to_number(lMapIndex, row[0]);
  151.  
  152. BYTE bChannel = 0;
  153. str_to_number(bChannel, row[1]);
  154.  
  155. if (g_bChannel != bChannel)
  156. {
  157. ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("You can't close your offline shop from %d channel. You must login %d channel"), g_bChannel, bChannel);
  158. return;
  159. }
  160.  
  161. char szName[CHARACTER_NAME_MAX_LEN + 1];
  162. snprintf(szName, sizeof(szName), "%s", ch->GetName());
  163. LPSECTREE_MAP pMap = SECTREE_MANAGER::instance().GetMap(lMapIndex);
  164. FFindOfflineShop offlineShop(szName);
  165. pMap->for_each(offlineShop);
  166.  
  167. if (bDestroyAll)
  168. {
  169. Giveback(ch);
  170. m_map_pkOfflineShopByNPC.erase(offlineShop.dwVID);
  171. m_Map_pkOfflineShopByNPC2.erase(offlineShop.dwRealOwner);
  172. DBManager::instance().DirectQuery("DELETE FROM %soffline_shop_npc WHERE owner_id = %u", get_table_postfix(), offlineShop.dwRealOwner);
  173. return;
  174. }
  175. }
  176.  
  177. LPOFFLINESHOP pkOfflineShop;
  178. if (!ch)
  179. pkOfflineShop = FindOfflineShop(dwVID);
  180. else
  181. pkOfflineShop = FindOfflineShop(FindMyOfflineShop(ch->GetPlayerID()));
  182.  
  183. if (!pkOfflineShop)
  184. return;
  185.  
  186. if (npc->GetOfflineShopChannel() != g_bChannel)
  187. {
  188. ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("You can't close your offline shop from %d channel. You must login %d channel"), g_bChannel, npc->GetOfflineShopChannel());
  189. return;
  190. }
  191.  
  192. if (bDestroyAll)
  193. {
  194. Giveback(ch);
  195. pkOfflineShop->Destroy(npc);
  196. }
  197.  
  198. m_map_pkOfflineShopByNPC.erase(npc->GetVID());
  199. m_Map_pkOfflineShopByNPC2.erase(npc->GetOfflineShopRealOwner());
  200. M2_DELETE(pkOfflineShop);
  201. }
  202. }
  203.  
  204. void COfflineShopManager::Giveback(LPCHARACTER ch)
  205. {
  206. if (!ch)
  207. return;
  208.  
  209. char szQuery[1024];
  210. if (g_bOfflineShopSocketMax == 3)
  211. snprintf(szQuery, sizeof(szQuery), "SELECT pos,count,vnum,socket0,socket1,socket2, attrtype0, attrvalue0, attrtype1, attrvalue1, attrtype2, attrvalue2, attrtype3, attrvalue3, attrtype4, attrvalue4, attrtype5, attrvalue5, attrtype6, attrvalue6, applytype0, applyvalue0, applytype1, applyvalue1, applytype2, applyvalue2, applytype3, applyvalue3, applytype4, applyvalue4, applytype5, applyvalue5, applytype6, applyvalue6, applytype7, applyvalue7 FROM %soffline_shop_item WHERE owner_id = %u", get_table_postfix(), ch->GetPlayerID());
  212. else if(g_bOfflineShopSocketMax == 4)
  213. snprintf(szQuery, sizeof(szQuery), "SELECT pos,count,vnum,socket0,socket1,socket2,socket3, attrtype0, attrvalue0, attrtype1, attrvalue1, attrtype2, attrvalue2, attrtype3, attrvalue3, attrtype4, attrvalue4, attrtype5, attrvalue5, attrtype6, attrvalue6, applytype0, applyvalue0, applytype1, applyvalue1, applytype2, applyvalue2, applytype3, applyvalue3, applytype4, applyvalue4, applytype5, applyvalue5, applytype6, applyvalue6, applytype7, applyvalue7 FROM %soffline_shop_item WHERE owner_id = %u", get_table_postfix(), ch->GetPlayerID());
  214. else if(g_bOfflineShopSocketMax == 5)
  215. snprintf(szQuery, sizeof(szQuery), "SELECT pos,count,vnum,socket0,socket1,socket2,socket3,socket4, attrtype0, attrvalue0, attrtype1, attrvalue1, attrtype2, attrvalue2, attrtype3, attrvalue3, attrtype4, attrvalue4, attrtype5, attrvalue5, attrtype6, attrvalue6, applytype0, applyvalue0, applytype1, applyvalue1, applytype2, applyvalue2, applytype3, applyvalue3, applytype4, applyvalue4, applytype5, applyvalue5, applytype6, applyvalue6, applytype7, applyvalue7 FROM %soffline_shop_item WHERE owner_id = %u", get_table_postfix(), ch->GetPlayerID());
  216. else if(g_bOfflineShopSocketMax == 6)
  217. snprintf(szQuery, sizeof(szQuery), "SELECT pos,count,vnum,socket0,socket1,socket2,socket3,socket4,socket5, attrtype0, attrvalue0, attrtype1, attrvalue1, attrtype2, attrvalue2, attrtype3, attrvalue3, attrtype4, attrvalue4, attrtype5, attrvalue5, attrtype6, attrvalue6, applytype0, applyvalue0, applytype1, applyvalue1, applytype2, applyvalue2, applytype3, applyvalue3, applytype4, applyvalue4, applytype5, applyvalue5, applytype6, applyvalue6, applytype7, applyvalue7 FROM %soffline_shop_item WHERE owner_id = %u", get_table_postfix(), ch->GetPlayerID());
  218.  
  219. std::auto_ptr<SQLMsg> pMsg(DBManager::Instance().DirectQuery(szQuery));
  220.  
  221. if (pMsg->Get()->uiNumRows == 0)
  222. {
  223. // sys_err("COfflineShopManager::GiveBack - There is nothing for this player [%s]", ch->GetName());
  224. return;
  225. }
  226.  
  227. MYSQL_ROW row;
  228. while (NULL != (row = mysql_fetch_row(pMsg->Get()->pSQLResult)))
  229. {
  230. TPlayerItem item;
  231.  
  232. str_to_number(item.pos, row[0]);
  233. str_to_number(item.count, row[1]);
  234. str_to_number(item.vnum, row[2]);
  235.  
  236. // Set Sockets
  237. for (int i = 0, n = 3; i < ITEM_SOCKET_MAX_NUM; ++i, n++)
  238. str_to_number(item.alSockets[i], row[n]);
  239. // End Of Set Sockets
  240.  
  241. // Set Attributes
  242. for (int i = 0, iStartAttributeType = 6, iStartAttributeValue = 7 ; i < ITEM_ATTRIBUTE_MAX_NUM; ++i, iStartAttributeType += 2, iStartAttributeValue += 2)
  243. {
  244. str_to_number(item.aAttr[i].bType, row[iStartAttributeType]);
  245. str_to_number(item.aAttr[i].sValue, row[iStartAttributeValue]);
  246. }
  247. // End Of Set Attributes
  248.  
  249. LPITEM pItem = ITEM_MANAGER::instance().CreateItem(item.vnum, item.count);
  250. if (pItem)
  251. {
  252. int iEmptyPos = 0;
  253.  
  254. if (pItem->IsDragonSoul())
  255. iEmptyPos = ch->GetEmptyDragonSoulInventory(pItem);
  256. else
  257. iEmptyPos = ch->GetEmptyInventory(pItem->GetSize());
  258.  
  259. if (iEmptyPos < 0)
  260. {
  261. ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("There is no enough slot for store the items!"));
  262. return;
  263. }
  264.  
  265. pItem->SetSockets(item.alSockets);
  266. pItem->SetAttributes(item.aAttr);
  267.  
  268. if (pItem->IsDragonSoul())
  269. pItem->AddToCharacter(ch, TItemPos(DRAGON_SOUL_INVENTORY, iEmptyPos));
  270. else
  271. pItem->AddToCharacter(ch, TItemPos(INVENTORY, iEmptyPos));
  272.  
  273. ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("You are gain %s item from your offline shop."), pItem->GetName());
  274. DBManager::instance().DirectQuery("DELETE FROM %soffline_shop_item WHERE owner_id = %u and pos = %d", get_table_postfix(), ch->GetPlayerID(), item.pos);
  275. }
  276. }
  277. }
  278.  
  279. void COfflineShopManager::Giveback3(LPCHARACTER ch)
  280. {
  281. if (!ch)
  282. return;
  283.  
  284. char szQuery[1024];
  285. if (g_bOfflineShopSocketMax == 3)
  286. snprintf(szQuery, sizeof(szQuery), "SELECT pos,count,vnum,socket0,socket1,socket2, attrtype0, attrvalue0, attrtype1, attrvalue1, attrtype2, attrvalue2, attrtype3, attrvalue3, attrtype4, attrvalue4, attrtype5, attrvalue5, attrtype6, attrvalue6, applytype0, applyvalue0, applytype1, applyvalue1, applytype2, applyvalue2, applytype3, applyvalue3, applytype4, applyvalue4, applytype5, applyvalue5, applytype6, applyvalue6, applytype7, applyvalue7 FROM %soffline_shop_item WHERE owner_id = %u", get_table_postfix(), ch->GetPlayerID());
  287. else if(g_bOfflineShopSocketMax == 4)
  288. snprintf(szQuery, sizeof(szQuery), "SELECT pos,count,vnum,socket0,socket1,socket2,socket3, attrtype0, attrvalue0, attrtype1, attrvalue1, attrtype2, attrvalue2, attrtype3, attrvalue3, attrtype4, attrvalue4, attrtype5, attrvalue5, attrtype6, attrvalue6, applytype0, applyvalue0, applytype1, applyvalue1, applytype2, applyvalue2, applytype3, applyvalue3, applytype4, applyvalue4, applytype5, applyvalue5, applytype6, applyvalue6, applytype7, applyvalue7 FROM %soffline_shop_item WHERE owner_id = %u", get_table_postfix(), ch->GetPlayerID());
  289. else if(g_bOfflineShopSocketMax == 5)
  290. snprintf(szQuery, sizeof(szQuery), "SELECT pos,count,vnum,socket0,socket1,socket2,socket3,socket4, attrtype0, attrvalue0, attrtype1, attrvalue1, attrtype2, attrvalue2, attrtype3, attrvalue3, attrtype4, attrvalue4, attrtype5, attrvalue5, attrtype6, attrvalue6, applytype0, applyvalue0, applytype1, applyvalue1, applytype2, applyvalue2, applytype3, applyvalue3, applytype4, applyvalue4, applytype5, applyvalue5, applytype6, applyvalue6, applytype7, applyvalue7 FROM %soffline_shop_item WHERE owner_id = %u", get_table_postfix(), ch->GetPlayerID());
  291. else if(g_bOfflineShopSocketMax == 6)
  292. snprintf(szQuery, sizeof(szQuery), "SELECT pos,count,vnum,socket0,socket1,socket2,socket3,socket4,socket5, attrtype0, attrvalue0, attrtype1, attrvalue1, attrtype2, attrvalue2, attrtype3, attrvalue3, attrtype4, attrvalue4, attrtype5, attrvalue5, attrtype6, attrvalue6, applytype0, applyvalue0, applytype1, applyvalue1, applytype2, applyvalue2, applytype3, applyvalue3, applytype4, applyvalue4, applytype5, applyvalue5, applytype6, applyvalue6, applytype7, applyvalue7 FROM %soffline_shop_item WHERE owner_id = %u", get_table_postfix(), ch->GetPlayerID());
  293.  
  294. std::auto_ptr<SQLMsg> pMsg(DBManager::Instance().DirectQuery(szQuery));
  295.  
  296. if (pMsg->Get()->uiNumRows == 0)
  297. {
  298. ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("geri_verilecek_nesne_yok"));
  299. return;
  300. }
  301.  
  302. std::auto_ptr<SQLMsg> pMsg1(DBManager::instance().DirectQuery("SELECT mapIndex, mapIndex,channel FROM %soffline_shop_npc WHERE owner_id = %u", get_table_postfix(), ch->GetPlayerID()));
  303. if (pMsg1->Get()->uiNumRows != 0)
  304. {
  305. ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("ct_oldugu_icin_kullanamazsin"));
  306. return;
  307. }
  308.  
  309. MYSQL_ROW row;
  310. while (NULL != (row = mysql_fetch_row(pMsg->Get()->pSQLResult)))
  311. {
  312. TPlayerItem item;
  313.  
  314. str_to_number(item.pos, row[0]);
  315. str_to_number(item.count, row[1]);
  316. str_to_number(item.vnum, row[2]);
  317.  
  318. // Set Sockets
  319. for (int i = 0, n = 3; i < ITEM_SOCKET_MAX_NUM; ++i, n++)
  320. str_to_number(item.alSockets[i], row[n]);
  321. // End Of Set Sockets
  322.  
  323. // Set Attributes
  324. for (int i = 0, iStartAttributeType = 6, iStartAttributeValue = 7 ; i < ITEM_ATTRIBUTE_MAX_NUM; ++i, iStartAttributeType += 2, iStartAttributeValue += 2)
  325. {
  326. str_to_number(item.aAttr[i].bType, row[iStartAttributeType]);
  327. str_to_number(item.aAttr[i].sValue, row[iStartAttributeValue]);
  328. }
  329. // End Of Set Attributes
  330.  
  331. LPITEM pItem = ITEM_MANAGER::instance().CreateItem(item.vnum, item.count);
  332. if (pItem)
  333. {
  334. int iEmptyPos = 0;
  335.  
  336. if (pItem->IsDragonSoul())
  337. iEmptyPos = ch->GetEmptyDragonSoulInventory(pItem);
  338. else
  339. iEmptyPos = ch->GetEmptyInventory(pItem->GetSize());
  340.  
  341. if (iEmptyPos < 0)
  342. {
  343. ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("There is no enough slot for store the items!"));
  344. return;
  345. }
  346.  
  347. pItem->SetSockets(item.alSockets);
  348. pItem->SetAttributes(item.aAttr);
  349.  
  350. if (pItem->IsDragonSoul())
  351. pItem->AddToCharacter(ch, TItemPos(DRAGON_SOUL_INVENTORY, iEmptyPos));
  352. else
  353. pItem->AddToCharacter(ch, TItemPos(INVENTORY, iEmptyPos));
  354.  
  355. ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("You are gain %s item from your offline shop."), pItem->GetName());
  356. DBManager::instance().DirectQuery("DELETE FROM %soffline_shop_item WHERE owner_id = %u and pos = %d", get_table_postfix(), ch->GetPlayerID(), item.pos);
  357. }
  358. }
  359. }
  360.  
  361. void COfflineShopManager::Giveback2(LPCHARACTER ch)
  362. {
  363. if (!ch)
  364. return;
  365.  
  366. char szQuery[1024];
  367. snprintf(szQuery, sizeof(szQuery), "SELECT pos,count,vnum,socket0,socket1,socket2, attrtype0, attrvalue0, attrtype1, attrvalue1, attrtype2, attrvalue2, attrtype3, attrvalue3, attrtype4, attrvalue4, attrtype5, attrvalue5, attrtype6, attrvalue6, applytype0, applyvalue0, applytype1, applyvalue1, applytype2, applyvalue2, applytype3, applyvalue3, applytype4, applyvalue4, applytype5, applyvalue5, applytype6, applyvalue6, applytype7, applyvalue7 FROM %soffline_shop_item WHERE owner_id = %u and status = 1", get_table_postfix(), ch->GetPlayerID());
  368. std::auto_ptr<SQLMsg> pMsg(DBManager::instance().DirectQuery(szQuery));
  369.  
  370. if (pMsg->Get()->uiNumRows == 0)
  371. return;
  372.  
  373. MYSQL_ROW row;
  374. while (NULL != (row = mysql_fetch_row(pMsg->Get()->pSQLResult)))
  375. {
  376. TPlayerItem item;
  377.  
  378. str_to_number(item.pos, row[0]);
  379. str_to_number(item.count, row[1]);
  380. str_to_number(item.vnum, row[2]);
  381.  
  382. // Set Sockets
  383. for (int i = 0, n = 3; i < ITEM_SOCKET_MAX_NUM; ++i, n++)
  384. str_to_number(item.alSockets[i], row[n]);
  385. // End Of Set Sockets
  386.  
  387. // Set Attributes
  388. for (int i = 0, iStartAttributeType = 6, iStartAttributeValue = ITEM_ATTRIBUTE_MAX_NUM; i < ITEM_ATTRIBUTE_MAX_NUM; ++i, iStartAttributeType += 2, iStartAttributeValue += 2)
  389. {
  390. str_to_number(item.aAttr[i].bType, row[iStartAttributeType]);
  391. str_to_number(item.aAttr[i].sValue, row[iStartAttributeValue]);
  392. }
  393. // End Of Set Attributes
  394.  
  395. LPITEM pItem = ITEM_MANAGER::instance().CreateItem(item.vnum, item.count);
  396. if (pItem)
  397. {
  398. int iEmptyPos = 0;
  399.  
  400. if (pItem->IsDragonSoul())
  401. iEmptyPos = ch->GetEmptyDragonSoulInventory(pItem);
  402. else
  403. iEmptyPos = ch->GetEmptyInventory(pItem->GetSize());
  404.  
  405. if (iEmptyPos < 0)
  406. {
  407. ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("There is no enough slot for store the items!"));
  408. return;
  409. }
  410.  
  411. pItem->SetSockets(item.alSockets);
  412. pItem->SetAttributes(item.aAttr);
  413.  
  414. if (pItem->IsDragonSoul())
  415. pItem->AddToCharacter(ch, TItemPos(DRAGON_SOUL_INVENTORY, iEmptyPos));
  416. else
  417. pItem->AddToCharacter(ch, TItemPos(INVENTORY, iEmptyPos));
  418.  
  419. ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("You are gain %s item from your offline shop."), pItem->GetName());
  420. DBManager::instance().DirectQuery("DELETE FROM %soffline_shop_item WHERE owner_id = %u and pos = %d", get_table_postfix(), ch->GetPlayerID(), item.pos);
  421. }
  422. }
  423. }
  424.  
  425. void COfflineShopManager::AddItem(LPCHARACTER ch, BYTE bDisplayPos, BYTE bPos, long long llPrice)
  426. {
  427. if (!ch)
  428. return;
  429.  
  430. // Fixed bug 6.21.2015
  431. if (bDisplayPos >= OFFLINE_SHOP_HOST_ITEM_MAX_NUM)
  432. {
  433. sys_err("Overflow offline shop slot count [%s]", ch->GetName());
  434. return;
  435. }
  436. // End Of fixed bug 6.21.2015
  437.  
  438. // Check player has offline shop or not
  439. std::auto_ptr<SQLMsg> pmsg(DBManager::instance().DirectQuery("SELECT COUNT(*) FROM player.offline_shop_npc WHERE owner_id = %u", ch->GetPlayerID()));
  440. MYSQL_ROW row = mysql_fetch_row(pmsg->Get()->pSQLResult);
  441.  
  442. BYTE bResult = 0;
  443. str_to_number(bResult, row[0]);
  444.  
  445. if (!bResult)
  446. {
  447. ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("You can't use this option because you don't have a offline shop!"));
  448. return;
  449. }
  450. // End Of Check player has offline shop or not
  451.  
  452. LPITEM pkItem = ch->GetInventoryItem(bPos);
  453.  
  454. if (!pkItem)
  455. return;
  456.  
  457. // Check
  458. const TItemTable * itemTable = pkItem->GetProto();
  459. if (IS_SET(itemTable->dwAntiFlags, ITEM_ANTIFLAG_GIVE | ITEM_ANTIFLAG_MY_OFFLINE_SHOP))
  460. {
  461. ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("This item is not tradeable!"));
  462. return;
  463. }
  464.  
  465. if (pkItem->isLocked())
  466. return;
  467.  
  468. if (pkItem->IsEquipped()) // burdan yap
  469. {
  470. ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("You can't sell equipped items!"));
  471. return;
  472. }
  473.  
  474. if (pkItem->IsBind() || pkItem->IsUntilBind())
  475. {
  476. ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("ruha_bagli_nesne"));
  477. return;
  478. }
  479.  
  480. char szColumns[QUERY_MAX_LEN], szValues[QUERY_MAX_LEN];
  481.  
  482. int iLen = snprintf(szColumns, sizeof(szColumns), "id,owner_id,pos,count,price,vnum");
  483. int iUpdateLen = snprintf(szValues, sizeof(szValues), "%u,%u,%d,%u,%lld,%u", pkItem->GetID(), ch->GetPlayerID(), bDisplayPos, pkItem->GetCount(), llPrice, pkItem->GetVnum());
  484.  
  485. if (g_bOfflineShopSocketMax == 3)
  486. {
  487. iLen += snprintf(szColumns + iLen, sizeof(szColumns) - iLen, ",socket0,socket1,socket2");
  488. iUpdateLen += snprintf(szValues + iUpdateLen, sizeof(szValues) - iUpdateLen, ",%ld,%ld,%ld", pkItem->GetSocket(0), pkItem->GetSocket(1), pkItem->GetSocket(2));
  489. }
  490. else if(g_bOfflineShopSocketMax == 4)
  491. {
  492. iLen += snprintf(szColumns + iLen, sizeof(szColumns) - iLen, ",socket0,socket1,socket2,socket3");
  493. iUpdateLen += snprintf(szValues + iUpdateLen, sizeof(szValues) - iUpdateLen, ",%ld,%ld,%ld,%ld", pkItem->GetSocket(0), pkItem->GetSocket(1), pkItem->GetSocket(2), pkItem->GetSocket(3));
  494. }
  495. else if(g_bOfflineShopSocketMax == 5)
  496. {
  497. iLen += snprintf(szColumns + iLen, sizeof(szColumns) - iLen, ",socket0,socket1,socket2,socket3,socket4");
  498. iUpdateLen += snprintf(szValues + iUpdateLen, sizeof(szValues) - iUpdateLen, ",%ld,%ld,%ld,%ld,%ld", pkItem->GetSocket(0), pkItem->GetSocket(1), pkItem->GetSocket(2), pkItem->GetSocket(3), pkItem->GetSocket(4));
  499. }
  500. else if(g_bOfflineShopSocketMax == 6)
  501. {
  502. iLen += snprintf(szColumns + iLen, sizeof(szColumns) - iLen, ",socket0,socket1,socket2,socket3,socket4,socket5");
  503. iUpdateLen += snprintf(szValues + iUpdateLen, sizeof(szValues) - iUpdateLen, ",%ld,%ld,%ld,%ld,%ld,%ld", pkItem->GetSocket(0), pkItem->GetSocket(1), pkItem->GetSocket(2), pkItem->GetSocket(3), pkItem->GetSocket(4), pkItem->GetSocket(5));
  504. }
  505.  
  506. iLen += snprintf(szColumns + iLen, sizeof(szColumns) - iLen, ", attrtype0, attrvalue0, attrtype1, attrvalue1, attrtype2, attrvalue2, attrtype3, attrvalue3, attrtype4, attrvalue4, attrtype5, attrvalue5, attrtype6, attrvalue6, applytype0, applyvalue0, applytype1, applyvalue1, applytype2, applyvalue2, applytype3, applyvalue3, applytype4, applyvalue4, applytype5, applyvalue5, applytype6, applyvalue6, applytype7, applyvalue7");
  507. iUpdateLen += snprintf(szValues + iUpdateLen, sizeof(szValues) - iUpdateLen, ",%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
  508. pkItem->GetAttributeType(0), pkItem->GetAttributeValue(0),
  509. pkItem->GetAttributeType(1), pkItem->GetAttributeValue(1),
  510. pkItem->GetAttributeType(2), pkItem->GetAttributeValue(2),
  511. pkItem->GetAttributeType(3), pkItem->GetAttributeValue(3),
  512. pkItem->GetAttributeType(4), pkItem->GetAttributeValue(4),
  513. pkItem->GetAttributeType(5), pkItem->GetAttributeValue(5),
  514. pkItem->GetAttributeType(6), pkItem->GetAttributeValue(6),
  515. pkItem->GetAttributeType(7), pkItem->GetAttributeValue(7),
  516. pkItem->GetAttributeType(8), pkItem->GetAttributeValue(8),
  517. pkItem->GetAttributeType(9), pkItem->GetAttributeValue(9),
  518. pkItem->GetAttributeType(10), pkItem->GetAttributeValue(10),
  519. pkItem->GetAttributeType(11), pkItem->GetAttributeValue(11),
  520. pkItem->GetAttributeType(12), pkItem->GetAttributeValue(12),
  521. pkItem->GetAttributeType(12), pkItem->GetAttributeValue(13),
  522. pkItem->GetAttributeType(14), pkItem->GetAttributeValue(14));
  523.  
  524. char szInsertQuery[QUERY_MAX_LEN];
  525. snprintf(szInsertQuery, sizeof(szInsertQuery), "INSERT INTO %soffline_shop_item (%s) VALUES (%s)", get_table_postfix(), szColumns, szValues);
  526. std::auto_ptr<SQLMsg> pMsg(DBManager::instance().DirectQuery(szInsertQuery));
  527. pkItem->RemoveFromCharacter();
  528.  
  529. LPCHARACTER npc = CHARACTER_MANAGER::instance().Find(FindMyOfflineShop(ch->GetPlayerID()));
  530. if (!npc)
  531. return;
  532.  
  533. LPOFFLINESHOP pkOfflineShop = FindOfflineShop(npc->GetVID());
  534. if (!pkOfflineShop)
  535. return;
  536.  
  537. pkOfflineShop->BroadcastUpdateItem(bDisplayPos, ch->GetPlayerID());
  538. LogManager::instance().ItemLog(ch, pkItem, "ADD ITEM OFFLINE SHOP", "");
  539. }
  540.  
  541. void COfflineShopManager::RemoveItem(LPCHARACTER ch, BYTE bPos)
  542. {
  543. if (!ch)
  544. return;
  545.  
  546. if (bPos >= OFFLINE_SHOP_HOST_ITEM_MAX_NUM)
  547. {
  548. sys_log(0, "OfflineShopManager::RemoveItem - Overflow slot! [%s]", ch->GetName());
  549. return;
  550. }
  551.  
  552. LPCHARACTER npc = CHARACTER_MANAGER::instance().Find(FindMyOfflineShop(ch->GetPlayerID()));
  553.  
  554. // Check npc
  555. if (!npc)
  556. {
  557. char szQuery[1024];
  558.  
  559. if (g_bOfflineShopSocketMax == 3)
  560. snprintf(szQuery, sizeof(szQuery), "SELECT pos,count,vnum,socket0,socket1,socket2, attrtype0, attrvalue0, attrtype1, attrvalue1, attrtype2, attrvalue2, attrtype3, attrvalue3, attrtype4, attrvalue4, attrtype5, attrvalue5, attrtype6, attrvalue6, applytype0, applyvalue0, applytype1, applyvalue1, applytype2, applyvalue2, applytype3, applyvalue3, applytype4, applyvalue4, applytype5, applyvalue5, applytype6, applyvalue6, applytype7, applyvalue7 FROM %soffline_shop_item WHERE owner_id = %u and pos = %d", get_table_postfix(), ch->GetPlayerID(), bPos);
  561. else if(g_bOfflineShopSocketMax == 4)
  562. snprintf(szQuery, sizeof(szQuery), "SELECT pos,count,vnum,socket0,socket1,socket2,socket3, attrtype0, attrvalue0, attrtype1, attrvalue1, attrtype2, attrvalue2, attrtype3, attrvalue3, attrtype4, attrvalue4, attrtype5, attrvalue5, attrtype6, attrvalue6, applytype0, applyvalue0, applytype1, applyvalue1, applytype2, applyvalue2, applytype3, applyvalue3, applytype4, applyvalue4, applytype5, applyvalue5, applytype6, applyvalue6, applytype7, applyvalue7 FROM %soffline_shop_item WHERE owner_id = %u and pos = %d", get_table_postfix(), ch->GetPlayerID(), bPos);
  563. else if(g_bOfflineShopSocketMax == 5)
  564. snprintf(szQuery, sizeof(szQuery), "SELECT pos,count,vnum,socket0,socket1,socket2,socket3,socket4, attrtype0, attrvalue0, attrtype1, attrvalue1, attrtype2, attrvalue2, attrtype3, attrvalue3, attrtype4, attrvalue4, attrtype5, attrvalue5, attrtype6, attrvalue6, applytype0, applyvalue0, applytype1, applyvalue1, applytype2, applyvalue2, applytype3, applyvalue3, applytype4, applyvalue4, applytype5, applyvalue5, applytype6, applyvalue6, applytype7, applyvalue7 FROM %soffline_shop_item WHERE owner_id = %u and pos = %d", get_table_postfix(), ch->GetPlayerID(), bPos);
  565. else if(g_bOfflineShopSocketMax == 6)
  566. snprintf(szQuery, sizeof(szQuery), "SELECT pos,count,vnum,socket0,socket1,socket2,socket3,socket4,socket5, attrtype0, attrvalue0, attrtype1, attrvalue1, attrtype2, attrvalue2, attrtype3, attrvalue3, attrtype4, attrvalue4, attrtype5, attrvalue5, attrtype6, attrvalue6, applytype0, applyvalue0, applytype1, applyvalue1, applytype2, applyvalue2, applytype3, applyvalue3, applytype4, applyvalue4, applytype5, applyvalue5, applytype6, applyvalue6, applytype7, applyvalue7 FROM %soffline_shop_item WHERE owner_id = %u and pos = %d", get_table_postfix(), ch->GetPlayerID(), bPos);
  567.  
  568. std::auto_ptr<SQLMsg> pMsg(DBManager::instance().DirectQuery(szQuery));
  569. if (pMsg->Get()->uiNumRows == 0)
  570. {
  571. sys_log(0, "OfflineShopManager::RemoveItem - This slot is empty! [%s]", ch->GetName());
  572. return;
  573. }
  574.  
  575. TPlayerItem item;
  576. int rows;
  577. if (!(rows = mysql_num_rows(pMsg->Get()->pSQLResult)))
  578. return;
  579.  
  580. for (int i = 0; i < rows; ++i)
  581. {
  582. MYSQL_ROW row = mysql_fetch_row(pMsg->Get()->pSQLResult);
  583. int cur = 0;
  584.  
  585. str_to_number(item.pos, row[cur++]);
  586. str_to_number(item.count, row[cur++]);
  587. str_to_number(item.vnum, row[cur++]);
  588. str_to_number(item.alSockets[0], row[cur++]);
  589. str_to_number(item.alSockets[1], row[cur++]);
  590. str_to_number(item.alSockets[2], row[cur++]);
  591.  
  592. for (int j = 0; j < ITEM_ATTRIBUTE_MAX_NUM; j++)
  593. {
  594. str_to_number(item.aAttr[j].bType, row[cur++]);
  595. str_to_number(item.aAttr[j].sValue, row[cur++]);
  596. }
  597. }
  598.  
  599. LPITEM pItem = ITEM_MANAGER::instance().CreateItem(item.vnum, item.count);
  600. if (!pItem)
  601. {
  602. ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("There is problem in the system at the moment. Please try again!"));
  603. return;
  604. }
  605.  
  606. pItem->SetAttributes(item.aAttr);
  607. pItem->SetSockets(item.alSockets);
  608.  
  609. int iEmptyPos;
  610. if (pItem->IsDragonSoul())
  611. iEmptyPos = ch->GetEmptyDragonSoulInventory(pItem);
  612. else
  613. iEmptyPos = ch->GetEmptyInventory(pItem->GetSize());
  614.  
  615. if (iEmptyPos < 0)
  616. {
  617. ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("You don't have enough space for store the item!"));
  618. return;
  619. }
  620.  
  621. if (pItem->IsDragonSoul())
  622. pItem->AddToCharacter(ch, TItemPos(DRAGON_SOUL_INVENTORY, iEmptyPos));
  623. else
  624. pItem->AddToCharacter(ch, TItemPos(INVENTORY, iEmptyPos));
  625.  
  626. DBManager::instance().DirectQuery("DELETE FROM %soffline_shop_item WHERE owner_id = %u and pos = %d", get_table_postfix(), ch->GetPlayerID(), bPos);
  627. LogManager::instance().ItemLog(ch, pItem, "DELETE OFFLINE SHOP ITEM", "");
  628. }
  629. else
  630. {
  631. LPOFFLINESHOP pkOfflineShop = npc->GetOfflineShop();
  632.  
  633. // Check pkOfflineShop
  634. if (!pkOfflineShop)
  635. return;
  636.  
  637. std::auto_ptr<SQLMsg> pMsg(DBManager::instance().DirectQuery("SELECT pos,count,vnum,socket0,socket1,socket2, attrtype0, attrvalue0, attrtype1, attrvalue1, attrtype2, attrvalue2, attrtype3, attrvalue3, attrtype4, attrvalue4, attrtype5, attrvalue5, attrtype6, attrvalue6, applytype0, applyvalue0, applytype1, applyvalue1, applytype2, applyvalue2, applytype3, applyvalue3, applytype4, applyvalue4, applytype5, applyvalue5, applytype6, applyvalue6, applytype7, applyvalue7 FROM %soffline_shop_item WHERE owner_id = %u and pos = %d", get_table_postfix(), ch->GetPlayerID(), bPos));
  638. if (pMsg->Get()->uiNumRows == 0)
  639. {
  640. sys_log(0, "OfflineShopManager::RemoveItem - This slot is empty! [%s]", ch->GetName());
  641. return;
  642. }
  643.  
  644. TPlayerItem item;
  645. int rows;
  646. if (!(rows = mysql_num_rows(pMsg->Get()->pSQLResult)))
  647. return;
  648.  
  649. for (int i = 0; i < rows; ++i)
  650. {
  651. MYSQL_ROW row = mysql_fetch_row(pMsg->Get()->pSQLResult);
  652. int cur = 0;
  653.  
  654. str_to_number(item.pos, row[cur++]);
  655. str_to_number(item.count, row[cur++]);
  656. str_to_number(item.vnum, row[cur++]);
  657. str_to_number(item.alSockets[0], row[cur++]);
  658. str_to_number(item.alSockets[1], row[cur++]);
  659. str_to_number(item.alSockets[2], row[cur++]);
  660.  
  661. for (int j = 0; j < ITEM_ATTRIBUTE_MAX_NUM; j++)
  662. {
  663. str_to_number(item.aAttr[j].bType, row[cur++]);
  664. str_to_number(item.aAttr[j].sValue, row[cur++]);
  665. }
  666. }
  667.  
  668. LPITEM pItem = ITEM_MANAGER::instance().CreateItem(item.vnum, item.count);
  669. if (!pItem)
  670. {
  671. ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("There is problem in the system at the moment. Please try again!"));
  672. return;
  673. }
  674.  
  675. pItem->SetAttributes(item.aAttr);
  676. pItem->SetSockets(item.alSockets);
  677.  
  678. int iEmptyPos;
  679. if (pItem->IsDragonSoul())
  680. iEmptyPos = ch->GetEmptyDragonSoulInventory(pItem);
  681. else
  682. iEmptyPos = ch->GetEmptyInventory(pItem->GetSize());
  683.  
  684. if (iEmptyPos < 0)
  685. {
  686. ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("You don't have enough space for store the item!"));
  687. return;
  688. }
  689.  
  690. if (pItem->IsDragonSoul())
  691. pItem->AddToCharacter(ch, TItemPos(DRAGON_SOUL_INVENTORY, iEmptyPos));
  692. else
  693. pItem->AddToCharacter(ch, TItemPos(INVENTORY, iEmptyPos));
  694.  
  695. DBManager::instance().DirectQuery("DELETE FROM %soffline_shop_item WHERE owner_id = %u and pos = %d", get_table_postfix(), ch->GetPlayerID(), bPos);
  696. pkOfflineShop->BroadcastUpdateItem(bPos, ch->GetPlayerID(), true);
  697.  
  698. if (LeftItemCount(ch) == 0)
  699. pkOfflineShop->Destroy(npc);
  700.  
  701. LogManager::instance().ItemLog(ch, pItem, "DELETE OFFLINE SHOP ITEM", "");
  702. }
  703. }
  704.  
  705. void COfflineShopManager::ChangePrice(LPCHARACTER ch, BYTE bPos, long long llPrice)
  706. {
  707. if (!ch)
  708. return;
  709.  
  710. if (bPos >= OFFLINE_SHOP_HOST_ITEM_MAX_NUM)
  711. {
  712. sys_err("Offlineshop overflow slot count [%s][%d]", ch->GetName(), bPos);
  713. return;
  714. }
  715.  
  716. if (llPrice >= GOLD_MAX)
  717. {
  718. ch->ChatPacket(CHAT_TYPE_INFO, "You can't change price of the item because of the overflow gold max!");
  719. return;
  720. }
  721.  
  722. LPOFFLINESHOP pkOfflineShop = FindOfflineShop(FindMyOfflineShop(ch->GetPlayerID()));
  723. if (pkOfflineShop)
  724. pkOfflineShop->BroadcastUpdatePrice(bPos, llPrice);
  725.  
  726. DBManager::instance().DirectQuery("UPDATE offline_shop_item%s SET price = %lld WHERE owner_id = %u and pos = %d", get_table_postfix(), llPrice, ch->GetPlayerID(), bPos);
  727.  
  728. sys_log(0, "PRET: %lld | ITEM ID: %u | POZITIE: %d", llPrice, ch->GetPlayerID(), bPos);
  729. }
  730.  
  731. void COfflineShopManager::Refresh(LPCHARACTER ch)
  732. {
  733. if (!ch)
  734. return;
  735.  
  736. LPCHARACTER npc = CHARACTER_MANAGER::instance().Find(FindMyOfflineShop(ch->GetPlayerID()));
  737. if (!npc)
  738. {
  739. TPacketGCShop pack;
  740. pack.header = HEADER_GC_OFFLINE_SHOP;
  741. pack.subheader = SHOP_SUBHEADER_GC_UPDATE_ITEM2;
  742.  
  743. TPacketGCOfflineShopStart pack2;
  744. memset(&pack2, 0, sizeof(pack2));
  745. pack2.owner_vid = 0;
  746.  
  747. std::auto_ptr<SQLMsg> pMsg(DBManager::instance().DirectQuery("SELECT pos,count,vnum,price,socket0,socket1,socket2, attrtype0, attrvalue0, attrtype1, attrvalue1, attrtype2, attrvalue2, attrtype3, attrvalue3, attrtype4, attrvalue4, attrtype5, attrvalue5, attrtype6, attrvalue6, applytype0, applyvalue0, applytype1, applyvalue1, applytype2, applyvalue2, applytype3, applyvalue3, applytype4, applyvalue4, applytype5, applyvalue5, applytype6, applyvalue6, applytype7, applyvalue7 FROM %soffline_shop_item WHERE owner_id = %u", get_table_postfix(), ch->GetPlayerID()));
  748.  
  749. MYSQL_ROW row;
  750. while (NULL != (row = mysql_fetch_row(pMsg->Get()->pSQLResult)))
  751. {
  752. BYTE bPos = 0;
  753. str_to_number(bPos, row[0]);
  754.  
  755. str_to_number(pack2.items[bPos].count, row[1]);
  756. str_to_number(pack2.items[bPos].vnum, row[2]);
  757. str_to_number(pack2.items[bPos].price, row[3]);
  758.  
  759. DWORD alSockets[ITEM_SOCKET_MAX_NUM];
  760. for (int i = 0, n = 4; i < ITEM_SOCKET_MAX_NUM; ++i, n++)
  761. str_to_number(alSockets[i], row[n]);
  762.  
  763. TPlayerItemAttribute aAttr[ITEM_ATTRIBUTE_MAX_NUM];
  764. for (int i = 0, iStartType = 7, iStartValue = 8; i < ITEM_ATTRIBUTE_MAX_NUM; ++i, iStartType += 2, iStartValue += 2)
  765. {
  766. str_to_number(aAttr[i].bType, row[iStartType]);
  767. str_to_number(aAttr[i].sValue, row[iStartValue]);
  768. }
  769.  
  770. thecore_memcpy(pack2.items[bPos].alSockets, alSockets, sizeof(pack2.items[bPos].alSockets));
  771. thecore_memcpy(pack2.items[bPos].aAttr, aAttr, sizeof(pack2.items[bPos].aAttr));
  772. }
  773.  
  774. pack.size = sizeof(pack) + sizeof(pack2);
  775.  
  776. if (ch->GetDesc())
  777. {
  778. ch->GetDesc()->BufferedPacket(&pack, sizeof(TPacketGCShop));
  779. ch->GetDesc()->Packet(&pack2, sizeof(TPacketGCOfflineShopStart));
  780. }
  781. }
  782. else
  783. {
  784. LPOFFLINESHOP pkOfflineShop = npc->GetOfflineShop();
  785. if (!pkOfflineShop)
  786. return;
  787.  
  788. pkOfflineShop->Refresh(ch);
  789. }
  790. }
  791.  
  792. void COfflineShopManager::RefreshMoney(LPCHARACTER ch)
  793. {
  794. if (!ch) return;
  795.  
  796. std::auto_ptr<SQLMsg> pMsg(DBManager::instance().DirectQuery("SELECT money2 FROM player%s WHERE id = %u", get_table_postfix(), ch->GetPlayerID()));
  797. //std::auto_ptr<SQLMsg> pMsg(DBManager::instance().DirectQuery("SELECT money2 FROM player.player WHERE id = %u", ch->GetPlayerID()));
  798.  
  799. TPacketGCShop p;
  800. p.header = HEADER_GC_OFFLINE_SHOP;
  801. p.subheader = SHOP_SUBHEADER_GC_REFRESH_MONEY;
  802.  
  803. TPacketGCOfflineShopMoney p2;
  804. if (pMsg->Get()->uiNumRows == 0)
  805. memset(&p2, 0, sizeof(p2));
  806. else
  807. {
  808. MYSQL_ROW row = mysql_fetch_row(pMsg->Get()->pSQLResult);
  809. str_to_number(p2.llMoney, row[0]);
  810. }
  811.  
  812. p.size = sizeof(p) + sizeof(p2);
  813. if (ch->GetDesc())
  814. {
  815. ch->GetDesc()->BufferedPacket(&p, sizeof(TPacketGCShop));
  816. ch->GetDesc()->Packet(&p2, sizeof(TPacketGCOfflineShopMoney));
  817. }
  818. }
  819.  
  820. DWORD COfflineShopManager::FindMyOfflineShop(DWORD dwPID)
  821. {
  822. TOfflineShopMap::iterator it = m_Map_pkOfflineShopByNPC2.find(dwPID);
  823. if (m_Map_pkOfflineShopByNPC2.end() == it)
  824. return 0;
  825.  
  826. return it->second;
  827. }
  828.  
  829. void COfflineShopManager::ChangeOfflineShopTime(LPCHARACTER ch, BYTE bTime)
  830. {
  831. if (!ch)
  832. return;
  833.  
  834. // Remember
  835. DWORD dwOfflineShopVID = FindMyOfflineShop(ch->GetPlayerID());
  836.  
  837. if (!dwOfflineShopVID)
  838. {
  839. ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("You can't use this option because you don't have offline shop yet!"));
  840. return;
  841. }
  842.  
  843. LPCHARACTER npc = CHARACTER_MANAGER::instance().Find(FindMyOfflineShop(ch->GetPlayerID()));
  844. if (npc)
  845. {
  846. if (test_server)
  847. SendNotice("#DEBUG - Offline Shop NPC Found!");
  848.  
  849. if (bTime == 4)
  850. {
  851. npc->StopOfflineShopUpdateEvent();
  852. }
  853. else
  854. {
  855. int iTime = 0;
  856. switch (bTime)
  857. {
  858. case 1:
  859. iTime = 1 * 60 * 60;
  860. break;
  861. case 2:
  862. iTime = 2 * 60 * 60;
  863. break;
  864. case 3:
  865. iTime = 4 * 60 * 60;
  866. break;
  867. }
  868. std::auto_ptr<SQLMsg> pMsg(DBManager::instance().DirectQuery("UPDATE %soffline_shop_npc SET time = %d WHERE owner_id = %u", get_table_postfix(), iTime, ch->GetPlayerID()));
  869. // ch->ChatPacket(CHAT_TYPE_INFO, "Timp schimbat: %u ora(e)", iTime / 3600);
  870. sys_log(0, "TIMP: %u | PLAYER ID: %u", iTime, ch->GetPlayerID());
  871. npc->StopOfflineShopUpdateEvent();
  872. npc->SetOfflineShopTimer(iTime);
  873. npc->StartOfflineShopUpdateEvent();
  874. LogManager::instance().CharLog(ch, 0, "OFFLINE SHOP", "CHANGE TIME");
  875. }
  876. }
  877. else
  878. {
  879. TPacketGGChangeOfflineShopTime p;
  880. p.bHeader = HEADER_GG_CHANGE_OFFLINE_SHOP_TIME;
  881. p.bTime = bTime;
  882. std::auto_ptr<SQLMsg> pMsg(DBManager::instance().DirectQuery("SELECT mapIndex FROM %soffline_shop_npc WHERE owner_id = %u", get_table_postfix(), ch->GetPlayerID()));
  883. if (pMsg->Get()->uiNumRows == 0)
  884. {
  885. ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("You can't use this option!"));
  886. return;
  887. }
  888.  
  889. MYSQL_ROW row = mysql_fetch_row(pMsg->Get()->pSQLResult);
  890. str_to_number(p.lMapIndex, row[0]);
  891. p.dwOwnerPID = ch->GetPlayerID();
  892.  
  893. P2P_MANAGER::instance().Send(&p, sizeof(TPacketGGChangeOfflineShopTime));
  894. }
  895. }
  896.  
  897. void COfflineShopManager::StopShopping(LPCHARACTER ch)
  898. {
  899. LPOFFLINESHOP pkOfflineShop;
  900.  
  901. if (!(pkOfflineShop = ch->GetOfflineShop()))
  902. return;
  903.  
  904. pkOfflineShop->RemoveGuest(ch);
  905. sys_log(0, "OFFLINE_SHOP: END: %s", ch->GetName());
  906. }
  907.  
  908. void COfflineShopManager::Buy(LPCHARACTER ch, BYTE pos)
  909. {
  910. if (!ch->GetOfflineShop())
  911. return;
  912.  
  913. if (!ch->GetOfflineShopOwner())
  914. return;
  915.  
  916. if (DISTANCE_APPROX(ch->GetX() - ch->GetOfflineShopOwner()->GetX(), ch->GetY() - ch->GetOfflineShopOwner()->GetY()) > 1500)
  917. {
  918. ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("상점과의 거리가 너무 멀어 물건을 살 수 없습니다."));
  919. return;
  920. }
  921.  
  922. LPOFFLINESHOP pkOfflineShop = ch->GetOfflineShop();
  923.  
  924. if (ch->IsGM())
  925. {
  926. ch->ChatPacket(CHAT_TYPE_INFO, "GameMasters cannot buy items from players' shops.");
  927. return;
  928. }
  929.  
  930. if (!pkOfflineShop)
  931. return;
  932.  
  933. long long ret = pkOfflineShop->Buy(ch, pos);
  934.  
  935. // The result is not equal to SHOP_SUBHEADER_GC_OK, send the error to the character.
  936. if (SHOP_SUBHEADER_GC_OK != ret)
  937. {
  938. TPacketGCShop pack;
  939. pack.header = HEADER_GC_OFFLINE_SHOP;
  940. pack.subheader = ret;
  941. pack.size = sizeof(TPacketGCShop);
  942.  
  943. if (ch->GetDesc())
  944. ch->GetDesc()->Packet(&pack, sizeof(pack));
  945. }
  946. }
  947.  
  948. void COfflineShopManager::WithdrawMoney(LPCHARACTER ch, long long llRequiredMoney)
  949. {
  950. if (!ch)
  951. return;
  952.  
  953. if (llRequiredMoney < 0)
  954. return;
  955.  
  956. std::auto_ptr<SQLMsg> pMsg(DBManager::instance().DirectQuery("SELECT money2 FROM player.player WHERE id = %u", ch->GetPlayerID()));
  957. if (pMsg->Get()->uiNumRows == 0)
  958. return;
  959.  
  960. long long llCurrentMoney = 0;
  961. MYSQL_ROW row = mysql_fetch_row(pMsg->Get()->pSQLResult);
  962. str_to_number(llCurrentMoney, row[0]);
  963.  
  964. if (llRequiredMoney >= llCurrentMoney)
  965. {
  966. if (test_server)
  967. ch->ChatPacket(CHAT_TYPE_INFO, "llCurrentMoney(%lu) - llRequiredMoney(%lu)", llCurrentMoney, llRequiredMoney);
  968.  
  969. ch->ChatPacket(CHAT_TYPE_INFO, "Ai introdus o valoare mai mare decat suma curenta !");
  970. return;
  971. }
  972.  
  973. bool isOverFlow = static_cast<long long>(ch->GetGold()) + llRequiredMoney > GOLD_MAX - 1 ? true : false;
  974.  
  975. if (isOverFlow)
  976. {
  977. ch->ChatPacket(CHAT_TYPE_INFO, "Valoare introdusa trebuie sa fie SUMA dorita - 1.");
  978. return;
  979. }
  980.  
  981. ch->PointChange(POINT_GOLD, llRequiredMoney, false);
  982. ch->ChatPacket(CHAT_TYPE_INFO, "Ai primit %lld yang", llRequiredMoney);
  983. DBManager::instance().DirectQuery("UPDATE player.player SET money2 = money2 - %lld WHERE id = %u", llRequiredMoney, ch->GetPlayerID());
  984. LogManager::instance().CharLog(ch, 0, "OFFLINE SHOP", "WITHDRAW MONEY");
  985. }
  986.  
  987. BYTE COfflineShopManager::LeftItemCount(LPCHARACTER ch)
  988. {
  989. if (!ch)
  990. return -1;
  991.  
  992. std::auto_ptr<SQLMsg> pMsg(DBManager::instance().DirectQuery("SELECT COUNT(*) FROM %soffline_shop_item WHERE owner_id = %u", get_table_postfix(), ch->GetPlayerID()));
  993. if (pMsg->Get()->uiNumRows == 0)
  994. return 0;
  995.  
  996. MYSQL_ROW row = mysql_fetch_row(pMsg->Get()->pSQLResult);
  997. BYTE bCount = 0;
  998. str_to_number(bCount, row[0]);
  999. return bCount;
  1000. }
Advertisement
Add Comment
Please, Sign In to add comment