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

6
}  // namespace Ea