9
3

Trade.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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 = other->GetZone()->GetClientBySpawn(other);
  149. PacketStruct* packet = configReader.getStruct("WS_PlayerTrade", client->GetVersion());
  150. if (packet) {
  151. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(character));
  152. packet->setDataByName("type", 16);
  153. client->QueuePacket(packet->serialize());
  154. safe_delete(packet);
  155. }
  156. }
  157. else if (other->IsBot()) {
  158. Client* client = character->GetZone()->GetClientBySpawn(character);
  159. if (trader1_coins > 0) {
  160. CancelTrade(other);
  161. if (client)
  162. client->SimpleMessage(CHANNEL_ERROR, "Bots can't take coins so the trade was canceled.");
  163. return true;
  164. }
  165. else {
  166. if (!((Bot*)other)->CheckTradeItems(&trader1_items)) {
  167. CancelTrade(other);
  168. if (client)
  169. client->SimpleMessage(CHANNEL_ERROR, "There was an item the bot could not equip so the trade was canceled.");
  170. return true;
  171. }
  172. else
  173. trader2_accepted = true;
  174. }
  175. }
  176. if (HasAcceptedTrade(other)) {
  177. CompleteTrade();
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183. bool Trade::HasAcceptedTrade(Entity* character) {
  184. if (character == trader1)
  185. return trader1_accepted;
  186. else if (character == trader2)
  187. return trader2_accepted;
  188. return false;
  189. }
  190. void Trade::CancelTrade(Entity* character) {
  191. Entity* other = GetTradee(character);
  192. if (other){
  193. if (other->IsPlayer()) {
  194. Client* client = other->GetZone()->GetClientBySpawn(other);
  195. PacketStruct* packet = configReader.getStruct("WS_PlayerTrade", client->GetVersion());
  196. if (packet) {
  197. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(character));
  198. packet->setDataByName("type", 2);
  199. client->QueuePacket(packet->serialize());
  200. safe_delete(packet);
  201. }
  202. }
  203. else if (other->IsBot())
  204. ((Bot*)other)->FinishTrade();
  205. }
  206. trader1->trade = 0;
  207. trader2->trade = 0;
  208. }
  209. void Trade::Trader1ItemAdd(Item* item, int8 quantity, int8 slot) {
  210. trader1_items[slot].item = item;
  211. trader1_items[slot].quantity = quantity;
  212. }
  213. void Trade::Trader2ItemAdd(Item* item, int8 quantity, int8 slot) {
  214. trader2_items[slot].item = item;
  215. trader2_items[slot].quantity = quantity;
  216. }
  217. void Trade::CompleteTrade() {
  218. map<int8, TradeItemInfo>::iterator itr;
  219. vector<Item*> trader1_item_pass;
  220. vector<Item*>::iterator itr2;
  221. string log_string = "TradeComplete:\n";
  222. if (trader1->IsPlayer()) {
  223. Player* player = (Player*)trader1;
  224. Client* client = player->GetZone()->GetClientBySpawn(player);
  225. if (client) {
  226. log_string += "Trader1 = ";
  227. log_string += trader1->GetName();
  228. log_string += "(" + to_string(client->GetCharacterID()) + ")\n";
  229. log_string += "Coins: " + to_string(trader1_coins) + "\n";
  230. log_string += "Items:\n";
  231. player->RemoveCoins(trader1_coins);
  232. for (itr = trader1_items.begin(); itr != trader1_items.end(); itr++) {
  233. // client->RemoveItem can delete the item so we need to store the item id's and quantity to give to trader2
  234. Item* newitem = new Item(itr->second.item);
  235. newitem->details.count = itr->second.quantity;
  236. trader1_item_pass.push_back(newitem);
  237. log_string += itr->second.item->name + " (" + to_string(itr->second.item->details.item_id) + ") x" + to_string(itr->second.quantity) + "\n";
  238. client->RemoveItem(itr->second.item, itr->second.quantity);
  239. }
  240. player->AddCoins(trader2_coins);
  241. for (itr = trader2_items.begin(); itr != trader2_items.end(); itr++) {
  242. Item* newitem = new Item(itr->second.item);
  243. newitem->details.count = itr->second.quantity;
  244. client->AddItem(newitem, nullptr);
  245. }
  246. PacketStruct* packet = configReader.getStruct("WS_PlayerTrade", client->GetVersion());
  247. if (packet) {
  248. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(trader2));
  249. packet->setDataByName("type", 24);
  250. client->QueuePacket(packet->serialize());
  251. safe_delete(packet);
  252. }
  253. }
  254. }
  255. // trader1 is the player who starts the trade, will never be a bot, if trader1 is not a player something went horribly wrong.
  256. if (trader2->IsPlayer()) {
  257. Player* player = (Player*)trader2;
  258. Client* client = player->GetZone()->GetClientBySpawn(player);
  259. if (client) {
  260. log_string += "Trader2 = ";
  261. log_string += trader2->GetName();
  262. log_string += "(" + to_string(client->GetCharacterID()) + ")\n";
  263. log_string += "Coins: " + to_string(trader2_coins) + "\n";
  264. log_string += "Items:\n";
  265. player->RemoveCoins(trader2_coins);
  266. for (itr = trader2_items.begin(); itr != trader2_items.end(); itr++) {
  267. log_string += itr->second.item->name + " (" + to_string(itr->second.item->details.item_id) + ") x" + to_string(itr->second.quantity) + "\n";
  268. client->RemoveItem(itr->second.item, itr->second.quantity);
  269. }
  270. player->AddCoins(trader1_coins);
  271. for (itr2 = trader1_item_pass.begin(); itr2 != trader1_item_pass.end(); itr2++) {
  272. client->AddItem(*itr2, nullptr);
  273. }
  274. PacketStruct* packet = configReader.getStruct("WS_PlayerTrade", client->GetVersion());
  275. if (packet) {
  276. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(trader1));
  277. packet->setDataByName("type", 24);
  278. client->QueuePacket(packet->serialize());
  279. safe_delete(packet);
  280. }
  281. }
  282. }
  283. else if (trader2->IsBot()) {
  284. Bot* bot = (Bot*)trader2;
  285. log_string += "Trader2 is a bot";
  286. for (itr = trader2_items.begin(); itr != trader2_items.end(); itr++) {
  287. bot->RemoveItem(itr->second.item);
  288. }
  289. for (itr2 = trader1_item_pass.begin(); itr2 != trader1_item_pass.end(); itr2++) {
  290. bot->GiveItem(*itr2);
  291. }
  292. bot->FinishTrade();
  293. }
  294. LogWrite(PLAYER__INFO, 0, "Trade", log_string.c_str());
  295. trader1->trade = 0;
  296. trader2->trade = 0;
  297. }
  298. void Trade::OpenTradeWindow() {
  299. if (trader1->IsPlayer()) {
  300. Client* client = trader1->GetZone()->GetClientBySpawn(trader1);
  301. PacketStruct* packet = configReader.getStruct("WS_PlayerTrade", client->GetVersion());
  302. if (packet) {
  303. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(trader2));
  304. packet->setDataByName("type", 1);
  305. client->QueuePacket(packet->serialize());
  306. safe_delete(packet);
  307. }
  308. }
  309. if (trader2->IsPlayer()) {
  310. Client* client = trader2->GetZone()->GetClientBySpawn(trader2);
  311. PacketStruct* packet = configReader.getStruct("WS_PlayerTrade", client->GetVersion());
  312. if (packet) {
  313. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(trader1));
  314. packet->setDataByName("type", 1);
  315. client->QueuePacket(packet->serialize());
  316. safe_delete(packet);
  317. }
  318. }
  319. }
  320. void Trade::SendTradePacket() {
  321. if (trader1->IsPlayer()) {
  322. Client* client = trader1->GetZone()->GetClientBySpawn(trader1);
  323. PacketStruct* packet = configReader.getStruct("WS_PlayerTrade", client->GetVersion());
  324. if (packet) {
  325. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(trader2));
  326. packet->setDataByName("type", 1);
  327. int8 size = (int8)(trader1_items.size());
  328. int8 i = 0;
  329. map<int8, TradeItemInfo>::iterator itr;
  330. packet->setArrayLengthByName("your_item_count", size);
  331. for (itr = trader1_items.begin(); itr != trader1_items.end(); itr++) {
  332. packet->setArrayDataByName("your_item_unknown1", 1, i);
  333. packet->setArrayDataByName("your_item_unknown2", 1, i);
  334. packet->setArrayDataByName("your_item_slot", itr->first, i);
  335. packet->setArrayDataByName("your_item_id", itr->second.item->details.item_id, i);
  336. packet->setArrayDataByName("your_item_quantity", itr->second.quantity, i);
  337. packet->setArrayDataByName("your_item_icon", itr->second.item->details.icon, i);
  338. packet->setArrayDataByName("your_item_background", 0, i); // No clue on this value yet
  339. i++;
  340. }
  341. int32 plat = 0;
  342. int32 gold = 0;
  343. int32 silver = 0;
  344. int32 copper = 0;
  345. CalculateCoins(trader1_coins, plat, gold, silver, copper);
  346. packet->setDataByName("your_copper", copper);
  347. packet->setDataByName("your_silver", silver);
  348. packet->setDataByName("your_gold", gold);
  349. packet->setDataByName("your_plat", plat);
  350. size = (int8)(trader2_items.size());
  351. i = 0;
  352. packet->setArrayLengthByName("their_item_count", size);
  353. for (itr = trader2_items.begin(); itr != trader2_items.end(); itr++) {
  354. packet->setArrayDataByName("their_item_unknown1", 1, i);
  355. packet->setArrayDataByName("their_item_unknown2", 1, i);
  356. packet->setArrayDataByName("their_item_slot", itr->first, i);
  357. packet->setArrayDataByName("their_item_id", itr->second.item->details.item_id, i);
  358. packet->setArrayDataByName("their_item_quantity", itr->second.quantity, i);
  359. packet->setArrayDataByName("their_item_icon", itr->second.item->details.icon, i);
  360. packet->setArrayDataByName("their_item_background", 0, i); // No clue on this value yet
  361. i++;
  362. }
  363. plat = 0;
  364. gold = 0;
  365. silver = 0;
  366. copper = 0;
  367. CalculateCoins(trader2_coins, plat, gold, silver, copper);
  368. packet->setDataByName("their_copper", copper);
  369. packet->setDataByName("their_silver", silver);
  370. packet->setDataByName("their_gold", gold);
  371. packet->setDataByName("their_plat", plat);
  372. LogWrite(PLAYER__ERROR, 0, "Trade", "packet sent");
  373. client->QueuePacket(packet->serialize());
  374. safe_delete(packet);
  375. }
  376. }
  377. if (trader2->IsPlayer()) {
  378. Client* client = trader2->GetZone()->GetClientBySpawn(trader2);
  379. PacketStruct* packet = configReader.getStruct("WS_PlayerTrade", client->GetVersion());
  380. if (packet) {
  381. packet->setDataByName("spawn_id", client->GetPlayer()->GetIDWithPlayerSpawn(trader1));
  382. packet->setDataByName("type", 1);
  383. int8 size = (int8)(trader2_items.size());
  384. int8 i = 0;
  385. map<int8, TradeItemInfo>::iterator itr;
  386. packet->setArrayLengthByName("your_item_count", size);
  387. for (itr = trader2_items.begin(); itr != trader2_items.end(); itr++) {
  388. packet->setArrayDataByName("your_item_unknown1", 1, i);
  389. packet->setArrayDataByName("your_item_unknown2", 1, i);
  390. packet->setArrayDataByName("your_item_slot", itr->first, i);
  391. packet->setArrayDataByName("your_item_id", itr->second.item->details.item_id, i);
  392. packet->setArrayDataByName("your_item_quantity", itr->second.quantity, i);
  393. packet->setArrayDataByName("your_item_icon", itr->second.item->details.icon, i);
  394. packet->setArrayDataByName("your_item_background", 0, i); // No clue on this value yet
  395. i++;
  396. }
  397. int32 plat = 0;
  398. int32 gold = 0;
  399. int32 silver = 0;
  400. int32 copper = 0;
  401. CalculateCoins(trader2_coins, plat, gold, silver, copper);
  402. packet->setDataByName("your_copper", copper);
  403. packet->setDataByName("your_silver", silver);
  404. packet->setDataByName("your_gold", gold);
  405. packet->setDataByName("your_plat", plat);
  406. size = (int8)(trader1_items.size());
  407. i = 0;
  408. packet->setArrayLengthByName("their_item_count", size);
  409. for (itr = trader1_items.begin(); itr != trader1_items.end(); itr++) {
  410. packet->setArrayDataByName("their_item_unknown1", 1, i);
  411. packet->setArrayDataByName("their_item_unknown2", 1, i);
  412. packet->setArrayDataByName("their_item_slot", itr->first, i);
  413. packet->setArrayDataByName("their_item_id", itr->second.item->details.item_id, i);
  414. packet->setArrayDataByName("their_item_quantity", itr->second.quantity, i);
  415. packet->setArrayDataByName("their_item_icon", itr->second.item->details.icon, i);
  416. packet->setArrayDataByName("their_item_background", 0, i); // No clue on this value yet
  417. i++;
  418. }
  419. plat = 0;
  420. gold = 0;
  421. silver = 0;
  422. copper = 0;
  423. CalculateCoins(trader1_coins, plat, gold, silver, copper);
  424. packet->setDataByName("their_copper", copper);
  425. packet->setDataByName("their_silver", silver);
  426. packet->setDataByName("their_gold", gold);
  427. packet->setDataByName("their_plat", plat);
  428. client->QueuePacket(packet->serialize());
  429. safe_delete(packet);
  430. }
  431. }
  432. }
  433. void Trade::CalculateCoins(int64 val, int32& plat, int32& gold, int32& silver, int32& copper) {
  434. int32 tmp = 0;
  435. if (val >= 1000000) {
  436. tmp = val / 1000000;
  437. val -= tmp * 1000000;
  438. plat = tmp;
  439. }
  440. if (val >= 10000) {
  441. tmp = val / 10000;
  442. val -= tmp * 10000;
  443. gold = tmp;
  444. }
  445. if (val >= 100) {
  446. tmp = val / 100;
  447. val -= tmp * 100;
  448. silver = tmp;
  449. }
  450. if (val > 0) {
  451. copper = val;
  452. }
  453. }
  454. int8 Trade::GetNextFreeSlot(Entity* character) {
  455. map<int8, TradeItemInfo>* list = 0;
  456. if (character == trader1)
  457. list = &trader1_items;
  458. else if (character == trader2)
  459. list = &trader2_items;
  460. else
  461. return 255;
  462. int8 ret = 255;
  463. for (int8 index = 0; index < 12; index++) {
  464. if (list->count(index) == 0) {
  465. ret = index;
  466. break;
  467. }
  468. }
  469. return ret;
  470. }