aboutsummaryrefslogtreecommitdiff
path: root/src/game/TicketMgr.cpp
blob: ff26d3ba0f484a704484d423b505a71816f92fa2 (plain)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * Copyright (C) 2005-2008 MaNGOS
 *
 * Copyright (C) 2008 Trinity
 *
 * This program 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
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "TicketMgr.h"
#include "Policies/SingletonImp.h"
#include "World.h"
#include "ObjectMgr.h"
#include "Language.h"
#include "Player.h"
#include "Common.h"
#include "ObjectAccessor.h"
INSTANTIATE_SINGLETON_1( TicketMgr );

GM_Ticket* TicketMgr::GetGMTicket(uint64 ticketGuid)
{
    for(GmTicketList::iterator i = GM_TicketList.begin(); i != GM_TicketList.end();)
    {
        if((*i)->guid == ticketGuid)
        {
            return (*i);
        }
        ++i;
    }
    return NULL;
}

GM_Ticket* TicketMgr::GetGMTicketByPlayer(uint64 playerGuid)
{
    for(GmTicketList::iterator i = GM_TicketList.begin(); i != GM_TicketList.end();)
    {
        if((*i)->playerGuid == playerGuid && (*i)->closed == 0)
        {
            return (*i);
        }
        ++i;
    }
    return NULL;
}

GM_Ticket* TicketMgr::GetGMTicketByName(const char* name)
{
    std::string pname = name;
    if(!normalizePlayerName(pname))
        return NULL;

    uint64 playerGuid = objmgr.GetPlayerGUIDByName(pname.c_str());
    if(!playerGuid)
        return NULL;

    for(GmTicketList::iterator i = GM_TicketList.begin(); i != GM_TicketList.end();)
    {
        if((*i)->playerGuid == playerGuid && (*i)->closed == 0)
        {
            return (*i);
        }
        ++i;
    }
    return NULL;
}

void TicketMgr::AddGMTicket(GM_Ticket *ticket, bool startup)
{
    ASSERT( ticket );
    GM_TicketList.push_back(ticket);

    // save
    if(!startup)
        SaveGMTicket(ticket);
}

void TicketMgr::DeleteGMTicketPermanently(uint64 ticketGuid)
{
    for(GmTicketList::iterator i = GM_TicketList.begin(); i != GM_TicketList.end();)
    {
        if((*i)->guid == ticketGuid)
            i = GM_TicketList.erase(i);
        else
            ++i;
    }

    // delete database record
    CharacterDatabase.PExecute("DELETE FROM `gm_tickets` WHERE guid= '%u'", ticketGuid);
}


void TicketMgr::LoadGMTickets()
{
    // Delete all out of object holder
    GM_TicketList.clear();
    QueryResult *result = CharacterDatabase.Query( "SELECT `guid`, `playerGuid`, `name`, `message`, `createtime`, `map`, `posX`, `posY`, `posZ`, `timestamp`, `closed`, `assignedto`, `comment` FROM `gm_tickets`" );
    GM_Ticket *ticket;

    if(!result)
        return;

    // Assign values from SQL to the object holder
    do
    {
        Field *fields = result->Fetch();
        ticket = new GM_Ticket;
        ticket->guid = fields[0].GetUInt64();
        ticket->playerGuid = fields[1].GetUInt64();
        ticket->name = fields[2].GetString();
        ticket->message = fields[3].GetString();
        ticket->createtime = fields[4].GetUInt64();
        ticket->map = fields[5].GetUInt32();
        ticket->pos_x = fields[6].GetFloat();
        ticket->pos_y = fields[7].GetFloat();
        ticket->pos_z = fields[8].GetFloat();
        ticket->timestamp = fields[9].GetUInt64();
        ticket->closed = fields[10].GetUInt64();
        ticket->assignedToGM = fields[11].GetUInt64();
        ticket->comment = fields[12].GetString();

        AddGMTicket(ticket, true);

    } while( result->NextRow() );

    sWorld.SendGMText(LANG_COMMAND_TICKETRELOAD, result->GetRowCount());

    delete result;
}

void TicketMgr::RemoveGMTicket(uint64 ticketGuid, uint64 GMguid)
{
    for(GmTicketList::iterator i = GM_TicketList.begin(); i != GM_TicketList.end();)
    {
        if((*i)->guid == ticketGuid && (*i)->closed == 0)
        {
            (*i)->closed = GMguid;
            SaveGMTicket((*i));
        }
        ++i;
    }
}


void TicketMgr::RemoveGMTicketByPlayer(uint64 playerGuid, uint64 GMguid)
{
    for(GmTicketList::iterator i = GM_TicketList.begin(); i != GM_TicketList.end();)
    {
        if((*i)->playerGuid == playerGuid && (*i)->closed == 0)
        {
            (*i)->closed = GMguid;
            SaveGMTicket((*i));
        }
        ++i;
    }
}

void TicketMgr::SaveGMTicket(GM_Ticket* ticket)
{
    std::string msg = ticket->message;
    CharacterDatabase.escape_string(msg);
    std::stringstream ss;
    ss << "REPLACE INTO `gm_tickets` (`guid`, `playerGuid`, `name`, `message`, `createtime`, `map`, `posX`, `posY`, `posZ`, `timestamp`, `closed`, `assignedto`, `comment`) VALUES('";
    ss << ticket->guid << "', '";
    ss << ticket->playerGuid << "', '";
    ss << ticket->name << "', '";
    ss << msg << "', '" ;
    ss << ticket->createtime << "', '";
    ss << ticket->map << "', '";
    ss << ticket->pos_x << "', '";
    ss << ticket->pos_y << "', '";
    ss << ticket->pos_z << "', '";
    ss << ticket->timestamp << "', '";
    ss << ticket->closed << "', '";
    ss << ticket->assignedToGM << "', '";
    ss << ticket->comment << "');";
    CharacterDatabase.BeginTransaction();
    CharacterDatabase.Execute(ss.str().c_str());
    CharacterDatabase.CommitTransaction();

}

void TicketMgr::UpdateGMTicket(GM_Ticket *ticket)
{
    SaveGMTicket(ticket);
}

void TicketMgr::InitTicketID()
{
    QueryResult *result = CharacterDatabase.Query("SELECT MAX(guid) FROM gm_tickets");
    if(result)
    {
        m_ticketid = result->Fetch()[0].GetUInt64();
        delete result;
    }
}

uint64 TicketMgr::GenerateTicketID()
{
    return ++m_ticketid;
}