Пример #1
0
 public static void Stop()
 {
     Logger.log(31, Category.Engine, Severity.Info, "Engine shutting down");
     TickEngine.Dispose();
     EventEngine.ProcessEvents(); //Wait for next tick so all events get calculated
     StateEngine.Dispose();
 }
Пример #2
0
 public static void Init()
 {
     if (!entitiesInitialized)
     {
         Entities.Settings.BuildingSettings.MakeSettings();
         Entities.Settings.UnitSettings.MakeSettings();
         Entities.Settings.ItemSettings.MakeSettings();
         entitiesInitialized = true;
     }
     StateEngine.Init();
     TickEngine.init();
 }
Пример #3
0
        public static void Tick()
        {
            var stopwatch = new Stopwatch();

            stopwatch.Start();


            List <IEvent> EventsProcessed = EventEngine.ProcessEvents();

            if (EventsProcessed.Count > 0)
            {
                StateEngine.CopyWriteStateToReadState();

                foreach (var e in EventsProcessed)
                {
                    e.Processed.Set();
                }
                if (makePersitentSave)
                {
                    StateEngine.TryPersistentSave();
                    makePersitentSave = false;
                }
            }

            if (tickcount % Settings.persistenSaveEveryXTick == 0)
            {
                makePersitentSave = true;
            }

            stopwatch.Stop();
            var ms = stopwatch.ElapsedMilliseconds;

            if (ms > Settings.tickIntervallInMillisec)
            {
                Logger.log(3, Category.TickEngine, Severity.Warn, String.Format("Tick took longer than expected: {0}ms", ms));
            }
            tickcount += 1;
        }
Пример #4
0
        public static List <IEvent> ProcessEvents()
        {
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            State state            = StateEngine.GetWriteState();
            var   processingEvents = GetEventList(state);

            var AllUpdatedSubscribables = new HashSet <Subscribable>();
            var OnDemandCalculations    = new HashSet <Subscribable>();
            var newTimedEvents          = new List <Event.Event>();

            List <IEvent> eventsFailedToGetEntities = new List <IEvent>();
            var           sentCount = 0;

            foreach (IEvent e in processingEvents)
            {
                try {
                    e.GetEntities(state);
                    var onDemandCalculation = e.NeedsOnDemandCalculation();
                    if (onDemandCalculation != null)
                    {
                        OnDemandCalculations.UnionWith(e.NeedsOnDemandCalculation());
                    }
                }
                catch (Exception ex) {
                    eventsFailedToGetEntities.Add(e);
                    Logger.log(41, Category.EventEngine, Severity.Error, "Event failed to get Entities: " + ex.ToString());
                }
            }
            foreach (IEvent e in eventsFailedToGetEntities)
            {
                processingEvents.Remove(e);
            }
            var processedEvents = new List <Event.IEvent>();

            if (processingEvents.Count > 0)
            {
                foreach (var s in OnDemandCalculations)
                {
                    s.OnDemandCalculation();
                }
                foreach (var e in processingEvents)
                {
                    if (e.Conditions())
                    {
                        try {
                            e.Execute();
                            var timedEvents = e.FollowUpEvents();
                            AllUpdatedSubscribables.UnionWith(e.UpdatedSubscribables());
                            if (timedEvents != null)
                            {
                                newTimedEvents.AddRange(timedEvents);
                                AllUpdatedSubscribables.UnionWith(timedEvents);
                            }
                            processedEvents.Add(e);
                        }
                        catch (Exception ex) {
                            Logger.log(40, Category.Event, Severity.Warn, "Event failed to execute: " + ex.GetType().ToString() + ": " + ex.ToString());
                        }
                    }
                    else
                    {
                        Logger.log(40, Category.Event, Severity.Warn, "Events conditions failed: " + e.GetType().ToString());
                    }
                }
                foreach (Event.Event newTimedEvent in newTimedEvents)
                {
                    AddTimedEvent(newTimedEvent, state);
                }
                sentCount = 0;
                foreach (var s in AllUpdatedSubscribables)
                {
                    s.UpdateSubscribers();
                }
            }

            //Log
            stopwatch.Stop();
            if (processedEvents.Count > 0)
            {
                string eventNames = "Events: ";
                foreach (IEvent e in processedEvents)
                {
                    eventNames += e.GetType().Name + " ";
                }
                string msg = String.Format("{0} events processed. {3} new timed Events. {1} subscribables sent in {2}ms. ", processedEvents.Count, sentCount, stopwatch.ElapsedMilliseconds, newTimedEvents.Count);
                Logger.log(1, Category.EventEngine, Severity.Debug, msg + eventNames);
            }
            return(processedEvents);
        }