[263] | 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 | |
---|
| 27 | class 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); |
---|
[272] | 53 | CharacterDatabase.PExecute("UPDATE character_ticket SET ticket_text = '%s' WHERE guid = '%u'", m_text.c_str(), m_guid); |
---|
[263] | 54 | } |
---|
| 55 | |
---|
| 56 | void DeleteFromDB() const |
---|
| 57 | { |
---|
| 58 | CharacterDatabase.PExecute("DELETE FROM character_ticket WHERE guid = '%u' LIMIT 1", m_guid); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | void SaveToDB() const |
---|
| 62 | { |
---|
| 63 | CharacterDatabase.BeginTransaction(); |
---|
| 64 | DeleteFromDB(); |
---|
[272] | 65 | CharacterDatabase.PExecute("INSERT INTO character_ticket (guid, ticket_text) VALUES ('%u', '%s')", m_guid, GetText()); |
---|
[263] | 66 | CharacterDatabase.CommitTransaction(); |
---|
| 67 | } |
---|
| 68 | private: |
---|
| 69 | uint32 m_guid; |
---|
| 70 | std::string m_text; |
---|
| 71 | time_t m_lastUpdate; |
---|
| 72 | }; |
---|
| 73 | typedef std::map<uint32, GMTicket> GMTicketMap; |
---|
| 74 | |
---|
| 75 | class GMTicketMgr |
---|
| 76 | { |
---|
| 77 | public: |
---|
| 78 | GMTicketMgr() { } |
---|
| 79 | ~GMTicketMgr() { } |
---|
| 80 | |
---|
| 81 | void LoadGMTickets(); |
---|
| 82 | |
---|
| 83 | GMTicket* GetGMTicket(uint32 guid) |
---|
| 84 | { |
---|
| 85 | GMTicketMap::iterator itr = m_GMTicketMap.find(guid); |
---|
| 86 | if(itr == m_GMTicketMap.end()) |
---|
| 87 | return NULL; |
---|
| 88 | return &(itr->second); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | size_t GetTicketCount() const |
---|
| 92 | { |
---|
| 93 | return m_GMTicketMap.size(); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | void Delete(uint32 guid) |
---|
| 97 | { |
---|
| 98 | GMTicketMap::iterator itr = m_GMTicketMap.find(guid); |
---|
| 99 | if(itr == m_GMTicketMap.end()) |
---|
| 100 | return; |
---|
| 101 | itr->second.DeleteFromDB(); |
---|
| 102 | m_GMTicketMap.erase(itr); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | void DeleteAll(); |
---|
| 106 | |
---|
| 107 | void Create(uint32 guid, const char* text) |
---|
| 108 | { |
---|
| 109 | GMTicket t = GMTicket(guid, text, time(NULL)); |
---|
| 110 | t.SaveToDB(); |
---|
| 111 | m_GMTicketMap[guid] = t; |
---|
| 112 | } |
---|
| 113 | private: |
---|
| 114 | GMTicketMap m_GMTicketMap; |
---|
| 115 | }; |
---|
| 116 | |
---|
| 117 | #define ticketmgr Trinity::Singleton<GMTicketMgr>::Instance() |
---|
[272] | 118 | #endif |
---|