root/trunk/src/game/GMTicketMgr.h @ 268

Revision 263, 3.4 kB (checked in by yumileroy, 17 years ago)

Some missing changes. This should fix the bug that loading char causes crash.
Please do not commit to the other tip (I do not know how to delete it).

Original author: megamage
Date: 2008-11-20 17:40:13-06:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://getmangos.com/>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18
19#ifndef _GMTICKETMGR_H
20#define _GMTICKETMGR_H
21
22#include "Policies/Singleton.h"
23#include "Database/DatabaseEnv.h"
24#include "Util.h"
25#include <map>
26
27class GMTicket
28{
29    public:
30        explicit GMTicket()
31        {
32        }
33
34        GMTicket(uint32 guid, std::string text, time_t update) : m_guid(guid), m_text(text), m_lastUpdate(update)
35        {
36
37        }
38
39        const char* GetText() const
40        {
41            return m_text.c_str();
42        }
43
44        uint64 GetLastUpdate() const
45        {
46            return m_lastUpdate;
47        }
48
49        void SetText(const char* text)
50        {
51            m_text = text ? text : "";
52            m_lastUpdate = time(NULL);
53
54            std::string escapedString = m_text;
55            CharacterDatabase.escape_string(escapedString);
56            CharacterDatabase.PExecute("UPDATE character_ticket SET ticket_text = '%s' WHERE guid = '%u'", escapedString.c_str(), m_guid);
57        }
58
59        void DeleteFromDB() const
60        {
61            CharacterDatabase.PExecute("DELETE FROM character_ticket WHERE guid = '%u' LIMIT 1", m_guid);
62        }
63
64        void SaveToDB() const
65        {
66            CharacterDatabase.BeginTransaction();
67            DeleteFromDB();
68
69            std::string escapedString = m_text;
70            CharacterDatabase.escape_string(escapedString);
71
72            CharacterDatabase.PExecute("INSERT INTO character_ticket (guid, ticket_text) VALUES ('%u', '%s')", m_guid, escapedString.c_str());
73            CharacterDatabase.CommitTransaction();
74        }
75    private:
76        uint32 m_guid;
77        std::string m_text;
78        time_t m_lastUpdate;
79};
80typedef std::map<uint32, GMTicket> GMTicketMap;
81
82class GMTicketMgr
83{
84    public:
85        GMTicketMgr() {  }
86        ~GMTicketMgr() {  }
87
88        void LoadGMTickets();
89
90        GMTicket* GetGMTicket(uint32 guid)
91        {
92            GMTicketMap::iterator itr = m_GMTicketMap.find(guid);
93            if(itr == m_GMTicketMap.end())
94                return NULL;
95            return &(itr->second);
96        }
97
98        size_t GetTicketCount() const
99        {
100            return m_GMTicketMap.size();
101        }
102
103        void Delete(uint32 guid)
104        {
105            GMTicketMap::iterator itr = m_GMTicketMap.find(guid);
106            if(itr == m_GMTicketMap.end())
107                return;
108            itr->second.DeleteFromDB();
109            m_GMTicketMap.erase(itr);
110        }
111
112        void DeleteAll();
113
114        void Create(uint32 guid, const char* text)
115        {
116            GMTicket t = GMTicket(guid, text, time(NULL));
117            t.SaveToDB();
118            m_GMTicketMap[guid] = t;
119        }
120    private:
121        GMTicketMap m_GMTicketMap;
122};
123
124#define ticketmgr Trinity::Singleton<GMTicketMgr>::Instance()
125#endif
Note: See TracBrowser for help on using the browser.