GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/cashshophandler.cpp Lines: 0 70 0.0 %
Date: 2017-11-29 Branches: 0 74 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2017  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
35
namespace EAthena
36
{
37
38
CashShopHandler::CashShopHandler() :
39
    Net::CashShopHandler()
40
{
41
    cashShopHandler = this;
42
    CashShopRecv::mBuyDialog = nullptr;
43
}
44
45
CashShopHandler::~CashShopHandler()
46
{
47
    cashShopHandler = nullptr;
48
}
49
50
void CashShopHandler::buyItem(const int points,
51
                              const int itemId,
52
                              const ItemColor color A_UNUSED,
53
                              const int amount) const
54
{
55
    if (packetVersion < 20101124)
56
        return;
57
    createOutPacket(CMSG_NPC_CASH_SHOP_BUY);
58
    outMsg.writeInt16(10 + 4, "len");
59
    outMsg.writeInt32(points, "points");
60
    outMsg.writeInt16(1, "count");
61
    outMsg.writeInt16(CAST_S16(amount), "amount");
62
    outMsg.writeInt16(CAST_S16(itemId), "item id");
63
}
64
65
void CashShopHandler::buyItems(const int points,
66
                               const STD_VECTOR<ShopItem*> &items) const
67
{
68
    if (packetVersion < 20101124)
69
        return;
70
71
    int cnt = 0;
72
    const int pairSize = 4;
73
74
    FOR_EACH (STD_VECTOR<ShopItem*>::const_iterator, it, items)
75
    {
76
        const ShopItem *const item = *it;
77
        const int usedQuantity = item->getUsedQuantity();
78
        const ItemTypeT type = item->getType();
79
        if (usedQuantity == 0)
80
            continue;
81
        if (type == ItemType::Weapon ||
82
            type == ItemType::Armor ||
83
            type == ItemType::PetEgg ||
84
            type == ItemType::PetArmor)
85
        {
86
            cnt += item->getUsedQuantity();
87
        }
88
        else
89
        {
90
            cnt ++;
91
        }
92
    }
93
94
    if (cnt > 100)
95
        return;
96
97
    createOutPacket(CMSG_NPC_CASH_SHOP_BUY);
98
    outMsg.writeInt16(CAST_S16(10 + pairSize * cnt), "len");
99
    outMsg.writeInt32(points, "points");
100
    outMsg.writeInt16(CAST_S16(cnt), "count");
101
    FOR_EACH (STD_VECTOR<ShopItem*>::const_iterator, it, items)
102
    {
103
        ShopItem *const item = *it;
104
        const int usedQuantity = item->getUsedQuantity();
105
        if (usedQuantity == 0)
106
            continue;
107
        item->increaseQuantity(usedQuantity);
108
        item->increaseUsedQuantity(-usedQuantity);
109
        item->update();
110
        const ItemTypeT type = item->getType();
111
        if (type == ItemType::Weapon ||
112
            type == ItemType::Armor ||
113
            type == ItemType::PetEgg ||
114
            type == ItemType::PetArmor)
115
        {
116
            for (int f = 0; f < usedQuantity; f ++)
117
            {
118
                outMsg.writeInt16(CAST_S16(1), "amount");
119
                outMsg.writeInt16(CAST_S16(item->getId()),
120
                    "item id");
121
            }
122
        }
123
        else
124
        {
125
            outMsg.writeInt16(CAST_S16(usedQuantity), "amount");
126
            outMsg.writeInt16(CAST_S16(item->getId()), "item id");
127
        }
128
    }
129
}
130
131
void CashShopHandler::close() const
132
{
133
    if (packetVersion < 20110718)
134
        return;
135
    createOutPacket(CMSG_NPC_CASH_SHOP_CLOSE);
136
}
137
138
void CashShopHandler::requestPoints() const
139
{
140
    if (packetVersion < 20110718)
141
        return;
142
    createOutPacket(CMSG_NPC_CASH_SHOP_OPEN);
143
}
144
145
void CashShopHandler::requestTab(const int tab) const
146
{
147
    if (packetVersion < 20110718)
148
        return;
149
    createOutPacket(CMSG_NPC_CASH_SHOP_REQUEST_TAB);
150
    outMsg.writeInt16(CAST_S16(tab), "tab");
151
}
152
153
void CashShopHandler::schedule() const
154
{
155
    if (packetVersion < 20110614)
156
        return;
157
    createOutPacket(CMSG_NPC_CASH_SHOP_SCHEDULE);
158
}
159
160
}  // namespace EAthena