GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/cashshophandler.cpp Lines: 0 69 0.0 %
Date: 2021-03-17 Branches: 0 74 0.0 %

Line Branch Exec Source
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/cashshophandler.h"
23
24
#include "net/eathena/cashshoprecv.h"
25
#include "net/eathena/messageout.h"
26
#include "net/eathena/protocolout.h"
27
28
#include "utils/foreach.h"
29
30
#include "resources/item/shopitem.h"
31
32
#include "debug.h"
33
34
extern int packetVersion;
35
extern int itemIdLen;
36
37
namespace EAthena
38
{
39
40
CashShopHandler::CashShopHandler() :
41
    Net::CashShopHandler()
42
{
43
    cashShopHandler = this;
44
    CashShopRecv::mBuyDialog = nullptr;
45
}
46
47
CashShopHandler::~CashShopHandler()
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
133
void CashShopHandler::close() const
134
{
135
    if (packetVersion < 20110718)
136
        return;
137
    createOutPacket(CMSG_NPC_CASH_SHOP_CLOSE);
138
}
139
140
void CashShopHandler::requestPoints() const
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
158
void CashShopHandler::schedule() const
159
{
160
    if (packetVersion < 20110614)
161
        return;
162
    createOutPacket(CMSG_NPC_CASH_SHOP_SCHEDULE);
163
}
164
165
}  // namespace EAthena