Move object update from ObjectAccessor to Map update.
[getmangos.git] / src / game / PetitionMgr.h
blob6a763bb1d245d883c0d90a64c2ff178a53980e5c
1 /*
2 * Copyright (C) 2005-2008 MaNGOS <http://getmangos.com/>
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.
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.
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
19 #ifndef _PETITIONMGR_H
20 #define _PETITIONMGR_H
22 #include "Policies/Singleton.h"
23 #include "Database/DatabaseEnv.h"
24 #include "Util.h"
25 #include <map>
27 class Petition
29 public:
30 explicit Petition()
34 GMTicket(uint32 guid, std::string text, time_t update) : m_guid(guid), m_text(text), m_lastUpdate(update)
39 const char* GetText() const
41 return m_text.c_str();
44 uint64 GetLastUpdate() const
46 return m_lastUpdate;
49 void SetText(const char* text)
51 m_text = text ? text : "";
52 m_lastUpdate = time(NULL);
53 CharacterDatabase.PExecute("UPDATE character_ticket SET ticket_text = '%s' WHERE guid = '%u'", m_text.c_str(), m_guid);
56 void DeleteFromDB() const
58 CharacterDatabase.PExecute("DELETE FROM character_ticket WHERE guid = '%u' LIMIT 1", m_guid);
61 void SaveToDB() const
63 CharacterDatabase.BeginTransaction();
64 DeleteFromDB();
65 CharacterDatabase.PExecute("INSERT INTO character_ticket (guid, ticket_text) VALUES ('%u', '%s')", m_guid, GetText());
66 CharacterDatabase.CommitTransaction();
68 private:
69 uint32 m_guid;
70 std::string m_text;
71 time_t m_lastUpdate;
73 typedef std::map<uint32, Petition> PetitionMap;
75 class PetitionMgr
77 public:
78 PetitionMgr() { }
79 ~PetitionMgr() { }
81 void LoadGMTickets();
83 GMTicket* GetGMTicket(uint32 guid)
85 GMTicketMap::iterator itr = m_GMTicketMap.find(guid);
86 if(itr == m_GMTicketMap.end())
87 return NULL;
88 return &(itr->second);
91 size_t GetTicketCount() const
93 return m_GMTicketMap.size();
96 void Delete(uint32 guid)
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);
105 void DeleteAll();
107 void Create(uint32 guid, const char* text)
109 GMTicket t = GMTicket(guid, text, time(NULL));
110 t.SaveToDB();
111 m_GMTicketMap[guid] = t;
113 private:
114 PetitionMap m_PetitionMap;
117 #define petitionmgr MaNGOS::Singleton<PetitionMgr>::Instance()
118 #endif