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

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