| 1 |  |  | /* | 
    
    | 2 |  |  |  *  The ManaPlus Client | 
    
    | 3 |  |  |  *  Copyright (C) 2011-2019  The ManaPlus Developers | 
    
    | 4 |  |  |  *  Copyright (C) 2019-2021  Andrei Karas | 
    
    | 5 |  |  |  * | 
    
    | 6 |  |  |  *  This file is part of The ManaPlus Client. | 
    
    | 7 |  |  |  * | 
    
    | 8 |  |  |  *  This program is free software; you can redistribute it and/or modify | 
    
    | 9 |  |  |  *  it under the terms of the GNU General Public License as published by | 
    
    | 10 |  |  |  *  the Free Software Foundation; either version 2 of the License, or | 
    
    | 11 |  |  |  *  any later version. | 
    
    | 12 |  |  |  * | 
    
    | 13 |  |  |  *  This program is distributed in the hope that it will be useful, | 
    
    | 14 |  |  |  *  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
    
    | 15 |  |  |  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
    
    | 16 |  |  |  *  GNU General Public License for more details. | 
    
    | 17 |  |  |  * | 
    
    | 18 |  |  |  *  You should have received a copy of the GNU General Public License | 
    
    | 19 |  |  |  *  along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
    
    | 20 |  |  |  */ | 
    
    | 21 |  |  |  | 
    
    | 22 |  |  | #include "net/eathena/buyingstorehandler.h" | 
    
    | 23 |  |  |  | 
    
    | 24 |  |  | #include "being/being.h" | 
    
    | 25 |  |  | #include "being/playerinfo.h" | 
    
    | 26 |  |  |  | 
    
    | 27 |  |  | #include "const/net/inventory.h" | 
    
    | 28 |  |  |  | 
    
    | 29 |  |  | #include "net/eathena/messageout.h" | 
    
    | 30 |  |  | #include "net/eathena/protocolout.h" | 
    
    | 31 |  |  |  | 
    
    | 32 |  |  | #include "utils/foreach.h" | 
    
    | 33 |  |  |  | 
    
    | 34 |  |  | #include "resources/item/shopitem.h" | 
    
    | 35 |  |  |  | 
    
    | 36 |  |  | #include "debug.h" | 
    
    | 37 |  |  |  | 
    
    | 38 |  |  | extern int packetVersion; | 
    
    | 39 |  |  | extern int itemIdLen; | 
    
    | 40 |  |  |  | 
    
    | 41 |  |  | namespace EAthena | 
    
    | 42 |  |  | { | 
    
    | 43 |  |  |  | 
    
    | 44 |  |  | BuyingStoreHandler::BuyingStoreHandler() | 
    
    | 45 |  |  | { | 
    
    | 46 |  |  |     buyingStoreHandler = this; | 
    
    | 47 |  |  | } | 
    
    | 48 |  |  |  | 
    
    | 49 |  |  | BuyingStoreHandler::~BuyingStoreHandler() | 
    
    | 50 |  |  | { | 
    
    | 51 |  |  |     buyingStoreHandler = nullptr; | 
    
    | 52 |  |  | } | 
    
    | 53 |  |  |  | 
    
    | 54 |  |  | void BuyingStoreHandler::create(const std::string &name, | 
    
    | 55 |  |  |                                 const int maxMoney, | 
    
    | 56 |  |  |                                 const bool flag, | 
    
    | 57 |  |  |                                 const STD_VECTOR<ShopItem*> &items) const | 
    
    | 58 |  |  | { | 
    
    | 59 |  |  |     if (packetVersion < 20100303) | 
    
    | 60 |  |  |         return; | 
    
    | 61 |  |  |     createOutPacket(CMSG_BUYINGSTORE_CREATE); | 
    
    | 62 |  |  |     outMsg.writeInt16(CAST_S16(89 + items.size() * (6 + itemIdLen)), "len"); | 
    
    | 63 |  |  |     outMsg.writeInt32(maxMoney, "limit money"); | 
    
    | 64 |  |  |     outMsg.writeInt8(static_cast<int8_t>(flag), "flag"); | 
    
    | 65 |  |  |     outMsg.writeString(name, 80, "store name"); | 
    
    | 66 |  |  |     FOR_EACH (STD_VECTOR<ShopItem*>::const_iterator, it, items) | 
    
    | 67 |  |  |     { | 
    
    | 68 |  |  |         const ShopItem *const item = *it; | 
    
    | 69 |  |  |         outMsg.writeItemId(item->getId(), "item id"); | 
    
    | 70 |  |  |         outMsg.writeInt16(CAST_S16(item->getQuantity()), "amount"); | 
    
    | 71 |  |  |         outMsg.writeInt32(item->getPrice(), "price"); | 
    
    | 72 |  |  |     } | 
    
    | 73 |  |  | } | 
    
    | 74 |  |  |  | 
    
    | 75 |  |  | void BuyingStoreHandler::close() const | 
    
    | 76 |  |  | { | 
    
    | 77 |  |  |     if (packetVersion < 20100420) | 
    
    | 78 |  |  |         return; | 
    
    | 79 |  |  |     createOutPacket(CMSG_BUYINGSTORE_CLOSE); | 
    
    | 80 |  |  |     PlayerInfo::enableVending(false); | 
    
    | 81 |  |  | } | 
    
    | 82 |  |  |  | 
    
    | 83 |  |  | void BuyingStoreHandler::open(const Being *const being) const | 
    
    | 84 |  |  | { | 
    
    | 85 |  |  |     if (being == nullptr) | 
    
    | 86 |  |  |         return; | 
    
    | 87 |  |  |     if (packetVersion < 20100420) | 
    
    | 88 |  |  |         return; | 
    
    | 89 |  |  |     createOutPacket(CMSG_BUYINGSTORE_OPEN); | 
    
    | 90 |  |  |     outMsg.writeBeingId(being->getId(), "account id"); | 
    
    | 91 |  |  | } | 
    
    | 92 |  |  |  | 
    
    | 93 |  |  | void BuyingStoreHandler::sell(const Being *const being, | 
    
    | 94 |  |  |                               const int storeId, | 
    
    | 95 |  |  |                               const Item *const item, | 
    
    | 96 |  |  |                               const int amount) const | 
    
    | 97 |  |  | { | 
    
    | 98 |  |  |     if ((being == nullptr) || (item == nullptr)) | 
    
    | 99 |  |  |         return; | 
    
    | 100 |  |  |     if (packetVersion < 20100420) | 
    
    | 101 |  |  |         return; | 
    
    | 102 |  |  |  | 
    
    | 103 |  |  |     createOutPacket(CMSG_BUYINGSTORE_SELL); | 
    
    | 104 |  |  |     outMsg.writeInt16(18, "len"); | 
    
    | 105 |  |  |     outMsg.writeBeingId(being->getId(), "account id"); | 
    
    | 106 |  |  |     outMsg.writeInt32(storeId, "store id"); | 
    
    | 107 |  |  |     outMsg.writeInt16(CAST_S16( | 
    
    | 108 |  |  |         item->getInvIndex() + INVENTORY_OFFSET), | 
    
    | 109 |  |  |         "index"); | 
    
    | 110 |  |  |     outMsg.writeItemId(item->getId(), "item id"); | 
    
    | 111 |  |  |     outMsg.writeInt16(CAST_S16(amount), "amount"); | 
    
    | 112 |  |  | } | 
    
    | 113 |  |  |  | 
    
    | 114 |  | 2 | }  // namespace EAthena |