GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/ea/traderecv.cpp Lines: 2 64 3.1 %
Date: 2017-11-29 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-2017  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include "net/ea/traderecv.h"
24
25
#include "notifymanager.h"
26
27
#include "being/playerinfo.h"
28
#include "being/playerrelation.h"
29
30
#include "enums/resources/notifytypes.h"
31
32
#include "gui/windows/tradewindow.h"
33
34
#include "gui/widgets/createwidget.h"
35
36
#include "net/messagein.h"
37
38
#include "utils/gettext.h"
39
#include "utils/stringutils.h"
40
41
#include "listeners/requesttradelistener.h"
42
43
#include "debug.h"
44
45
46
ConfirmDialog *confirmDlg = nullptr;
47
48
/**
49
 * Listener for request trade dialogs
50
 */
51
namespace
52
{
53
2
    RequestTradeListener listener;
54
}  // namespace
55
56
namespace Ea
57
{
58
59
void TradeRecv::processTradeResponseContinue(const uint8_t type)
60
{
61
    switch (type)
62
    {
63
        case 0:  // Too far away
64
            NotifyManager::notify(NotifyTypes::TRADE_FAIL_FAR_AWAY,
65
                tradePartnerName);
66
            break;
67
        case 1:  // Character doesn't exist
68
            NotifyManager::notify(NotifyTypes::TRADE_FAIL_CHAR_NOT_EXISTS,
69
                tradePartnerName);
70
            break;
71
        case 2:  // Invite request check failed...
72
            NotifyManager::notify(NotifyTypes::TRADE_CANCELLED_ERROR);
73
            break;
74
        case 3:  // Trade accepted
75
            if (tradeWindow != nullptr)
76
            {
77
                tradeWindow->reset();
78
                // TRANSLATORS: trade header
79
                tradeWindow->setCaption(strprintf(_("Trade: You and %s"),
80
                    tradePartnerName.c_str()));
81
                tradeWindow->initTrade(tradePartnerName);
82
                tradeWindow->setVisible(Visible_true);
83
            }
84
            break;
85
        case 4:  // Trade cancelled
86
            if (playerRelations.hasPermission(tradePartnerName,
87
                PlayerRelation::SPEECH_LOG))
88
            {
89
                NotifyManager::notify(NotifyTypes::TRADE_CANCELLED_NAME,
90
                    tradePartnerName);
91
            }
92
            // otherwise ignore silently
93
94
            if (tradeWindow != nullptr)
95
            {
96
                tradeWindow->setVisible(Visible_false);
97
//                        tradeWindow->clear();
98
            }
99
            PlayerInfo::setTrading(Trading_false);
100
            break;
101
        case 5:
102
            NotifyManager::notify(NotifyTypes::TRADE_CANCELLED_BUSY,
103
                tradePartnerName);
104
            break;
105
        default:  // Shouldn't happen as well, but to be sure
106
            NotifyManager::notify(NotifyTypes::TRADE_ERROR_UNKNOWN,
107
                tradePartnerName);
108
            if (tradeWindow != nullptr)
109
                tradeWindow->clear();
110
            break;
111
    }
112
}
113
114
void TradeRecv::processTradeOk(Net::MessageIn &msg)
115
{
116
    // 0 means ok from myself, 1 means ok from other;
117
    if (tradeWindow != nullptr)
118
        tradeWindow->receivedOk(msg.readUInt8("status") == 0U);
119
    else
120
        msg.readUInt8("status");
121
}
122
123
void TradeRecv::processTradeCancel(Net::MessageIn &msg A_UNUSED)
124
{
125
    NotifyManager::notify(NotifyTypes::TRADE_CANCELLED);
126
    if (tradeWindow != nullptr)
127
    {
128
        tradeWindow->setVisible(Visible_false);
129
        tradeWindow->reset();
130
    }
131
    PlayerInfo::setTrading(Trading_false);
132
}
133
134
void TradeRecv::processTradeComplete(Net::MessageIn &msg A_UNUSED)
135
{
136
    NotifyManager::notify(NotifyTypes::TRADE_COMPLETE);
137
    if (tradeWindow != nullptr)
138
        tradeWindow->completeTrade();
139
    PlayerInfo::setTrading(Trading_false);
140
}
141
142
void TradeRecv::processTradeRequestContinue(const std::string &partner)
143
{
144
    if (playerRelations.hasPermission(partner,
145
        PlayerRelation::TRADE))
146
    {
147
        if (PlayerInfo::isTrading() == Trading_true || (confirmDlg != nullptr))
148
        {
149
            tradeHandler->respond(false);
150
            return;
151
        }
152
153
        tradePartnerName = partner;
154
        PlayerInfo::setTrading(Trading_true);
155
        if (tradeWindow != nullptr)
156
        {
157
            if (tradePartnerName.empty() || tradeWindow->getAutoTradeNick()
158
                != tradePartnerName)
159
            {
160
                tradeWindow->clear();
161
                CREATEWIDGETV(confirmDlg, ConfirmDialog,
162
                    // TRANSLATORS: trade message
163
                    _("Request for Trade"),
164
                    // TRANSLATORS: trade message
165
                    strprintf(_("%s wants to trade with you, do"
166
                    " you accept?"), tradePartnerName.c_str()),
167
                    SOUND_REQUEST,
168
                    true);
169
                confirmDlg->addActionListener(&listener);
170
            }
171
            else
172
            {
173
                tradeHandler->respond(true);
174
            }
175
        }
176
    }
177
    else
178
    {
179
        tradeHandler->respond(false);
180
        return;
181
    }
182
}
183
184

6
}  // namespace Ea