summaryrefslogtreecommitdiff
path: root/src/game-server/trade.cpp
blob: 5150930706c78b790e1f879cdf8185fa5ff678db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 *  The Mana Server
 *  Copyright (C) 2007-2010  The Mana World Development Team
 *
 *  This file is part of The Mana Server.
 *
 *  The Mana Server is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  The Mana Server is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with The Mana Server.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <algorithm>
#include <cassert>

#include "game-server/trade.h"

#include "game-server/character.h"
#include "game-server/gamehandler.h"
#include "game-server/inventory.h"
#include "net/messageout.h"

/*
 * States :
 * TRADE_INIT : A player has ask to make a trade, waiting for accept
 * TRADE_RUN : Both player are now trading and can add objects and GP (1)
 * TRADE_CONFIRM_WAIT : One player has confirm, waiting for the other one
 * TRADE_CONFIRMED : Both player has confirmed and agree button is unlock (2)
 * TRADE_AGREE_WAIT : One player has agreed, waiting for the other one
 */

Trade::Trade(Character *c1, Character *c2):
    mChar1(c1), mChar2(c2), mMoney1(0), mMoney2(0), mState(TRADE_INIT), mCurrencyId(ATTR_GP)
{
    MessageOut msg(GPMSG_TRADE_REQUEST);
    msg.writeInt16(c1->getPublicID());
    c2->getClient()->send(msg);
    c1->setTrading(this);
    c2->setTrading(this);
}

Trade::~Trade()
{
    mChar1->setTrading(NULL);
    mChar2->setTrading(NULL);
}

void Trade::cancel()
{
    MessageOut msg(GPMSG_TRADE_CANCEL);
    mChar1->getClient()->send(msg);
    mChar2->getClient()->send(msg);
    delete this;
}

bool Trade::request(Character *c, int id)
{
    //The trade isn't confirmed, the player which is request is the same.
    if (mState != TRADE_INIT || c != mChar2 || mChar1->getPublicID() != id)
    {
        /* This is not an ack for the current transaction. So assume
           a new one is about to start and cancel the current one. */
        cancel();
        return false;
    }

    //Second player confirmed.

    //Starts trading.
    mState = TRADE_RUN;

    //Telling both player that the trade has started
    MessageOut msg(GPMSG_TRADE_START);
    mChar1->getClient()->send(msg);
    mChar2->getClient()->send(msg);
    return true;
}

bool Trade::perform(TradedItems items, Inventory &inv1, Inventory &inv2)
{
    for (TradedItems::const_iterator i = items.begin(),
         i_end = items.end(); i != i_end; ++i)
    {
        if (i->id != inv1.getItem(i->slot) ||
            inv1.removeFromSlot(i->slot, i->amount) != 0 ||
            inv2.insert(i->id, i->amount) != 0)
        {
            return false;
        }
    }
    return true;
}

void Trade::agree(Character *c)
{
    // No player agreed
    if (mState == TRADE_CONFIRMED)
    {
        // One player agreed, if it's the player 2, make it player 1
        if (c == mChar2)
        {
            std::swap(mChar1, mChar2);
            std::swap(mItems1, mItems2);
            std::swap(mMoney1, mMoney2);
        }
        // First player agrees.
        mState = TRADE_CONFIRM_WAIT;

        // Send the other player that the first player has confirmed
        MessageOut msg(GPMSG_TRADE_AGREED);
        mChar2->getClient()->send(msg);
        return;
    }

    if (mState == TRADE_AGREE_WAIT && c == mChar1)
    {
        // We don't care about the first player, he already agreed
        return;
    }

    // The second player has agreed

    // Check if both player has the objects in their inventories
    // and enouth money, then swap them.
    Inventory v1(mChar1, true), v2(mChar2, true);
    if (mChar1->getAttribute(mCurrencyId) >= mMoney1 - mMoney2 &&
        mChar2->getAttribute(mCurrencyId) >= mMoney2 - mMoney1 &&
        perform(mItems1, v1, v2) &&
        perform(mItems2, v2, v1))
    {
        mChar1->setAttribute(mCurrencyId, mChar1->getAttribute(mCurrencyId)
                             - mMoney1 + mMoney2);
        mChar2->setAttribute(mCurrencyId, mChar2->getAttribute(mCurrencyId)
                             - mMoney2 + mMoney1);
    }
    else
    {
        v1.cancel();
        v2.cancel();
        cancel();
        return;
    }

    MessageOut msg(GPMSG_TRADE_COMPLETE);
    mChar1->getClient()->send(msg);
    mChar2->getClient()->send(msg);
    delete this;
}

void Trade::confirm(Character *c)
{
    if (mState == TRADE_CONFIRMED || mState == TRADE_AGREE_WAIT)
        return;

    if (mState == TRADE_RUN) //No player has confirmed
    {
        //One player confirms, if it's the player 2, make it player 1
        if (c == mChar2)
        {
            std::swap(mChar1, mChar2);
            std::swap(mItems1, mItems2);
            std::swap(mMoney1, mMoney2);
        }
        assert(c == mChar1);
        // First player agrees.
        mState = TRADE_CONFIRM_WAIT;

        //Send the other player that the first player has confirmed
        MessageOut msg(GPMSG_TRADE_CONFIRM);
        mChar2->getClient()->send(msg);
        return;
    }

    if (mState != TRADE_CONFIRM_WAIT || c != mChar2)
    {
        // First player has already agreed. We only care about the second one.
        return;
    }

    mState = TRADE_CONFIRMED;
    MessageOut msg(GPMSG_TRADE_BOTH_CONFIRM);
    mChar1->getClient()->send(msg);
    mChar2->getClient()->send(msg);
}

void Trade::setMoney(Character *c, int amount)
{
    //If the player has already confirmed, exit.
    if ((mState != TRADE_RUN && (mState != TRADE_CONFIRM_WAIT || c != mChar1))
            || amount < 0) return;

    /* Checking now if there is enough money is useless as it can change
       later on. At worst, the transaction will be canceled at the end if
       the client lied. */

    MessageOut msg(GPMSG_TRADE_SET_MONEY);
    msg.writeInt32(amount);

    if (c == mChar1)
    {
        mMoney1 = amount;
        mChar2->getClient()->send(msg);
    }
    else
    {
        assert(c == mChar2);
        mMoney2 = amount;
        mChar1->getClient()->send(msg);
    }

    // Go back to normal run.
    mState = TRADE_RUN;
}

void Trade::addItem(Character *c, int slot, int amount)
{
    //If the player has already confirmed, exit.
    if ((mState != TRADE_RUN && (mState != TRADE_CONFIRM_WAIT || c != mChar1))
            || amount < 0) return;

    Character *other;
    TradedItems *items;
    if (c == mChar1)
    {
        other = mChar2;
        items = &mItems1;
    }
    else
    {
        assert(c == mChar2);
        other = mChar1;
        items = &mItems2;
    }

    // Arbitrary limit to prevent a client from DOSing the server.
    if (items->size() >= 50) return;

    Inventory inv(c);
    int id = inv.getItem(slot);
    if (id == 0) return;

    /* Checking now if there is enough items is useless as it can change
       later on. At worst, the transaction will be canceled at the end if
       the client lied. */

    TradedItem ti = { id, slot, amount };
    items->push_back(ti);

    MessageOut msg(GPMSG_TRADE_ADD_ITEM);
    msg.writeInt16(id);
    msg.writeInt8(amount);
    other->getClient()->send(msg);
}