Пример #1
0
        /// <summary>Gets the state of a city event in the specified building.</summary>
        /// <param name="buildingId">The building ID to check events in.</param>
        /// <param name="latestStart">The latest start time of events to consider.</param>
        /// <returns>
        /// The state of an event that meets the specified criteria, or <see cref="CityEventState.None"/> if none found.
        /// </returns>
        public CityEventState GetEventState(ushort buildingId, DateTime latestStart)
        {
            if (buildingId == 0)
            {
                return(CityEventState.None);
            }

            ushort eventId = buildingManager.GetEvent(buildingId);

            if (eventId != 0)
            {
                EventData.Flags vanillaEventState = eventManager.GetEventFlags(eventId);
                if ((vanillaEventState & (EventData.Flags.Preparing | EventData.Flags.Ready)) != 0)
                {
                    if (eventManager.TryGetEventInfo(eventId, out _, out DateTime startTime, out _, out _) && startTime <= latestStart)
                    {
                        return(CityEventState.Upcoming);
                    }

                    return(CityEventState.None);
                }

                if ((vanillaEventState & EventData.Flags.Active) != 0)
                {
                    return(CityEventState.Ongoing);
                }

                if (vanillaEventState != EventData.Flags.None)
                {
                    return(CityEventState.Finished);
                }
            }

            if (activeEvent != null && activeEvent.BuildingId == buildingId)
            {
                return(CityEventState.Ongoing);
            }

            if (lastActiveEvent != null && lastActiveEvent.BuildingId == buildingId)
            {
                return(CityEventState.Finished);
            }

            if (upcomingEvents.FirstOrDefaultNode(e => e.BuildingId == buildingId && e.StartTime <= latestStart) != null)
            {
                return(CityEventState.Upcoming);
            }

            return(CityEventState.None);
        }
Пример #2
0
        private bool SynchronizeWithVanillaEvent(ushort eventId)
        {
            if (!eventManager.TryGetEventInfo(eventId, out var eventInfo))
            {
                return(false);
            }

            var startTime = eventInfo.StartTime;

            if (startTime.AddHours(eventInfo.Duration) < timeInfo.Now)
            {
                return(false);
            }

            var existingVanillaEvent = GetVanillaEvent(AllEvents, eventId, eventInfo.BuildingId);

            if (existingVanillaEvent != null)
            {
                if (Math.Abs((startTime - existingVanillaEvent.StartTime).TotalMinutes) <= 5d)
                {
                    return(false);
                }
                else if (activeEvents.Contains(existingVanillaEvent))
                {
                    activeEvents.Remove(existingVanillaEvent);
                }
                else
                {
                    upcomingEvents.Remove(existingVanillaEvent);
                }
            }

            var newStartTime = EnsureUniqueStartTime(startTime);

            if (newStartTime != startTime)
            {
                startTime = newStartTime;
                eventManager.SetStartTime(eventId, startTime);
            }

            var newEvent = new VanillaEvent(eventId, eventInfo.Duration, eventInfo.TicketPrice, eventManager);

            newEvent.Configure(eventInfo.BuildingId, buildingManager.GetBuildingName(eventInfo.BuildingId), startTime);
            Log.Debug(LogCategory.Events, timeInfo.Now, $"Vanilla event registered for {newEvent.BuildingId}, start time {newEvent.StartTime}, end time {newEvent.EndTime}");

            var existingEvent = upcomingEvents.FirstOrDefaultNode(e => e.StartTime >= startTime);

            if (existingEvent == null)
            {
                upcomingEvents.AddLast(newEvent);
            }
            else
            {
                upcomingEvents.AddBefore(existingEvent, newEvent);
            }

            return(true);
        }