Trade.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. #include "Trade.h"
  2. #include "Items/Items.h"
  3. #include "Entity.h"
  4. #include "Bots/Bot.h"
  5. #include "../common/Log.h"
  6. #include "Rules/Rules.h"
  7. extern ConfigReader configReader;
  8. extern MasterItemList master_item_list;
  9. extern RuleManager rule_manager;
  10. Trade::Trade(Entity* trader1, Entity* trader2) {
  11. this->trader1 = trader1;
  12. this->trader2 = trader2;
  13. trader1_accepted = false;
  14. trader2_accepted = false;
  15. trader1_coins = 0;
  16. trader2_coins = 0;
  17. OpenTradeWindow();
  18. }
  19. Trade::~Trade() {
  20. }
  21. int8 Trade::AddItemToTrade(Entity* character, Item* item, int8 quantity, int8 slot) {
  22. LogWrite(PLAYER__ERROR, 0, "Trade", "Player (%s) adding item (%u) to slot %u of the trade window", character->GetName(), item->details.item_id, slot);
  23. if (slot == 255)
  24. slot = GetNextFreeSlot(character);
  25. if (slot < 0 || slot > 11) {
  26. LogWrite(PLAYER__ERROR, 0, "Trade", "Player (%s) tried to add an item to an invalid trade slot (%u)", character->GetName(), slot);
  27. return 255;
  28. }
  29. Entity* other = GetTradee(character);
  30. int8 result = CheckItem(character, item, other);
  31. if (result == 0) {
  32. if (character == trader1) {
  33. Trader1ItemAdd(item, quantity, slot);
  34. // Only trader2 can be a bot so only
  35. // need to do the bot check here
  36. if (trader2->IsBot()) {
  37. ((Bot*)trader2)->TradeItemAdded(item);
  38. }
  39. }
  40. else if (character == trader2)
  41. Trader2ItemAdd(item, quantity, slot);
  42. else {
  43. LogWrite(PLAYER__ERROR, 0, "Trade", "Player (%s) tried to add an item to a trade but was neither trader1 or trader2", character->GetName());
  44. return 255;
  45. }
  46. }
  47. SendTradePacket();
  48. return result;
  49. }
  50. int8 Trade::CheckItem(Entity* trader, Item* item, Entity* other) {
  51. int8 ret = 0;
  52. map<int8, TradeItemInfo>* list = 0;
  53. map<int8, TradeItemInfo>::iterator itr;
  54. bool other_is_bot = false;
  55. if(other)
  56. other_is_bot = other->IsBot();
  57. if (trader == trader1)
  58. list = &trader1_items;
  59. else if (trader == trader2)
  60. list = &trader2_items;
  61. if (list) {
  62. if (trader->IsPlayer()) {
  63. // Check to see if the item is already in the trade
  64. for (itr = list->begin(); itr != list->end(); itr++) {
  65. if (itr->second.item->details.unique_id == item->details.unique_id) {
  66. ret = 1;
  67. break;
  68. }
  69. }
  70. // Only allow heirloom and no-trade items to be traded with a bot
  71. if (!other_is_bot) {
  72. if (item->CheckFlag(NO_TRADE))
  73. ret = 2;
  74. if (item->CheckFlag2(HEIRLOOM)) {
  75. if(other->IsPlayer() && item->grouped_char_ids.find(((Player*)other)->GetCharacterID()) != item->grouped_char_ids.end()) {
  76. double diffInSeconds = 0.0; std::difftime(std::time(nullptr), item->created);
  77. if(item->CheckFlag(ATTUNED) || ((diffInSeconds = std::difftime(std::time(nullptr), item->created)) && diffInSeconds >= rule_manager.GetGlobalRule(R_Player, HeirloomItemShareExpiration)->GetFloat())) {
  78. ret = 3; // denied heirloom cannot be transferred to outside of group after 48 hours (by rule setting) or if already attuned
  79. }
  80. }
  81. else {
  82. ret = 3; // not part of the group/raid
  83. }
  84. }
  85. }
  86. }
  87. }
  88. return ret;
  89. }
  90. void Trade::RemoveItemFromTrade(Entity* character, int8 slot) {
  91. map<int8, TradeItemInfo>* list = 0;
  92. if (character == trader1)
  93. list = &trader1_items;
  94. else if (character == trader2)
  95. list = &trader2_items;
  96. if (list) {
  97. if (list->count(slot) > 0) {
  98. list->erase(slot);
  99. SendTradePacket();
  100. }
  101. else
  102. LogWrite(PLAYER__ERROR, 0, "Trade", "Player (%s) tried to remove an item from a trade slot that was empty ", character->GetName());
  103. }
  104. }
  105. void Trade::AddCoinToTrade(Entity* character, int64 amount) {
  106. if (!character->IsPlayer())
  107. return;
  108. if (character == trader1) {
  109. trader1_coins += amount;
  110. if (!((Player*)character)->HasCoins(trader1_coins)) {
  111. trader1_coins -= amount;
  112. LogWrite(PLAYER__ERROR, 0, "Trade", "Player (%s) tried to add more coins then they had ", character->GetName());
  113. }
  114. }
  115. else if (character == trader2) {
  116. trader2_coins += amount;
  117. if (!((Player*)character)->HasCoins(trader2_coins)) {
  118. trader2_coins -= amount;
  119. LogWrite(PLAYER__ERROR, 0, "Trade", "Player (%s) tried to add more coins then they had ", character->GetName());
  120. }
  121. }
  122. SendTradePacket();
  123. }
  124. void Trade::RemoveCoinFromTrade(Entity* character, int64 amount) {
  125. if (character == trader1)
  126. trader1_coins = (amount >= trader1_coins) ? 0 : trader1_coins - amount;
  127. else if (character == trader2)
  128. trader2_coins = (amount >= trader2_coins) ? 0 : trader2_coins - amount;
  129. SendTradePacket();
  130. }
  131. Entity* Trade::GetTradee(Entity* character) {
  132. if (character == trader1)
  133. return trader2;
  134. else if (character == trader2)
  135. return trader1;
  136. return 0;
  137. }
  138. bool Trade::SetTradeAccepted(Entity* character) {
  139. if (character == trader1)
  140. trader1_accepted = true;
  141. else if (character == trader2)
  142. trader2_accepted = true;
  143. else
  144. return false;
  145. Entity* other = GetTradee(character);
  146. if (other) {
  147. if (other->IsPlayer()) {
  148. Client* client = ((Player*)other)->GetClient();
  149. if(client) {
  150. PacketStruct* packet = configReader.getStruct("WS_PlayerTrade", client->GetVersion());
  151. if (packet) {
  152. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(character));
  153. packet->setDataByName("type", 16);
  154. client->QueuePacket(packet->serialize());
  155. safe_delete(packet);
  156. }
  157. }
  158. }
  159. else if (other->IsBot()) {
  160. Client* client = ((Player*)character)->GetClient();
  161. if (trader1_coins > 0) {
  162. CancelTrade(other);
  163. if (client)
  164. client->SimpleMessage(CHANNEL_ERROR, "Bots can't take coins so the trade was canceled.");
  165. return true;
  166. }
  167. else {
  168. if (!((Bot*)other)->CheckTradeItems(&trader1_items)) {
  169. CancelTrade(other);
  170. if (client)
  171. client->SimpleMessage(CHANNEL_ERROR, "There was an item the bot could not equip so the trade was canceled.");
  172. return true;
  173. }
  174. else
  175. trader2_accepted = true;
  176. }
  177. }
  178. if (HasAcceptedTrade(other)) {
  179. CompleteTrade();
  180. return true;
  181. }
  182. }
  183. return false;
  184. }
  185. bool Trade::HasAcceptedTrade(Entity* character) {
  186. if (character == trader1)
  187. return trader1_accepted;
  188. else if (character == trader2)
  189. return trader2_accepted;
  190. return false;
  191. }
  192. void Trade::CancelTrade(Entity* character) {
  193. Entity* other = GetTradee(character);
  194. if (other){
  195. if (other->IsPlayer()) {
  196. Client* client = ((Player*)other)->GetClient();
  197. if(client) {
  198. PacketStruct* packet = configReader.getStruct("WS_PlayerTrade", client->GetVersion());
  199. if (packet) {
  200. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(character));
  201. packet->setDataByName("type", 2);
  202. client->QueuePacket(packet->serialize());
  203. safe_delete(packet);
  204. }
  205. }
  206. }
  207. else if (other->IsBot())
  208. ((Bot*)other)->FinishTrade();
  209. }
  210. trader1->trade = 0;
  211. trader2->trade = 0;
  212. }
  213. void Trade::Trader1ItemAdd(Item* item, int8 quantity, int8 slot) {
  214. trader1_items[slot].item = item;
  215. trader1_items[slot].quantity = quantity;
  216. }
  217. void Trade::Trader2ItemAdd(Item* item, int8 quantity, int8 slot) {
  218. trader2_items[slot].item = item;
  219. trader2_items[slot].quantity = quantity;
  220. }
  221. void Trade::CompleteTrade() {
  222. map<int8, TradeItemInfo>::iterator itr;
  223. vector<Item*> trader1_item_pass;
  224. vector<Item*>::iterator itr2;
  225. string log_string = "TradeComplete:\n";
  226. if (trader1->IsPlayer()) {
  227. Player* player = (Player*)trader1;
  228. Client* client = ((Player*)player)->GetClient();
  229. if (client) {
  230. log_string += "Trader1 = ";
  231. log_string += trader1->GetName();
  232. log_string += "(" + to_string(client->GetCharacterID()) + ")\n";
  233. log_string += "Coins: " + to_string(trader1_coins) + "\n";
  234. log_string += "Items:\n";
  235. player->RemoveCoins(trader1_coins);
  236. for (itr = trader1_items.begin(); itr != trader1_items.end(); itr++) {
  237. // client->RemoveItem can delete the item so we need to store the item id's and quantity to give to trader2
  238. Item* newitem = new Item(itr->second.item);
  239. newitem->details.count = itr->second.quantity;
  240. trader1_item_pass.push_back(newitem);
  241. log_string += itr->second.item->name + " (" + to_string(itr->second.item->details.item_id) + ") x" + to_string(itr->second.quantity) + "\n";
  242. client->RemoveItem(itr->second.item, itr->second.quantity);
  243. }
  244. player->AddCoins(trader2_coins);
  245. for (itr = trader2_items.begin(); itr != trader2_items.end(); itr++) {
  246. Item* newitem = new Item(itr->second.item);
  247. newitem->details.count = itr->second.quantity;
  248. client->AddItem(newitem, nullptr);
  249. }
  250. PacketStruct* packet = configReader.getStruct("WS_PlayerTrade", client->GetVersion());
  251. if (packet) {
  252. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(trader2));
  253. packet->setDataByName("type", 24);
  254. client->QueuePacket(packet->serialize());
  255. safe_delete(packet);
  256. }
  257. }
  258. }
  259. // trader1 is the player who starts the trade, will never be a bot, if trader1 is not a player something went horribly wrong.
  260. if (trader2->IsPlayer()) {
  261. Player* player = (Player*)trader2;
  262. Client* client = ((Player*)player)->GetClient();
  263. if (client) {
  264. log_string += "Trader2 = ";
  265. log_string += trader2->GetName();
  266. log_string += "(" + to_string(client->GetCharacterID()) + ")\n";
  267. log_string += "Coins: " + to_string(trader2_coins) + "\n";
  268. log_string += "Items:\n";
  269. player->RemoveCoins(trader2_coins);
  270. for (itr = trader2_items.begin(); itr != trader2_items.end(); itr++) {
  271. log_string += itr->second.item->name + " (" + to_string(itr->second.item->details.item_id) + ") x" + to_string(itr->second.quantity) + "\n";
  272. client->RemoveItem(itr->second.item, itr->second.quantity);
  273. }
  274. player->AddCoins(trader1_coins);
  275. for (itr2 = trader1_item_pass.begin(); itr2 != trader1_item_pass.end(); itr2++) {
  276. client->AddItem(*itr2, nullptr);
  277. }
  278. PacketStruct* packet = configReader.getStruct("WS_PlayerTrade", client->GetVersion());
  279. if (packet) {
  280. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(trader1));
  281. packet->setDataByName("type", 24);
  282. client->QueuePacket(packet->serialize());
  283. safe_delete(packet);
  284. }
  285. }
  286. }
  287. else if (trader2->IsBot()) {
  288. Bot* bot = (Bot*)trader2;
  289. log_string += "Trader2 is a bot";
  290. for (itr = trader2_items.begin(); itr != trader2_items.end(); itr++) {
  291. bot->RemoveItem(itr->second.item);
  292. }
  293. for (itr2 = trader1_item_pass.begin(); itr2 != trader1_item_pass.end(); itr2++) {
  294. bot->GiveItem(*itr2);
  295. }
  296. bot->FinishTrade();
  297. }
  298. LogWrite(PLAYER__INFO, 0, "Trade", log_string.c_str());
  299. trader1->trade = 0;
  300. trader2->trade = 0;
  301. }
  302. void Trade::OpenTradeWindow() {
  303. if (trader1->IsPlayer()) {
  304. Client* client = ((Player*)trader1)->GetClient();
  305. if(client) {
  306. PacketStruct* packet = configReader.getStruct("WS_PlayerTrade", client->GetVersion());
  307. if (packet) {
  308. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(trader2));
  309. packet->setDataByName("type", 1);
  310. client->QueuePacket(packet->serialize());
  311. safe_delete(packet);
  312. }
  313. }
  314. }
  315. if (trader2->IsPlayer()) {
  316. Client* client = ((Player*)trader2)->GetClient();
  317. if(client) {
  318. PacketStruct* packet = configReader.getStruct("WS_PlayerTrade", client->GetVersion());
  319. if (packet) {
  320. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(trader1));
  321. packet->setDataByName("type", 1);
  322. client->QueuePacket(packet->serialize());
  323. safe_delete(packet);
  324. }
  325. }
  326. }
  327. }
  328. void Trade::SendTradePacket() {
  329. if (trader1->IsPlayer()) {
  330. Client* client = ((Player*)trader1)->GetClient();
  331. if(!client) {
  332. return;
  333. }
  334. PacketStruct* packet = configReader.getStruct("WS_PlayerTrade", client->GetVersion());
  335. if (packet) {
  336. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(trader2));
  337. packet->setDataByName("type", 1);
  338. int8 size = (int8)(trader1_items.size());
  339. int8 i = 0;
  340. map<int8, TradeItemInfo>::iterator itr;
  341. packet->setArrayLengthByName("your_item_count", size);
  342. for (itr = trader1_items.begin(); itr != trader1_items.end(); itr++) {
  343. packet->setArrayDataByName("your_item_unknown1", 1, i);
  344. packet->setArrayDataByName("your_item_unknown2", 1, i);
  345. packet->setArrayDataByName("your_item_slot", itr->first, i);
  346. packet->setArrayDataByName("your_item_id", itr->second.item->details.item_id, i);
  347. packet->setArrayDataByName("your_item_quantity", itr->second.quantity, i);
  348. packet->setArrayDataByName("your_item_icon", itr->second.item->details.icon, i);
  349. packet->setArrayDataByName("your_item_background", 0, i); // No clue on this value yet
  350. i++;
  351. }
  352. int32 plat = 0;
  353. int32 gold = 0;
  354. int32 silver = 0;
  355. int32 copper = 0;
  356. CalculateCoins(trader1_coins, plat, gold, silver, copper);
  357. packet->setDataByName("your_copper", copper);
  358. packet->setDataByName("your_silver", silver);
  359. packet->setDataByName("your_gold", gold);
  360. packet->setDataByName("your_plat", plat);
  361. size = (int8)(trader2_items.size());
  362. i = 0;
  363. packet->setArrayLengthByName("their_item_count", size);
  364. for (itr = trader2_items.begin(); itr != trader2_items.end(); itr++) {
  365. packet->setArrayDataByName("their_item_unknown1", 1, i);
  366. packet->setArrayDataByName("their_item_unknown2", 1, i);
  367. packet->setArrayDataByName("their_item_slot", itr->first, i);
  368. packet->setArrayDataByName("their_item_id", itr->second.item->details.item_id, i);
  369. packet->setArrayDataByName("their_item_quantity", itr->second.quantity, i);
  370. packet->setArrayDataByName("their_item_icon", itr->second.item->details.icon, i);
  371. packet->setArrayDataByName("their_item_background", 0, i); // No clue on this value yet
  372. i++;
  373. }
  374. plat = 0;
  375. gold = 0;
  376. silver = 0;
  377. copper = 0;
  378. CalculateCoins(trader2_coins, plat, gold, silver, copper);
  379. packet->setDataByName("their_copper", copper);
  380. packet->setDataByName("their_silver", silver);
  381. packet->setDataByName("their_gold", gold);
  382. packet->setDataByName("their_plat", plat);
  383. LogWrite(PLAYER__ERROR, 0, "Trade", "packet sent");
  384. client->QueuePacket(packet->serialize());
  385. safe_delete(packet);
  386. }
  387. }
  388. if (trader2->IsPlayer()) {
  389. Client* client = ((Player*)trader2)->GetClient();
  390. if(!client) {
  391. return;
  392. }
  393. PacketStruct* packet = configReader.getStruct("WS_PlayerTrade", client->GetVersion());
  394. if (packet) {
  395. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(trader1));
  396. packet->setDataByName("type", 1);
  397. int8 size = (int8)(trader2_items.size());
  398. int8 i = 0;
  399. map<int8, TradeItemInfo>::iterator itr;
  400. packet->setArrayLengthByName("your_item_count", size);
  401. for (itr = trader2_items.begin(); itr != trader2_items.end(); itr++) {
  402. packet->setArrayDataByName("your_item_unknown1", 1, i);
  403. packet->setArrayDataByName("your_item_unknown2", 1, i);
  404. packet->setArrayDataByName("your_item_slot", itr->first, i);
  405. packet->setArrayDataByName("your_item_id", itr->second.item->details.item_id, i);
  406. packet->setArrayDataByName("your_item_quantity", itr->second.quantity, i);
  407. packet->setArrayDataByName("your_item_icon", itr->second.item->details.icon, i);
  408. packet->setArrayDataByName("your_item_background", 0, i); // No clue on this value yet
  409. i++;
  410. }
  411. int32 plat = 0;
  412. int32 gold = 0;
  413. int32 silver = 0;
  414. int32 copper = 0;
  415. CalculateCoins(trader2_coins, plat, gold, silver, copper);
  416. packet->setDataByName("your_copper", copper);
  417. packet->setDataByName("your_silver", silver);
  418. packet->setDataByName("your_gold", gold);
  419. packet->setDataByName("your_plat", plat);
  420. size = (int8)(trader1_items.size());
  421. i = 0;
  422. packet->setArrayLengthByName("their_item_count", size);
  423. for (itr = trader1_items.begin(); itr != trader1_items.end(); itr++) {
  424. packet->setArrayDataByName("their_item_unknown1", 1, i);
  425. packet->setArrayDataByName("their_item_unknown2", 1, i);
  426. packet->setArrayDataByName("their_item_slot", itr->first, i);
  427. packet->setArrayDataByName("their_item_id", itr->second.item->details.item_id, i);
  428. packet->setArrayDataByName("their_item_quantity", itr->second.quantity, i);
  429. packet->setArrayDataByName("their_item_icon", itr->second.item->details.icon, i);
  430. packet->setArrayDataByName("their_item_background", 0, i); // No clue on this value yet
  431. i++;
  432. }
  433. plat = 0;
  434. gold = 0;
  435. silver = 0;
  436. copper = 0;
  437. CalculateCoins(trader1_coins, plat, gold, silver, copper);
  438. packet->setDataByName("their_copper", copper);
  439. packet->setDataByName("their_silver", silver);
  440. packet->setDataByName("their_gold", gold);
  441. packet->setDataByName("their_plat", plat);
  442. client->QueuePacket(packet->serialize());
  443. safe_delete(packet);
  444. }
  445. }
  446. }
  447. void Trade::CalculateCoins(int64 val, int32& plat, int32& gold, int32& silver, int32& copper) {
  448. int32 tmp = 0;
  449. if (val >= 1000000) {
  450. tmp = val / 1000000;
  451. val -= tmp * 1000000;
  452. plat = tmp;
  453. }
  454. if (val >= 10000) {
  455. tmp = val / 10000;
  456. val -= tmp * 10000;
  457. gold = tmp;
  458. }
  459. if (val >= 100) {
  460. tmp = val / 100;
  461. val -= tmp * 100;
  462. silver = tmp;
  463. }
  464. if (val > 0) {
  465. copper = val;
  466. }
  467. }
  468. int8 Trade::GetNextFreeSlot(Entity* character) {
  469. map<int8, TradeItemInfo>* list = 0;
  470. if (character == trader1)
  471. list = &trader1_items;
  472. else if (character == trader2)
  473. list = &trader2_items;
  474. else
  475. return 255;
  476. int8 ret = 255;
  477. for (int8 index = 0; index < 12; index++) {
  478. if (list->count(index) == 0) {
  479. ret = index;
  480. break;
  481. }
  482. }
  483. return ret;
  484. }