1 /*
2  *  The ManaPlus Client
3  *  Copyright (C) 2004-2009  The Mana World Development Team
4  *  Copyright (C) 2009-2010  The Mana Developers
5  *  Copyright (C) 2011-2019  The ManaPlus Developers
6  *  Copyright (C) 2019-2021  Andrei Karas
7  *
8  *  This file is part of The ManaPlus Client.
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include "net/tmwa/buysellrecv.h"
25 
26 #include "notifymanager.h"
27 
28 #include "being/playerinfo.h"
29 
30 #include "const/resources/currency.h"
31 
32 #include "enums/resources/notifytypes.h"
33 
34 #include "gui/windows/buydialog.h"
35 
36 #include "gui/widgets/createwidget.h"
37 
38 #include "net/messagein.h"
39 
40 #include "net/ea/buysellrecv.h"
41 
42 #include "debug.h"
43 
44 namespace TmwAthena
45 {
46 
processNpcBuy(Net::MessageIn & msg)47 void BuySellRecv::processNpcBuy(Net::MessageIn &msg)
48 {
49     msg.readInt16("len");
50     const unsigned int n_items = (msg.getLength() - 4U) / 11;
51     CREATEWIDGETV(Ea::BuySellRecv::mBuyDialog, BuyDialog,
52         Ea::BuySellRecv::mNpcId,
53         DEFAULT_CURRENCY);
54     Ea::BuySellRecv::mBuyDialog->setMoney(
55         PlayerInfo::getAttribute(Attributes::MONEY));
56 
57     for (unsigned int k = 0; k < n_items; k++)
58     {
59         const int value = msg.readInt32("price");
60         msg.readInt32("dc value?");
61         const ItemTypeT type = static_cast<ItemTypeT>(msg.readUInt8("type"));
62         const int itemId = msg.readInt16("item id");
63         const ItemColor color = ItemColor_one;
64         Ea::BuySellRecv::mBuyDialog->addItem(itemId, type, color, 0, value);
65     }
66     Ea::BuySellRecv::mBuyDialog->sort();
67 }
68 
processNpcSellResponse(Net::MessageIn & msg)69 void BuySellRecv::processNpcSellResponse(Net::MessageIn &msg)
70 {
71     switch (msg.readUInt8("result"))
72     {
73         case 0:
74             NotifyManager::notify(NotifyTypes::SOLD);
75             break;
76         case 1:
77         default:
78             NotifyManager::notify(NotifyTypes::SELL_FAILED);
79             break;
80         case 2:
81             NotifyManager::notify(NotifyTypes::SELL_TRADE_FAILED);
82             break;
83         case 3:
84             NotifyManager::notify(NotifyTypes::SELL_UNSELLABLE_FAILED);
85             break;
86     }
87 }
88 
processNpcBuyResponse(Net::MessageIn & msg)89 void BuySellRecv::processNpcBuyResponse(Net::MessageIn &msg)
90 {
91     const uint8_t response = msg.readUInt8("response");
92     if (response == 0U)
93     {
94         NotifyManager::notify(NotifyTypes::BUY_DONE);
95         return;
96     }
97     // Reset player money since buy dialog already assumed purchase
98     // would go fine
99     if (Ea::BuySellRecv::mBuyDialog != nullptr)
100     {
101         Ea::BuySellRecv::mBuyDialog->setMoney(
102             PlayerInfo::getAttribute(Attributes::MONEY));
103     }
104     switch (response)
105     {
106         case 1:
107             NotifyManager::notify(NotifyTypes::BUY_FAILED_NO_MONEY);
108             break;
109 
110         case 2:
111             NotifyManager::notify(NotifyTypes::BUY_FAILED_OVERWEIGHT);
112             break;
113 
114         case 3:
115             NotifyManager::notify(NotifyTypes::BUY_FAILED_TOO_MANY_ITEMS);
116             break;
117 
118         default:
119             NotifyManager::notify(NotifyTypes::BUY_FAILED);
120             break;
121     }
122 }
123 
124 }  // namespace TmwAthena
125