summaryrefslogtreecommitdiff
path: root/src/account-server/storage.hpp
blob: 27d12997cd266ed9dcd57b17d85c58342f577cda (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
/*
 *  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 STORAGE_H
#define STORAGE_H

#include <list>
#include <map>
#include <vector>

#include "dal/dataprovider.h"

#include "common/transaction.hpp"

class Account;
class Character;
class ChatChannel;
class Guild;
class Letter;
class Post;

/**
 * The high level interface to the database. Through the storage you can access
 * all accounts, characters, guilds, worlds states, transactions, etc.
 */
class Storage
{
    public:
        Storage();
        ~Storage();

        void open();
        void close();

        Account *getAccount(const std::string &userName);
        Account *getAccount(int accountID);

        Character *getCharacter(int id, Account *owner);
        Character *getCharacter(const std::string &name);

        void addAccount(Account *account);
        void delAccount(Account *account);

        void updateLastLogin(const Account *account);

        void updateCharacterPoints(int charId,
                                   int charPoints, int corrPoints);

        void updateExperience(int charId, int skillId, int skillValue);

        void updateAttribute(int charId, unsigned int attrId,
                             double base, double mod);

        void updateKillCount(int charId, int monsterId, int kills);

        void insertStatusEffect(int charId, int statusId, int time);

        void banCharacter(int id, int duration);

        void delCharacter(int charId) const;
        void delCharacter(Character *character) const;

        void checkBannedAccounts();

        bool doesUserNameExist(const std::string &name);
        bool doesEmailAddressExist(const std::string &email);
        bool doesCharacterNameExist(const std::string &name);

        bool updateCharacter(Character *ptr);

        void flushSkill(const Character *character, int skill_id);

        void addGuild(Guild *guild);
        void removeGuild(Guild *guild);

        void addGuildMember(int guild_id, int memberId);
        void removeGuildMember(int guildId, int memberId);

        void setMemberRights(int guildId, int memberId, int rights);

        std::list<Guild*> getGuildList();

        void flush(Account *);

        std::string getQuestVar(int id, const std::string &);
        void setQuestVar(int id, const std::string &, const std::string &);

        std::string getWorldStateVar(const std::string &name, int map_id = -1);
        void setWorldStateVar(const std::string &name,
                              const std::string &value);
        void setWorldStateVar(const std::string &name, int mapId,
                              const std::string &value);

        void setAccountLevel(int id, int level);
        void setPlayerLevel(int id, int level);

        void storeLetter(Letter *letter);
        Post *getStoredPost(int playerId);
        void deletePost(Letter *letter);

        /**
         * Returns the version of the local item database.
         */
        unsigned int getItemDatabaseVersion() const
        { return mItemDbVersion; }

        void setOnlineStatus(int charId, bool online);

        void addTransaction(const Transaction &trans);

        std::vector<Transaction> getTransactions(unsigned int num);
        std::vector<Transaction> getTransactions(time_t date);

        /**
         * Provides direct access to the database. Use with care!
         */
        dal::DataProvider *database() const { return mDb; }

    private:
        // Prevent copying
        Storage(const Storage &rhs);
        Storage &operator=(const Storage &rhs);

        Account *getAccountBySQL();
        Character *getCharacterBySQL(Account *owner);

        void syncDatabase();

        dal::DataProvider *mDb; /**< the data provider */
        unsigned int mItemDbVersion;    /**< Version of the item database. */
};

extern Storage *storage;

#endif // STORAGE_H