Files
Viano-Core/src/server/game/Tickets/TicketMgr.cpp
T
Spp 55ce180f28 Core/Logging: Add Asyncronous logging with Loggers ("What to log") and Appenders ("Where to log") system. Will allow to select to full log some parts of core while others are not even logged.
- Logging System is asyncronous to improve performance.
- Each msg and Logger has a Log Type and Log Level assigned. Each msg is assigned the Logger of same Log Type or "root" Logger is selected if there is no Logger configured for the given Log Type
- Loggers have a list of Appenders to send the msg to. The Msg in the Logger is not sent to Appenders if the msg LogLevel is lower than Logger LogLevel.
- There are three (at the moment) types of Appenders: Console, File or DB (this is WIP, not working ATM). Msg is not written to the resource if msg LogLevel is lower than Appender LogLevel.
- Appender and Console Log levels can be changed while server is active with command '.set loglevel (a/l) name level'

Explanation of use with Sample config:

Appender.Console.Type=1       (1 = Console)
Appender.Console.Level=2      (2 = Debug)

Appender.Server.Type=2        (2 = File)
Appender.Server.Level=3       (3 = Info)
Appender.Server.File=Server.log

Appender.SQL.Type=2           (2 = File)
Appender.SQL.Level=1          (1 = Trace)
Appender.SQL.File=sql.log

Appenders=Console Server      (NOTE: SQL has not been included here... that will make core ignore the config for "SQL" as it's not in this list)

Logger.root.Type=0            (0 = Default - if it's not created by config, server will create it with LogLevel = DISABLED)
Logger.root.Level=5           (5 = Error)
Logger.root.Appenders=Console

Logger.SQL.Type=26            (26 = SQL)
Logger.SQL.Level=3            (2 = Debug)
Logger.SQL.Appenders=Console Server SQL

Logger.SomeRandomName.Type=24 (24 = Guild)
Logger.SomeRandomName.Level=5 (5 = Error)
Loggers=root SQL SomeRandomName

* At loading Appender SQL will be ignored, as it's not present on "Appenders"

* sLog->outDebug(LOG_FILTER_GUILD, "Some log msg related to Guilds")
  - Msg is sent to Logger of Type LOG_FILTER_GUILD (24). Logger with name SomeRandomName is found but it's LogLevel = 5 and Msg LogLevel=2... Msg is not logged

* sLog->outError(LOG_FILTER_GUILD, "Some error log msg related to Guilds")
  - Msg is sent to Logger of Type LOG_FILTER_GUILD (24). Logger with name SomeRandomeName is found with proper LogLevel but Logger does not have any Appenders assigned to that logger... Msg is not logged

* sLog->outDebug(LOG_FILTER_SQL, "Some msg related to SQLs")
  - Msg is sent to Logger SQL (matches type), as it matches LogLevel the msg is sent to Appenders Console, Server and SQL
    - Appender Console has lower Log Level: Msg is logged to Console
    - Appender Server has higher Log Level: Msg is not logged to file
    - Appender SQL has lower Log Level: Msg is logged to file sql.log

* sLog->outDebug(LOG_FILTER_BATTLEGROUND, "Some msg related to Battelgrounds")
  - Msg is sent to Logger root (Type 0) as no Logger was found with Type LOG_FILTER_BATTLEGROUND (13). As Logger has higher LogLevel msg is not sent to any appender

* sLog->outError(LOG_FILTER_BATTLEGROUND, "Some error msg related to Battelgrounds")
  - Msg is sent to Logger root (Type 0) as no Logger was found with Type LOG_FILTER_BATTLEGROUND (13). Msg has lower LogLevel and is sent to Appender Console
    - Appender Console has lower LogLevel: Msg is logged to Console
2012-08-03 14:20:18 +02:00

391 lines
14 KiB
C++
Executable File

/*
* Copyright (C) 2008-2012 TrinityCore <http://www.trinitycore.org/>
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include "Common.h"
#include "TicketMgr.h"
#include "DatabaseEnv.h"
#include "Log.h"
#include "WorldPacket.h"
#include "WorldSession.h"
#include "Chat.h"
#include "World.h"
inline float GetAge(uint64 t) { return float(time(NULL) - t) / DAY; }
///////////////////////////////////////////////////////////////////////////////////////////////////
// GM ticket
GmTicket::GmTicket() { }
GmTicket::GmTicket(Player* player, WorldPacket& recv_data) : _createTime(time(NULL)), _lastModifiedTime(time(NULL)), _closedBy(0), _assignedTo(0), _completed(false), _escalatedStatus(TICKET_UNASSIGNED)
{
_id = sTicketMgr->GenerateTicketId();
_playerName = player->GetName();
_playerGuid = player->GetGUID();
uint32 mapId;
recv_data >> mapId; // Map is sent as UInt32!
_mapId = mapId;
recv_data >> _posX;
recv_data >> _posY;
recv_data >> _posZ;
recv_data >> _message;
uint32 needResponse;
recv_data >> needResponse;
_needResponse = (needResponse == 17); // Requires GM response. 17 = true, 1 = false (17 is default)
uint8 unk1;
recv_data >> unk1; // Requests further GM interaction on a ticket to which a GM has already responded
recv_data.rfinish();
/*
recv_data >> uint32(count); // text lines
for (int i = 0; i < count; i++)
recv_data >> uint32();
if (something)
recv_data >> uint32();
else
compressed uint32 + string;
*/
}
GmTicket::~GmTicket() { }
bool GmTicket::LoadFromDB(Field* fields)
{
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// ticketId, guid, name, message, createTime, mapId, posX, posY, posZ, lastModifiedTime, closedBy, assignedTo, comment, completed, escalated, viewed
uint8 index = 0;
_id = fields[ index].GetUInt32();
_playerGuid = MAKE_NEW_GUID(fields[++index].GetUInt32(), 0, HIGHGUID_PLAYER);
_playerName = fields[++index].GetString();
_message = fields[++index].GetString();
_createTime = fields[++index].GetUInt32();
_mapId = fields[++index].GetUInt16();
_posX = fields[++index].GetFloat();
_posY = fields[++index].GetFloat();
_posZ = fields[++index].GetFloat();
_lastModifiedTime = fields[++index].GetUInt32();
_closedBy = fields[++index].GetInt32();
_assignedTo = MAKE_NEW_GUID(fields[++index].GetUInt32(), 0, HIGHGUID_PLAYER);
_comment = fields[++index].GetString();
_completed = fields[++index].GetBool();
_escalatedStatus = GMTicketEscalationStatus(fields[++index].GetUInt8());
_viewed = fields[++index].GetBool();
return true;
}
void GmTicket::SaveToDB(SQLTransaction& trans) const
{
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// ticketId, guid, name, message, createTime, mapId, posX, posY, posZ, lastModifiedTime, closedBy, assignedTo, comment, completed, escalated, viewed
uint8 index = 0;
PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_REP_GM_TICKET);
stmt->setUInt32( index, _id);
stmt->setUInt32(++index, GUID_LOPART(_playerGuid));
stmt->setString(++index, _playerName);
stmt->setString(++index, _message);
stmt->setUInt32(++index, uint32(_createTime));
stmt->setUInt16(++index, _mapId);
stmt->setFloat (++index, _posX);
stmt->setFloat (++index, _posY);
stmt->setFloat (++index, _posZ);
stmt->setUInt32(++index, uint32(_lastModifiedTime));
stmt->setInt32 (++index, GUID_LOPART(_closedBy));
stmt->setUInt32(++index, GUID_LOPART(_assignedTo));
stmt->setString(++index, _comment);
stmt->setBool (++index, _completed);
stmt->setUInt8 (++index, uint8(_escalatedStatus));
stmt->setBool (++index, _viewed);
CharacterDatabase.ExecuteOrAppend(trans, stmt);
}
void GmTicket::DeleteFromDB()
{
PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_GM_TICKET);
stmt->setUInt32(0, _id);
CharacterDatabase.Execute(stmt);
}
void GmTicket::WritePacket(WorldPacket& data) const
{
data << GetAge(_lastModifiedTime);
if (GmTicket* ticket = sTicketMgr->GetOldestOpenTicket())
data << GetAge(ticket->GetLastModifiedTime());
else
data << float(0);
// I am not sure how blizzlike this is, and we don't really have a way to find out
data << GetAge(sTicketMgr->GetLastChange());
data << uint8(std::min(_escalatedStatus, TICKET_IN_ESCALATION_QUEUE)); // escalated data
data << uint8(_viewed ? GMTICKET_OPENEDBYGM_STATUS_OPENED : GMTICKET_OPENEDBYGM_STATUS_NOT_OPENED); // whether or not it has been viewed
}
void GmTicket::SendResponse(WorldSession* session) const
{
WorldPacket data(SMSG_GMRESPONSE_RECEIVED);
data << uint32(1); // responseID
data << uint32(_id); // ticketID
data << _message.c_str();
size_t len = _response.size();
char const* s = _response.c_str();
for (int i = 0; i < 4; i++)
{
if (len)
{
size_t writeLen = std::min<size_t>(len, 3999);
data.append(s, writeLen);
len -= writeLen;
s += writeLen;
}
data << uint8(0);
}
session->SendPacket(&data);
}
std::string GmTicket::FormatMessageString(ChatHandler& handler, bool detailed) const
{
time_t curTime = time(NULL);
std::stringstream ss;
ss << handler.PGetParseString(LANG_COMMAND_TICKETLISTGUID, _id);
ss << handler.PGetParseString(LANG_COMMAND_TICKETLISTNAME, _playerName.c_str());
ss << handler.PGetParseString(LANG_COMMAND_TICKETLISTAGECREATE, (secsToTimeString(curTime - _createTime, true, false)).c_str());
ss << handler.PGetParseString(LANG_COMMAND_TICKETLISTAGE, (secsToTimeString(curTime - _lastModifiedTime, true, false)).c_str());
std::string name;
if (sObjectMgr->GetPlayerNameByGUID(_assignedTo, name))
ss << handler.PGetParseString(LANG_COMMAND_TICKETLISTASSIGNEDTO, name.c_str());
if (detailed)
{
ss << handler.PGetParseString(LANG_COMMAND_TICKETLISTMESSAGE, _message.c_str());
if (!_comment.empty())
ss << handler.PGetParseString(LANG_COMMAND_TICKETLISTCOMMENT, _comment.c_str());
}
return ss.str();
}
std::string GmTicket::FormatMessageString(ChatHandler& handler, const char* szClosedName, const char* szAssignedToName, const char* szUnassignedName, const char* szDeletedName) const
{
std::stringstream ss;
ss << handler.PGetParseString(LANG_COMMAND_TICKETLISTGUID, _id);
ss << handler.PGetParseString(LANG_COMMAND_TICKETLISTNAME, _playerName.c_str());
if (szClosedName)
ss << handler.PGetParseString(LANG_COMMAND_TICKETCLOSED, szClosedName);
if (szAssignedToName)
ss << handler.PGetParseString(LANG_COMMAND_TICKETLISTASSIGNEDTO, szAssignedToName);
if (szUnassignedName)
ss << handler.PGetParseString(LANG_COMMAND_TICKETLISTUNASSIGNED, szUnassignedName);
if (szDeletedName)
ss << handler.PGetParseString(LANG_COMMAND_TICKETDELETED, szDeletedName);
return ss.str();
}
void GmTicket::SetUnassigned()
{
_assignedTo = 0;
switch (_escalatedStatus)
{
case TICKET_ASSIGNED: _escalatedStatus = TICKET_UNASSIGNED; break;
case TICKET_ESCALATED_ASSIGNED: _escalatedStatus = TICKET_IN_ESCALATION_QUEUE; break;
case TICKET_UNASSIGNED:
case TICKET_IN_ESCALATION_QUEUE:
default:
break;
}
}
void GmTicket::TeleportTo(Player* player) const
{
player->TeleportTo(_mapId, _posX, _posY, _posZ, 0.0f, 0);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Ticket manager
TicketMgr::TicketMgr() : _status(true), _lastTicketId(0), _lastSurveyId(0), _openTicketCount(0), _lastChange(time(NULL)) { }
TicketMgr::~TicketMgr()
{
for (GmTicketList::const_iterator itr = _ticketList.begin(); itr != _ticketList.end(); ++itr)
delete itr->second;
}
void TicketMgr::Initialize() { SetStatus(sWorld->getBoolConfig(CONFIG_ALLOW_TICKETS)); }
void TicketMgr::ResetTickets()
{
for (GmTicketList::const_iterator itr = _ticketList.begin(); itr != _ticketList.end(); ++itr)
if (itr->second->IsClosed())
sTicketMgr->RemoveTicket(itr->second->GetId());
_lastTicketId = 0;
PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_ALL_GM_TICKETS);
CharacterDatabase.Execute(stmt);
}
void TicketMgr::LoadTickets()
{
uint32 oldMSTime = getMSTime();
for (GmTicketList::const_iterator itr = _ticketList.begin(); itr != _ticketList.end(); ++itr)
delete itr->second;
_ticketList.clear();
_lastTicketId = 0;
_openTicketCount = 0;
PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_SEL_GM_TICKETS);
PreparedQueryResult result = CharacterDatabase.Query(stmt);
if (!result)
{
sLog->outInfo(LOG_FILTER_GENERAL, ">> Loaded 0 GM tickets. DB table `gm_tickets` is empty!");
return;
}
uint32 count = 0;
do
{
Field* fields = result->Fetch();
GmTicket* ticket = new GmTicket();
if (!ticket->LoadFromDB(fields))
{
delete ticket;
continue;
}
if (!ticket->IsClosed())
++_openTicketCount;
// Update max ticket id if necessary
uint32 id = ticket->GetId();
if (_lastTicketId < id)
_lastTicketId = id;
_ticketList[id] = ticket;
++count;
} while (result->NextRow());
sLog->outInfo(LOG_FILTER_GENERAL, ">> Loaded %u GM tickets in %u ms", count, GetMSTimeDiffToNow(oldMSTime));
}
void TicketMgr::LoadSurveys()
{
// we don't actually load anything into memory here as there's no reason to
_lastSurveyId = 0;
uint32 oldMSTime = getMSTime();
if (QueryResult result = CharacterDatabase.Query("SELECT MAX(surveyId) FROM gm_surveys"))
_lastSurveyId = (*result)[0].GetUInt32();
sLog->outInfo(LOG_FILTER_GENERAL, ">> Loaded GM Survey count from database in %u ms", GetMSTimeDiffToNow(oldMSTime));
}
void TicketMgr::AddTicket(GmTicket* ticket)
{
_ticketList[ticket->GetId()] = ticket;
if (!ticket->IsClosed())
++_openTicketCount;
SQLTransaction trans = SQLTransaction(NULL);
ticket->SaveToDB(trans);
}
void TicketMgr::CloseTicket(uint32 ticketId, int64 source)
{
if (GmTicket* ticket = GetTicket(ticketId))
{
SQLTransaction trans = SQLTransaction(NULL);
ticket->SetClosedBy(source);
if (source)
--_openTicketCount;
ticket->SaveToDB(trans);
}
}
void TicketMgr::RemoveTicket(uint32 ticketId)
{
if (GmTicket* ticket = GetTicket(ticketId))
{
ticket->DeleteFromDB();
_ticketList.erase(ticketId);
delete ticket;
}
}
void TicketMgr::ShowList(ChatHandler& handler, bool onlineOnly) const
{
handler.SendSysMessage(onlineOnly ? LANG_COMMAND_TICKETSHOWONLINELIST : LANG_COMMAND_TICKETSHOWLIST);
for (GmTicketList::const_iterator itr = _ticketList.begin(); itr != _ticketList.end(); ++itr)
if (!itr->second->IsClosed() && !itr->second->IsCompleted())
if (!onlineOnly || itr->second->GetPlayer())
handler.SendSysMessage(itr->second->FormatMessageString(handler).c_str());
}
void TicketMgr::ShowClosedList(ChatHandler& handler) const
{
handler.SendSysMessage(LANG_COMMAND_TICKETSHOWCLOSEDLIST);
for (GmTicketList::const_iterator itr = _ticketList.begin(); itr != _ticketList.end(); ++itr)
if (itr->second->IsClosed())
handler.SendSysMessage(itr->second->FormatMessageString(handler).c_str());
}
void TicketMgr::ShowEscalatedList(ChatHandler& handler) const
{
handler.SendSysMessage(LANG_COMMAND_TICKETSHOWESCALATEDLIST);
for (GmTicketList::const_iterator itr = _ticketList.begin(); itr != _ticketList.end(); ++itr)
if (!itr->second->IsClosed() && itr->second->GetEscalatedStatus() == TICKET_IN_ESCALATION_QUEUE)
handler.SendSysMessage(itr->second->FormatMessageString(handler).c_str());
}
void TicketMgr::SendTicket(WorldSession* session, GmTicket* ticket) const
{
uint32 status = GMTICKET_STATUS_DEFAULT;
std::string message;
if (ticket)
{
message = ticket->GetMessage();
status = GMTICKET_STATUS_HASTEXT;
}
WorldPacket data(SMSG_GMTICKET_GETTICKET, (4 + 4 + (ticket ? message.length() + 1 + 4 + 4 + 4 + 1 + 1 : 0)));
data << uint32(status); // standard 0x0A, 0x06 if text present
data << uint32(ticket ? ticket->GetId() : 0); // ticketID
if (ticket)
{
data << message.c_str(); // ticket text
data << uint8(0x7); // ticket category; why is this hardcoded? does it make a diff re: client?
// we've got the easy stuff done by now.
// Now we need to go through the client logic for displaying various levels of ticket load
ticket->WritePacket(data);
}
session->SendPacket(&data);
}