summaryrefslogtreecommitdiff
path: root/src/game-server/gamehandler.h
blob: d66caf528e2a943f72081febc5e53ee789914452 (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
/*
 *  The Mana Server
 *  Copyright (C) 2004-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/>.
 */

#ifndef SERVER_GAMEHANDLER_H
#define SERVER_GAMEHANDLER_H

#include "net/connectionhandler.h"
#include "net/netcomputer.h"
#include "utils/tokencollector.h"

class Entity;

enum
{
    CLIENT_LOGIN = 0,
    CLIENT_CONNECTED,
    CLIENT_CHANGE_SERVER,
    CLIENT_QUEUED
};

struct GameClient: NetComputer
{
    GameClient(ENetPeer *peer)
      : NetComputer(peer), character(nullptr), status(CLIENT_LOGIN) {}
    Entity *character;
    int status;
};

/**
 * Manages connections to game client.
 */
class GameHandler: public ConnectionHandler
{
    public:
        GameHandler();

        /**
         * Starts the handler
         */
        bool startListen(enet_uint16 port);

        /**
         * Sends message to the given character.
         */
        void sendTo(Entity *, MessageOut &msg);
        void sendTo(GameClient *, MessageOut &msg);

        /**
         * Kills connection with given character.
         */
        void kill(Entity *);

        /**
         * Prepares a server change for given character.
         */
        void prepareServerChange(Entity *);

        /**
         * Completes a server change for given character ID.
         */
        void completeServerChange(int id, const std::string &token,
                                  const std::string &address, int port);

        /**
         * Updates the party id of the character
         */
        void updateCharacter(int charid, int partyid);

        /**
         * Registers a character that should soon be claimed by a client.
         * @param token token used by the client when connecting.
         */
        void addPendingCharacter(const std::string &token, Entity *);

        /**
         * Combines a client with its character.
         * (Needed for TokenCollector)
         */
        void tokenMatched(GameClient *computer, Entity *character);

        /**
         * Deletes a pending client's data.
         * (Needed for TokenCollector)
         */
        void deletePendingClient(GameClient *computer);

        /**
         * Deletes a pending connection's data.
         * (Needed for TokenCollector)
         */
        void deletePendingConnect(Entity *character);

        /**
         * Gets the character associated to a character name. This method is
         * slow, so it should never be called for regular operations.
         */
        Entity *getCharacterByNameSlow(const std::string &) const;

    protected:
        NetComputer *computerConnected(ENetPeer *);
        void computerDisconnected(NetComputer *);

        /**
         * Processes messages related to core game events.
         */
        void processMessage(NetComputer *computer, MessageIn &message);

    private:
        void handleSay(GameClient &client, MessageIn &message);
        void handleNpc(GameClient &client, MessageIn &message);
        void handlePickup(GameClient &client, MessageIn &message);
        void handleUseItem(GameClient &client, MessageIn &message);
        void handleDrop(GameClient &client, MessageIn &message);
        void handleWalk(GameClient &client, MessageIn &message);

        void handleEquip(GameClient &client, MessageIn &message);
        void handleUnequip(GameClient &client, MessageIn &message);
        void handleMoveItem(GameClient &client, MessageIn &message);

        void handleAttack(GameClient &client, MessageIn &message);
        void handleUseSpecialOnBeing(GameClient &client, MessageIn &message);
        void handleUseSpecialOnPoint(GameClient &client, MessageIn &message);
        void handleActionChange(GameClient &client, MessageIn &message);
        void handleDirectionChange(GameClient &client, MessageIn &message);

        void handleDisconnect(GameClient &client, MessageIn &message);

        void handleTradeRequest(GameClient &client, MessageIn &message);
        void handleTrade(GameClient &client, MessageIn &message);

        void handleNpcBuySell(GameClient &client, MessageIn &message);

        void handleRaiseAttribute(GameClient &client, MessageIn &message);
        void handleLowerAttribute(GameClient &client, MessageIn &message);

        void handleNpcPostSend(GameClient &client, MessageIn &message);

        void handlePartyInvite(GameClient &client, MessageIn &message);

        void handleTriggerEmoticon(GameClient &client, MessageIn &message);

        void sendNpcError(GameClient &client, int id,
                          const std::string &errorMsg);

        /**
         * Container for pending clients and pending connections.
         */
        TokenCollector<GameHandler, GameClient *, Entity *> mTokenCollector;
};

extern GameHandler *gameHandler;

#endif