ManaPlus
vendinghandler.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2011-2018 The ManaPlus Developers
4  *
5  * This file is part of The ManaPlus Client.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
22 
23 #include "being/being.h"
24 #include "being/playerinfo.h"
25 
26 #include "const/net/inventory.h"
27 
28 #include "net/eathena/messageout.h"
31 
32 #include "utils/foreach.h"
33 
35 
36 #include "debug.h"
37 
38 namespace EAthena
39 {
40 
42 {
43  vendingHandler = this;
44  VendingRecv::mBuyDialog = nullptr;
45 }
46 
48 {
49  vendingHandler = nullptr;
50 }
51 
53 {
56 }
57 
58 void VendingHandler::open(const Being *const being) const
59 {
60  if (being == nullptr)
61  return;
62 
64  outMsg.writeBeingId(being->getId(), "account id");
65 }
66 
67 void VendingHandler::buy(const Being *const being,
68  const int index,
69  const int amount) const
70 {
71  if (being == nullptr)
72  return;
73 
75  outMsg.writeInt16(12, "len");
76  outMsg.writeBeingId(being->getId(), "account id");
77  outMsg.writeInt16(CAST_S16(amount), "amount");
78  outMsg.writeInt16(CAST_S16(index), "index");
79 }
80 
81 void VendingHandler::buyItems(const Being *const being,
82  const STD_VECTOR<ShopItem*> &items) const
83 {
84  int cnt = 0;
85  const int pairSize = 4;
86 
87  FOR_EACH (STD_VECTOR<ShopItem*>::const_iterator, it, items)
88  {
89  ShopItem *const item = *it;
90  const int usedQuantity = item->getUsedQuantity();
91  if (usedQuantity == 0)
92  continue;
93  cnt ++;
94  }
95 
96  if (cnt > 100)
97  return;
98 
100  outMsg.writeInt16(CAST_S16(4 + 4 + pairSize * cnt), "len");
101  outMsg.writeBeingId(being->getId(), "account id");
102  FOR_EACH (STD_VECTOR<ShopItem*>::const_iterator, it, items)
103  {
104  ShopItem *const item = *it;
105  const int usedQuantity = item->getUsedQuantity();
106  if (usedQuantity == 0)
107  continue;
108  item->increaseQuantity(usedQuantity);
109  item->increaseUsedQuantity(-usedQuantity);
110  item->update();
111  outMsg.writeInt16(CAST_S16(usedQuantity), "amount");
112  outMsg.writeInt16(CAST_S16(item->getInvIndex()), "index");
113  }
114 }
115 
116 void VendingHandler::buy2(const Being *const being,
117  const int vendId,
118  const int index,
119  const int amount) const
120 {
121  if (being == nullptr)
122  return;
123 
125  outMsg.writeInt16(16, "len");
126  outMsg.writeBeingId(being->getId(), "account id");
127  outMsg.writeInt32(vendId, "vend id");
128  outMsg.writeInt16(CAST_S16(amount), "amount");
129  outMsg.writeInt16(CAST_S16(index), "index");
130 }
131 
132 void VendingHandler::createShop(const std::string &name,
133  const bool flag,
134  const STD_VECTOR<ShopItem*> &items) const
135 {
137  outMsg.writeInt16(CAST_S16(85 + items.size() * 8), "len");
138  outMsg.writeString(name, 80, "shop name");
139  outMsg.writeInt8(CAST_S8(flag ? 1 : 0), "flag");
140  FOR_EACH (STD_VECTOR<ShopItem*>::const_iterator, it, items)
141  {
142  const ShopItem *const item = *it;
143  outMsg.writeInt16(CAST_S16(
144  item->getInvIndex() + INVENTORY_OFFSET), "index");
145  outMsg.writeInt16(CAST_S16(item->getQuantity()), "amount");
146  outMsg.writeInt32(item->getPrice(), "price");
147  }
148 }
149 
150 } // namespace EAthena
int getInvIndex() const
Definition: item.h:164
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
int CMSG_VENDING_CREATE_SHOP
int CMSG_VENDING_CLOSE
void buy(const Being *const being, const int index, const int amount) const
int CMSG_VENDING_LIST_REQ
int CMSG_VENDING_BUY
void buyItems(const Being *const being, const std::vector< ShopItem *> &items) const
BeingId getId() const
Definition: actorsprite.h:63
int getUsedQuantity() const
Definition: shopitem.h:150
static const int INVENTORY_OFFSET
Definition: inventory.h:26
void update()
Definition: shopitem.cpp:118
void createShop(const std::string &name, const bool flag, const std::vector< ShopItem *> &items) const
void buy2(const Being *const being, const int vendId, const int index, const int amount) const
int CMSG_VENDING_BUY2
int getPrice() const
Definition: shopitem.h:131
int getQuantity() const
Definition: item.h:104
void open(const Being *const being) const
BuyDialog * mBuyDialog
Definition: vendingrecv.cpp:67
#define CAST_S8
Definition: cast.h:25
void increaseUsedQuantity(const int amount)
Definition: shopitem.cpp:158
#define createOutPacket(name)
Definition: messageout.h:36
void enableVending(const bool b)
Definition: playerinfo.cpp:654
Net::VendingHandler * vendingHandler
Definition: net.cpp:111
#define CAST_S16
Definition: cast.h:27
Definition: being.h:93
void increaseQuantity(const int amount)
Definition: item.h:98