ManaPlus
markethandler.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 "net/eathena/marketrecv.h"
25 #include "net/eathena/messageout.h"
27 
28 #include "utils/foreach.h"
29 
31 
32 #include "debug.h"
33 
34 extern int packetVersion;
35 
36 namespace EAthena
37 {
38 
41 {
42  marketHandler = this;
43  MarketRecv::mBuyDialog = nullptr;
44 }
45 
47 {
48  marketHandler = nullptr;
49 }
50 
52 {
53  if (packetVersion < 20131218)
54  return;
55 
56  createOutPacket(CMSG_NPC_MARKET_CLOSE);
57 }
58 
59 void MarketHandler::buyItem(const int itemId,
60  const ItemTypeT type,
61  const ItemColor color A_UNUSED,
62  const int amount) const
63 {
64  if (packetVersion < 20131218)
65  return;
66  const bool nonStack = type == ItemType::Weapon ||
67  type == ItemType::Armor ||
68  type == ItemType::PetEgg ||
69  type == ItemType::PetArmor;
70  int cnt = nonStack ? amount : 1;
71  const int amount2 = nonStack ? 1 : amount;
72  if (cnt > 100)
73  cnt = 100;
74 
75  createOutPacket(CMSG_NPC_MARKET_BUY);
76  outMsg.writeInt16(CAST_S16(4 + 6 * cnt), "len");
77  for (int f = 0; f < cnt; f ++)
78  {
79  outMsg.writeItemId(itemId, "item id");
80  outMsg.writeInt32(CAST_S16(amount2), "amount");
81  }
82 }
83 
84 void MarketHandler::buyItems(const STD_VECTOR<ShopItem*> &items) const
85 {
86  if (packetVersion < 20131218)
87  return;
88  int cnt = 0;
89  const int pairSize = 6;
90 
91  FOR_EACH (STD_VECTOR<ShopItem*>::const_iterator, it, items)
92  {
93  const ShopItem *const item = *it;
94  const int usedQuantity = item->getUsedQuantity();
95  const ItemTypeT type = item->getType();
96  if (usedQuantity == 0)
97  continue;
98  if (type == ItemType::Weapon ||
99  type == ItemType::Armor ||
100  type == ItemType::PetEgg ||
101  type == ItemType::PetArmor)
102  {
103  cnt += item->getUsedQuantity();
104  }
105  else
106  {
107  cnt ++;
108  }
109  }
110 
111  if (cnt > 100)
112  return;
113 
114  createOutPacket(CMSG_NPC_MARKET_BUY);
115  outMsg.writeInt16(CAST_S16(4 + pairSize * cnt), "len");
116  FOR_EACH (STD_VECTOR<ShopItem*>::const_iterator, it, items)
117  {
118  ShopItem *const item = *it;
119  const int usedQuantity = item->getUsedQuantity();
120  if (usedQuantity == 0)
121  continue;
122  item->increaseQuantity(usedQuantity);
123  item->increaseUsedQuantity(-usedQuantity);
124  item->update();
125  const ItemTypeT type = item->getType();
126  if (type == ItemType::Weapon ||
127  type == ItemType::Armor ||
128  type == ItemType::PetEgg ||
129  type == ItemType::PetArmor)
130  {
131  for (int f = 0; f < usedQuantity; f ++)
132  {
133  outMsg.writeItemId(item->getId(),
134  "item id");
135  outMsg.writeInt32(CAST_S16(1), "amount");
136  }
137  }
138  else
139  {
140  outMsg.writeItemId(item->getId(),
141  "item id");
142  outMsg.writeInt32(CAST_S16(usedQuantity), "amount");
143  }
144  }
145 }
146 
147 } // namespace EAthena
#define CAST_S16
Definition: cast.h:28
void buyItem(const int itemId, const ItemTypeT type, const ItemColor color, const int amount) const
void buyItems(const std::vector< ShopItem * > &items) const
int getId() const
Definition: item.h:81
ItemTypeT getType() const
Definition: item.h:225
void increaseQuantity(const int amount)
Definition: item.h:99
void increaseUsedQuantity(const int amount)
Definition: shopitem.cpp:159
int getUsedQuantity() const
Definition: shopitem.h:151
void update()
Definition: shopitem.cpp:119
int packetVersion
Definition: client.cpp:125
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
uint16_t ItemColor
Definition: itemcolor.h:30
ItemType ::T ItemTypeT
Definition: itemtype.h:43
#define A_UNUSED
Definition: localconsts.h:160
Net::MarketHandler * marketHandler
Definition: net.cpp:116
#define createOutPacket(name)
Definition: messageout.h:37
BuyDialog * mBuyDialog
Definition: marketrecv.cpp:53
@ Armor
Definition: itemtype.h:34
@ Weapon
Definition: itemtype.h:33
@ PetArmor
Definition: itemtype.h:37
@ PetEgg
Definition: itemtype.h:36