Core/Misc: implemented petition manager

Port From (https://github.com/TrinityCore/TrinityCore/commit/48a40fae0a713c3d96e4796da78f7186260ebbd3)
This commit is contained in:
hondacrx
2020-05-19 21:17:12 -04:00
parent 8028b3bbb6
commit 6549d76ddf
6 changed files with 329 additions and 208 deletions
@@ -0,0 +1,219 @@
/*
* Copyright (C) 2012-2020 CypherCore <http://github.com/CypherCore>
*
* 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 3 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/>.
*/
using Framework.Constants;
using Framework.Database;
using System.Collections.Generic;
using System.Linq;
namespace Game.Entities
{
public class PetitionManager : Singleton<PetitionManager>
{
Dictionary<ObjectGuid, Petition> _petitionStorage = new Dictionary<ObjectGuid, Petition>();
PetitionManager() { }
public void LoadPetitions()
{
uint oldMSTime = Time.GetMSTime();
_petitionStorage.Clear();
SQLResult result = DB.Characters.Query("SELECT petitionguid, ownerguid, name FROM petition");
if (result.IsEmpty())
{
Log.outInfo(LogFilter.ServerLoading, "Loaded 0 petitions.");
return;
}
uint count = 0;
do
{
AddPetition(ObjectGuid.Create(HighGuid.Item, result.Read<ulong>(0)), ObjectGuid.Create(HighGuid.Player, result.Read<ulong>(1)), result.Read<string>(2), true);
++count;
} while (result.NextRow());
Log.outInfo(LogFilter.ServerLoading, $"Loaded {count} petitions in: {Time.GetMSTimeDiffToNow(oldMSTime)} ms.");
}
public void LoadSignatures()
{
uint oldMSTime = Time.GetMSTime();
SQLResult result = DB.Characters.Query("SELECT petitionguid, player_account, playerguid FROM petition_sign");
if (result.IsEmpty())
{
Log.outInfo(LogFilter.ServerLoading, "Loaded 0 Petition signs!");
return;
}
uint count = 0;
do
{
Petition petition = GetPetition(ObjectGuid.Create(HighGuid.Item, result.Read<ulong>(0)));
if (petition == null)
continue;
petition.AddSignature(petition.PetitionGuid, result.Read<uint>(1), ObjectGuid.Create(HighGuid.Player, result.Read<ulong>(2)), true);
++count;
} while (result.NextRow());
Log.outInfo(LogFilter.ServerLoading, $"Loaded {count} Petition signs in {Time.GetMSTimeDiffToNow(oldMSTime)} ms.");
}
public void AddPetition(ObjectGuid petitionGuid, ObjectGuid ownerGuid, string name, bool isLoading)
{
Petition p = new Petition();
p.PetitionGuid = petitionGuid;
p.ownerGuid = ownerGuid;
p.petitionName = name;
p.signatures.Clear();
_petitionStorage[petitionGuid] = p;
if (isLoading)
return;
PreparedStatement stmt = DB.Characters.GetPreparedStatement(CharStatements.INS_PETITION);
stmt.AddValue(0, ownerGuid.GetCounter());
stmt.AddValue(1, petitionGuid.GetCounter());
stmt.AddValue(2, name);
DB.Characters.Execute(stmt);
}
public void RemovePetition(ObjectGuid petitionGuid)
{
_petitionStorage.Remove(petitionGuid);
// Delete From DB
SQLTransaction trans = new SQLTransaction();
PreparedStatement stmt = DB.Characters.GetPreparedStatement(CharStatements.DEL_PETITION_BY_GUID);
stmt.AddValue(0, petitionGuid.GetCounter());
trans.Append(stmt);
stmt = DB.Characters.GetPreparedStatement(CharStatements.DEL_PETITION_SIGNATURE_BY_GUID);
stmt.AddValue(0, petitionGuid.GetCounter());
trans.Append(stmt);
DB.Characters.CommitTransaction(trans);
}
public Petition GetPetition(ObjectGuid petitionGuid)
{
return _petitionStorage.LookupByKey(petitionGuid);
}
public Petition GetPetitionByOwner(ObjectGuid ownerGuid)
{
return _petitionStorage.FirstOrDefault(p => p.Value.ownerGuid == ownerGuid).Value;
}
public void RemovePetitionsByOwner(ObjectGuid ownerGuid)
{
foreach (var key in _petitionStorage.Keys.ToList())
{
if (_petitionStorage[key].ownerGuid == ownerGuid)
{
_petitionStorage.Remove(key);
break;
}
}
SQLTransaction trans = new SQLTransaction();
PreparedStatement stmt = DB.Characters.GetPreparedStatement(CharStatements.DEL_PETITION_BY_OWNER);
stmt.AddValue(0, ownerGuid.GetCounter());
trans.Append(stmt);
stmt = DB.Characters.GetPreparedStatement(CharStatements.DEL_PETITION_SIGNATURE_BY_OWNER);
stmt.AddValue(0, ownerGuid.GetCounter());
trans.Append(stmt);
DB.Characters.CommitTransaction(trans);
}
public void RemoveSignaturesBySigner(ObjectGuid signerGuid)
{
foreach (var petitionPair in _petitionStorage)
petitionPair.Value.RemoveSignatureBySigner(signerGuid);
PreparedStatement stmt = DB.Characters.GetPreparedStatement(CharStatements.DEL_ALL_PETITION_SIGNATURES);
stmt.AddValue(0, signerGuid.GetCounter());
DB.Characters.Execute(stmt);
}
}
public class Petition
{
public ObjectGuid PetitionGuid;
public ObjectGuid ownerGuid;
public string petitionName;
public List<(uint AccountId, ObjectGuid PlayerGuid)> signatures = new List<(uint AccountId, ObjectGuid PlayerGuid)>();
public bool IsPetitionSignedByAccount(uint accountId)
{
foreach (var signature in signatures)
if (signature.AccountId == accountId)
return true;
return false;
}
public void AddSignature(ObjectGuid petitionGuid, uint accountId, ObjectGuid playerGuid, bool isLoading)
{
signatures.Add((accountId, playerGuid));
if (isLoading)
return;
PreparedStatement stmt = DB.Characters.GetPreparedStatement(CharStatements.INS_PETITION_SIGNATURE);
stmt.AddValue(0, ownerGuid.GetCounter());
stmt.AddValue(1, petitionGuid.GetCounter());
stmt.AddValue(2, playerGuid.GetCounter());
stmt.AddValue(3, accountId);
DB.Characters.Execute(stmt);
}
public void UpdateName(string newName)
{
petitionName = newName;
PreparedStatement stmt = DB.Characters.GetPreparedStatement(CharStatements.UPD_PETITION_NAME);
stmt.AddValue(0, newName);
stmt.AddValue(1, PetitionGuid.GetCounter());
DB.Characters.Execute(stmt);
}
public void RemoveSignatureBySigner(ObjectGuid playerGuid)
{
foreach (var itr in signatures)
{
if (itr.PlayerGuid == playerGuid)
{
signatures.Remove(itr);
// notify owner
Player owner = Global.ObjAccessor.FindConnectedPlayer(ownerGuid);
if (owner != null)
owner.GetSession().SendPetitionQuery(PetitionGuid);
break;
}
}
}
}
}
+2 -32
View File
@@ -3584,38 +3584,8 @@ namespace Game.Entities
} }
public static void RemovePetitionsAndSigns(ObjectGuid guid) public static void RemovePetitionsAndSigns(ObjectGuid guid)
{ {
PreparedStatement stmt = DB.Characters.GetPreparedStatement(CharStatements.SEL_PETITION_SIG_BY_GUID); Global.PetitionMgr.RemoveSignaturesBySigner(guid);
stmt.AddValue(0, guid.GetCounter()); Global.PetitionMgr.RemovePetitionsByOwner(guid);
SQLResult result = DB.Characters.Query(stmt);
if (!result.IsEmpty())
{
do // this part effectively does nothing, since the deletion / modification only takes place _after_ the PetitionSelect. Though I don't know if the result remains intact if I execute the delete Select beforehand.
{ // and SendPetitionSelectOpcode reads data from the DB
ObjectGuid ownerguid = ObjectGuid.Create(HighGuid.Player, result.Read<ulong>(0));
ObjectGuid petitionguid = ObjectGuid.Create(HighGuid.Item, result.Read<ulong>(1));
// send update if charter owner in game
Player owner = Global.ObjAccessor.FindPlayer(ownerguid);
if (owner != null)
owner.GetSession().SendPetitionQuery(petitionguid);
} while (result.NextRow());
stmt = DB.Characters.GetPreparedStatement(CharStatements.DEL_ALL_PETITION_SIGNATURES);
stmt.AddValue(0, guid.GetCounter());
DB.Characters.Execute(stmt);
}
SQLTransaction trans = new SQLTransaction();
stmt = DB.Characters.GetPreparedStatement(CharStatements.DEL_PETITION_BY_OWNER);
stmt.AddValue(0, guid.GetCounter());
trans.Append(stmt);
stmt = DB.Characters.GetPreparedStatement(CharStatements.DEL_PETITION_SIGNATURE_BY_OWNER);
stmt.AddValue(0, guid.GetCounter());
trans.Append(stmt);
DB.Characters.CommitTransaction(trans);
} }
public static void DeleteFromDB(ObjectGuid playerGuid, uint accountId, bool updateRealmChars = true, bool deleteFinally = false) public static void DeleteFromDB(ObjectGuid playerGuid, uint accountId, bool updateRealmChars = true, bool deleteFinally = false)
{ {
+1
View File
@@ -48,6 +48,7 @@ public static class Global
public static ServiceDispatcher ServiceMgr { get { return ServiceDispatcher.Instance; } } public static ServiceDispatcher ServiceMgr { get { return ServiceDispatcher.Instance; } }
//Guild //Guild
public static PetitionManager PetitionMgr { get { return PetitionManager.Instance; } }
public static GuildManager GuildMgr { get { return GuildManager.Instance; } } public static GuildManager GuildMgr { get { return GuildManager.Instance; } }
public static GuildFinderManager GuildFinderMgr { get { return GuildFinderManager.Instance; } } public static GuildFinderManager GuildFinderMgr { get { return GuildFinderManager.Instance; } }
+97 -172
View File
@@ -95,66 +95,50 @@ namespace Game
// a petition is invalid, if both the owner and the type matches // a petition is invalid, if both the owner and the type matches
// we checked above, if this player is in an arenateam, so this must be // we checked above, if this player is in an arenateam, so this must be
// datacorruption // datacorruption
PreparedStatement stmt = DB.Characters.GetPreparedStatement(CharStatements.SEL_PETITION_BY_OWNER); Petition petition = Global.PetitionMgr.GetPetitionByOwner(_player.GetGUID());
stmt.AddValue(0, GetPlayer().GetGUID().GetCounter()); if (petition != null)
SQLResult result = DB.Characters.Query(stmt);
StringBuilder ssInvalidPetitionGUIDs = new StringBuilder();
if (!result.IsEmpty())
{ {
do // clear from petition store
{ Global.PetitionMgr.RemovePetition(petition.PetitionGuid);
ssInvalidPetitionGUIDs.AppendFormat("'{0}', ", result.Read<uint>(0)); Log.outDebug(LogFilter.Network, $"Invalid petition GUID: {petition.PetitionGuid.GetCounter()}");
} while (result.NextRow());
} }
// delete petitions with the same guid as this one // fill petition store
ssInvalidPetitionGUIDs.AppendFormat("'{0}'", charter.GetGUID().GetCounter()); Global.PetitionMgr.AddPetition(charter.GetGUID(), _player.GetGUID(), packet.Title, false);
Log.outDebug(LogFilter.Network, "Invalid petition GUIDs: {0}", ssInvalidPetitionGUIDs.ToString());
SQLTransaction trans = new SQLTransaction();
trans.Append("DELETE FROM petition WHERE petitionguid IN ({0})", ssInvalidPetitionGUIDs.ToString());
trans.Append("DELETE FROM petition_sign WHERE petitionguid IN ({0})", ssInvalidPetitionGUIDs.ToString());
stmt = DB.Characters.GetPreparedStatement(CharStatements.INS_PETITION);
stmt.AddValue(0, GetPlayer().GetGUID().GetCounter());
stmt.AddValue(1, charter.GetGUID().GetCounter());
stmt.AddValue(2, packet.Title);
trans.Append(stmt);
DB.Characters.CommitTransaction(trans);
} }
[WorldPacketHandler(ClientOpcodes.PetitionShowSignatures)] [WorldPacketHandler(ClientOpcodes.PetitionShowSignatures)]
void HandlePetitionShowSignatures(PetitionShowSignatures packet) void HandlePetitionShowSignatures(PetitionShowSignatures packet)
{ {
Log.outDebug(LogFilter.Network, "Received opcode CMSG_PETITION_SHOW_SIGNATURES"); Petition petition = Global.PetitionMgr.GetPetition(packet.Item);
if (petition == null)
{
Log.outDebug(LogFilter.PlayerItems, $"Petition {packet.Item} is not found for player {GetPlayer().GetGUID().GetCounter()} {GetPlayer().GetName()}");
return;
}
// if has guild => error, return; // if has guild => error, return;
if (GetPlayer().GetGuildId() != 0) if (_player.GetGuildId() != 0)
return; return;
PreparedStatement stmt = DB.Characters.GetPreparedStatement(CharStatements.SEL_PETITION_SIGNATURE); SendPetitionSigns(petition, _player);
stmt.AddValue(0, packet.Item.GetCounter()); }
SQLResult result = DB.Characters.Query(stmt);
void SendPetitionSigns(Petition petition, Player sendTo)
{
ServerPetitionShowSignatures signaturesPacket = new ServerPetitionShowSignatures(); ServerPetitionShowSignatures signaturesPacket = new ServerPetitionShowSignatures();
signaturesPacket.Item = packet.Item; signaturesPacket.Item = petition.PetitionGuid;
signaturesPacket.Owner = GetPlayer().GetGUID(); signaturesPacket.Owner = petition.ownerGuid;
signaturesPacket.OwnerAccountID = ObjectGuid.Create(HighGuid.WowAccount, Global.CharacterCacheStorage.GetCharacterAccountIdByGuid(GetPlayer().GetGUID())); signaturesPacket.OwnerAccountID = ObjectGuid.Create(HighGuid.WowAccount, Global.CharacterCacheStorage.GetCharacterAccountIdByGuid(petition.ownerGuid));
signaturesPacket.PetitionID = (int)packet.Item.GetCounter(); // @todo verify that... signaturesPacket.PetitionID = (int)petition.PetitionGuid.GetCounter();
do foreach (var signature in petition.signatures)
{ {
ObjectGuid signerGUID = ObjectGuid.Create(HighGuid.Player, result.Read<ulong>(0)); ServerPetitionShowSignatures.PetitionSignature signaturePkt = new ServerPetitionShowSignatures.PetitionSignature();
signaturePkt.Signer = signature.PlayerGuid;
ServerPetitionShowSignatures.PetitionSignature signature = new ServerPetitionShowSignatures.PetitionSignature(); signaturePkt.Choice = 0;
signature.Signer = signerGUID; signaturesPacket.Signatures.Add(signaturePkt);
signature.Choice = 0;
signaturesPacket.Signatures.Add(signature);
} }
while (result.NextRow());
SendPacket(signaturesPacket); SendPacket(signaturesPacket);
} }
@@ -165,37 +149,28 @@ namespace Game
SendPetitionQuery(packet.ItemGUID); SendPetitionQuery(packet.ItemGUID);
} }
public void SendPetitionQuery(ObjectGuid petitionGUID) public void SendPetitionQuery(ObjectGuid petitionGuid)
{ {
ObjectGuid ownerGUID = ObjectGuid.Empty;
string title = "NO_NAME_FOR_GUID";
QueryPetitionResponse responsePacket = new QueryPetitionResponse(); QueryPetitionResponse responsePacket = new QueryPetitionResponse();
responsePacket.PetitionID = (uint)petitionGUID.GetCounter(); // PetitionID (in Trinity always same as GUID_LOPART(petition guid)) responsePacket.PetitionID = (uint)petitionGuid.GetCounter(); // PetitionID (in Trinity always same as GUID_LOPART(petition guid))
PreparedStatement stmt = DB.Characters.GetPreparedStatement(CharStatements.SEL_PETITION); Petition petition = Global.PetitionMgr.GetPetition(petitionGuid);
stmt.AddValue(0, petitionGUID.GetCounter()); if (petition == null)
SQLResult result = DB.Characters.Query(stmt);
if (!result.IsEmpty())
{ {
ownerGUID = ObjectGuid.Create(HighGuid.Player, result.Read<ulong>(0)); responsePacket.Allow = false;
title = result.Read<string>(1); SendPacket(responsePacket);
} Log.outDebug(LogFilter.Network, $"CMSG_PETITION_Select failed for petition ({petitionGuid})");
else
{
Log.outDebug(LogFilter.Network, "CMSG_PETITION_Select failed for petition ({0})", petitionGUID.ToString());
return; return;
} }
int reqSignatures = WorldConfig.GetIntValue(WorldCfg.MinPetitionSigns); uint reqSignatures = WorldConfig.GetUIntValue(WorldCfg.MinPetitionSigns);
PetitionInfo petitionInfo = new PetitionInfo(); PetitionInfo petitionInfo = new PetitionInfo();
petitionInfo.PetitionID = (int)petitionGUID.GetCounter(); petitionInfo.PetitionID = (int)petitionGuid.GetCounter();
petitionInfo.Petitioner = ownerGUID; petitionInfo.Petitioner = petition.ownerGuid;
petitionInfo.MinSignatures = reqSignatures; petitionInfo.MinSignatures = reqSignatures;
petitionInfo.MaxSignatures = reqSignatures; petitionInfo.MaxSignatures = reqSignatures;
petitionInfo.Title = title; petitionInfo.Title = petition.petitionName;
responsePacket.Allow = true; responsePacket.Allow = true;
responsePacket.Info = petitionInfo; responsePacket.Info = petitionInfo;
@@ -210,6 +185,13 @@ namespace Game
if (!item) if (!item)
return; return;
Petition petition = Global.PetitionMgr.GetPetition(packet.PetitionGuid);
if (petition == null)
{
Log.outDebug(LogFilter.Network, $"CMSG_PETITION_QUERY failed for petition {packet.PetitionGuid}");
return;
}
if (Global.GuildMgr.GetGuildByName(packet.NewGuildName)) if (Global.GuildMgr.GetGuildByName(packet.NewGuildName))
{ {
Guild.SendCommandResult(this, GuildCommandType.CreateGuild, GuildCommandError.NameExists_S, packet.NewGuildName); Guild.SendCommandResult(this, GuildCommandType.CreateGuild, GuildCommandError.NameExists_S, packet.NewGuildName);
@@ -221,10 +203,8 @@ namespace Game
return; return;
} }
PreparedStatement stmt = DB.Characters.GetPreparedStatement(CharStatements.UPD_PETITION_NAME); // update petition storage
stmt.AddValue(0, packet.NewGuildName); petition.UpdateName(packet.NewGuildName);
stmt.AddValue(1, packet.PetitionGuid.GetCounter());
DB.Characters.Execute(stmt);
PetitionRenameGuildResponse renameResponse = new PetitionRenameGuildResponse(); PetitionRenameGuildResponse renameResponse = new PetitionRenameGuildResponse();
renameResponse.PetitionGuid = packet.PetitionGuid; renameResponse.PetitionGuid = packet.PetitionGuid;
@@ -235,19 +215,15 @@ namespace Game
[WorldPacketHandler(ClientOpcodes.SignPetition)] [WorldPacketHandler(ClientOpcodes.SignPetition)]
void HandleSignPetition(SignPetition packet) void HandleSignPetition(SignPetition packet)
{ {
PreparedStatement stmt = DB.Characters.GetPreparedStatement(CharStatements.SEL_PETITION_SIGNATURES); Petition petition = Global.PetitionMgr.GetPetition(packet.PetitionGUID);
stmt.AddValue(0, packet.PetitionGUID.GetCounter()); if (petition == null)
stmt.AddValue(1, packet.PetitionGUID.GetCounter());
SQLResult result = DB.Characters.Query(stmt);
if (result.IsEmpty())
{ {
Log.outError(LogFilter.Network, "Petition {0} is not found for player {1} {2}", packet.PetitionGUID.ToString(), GetPlayer().GetGUID().ToString(), GetPlayer().GetName()); Log.outError(LogFilter.Network, $"Petition {packet.PetitionGUID} is not found for player {GetPlayer().GetGUID()} {GetPlayer().GetName()}");
return; return;
} }
ObjectGuid ownerGuid = ObjectGuid.Create(HighGuid.Player, result.Read<ulong>(0)); ObjectGuid ownerGuid = petition.ownerGuid;
ulong signs = result.Read<ulong>(1); int signs = petition.signatures.Count;
if (ownerGuid == GetPlayer().GetGUID()) if (ownerGuid == GetPlayer().GetGUID())
return; return;
@@ -270,40 +246,40 @@ namespace Game
return; return;
} }
if (++signs > 10) // client signs maximum
return;
// Client doesn't allow to sign petition two times by one character, but not check sign by another character from same account // Client doesn't allow to sign petition two times by one character, but not check sign by another character from same account
// not allow sign another player from already sign player account // not allow sign another player from already sign player account
stmt = DB.Characters.GetPreparedStatement(CharStatements.SEL_PETITION_SIG_BY_ACCOUNT);
stmt.AddValue(0, GetAccountId());
stmt.AddValue(1, packet.PetitionGUID.GetCounter());
result = DB.Characters.Query(stmt);
PetitionSignResults signResult = new PetitionSignResults(); PetitionSignResults signResult = new PetitionSignResults();
signResult.Player = GetPlayer().GetGUID(); signResult.Player = GetPlayer().GetGUID();
signResult.Item = packet.PetitionGUID; signResult.Item = packet.PetitionGUID;
if (!result.IsEmpty()) bool isSigned = petition.IsPetitionSignedByAccount(GetAccountId());
if (isSigned)
{ {
signResult.Error = PetitionSigns.AlreadySigned; signResult.Error = PetitionSigns.AlreadySigned;
// close at signer side // close at signer side
SendPacket(signResult); SendPacket(signResult);
// update for owner if online
Player owner = Global.ObjAccessor.FindConnectedPlayer(ownerGuid);
if (owner != null)
owner.GetSession().SendPacket(signResult);
return; return;
} }
stmt = DB.Characters.GetPreparedStatement(CharStatements.INS_PETITION_SIGNATURE); // fill petition store
stmt.AddValue(0, ownerGuid.GetCounter()); petition.AddSignature(packet.PetitionGUID, GetAccountId(), _player.GetGUID(), false);
stmt.AddValue(1, packet.PetitionGUID.GetCounter());
stmt.AddValue(2, GetPlayer().GetGUID().GetCounter());
stmt.AddValue(3, GetAccountId());
DB.Characters.Execute(stmt);
Log.outDebug(LogFilter.Network, "PETITION SIGN: {0} by player: {1} ({2} Account: {3})", packet.PetitionGUID.ToString(), GetPlayer().GetName(), GetPlayer().GetGUID().ToString(), GetAccountId()); Log.outDebug(LogFilter.Network, "PETITION SIGN: {0} by player: {1} ({2} Account: {3})", packet.PetitionGUID.ToString(), GetPlayer().GetName(), GetPlayer().GetGUID().ToString(), GetAccountId());
signResult.Error = PetitionSigns.Ok; signResult.Error = PetitionSigns.Ok;
// close at signer side
SendPacket(signResult); SendPacket(signResult);
// update signs count on charter
Item item = _player.GetItemByGuid(packet.PetitionGUID); Item item = _player.GetItemByGuid(packet.PetitionGUID);
if (item != null) if (item != null)
{ {
@@ -312,33 +288,29 @@ namespace Game
} }
// update for owner if online // update for owner if online
Player owner = Global.ObjAccessor.FindPlayer(ownerGuid); Player owner1 = Global.ObjAccessor.FindPlayer(ownerGuid);
if (owner) if (owner1)
owner.SendPacket(signResult); owner1.SendPacket(signResult);
} }
[WorldPacketHandler(ClientOpcodes.DeclinePetition)] [WorldPacketHandler(ClientOpcodes.DeclinePetition)]
void HandleDeclinePetition(DeclinePetition packet) void HandleDeclinePetition(DeclinePetition packet)
{ {
PreparedStatement stmt = DB.Characters.GetPreparedStatement(CharStatements.SEL_PETITION_OWNER_BY_GUID); // Disabled because packet isn't handled by the client in any way
stmt.AddValue(0, packet.PetitionGUID.GetCounter()); /*
SQLResult result = DB.Characters.Query(stmt); Petition petition = sPetitionMgr.GetPetition(packet.PetitionGUID);
if (petition == null)
if (result.IsEmpty())
return; return;
ObjectGuid ownerguid = ObjectGuid.Create(HighGuid.Player, result.Read<ulong>(0)); // petition owner online
Player owner = Global.ObjAccessor.FindConnectedPlayer(petition.ownerGuid);
Player owner = Global.ObjAccessor.FindPlayer(ownerguid); if (owner != null) // petition owner online
if (owner) // petition owner online
{ {
// Disabled because packet isn't handled by the client in any way PetitionDeclined packet = new PetitionDeclined();
/* packet.Decliner = _player.GetGUID();
WorldPacket data = new WorldPacket(ServerOpcodes.PetitionDecline); owner.GetSession().SendPacket(packet);
data.WritePackedGuid(GetPlayer().GetGUID());
owner.SendPacket(data);
*/
} }
*/
} }
[WorldPacketHandler(ClientOpcodes.OfferPetition)] [WorldPacketHandler(ClientOpcodes.OfferPetition)]
@@ -348,6 +320,10 @@ namespace Game
if (!player) if (!player)
return; return;
Petition petition = Global.PetitionMgr.GetPetition(packet.ItemGUID);
if (petition == null)
return;
if (!WorldConfig.GetBoolValue(WorldCfg.AllowTwoSideInteractionGuild) && GetPlayer().GetTeam() != player.GetTeam()) if (!WorldConfig.GetBoolValue(WorldCfg.AllowTwoSideInteractionGuild) && GetPlayer().GetTeam() != player.GetTeam())
{ {
Guild.SendCommandResult(this, GuildCommandType.CreateGuild, GuildCommandError.NotAllied); Guild.SendCommandResult(this, GuildCommandType.CreateGuild, GuildCommandError.NotAllied);
@@ -366,28 +342,7 @@ namespace Game
return; return;
} }
PreparedStatement stmt = DB.Characters.GetPreparedStatement(CharStatements.SEL_PETITION_SIGNATURE); SendPetitionSigns(petition, player);
stmt.AddValue(0, packet.ItemGUID.GetCounter());
SQLResult result = DB.Characters.Query(stmt);
ServerPetitionShowSignatures signaturesPacket = new ServerPetitionShowSignatures();
signaturesPacket.Item = packet.ItemGUID;
signaturesPacket.Owner = GetPlayer().GetGUID();
signaturesPacket.OwnerAccountID = ObjectGuid.Create(HighGuid.WowAccount, player.GetSession().GetAccountId());
signaturesPacket.PetitionID = (int)packet.ItemGUID.GetCounter(); // @todo verify that...
do
{
ObjectGuid signerGUID = ObjectGuid.Create(HighGuid.Player, result.Read<ulong>(0));
ServerPetitionShowSignatures.PetitionSignature signature = new ServerPetitionShowSignatures.PetitionSignature();
signature.Signer = signerGUID;
signature.Choice = 0;
signaturesPacket.Signatures.Add(signature);
}
while (result.NextRow());
player.SendPacket(signaturesPacket);
} }
[WorldPacketHandler(ClientOpcodes.TurnInPetition)] [WorldPacketHandler(ClientOpcodes.TurnInPetition)]
@@ -398,27 +353,17 @@ namespace Game
if (!item) if (!item)
return; return;
// Get petition data from db Petition petition = Global.PetitionMgr.GetPetition(packet.Item);
ObjectGuid ownerguid; if (petition == null)
string name;
PreparedStatement stmt = DB.Characters.GetPreparedStatement(CharStatements.SEL_PETITION);
stmt.AddValue(0, packet.Item.GetCounter());
SQLResult result = DB.Characters.Query(stmt);
if (!result.IsEmpty())
{
ownerguid = ObjectGuid.Create(HighGuid.Player, result.Read<ulong>(0));
name = result.Read<string>(1);
}
else
{ {
Log.outError(LogFilter.Network, "Player {0} ({1}) tried to turn in petition ({2}) that is not present in the database", GetPlayer().GetName(), GetPlayer().GetGUID().ToString(), packet.Item.ToString()); Log.outError(LogFilter.Network, "Player {0} ({1}) tried to turn in petition ({2}) that is not present in the database", GetPlayer().GetName(), GetPlayer().GetGUID().ToString(), packet.Item.ToString());
return; return;
} }
string name = petition.petitionName; // we need a copy, Guild::AddMember invalidates petition
// Only the petition owner can turn in the petition // Only the petition owner can turn in the petition
if (GetPlayer().GetGUID() != ownerguid) if (GetPlayer().GetGUID() != petition.ownerGuid)
return; return;
TurnInPetitionResult resultPacket = new TurnInPetitionResult(); TurnInPetitionResult resultPacket = new TurnInPetitionResult();
@@ -427,7 +372,7 @@ namespace Game
if (GetPlayer().GetGuildId() != 0) if (GetPlayer().GetGuildId() != 0)
{ {
resultPacket.Result = PetitionTurns.AlreadyInGuild; resultPacket.Result = PetitionTurns.AlreadyInGuild;
GetPlayer().SendPacket(resultPacket); SendPacket(resultPacket);
return; return;
} }
@@ -438,25 +383,11 @@ namespace Game
return; return;
} }
// Get petition signatures from db var signatures = petition.signatures; // we need a copy, Guild::AddMember invalidates petition
stmt = DB.Characters.GetPreparedStatement(CharStatements.SEL_PETITION_SIGNATURE);
stmt.AddValue(0, packet.Item.GetCounter());
result = DB.Characters.Query(stmt);
List<ObjectGuid> guids = new List<ObjectGuid>();
if (!result.IsEmpty())
{
do
{
guids.Add(ObjectGuid.Create(HighGuid.Player, result.Read<ulong>(0)));
}
while (result.NextRow());
}
uint requiredSignatures = WorldConfig.GetUIntValue(WorldCfg.MinPetitionSigns); uint requiredSignatures = WorldConfig.GetUIntValue(WorldCfg.MinPetitionSigns);
// Notify player if signatures are missing // Notify player if signatures are missing
if (guids.Count < requiredSignatures) if (signatures.Count < requiredSignatures)
{ {
resultPacket.Result = PetitionTurns.NeedMoreSignatures; resultPacket.Result = PetitionTurns.NeedMoreSignatures;
SendPacket(resultPacket); SendPacket(resultPacket);
@@ -480,21 +411,15 @@ namespace Game
SQLTransaction trans = new SQLTransaction(); SQLTransaction trans = new SQLTransaction();
// Add members from signatures // Add members from signatures
foreach (var guid in guids) foreach (var signature in signatures)
guild.AddMember(trans, guid); guild.AddMember(trans, signature.PlayerGuid);
stmt = DB.Characters.GetPreparedStatement(CharStatements.DEL_PETITION_BY_GUID);
stmt.AddValue(0, packet.Item.GetCounter());
trans.Append(stmt);
stmt = DB.Characters.GetPreparedStatement(CharStatements.DEL_PETITION_SIGNATURE_BY_GUID);
stmt.AddValue(0, packet.Item.GetCounter());
trans.Append(stmt);
DB.Characters.CommitTransaction(trans); DB.Characters.CommitTransaction(trans);
Global.PetitionMgr.RemovePetition(packet.Item);
// created // created
Log.outDebug(LogFilter.Network, "Player {0} ({1}) turning in petition {2}", GetPlayer().GetName(), GetPlayer().GetGUID().ToString(), packet.Item.ToString()); Log.outDebug(LogFilter.Network, $"Player {GetPlayer().GetName()} ({GetPlayer().GetGUID()}) turning in petition {packet.Item}");
resultPacket.Result = PetitionTurns.Ok; resultPacket.Result = PetitionTurns.Ok;
SendPacket(resultPacket); SendPacket(resultPacket);
@@ -295,8 +295,8 @@ namespace Game.Network.Packets
data.WriteInt32(PetitionID); data.WriteInt32(PetitionID);
data.WritePackedGuid(Petitioner); data.WritePackedGuid(Petitioner);
data.WriteInt32(MinSignatures); data.WriteUInt32(MinSignatures);
data.WriteInt32(MaxSignatures); data.WriteUInt32(MaxSignatures);
data.WriteInt32(DeadLine); data.WriteInt32(DeadLine);
data.WriteInt32(IssueDate); data.WriteInt32(IssueDate);
data.WriteInt32(AllowedGuildID); data.WriteInt32(AllowedGuildID);
@@ -328,8 +328,8 @@ namespace Game.Network.Packets
public ObjectGuid Petitioner; public ObjectGuid Petitioner;
public string Title; public string Title;
public string BodyText; public string BodyText;
public int MinSignatures; public uint MinSignatures;
public int MaxSignatures; public uint MaxSignatures;
public int DeadLine; public int DeadLine;
public int IssueDate; public int IssueDate;
public int AllowedGuildID; public int AllowedGuildID;
+6
View File
@@ -836,6 +836,12 @@ namespace Game
Log.outInfo(LogFilter.ServerLoading, "Loading Calendar data..."); Log.outInfo(LogFilter.ServerLoading, "Loading Calendar data...");
Global.CalendarMgr.LoadFromDB(); Global.CalendarMgr.LoadFromDB();
Log.outInfo(LogFilter.ServerLoading, "Loading Petitions...");
Global.PetitionMgr.LoadPetitions();
Log.outInfo(LogFilter.ServerLoading, "Loading Signatures...");
Global.PetitionMgr.LoadSignatures();
Log.outInfo(LogFilter.ServerLoading, "Loading Item loot..."); Log.outInfo(LogFilter.ServerLoading, "Loading Item loot...");
Global.LootItemStorage.LoadStorageFromDB(); Global.LootItemStorage.LoadStorageFromDB();