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/vendinghandler.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 |
|
|
#include "net/eathena/vendingrecv.h" |
32 |
|
|
|
33 |
|
|
#include "utils/foreach.h" |
34 |
|
|
|
35 |
|
|
#include "resources/item/shopitem.h" |
36 |
|
|
|
37 |
|
|
#include "debug.h" |
38 |
|
|
|
39 |
|
|
namespace EAthena |
40 |
|
|
{ |
41 |
|
|
|
42 |
|
|
VendingHandler::VendingHandler() |
43 |
|
|
{ |
44 |
|
|
vendingHandler = this; |
45 |
|
|
VendingRecv::mBuyDialog = nullptr; |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
VendingHandler::~VendingHandler() |
49 |
|
|
{ |
50 |
|
|
vendingHandler = nullptr; |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
void VendingHandler::close() const |
54 |
|
|
{ |
55 |
|
|
createOutPacket(CMSG_VENDING_CLOSE); |
56 |
|
|
PlayerInfo::enableVending(false); |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
void VendingHandler::open(const Being *const being) const |
60 |
|
|
{ |
61 |
|
|
if (being == nullptr) |
62 |
|
|
return; |
63 |
|
|
|
64 |
|
|
createOutPacket(CMSG_VENDING_LIST_REQ); |
65 |
|
|
outMsg.writeBeingId(being->getId(), "account id"); |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
void VendingHandler::buy(const Being *const being, |
69 |
|
|
const int index, |
70 |
|
|
const int amount) const |
71 |
|
|
{ |
72 |
|
|
if (being == nullptr) |
73 |
|
|
return; |
74 |
|
|
|
75 |
|
|
createOutPacket(CMSG_VENDING_BUY); |
76 |
|
|
outMsg.writeInt16(12, "len"); |
77 |
|
|
outMsg.writeBeingId(being->getId(), "account id"); |
78 |
|
|
outMsg.writeInt16(CAST_S16(amount), "amount"); |
79 |
|
|
outMsg.writeInt16(CAST_S16(index), "index"); |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
void VendingHandler::buyItems(const Being *const being, |
83 |
|
|
const STD_VECTOR<ShopItem*> &items) const |
84 |
|
|
{ |
85 |
|
|
int cnt = 0; |
86 |
|
|
const int pairSize = 4; |
87 |
|
|
|
88 |
|
|
FOR_EACH (STD_VECTOR<ShopItem*>::const_iterator, it, items) |
89 |
|
|
{ |
90 |
|
|
ShopItem *const item = *it; |
91 |
|
|
const int usedQuantity = item->getUsedQuantity(); |
92 |
|
|
if (usedQuantity == 0) |
93 |
|
|
continue; |
94 |
|
|
cnt ++; |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
if (cnt > 100) |
98 |
|
|
return; |
99 |
|
|
|
100 |
|
|
createOutPacket(CMSG_VENDING_BUY); |
101 |
|
|
outMsg.writeInt16(CAST_S16(4 + 4 + pairSize * cnt), "len"); |
102 |
|
|
outMsg.writeBeingId(being->getId(), "account id"); |
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 |
|
|
outMsg.writeInt16(CAST_S16(usedQuantity), "amount"); |
113 |
|
|
outMsg.writeInt16(CAST_S16(item->getInvIndex()), "index"); |
114 |
|
|
} |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
void VendingHandler::buy2(const Being *const being, |
118 |
|
|
const int vendId, |
119 |
|
|
const int index, |
120 |
|
|
const int amount) const |
121 |
|
|
{ |
122 |
|
|
if (being == nullptr) |
123 |
|
|
return; |
124 |
|
|
|
125 |
|
|
createOutPacket(CMSG_VENDING_BUY2); |
126 |
|
|
outMsg.writeInt16(16, "len"); |
127 |
|
|
outMsg.writeBeingId(being->getId(), "account id"); |
128 |
|
|
outMsg.writeInt32(vendId, "vend id"); |
129 |
|
|
outMsg.writeInt16(CAST_S16(amount), "amount"); |
130 |
|
|
outMsg.writeInt16(CAST_S16(index), "index"); |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
void VendingHandler::createShop(const std::string &name, |
134 |
|
|
const bool flag, |
135 |
|
|
const STD_VECTOR<ShopItem*> &items) const |
136 |
|
|
{ |
137 |
|
|
createOutPacket(CMSG_VENDING_CREATE_SHOP); |
138 |
|
|
outMsg.writeInt16(CAST_S16(85 + items.size() * 8), "len"); |
139 |
|
|
outMsg.writeString(name, 80, "shop name"); |
140 |
|
|
outMsg.writeInt8(CAST_S8(flag ? 1 : 0), "flag"); |
141 |
|
|
FOR_EACH (STD_VECTOR<ShopItem*>::const_iterator, it, items) |
142 |
|
|
{ |
143 |
|
|
const ShopItem *const item = *it; |
144 |
|
|
outMsg.writeInt16(CAST_S16( |
145 |
|
|
item->getInvIndex() + INVENTORY_OFFSET), "index"); |
146 |
|
|
outMsg.writeInt16(CAST_S16(item->getQuantity()), "amount"); |
147 |
|
|
outMsg.writeInt32(item->getPrice(), "price"); |
148 |
|
|
} |
149 |
|
|
} |
150 |
|
|
|
151 |
|
2 |
} // namespace EAthena |