ManaPlus
buyingstorehandler.cpp
Go to the documentation of this file.
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 
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"
31 
32 #include "utils/foreach.h"
33 
35 
36 #include "debug.h"
37 
38 extern int packetVersion;
39 extern int itemIdLen;
40 
41 namespace EAthena
42 {
43 
45 {
46  buyingStoreHandler = this;
47 }
48 
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 
76 {
77  if (packetVersion < 20100420)
78  return;
79  createOutPacket(CMSG_BUYINGSTORE_CLOSE);
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 } // namespace EAthena
Net::BuyingStoreHandler * buyingStoreHandler
Definition: net.cpp:107
#define CAST_S16
Definition: cast.h:28
BeingId getId() const
Definition: actorsprite.h:64
Definition: being.h:96
void sell(const Being *const being, const int storeId, const Item *const item, const int amount) const
void open(const Being *const being) const
void create(const std::string &name, const int maxMoney, const bool flag, const std::vector< ShopItem * > &items) const
Definition: item.h:50
int getQuantity() const
Definition: item.h:105
int getInvIndex() const
Definition: item.h:165
int getId() const
Definition: item.h:81
int getPrice() const
Definition: shopitem.h:132
static const int INVENTORY_OFFSET
Definition: inventory.h:27
int itemIdLen
Definition: client.cpp:130
int packetVersion
Definition: client.cpp:125
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
#define createOutPacket(name)
Definition: messageout.h:37
void enableVending(const bool b)
Definition: playerinfo.cpp:658