ManaPlus
cashshophandler.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 
25 #include "net/eathena/messageout.h"
27 
28 #include "utils/foreach.h"
29 
31 
32 #include "debug.h"
33 
34 extern int packetVersion;
35 extern int itemIdLen;
36 
37 namespace EAthena
38 {
39 
42 {
43  cashShopHandler = this;
44  CashShopRecv::mBuyDialog = nullptr;
45 }
46 
48 {
49  cashShopHandler = nullptr;
50 }
51 
52 void CashShopHandler::buyItem(const int points,
53  const int itemId,
54  const ItemColor color A_UNUSED,
55  const int amount) const
56 {
57  if (packetVersion < 20101124)
58  return;
59  createOutPacket(CMSG_NPC_CASH_SHOP_BUY);
60  outMsg.writeInt16(CAST_S16(10 + (2 + itemIdLen)), "len");
61  outMsg.writeInt32(points, "points");
62  outMsg.writeInt16(1, "count");
63  outMsg.writeInt16(CAST_S16(amount), "amount");
64  outMsg.writeItemId(itemId, "item id");
65 }
66 
67 void CashShopHandler::buyItems(const int points,
68  const STD_VECTOR<ShopItem*> &items) const
69 {
70  if (packetVersion < 20101124)
71  return;
72 
73  int cnt = 0;
74  const int pairSize = 2 + itemIdLen;
75 
76  FOR_EACH (STD_VECTOR<ShopItem*>::const_iterator, it, items)
77  {
78  const ShopItem *const item = *it;
79  const int usedQuantity = item->getUsedQuantity();
80  const ItemTypeT type = item->getType();
81  if (usedQuantity == 0)
82  continue;
83  if (type == ItemType::Weapon ||
84  type == ItemType::Armor ||
85  type == ItemType::PetEgg ||
86  type == ItemType::PetArmor)
87  {
88  cnt += item->getUsedQuantity();
89  }
90  else
91  {
92  cnt ++;
93  }
94  }
95 
96  if (cnt > 100)
97  return;
98 
99  createOutPacket(CMSG_NPC_CASH_SHOP_BUY);
100  outMsg.writeInt16(CAST_S16(10 + pairSize * cnt), "len");
101  outMsg.writeInt32(points, "points");
102  outMsg.writeInt16(CAST_S16(cnt), "count");
103  FOR_EACH (STD_VECTOR<ShopItem*>::const_iterator, it, items)
104  {
105  ShopItem *const item = *it;
106  const int usedQuantity = item->getUsedQuantity();
107  if (usedQuantity == 0)
108  continue;
109  item->increaseQuantity(usedQuantity);
110  item->increaseUsedQuantity(-usedQuantity);
111  item->update();
112  const ItemTypeT type = item->getType();
113  if (type == ItemType::Weapon ||
114  type == ItemType::Armor ||
115  type == ItemType::PetEgg ||
116  type == ItemType::PetArmor)
117  {
118  for (int f = 0; f < usedQuantity; f ++)
119  {
120  outMsg.writeInt16(CAST_S16(1), "amount");
121  outMsg.writeItemId(item->getId(),
122  "item id");
123  }
124  }
125  else
126  {
127  outMsg.writeInt16(CAST_S16(usedQuantity), "amount");
128  outMsg.writeItemId(item->getId(), "item id");
129  }
130  }
131 }
132 
134 {
135  if (packetVersion < 20110718)
136  return;
137  createOutPacket(CMSG_NPC_CASH_SHOP_CLOSE);
138 }
139 
141 {
142  if (packetVersion < 20110718)
143  return;
144  createOutPacket(CMSG_NPC_CASH_SHOP_OPEN);
145 }
146 
147 void CashShopHandler::requestTab(const int tab) const
148 {
149  if (packetVersion < 20110222 ||
150  packetVersion >= 20120000)
151  {
152  return;
153  }
154  createOutPacket(CMSG_NPC_CASH_SHOP_REQUEST_TAB);
155  outMsg.writeInt16(CAST_S16(tab), "tab");
156 }
157 
159 {
160  if (packetVersion < 20110614)
161  return;
162  createOutPacket(CMSG_NPC_CASH_SHOP_SCHEDULE);
163 }
164 
165 } // namespace EAthena
Net::CashShopHandler * cashShopHandler
Definition: net.cpp:108
#define CAST_S16
Definition: cast.h:28
void buyItems(const int points, const std::vector< ShopItem * > &items) const
void requestTab(const int tab) const
void buyItem(const int points, const int itemId, const ItemColor color, const int amount) 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 itemIdLen
Definition: client.cpp:130
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
#define createOutPacket(name)
Definition: messageout.h:37
@ Armor
Definition: itemtype.h:34
@ Weapon
Definition: itemtype.h:33
@ PetArmor
Definition: itemtype.h:37
@ PetEgg
Definition: itemtype.h:36