Core/Calendar: Add some additional validation when creating events

Port From (https://github.com/TrinityCore/TrinityCore/commit/562c567b1e0c878f0632994a79bcfbfd4a962504)
This commit is contained in:
hondacrx
2022-01-04 20:40:30 -05:00
parent 7f053ea099
commit 4b08dc2756
6 changed files with 222 additions and 33 deletions
+86 -6
View File
@@ -124,17 +124,51 @@ namespace Game
// prevent events in the past
// To Do: properly handle timezones and remove the "- time_t(86400L)" hack
if (calendarAddEvent.EventInfo.Time < (GameTime.GetGameTime() - 86400L))
{
Global.CalendarMgr.SendCalendarCommandResult(guid, CalendarError.EventPassed);
return;
}
// If the event is a guild event, check if the player is in a guild
if (CalendarEvent.IsGuildEvent(calendarAddEvent.EventInfo.Flags) || CalendarEvent.IsGuildAnnouncement(calendarAddEvent.EventInfo.Flags))
{
if (_player.GetGuildId() == 0)
{
Global.CalendarMgr.SendCalendarCommandResult(guid, CalendarError.GuildPlayerNotInGuild);
return;
}
}
// Check if the player reached the max number of events allowed to create
if (CalendarEvent.IsGuildEvent(calendarAddEvent.EventInfo.Flags) || CalendarEvent.IsGuildAnnouncement(calendarAddEvent.EventInfo.Flags))
{
if (Global.CalendarMgr.GetGuildEvents(_player.GetGuildId()).Count >= SharedConst.CalendarMaxGuildEvents)
{
Global.CalendarMgr.SendCalendarCommandResult(guid, CalendarError.GuildEventsExceeded);
return;
}
}
else
{
if (Global.CalendarMgr.GetEventsCreatedBy(guid).Count >= SharedConst.CalendarMaxEvents)
{
Global.CalendarMgr.SendCalendarCommandResult(guid, CalendarError.EventsExceeded);
return;
}
}
if (GetCalendarEventCreationCooldown() > GameTime.GetGameTime())
{
Global.CalendarMgr.SendCalendarCommandResult(guid, CalendarError.Internal);
return;
}
SetCalendarEventCreationCooldown(GameTime.GetGameTime() + SharedConst.CalendarCreateEventCooldown);
CalendarEvent calendarEvent = new(Global.CalendarMgr.GetFreeEventId(), guid, 0, (CalendarEventType)calendarAddEvent.EventInfo.EventType, calendarAddEvent.EventInfo.TextureID,
calendarAddEvent.EventInfo.Time, (CalendarFlags)calendarAddEvent.EventInfo.Flags, calendarAddEvent.EventInfo.Title, calendarAddEvent.EventInfo.Description, 0);
if (calendarEvent.IsGuildEvent() || calendarEvent.IsGuildAnnouncement())
{
Player creator = Global.ObjAccessor.FindPlayer(guid);
if (creator)
calendarEvent.GuildId = creator.GetGuildId();
}
calendarEvent.GuildId = _player.GetGuildId();
if (calendarEvent.IsGuildAnnouncement())
{
@@ -209,11 +243,57 @@ namespace Game
// prevent events in the past
// To Do: properly handle timezones and remove the "- time_t(86400L)" hack
if (calendarCopyEvent.Date < (GameTime.GetGameTime() - 86400L))
{
Global.CalendarMgr.SendCalendarCommandResult(guid, CalendarError.EventPassed);
return;
}
CalendarEvent oldEvent = Global.CalendarMgr.GetEvent(calendarCopyEvent.EventID);
if (oldEvent != null)
{
// Ensure that the player has access to the event
if (oldEvent.IsGuildEvent() || oldEvent.IsGuildAnnouncement())
{
if (oldEvent.GuildId != _player.GetGuildId())
{
Global.CalendarMgr.SendCalendarCommandResult(guid, CalendarError.EventInvalid);
return;
}
}
else
{
if (oldEvent.OwnerGuid != guid)
{
Global.CalendarMgr.SendCalendarCommandResult(guid, CalendarError.EventInvalid);
return;
}
}
// Check if the player reached the max number of events allowed to create
if (oldEvent.IsGuildEvent() || oldEvent.IsGuildAnnouncement())
{
if (Global.CalendarMgr.GetGuildEvents(_player.GetGuildId()).Count >= SharedConst.CalendarMaxGuildEvents)
{
Global.CalendarMgr.SendCalendarCommandResult(guid, CalendarError.GuildEventsExceeded);
return;
}
}
else
{
if (Global.CalendarMgr.GetEventsCreatedBy(guid).Count >= SharedConst.CalendarMaxEvents)
{
Global.CalendarMgr.SendCalendarCommandResult(guid, CalendarError.EventsExceeded);
return;
}
}
if (GetCalendarEventCreationCooldown() > GameTime.GetGameTime())
{
Global.CalendarMgr.SendCalendarCommandResult(guid, CalendarError.Internal);
return;
}
SetCalendarEventCreationCooldown(GameTime.GetGameTime() + SharedConst.CalendarCreateEventCooldown);
CalendarEvent newEvent = new(oldEvent, Global.CalendarMgr.GetFreeEventId());
newEvent.Date = calendarCopyEvent.Date;
Global.CalendarMgr.AddEvent(newEvent, CalendarSendEventType.Copy);