GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/ea/traderecv.cpp Lines: 2 59 3.4 %
Date: 2021-03-17 Branches: 2 49 4.1 %

Line Branch Exec Source
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/ea/traderecv.h"
25
26
#include "notifymanager.h"
27
28
#include "being/playerinfo.h"
29
#include "being/playerrelation.h"
30
31
#include "const/sound.h"
32
33
#include "enums/resources/notifytypes.h"
34
35
#include "gui/windows/tradewindow.h"
36
37
#include "gui/widgets/createwidget.h"
38
39
#include "net/messagein.h"
40
41
#include "utils/gettext.h"
42
#include "utils/stringutils.h"
43
44
#include "listeners/requesttradelistener.h"
45
46
#include "debug.h"
47
48
49
ConfirmDialog *confirmDlg = nullptr;
50
51
/**
52
 * Listener for request trade dialogs
53
 */
54
namespace
55
{
56
1
    RequestTradeListener listener;
57
}  // namespace
58
59
namespace Ea
60
{
61
62
void TradeRecv::processTradeResponseContinue(const uint8_t type)
63
{
64
    switch (type)
65
    {
66
        case 0:  // Too far away
67
            NotifyManager::notify(NotifyTypes::TRADE_FAIL_FAR_AWAY,
68
                tradePartnerName);
69
            break;
70
        case 1:  // Character doesn't exist
71
            NotifyManager::notify(NotifyTypes::TRADE_FAIL_CHAR_NOT_EXISTS,
72
                tradePartnerName);
73
            break;
74
        case 2:  // Invite request check failed...
75
            NotifyManager::notify(NotifyTypes::TRADE_CANCELLED_ERROR);
76
            break;
77
        case 3:  // Trade accepted
78
            if (tradeWindow != nullptr)
79
            {
80
                tradeWindow->reset();
81
                // TRANSLATORS: trade header
82
                tradeWindow->setCaption(strprintf(_("Trade: You and %s"),
83
                    tradePartnerName.c_str()));
84
                tradeWindow->initTrade(tradePartnerName);
85
                tradeWindow->setVisible(Visible_true);
86
            }
87
            break;
88
        case 4:  // Trade cancelled
89
            if (playerRelations.hasPermission(tradePartnerName,
90
                PlayerRelation::SPEECH_LOG))
91
            {
92
                NotifyManager::notify(NotifyTypes::TRADE_CANCELLED_NAME,
93
                    tradePartnerName);
94
            }
95
            // otherwise ignore silently
96
97
            if (tradeWindow != nullptr)
98
            {
99
                tradeWindow->setVisible(Visible_false);
100
//                        tradeWindow->clear();
101
            }
102
            PlayerInfo::setTrading(Trading_false);
103
            break;
104
        case 5:
105
            NotifyManager::notify(NotifyTypes::TRADE_CANCELLED_BUSY,
106
                tradePartnerName);
107
            break;
108
        default:  // Shouldn't happen as well, but to be sure
109
            NotifyManager::notify(NotifyTypes::TRADE_ERROR_UNKNOWN,
110
                tradePartnerName);
111
            if (tradeWindow != nullptr)
112
                tradeWindow->clear();
113
            break;
114
    }
115
}
116
117
void TradeRecv::processTradeOk(Net::MessageIn &msg)
118
{
119
    // 0 means ok from myself, 1 means ok from other;
120
    if (tradeWindow != nullptr)
121
        tradeWindow->receivedOk(msg.readUInt8("status") == 0U);
122
    else
123
        msg.readUInt8("status");
124
}
125
126
void TradeRecv::processTradeCancel(Net::MessageIn &msg A_UNUSED)
127
{
128
    NotifyManager::notify(NotifyTypes::TRADE_CANCELLED);
129
    if (tradeWindow != nullptr)
130
    {
131
        tradeWindow->setVisible(Visible_false);
132
        tradeWindow->reset();
133
    }
134
    PlayerInfo::setTrading(Trading_false);
135
}
136
137
void TradeRecv::processTradeComplete(Net::MessageIn &msg A_UNUSED)
138
{
139
    NotifyManager::notify(NotifyTypes::TRADE_COMPLETE);
140
    if (tradeWindow != nullptr)
141
        tradeWindow->completeTrade();
142
    PlayerInfo::setTrading(Trading_false);
143
}
144
145
void TradeRecv::processTradeRequestContinue(const std::string &partner)
146
{
147
    if (playerRelations.hasPermission(partner,
148
        PlayerRelation::TRADE))
149
    {
150
        if (PlayerInfo::isTrading() == Trading_true || (confirmDlg != nullptr))
151
        {
152
            tradeHandler->respond(false);
153
            return;
154
        }
155
156
        tradePartnerName = partner;
157
        PlayerInfo::setTrading(Trading_true);
158
        if (tradeWindow != nullptr)
159
        {
160
            if (tradePartnerName.empty() || tradeWindow->getAutoTradeNick()
161
                != tradePartnerName)
162
            {
163
                tradeWindow->clear();
164
                CREATEWIDGETV(confirmDlg, ConfirmDialog,
165
                    // TRANSLATORS: trade message
166
                    _("Request for Trade"),
167
                    // TRANSLATORS: trade message
168
                    strprintf(_("%s wants to trade with you, do"
169
                    " you accept?"), tradePartnerName.c_str()),
170
                    SOUND_REQUEST,
171
                    true,
172
                    Modal_false,
173
                    nullptr);
174
                confirmDlg->addActionListener(&listener);
175
            }
176
            else
177
            {
178
                tradeHandler->respond(true);
179
            }
180
        }
181
    }
182
    else
183
    {
184
        tradeHandler->respond(false);
185
        return;
186
    }
187
}
188
189

3
}  // namespace Ea