Exemplo n.º 1
0
        //---------------------------- DispatchMessage ---------------------------
        //  Given a message, a receiver, a sender and any time delay , this function
        //  routes the message to the correct agent (if no delay) or stores
        //  in the message queue to be dispatched at the correct time.
        //------------------------------------------------------------------------

        public void DispatchMessage(double delay, int sender, int reciever, int message)
        {
            BaseGameEntity pSender   = EntityManager.Instance.GetEntityFromID(sender);
            BaseGameEntity pReciever = EntityManager.Instance.GetEntityFromID(reciever);

            if (pReciever == null)
            {
                Console.WriteLine($"Warning! No reciever with ID of {reciever} found.");
                return;
            }

            Telegram telegram = new Telegram(0, sender, reciever, message);

            if (delay <= 0.0f)
            {
                ConsoleUtils.SetTextColor(ConsoleUtils.ColorConfigs.Telegram);
                Console.WriteLine($"Instant telegram dispatched at time: {Clock.CurrentTime:N3} by {pSender.Name} for {pReciever.Name}. Message is {Message.GetStringFromMessage(message)}");

                Discharge(pReciever, telegram);
            }
            else
            {
                telegram.dispatchTime = Clock.CurrentTime + delay;

                priorityQ.Enqueue(telegram);
                ConsoleUtils.SetTextColor(ConsoleUtils.ColorConfigs.Telegram);
                Console.WriteLine($"Delayed telegram from {pSender.Name} recorded at time: {Clock.CurrentTime:N3} for {pReciever.Name}." +
                                  $" Message is {Message.GetStringFromMessage(message)}");
            }
        }
Exemplo n.º 2
0
        //---------------------------- Discharge ---------------------------
        // This method is utilized by DispatchMessage or DispatchDelayedMessages.
        // This method calls the message handling member function of the receiving
        // entity, pReceiver, with the newly created telegram.
        //------------------------------------------------------------------------

        private void Discharge(BaseGameEntity reciever, Telegram message)
        {
            if (!reciever.HandleMessage(message))
            {
                Console.WriteLine("Message not handled.");
            }
        }
Exemplo n.º 3
0
        //---------------------- DispatchDelayedMessages -------------------------
        //  This function dispatches any telegrams with a timestamp that has
        //  expired. Any dispatched telegrams are removed from the queue.
        //------------------------------------------------------------------------

        public void DispatchDelayedMessage()
        {
            double currentTime = Clock.CurrentTime;

            while ((priorityQ.Count != 0) &&
                   (priorityQ.First().dispatchTime < currentTime) &&
                   (priorityQ.First().dispatchTime > 0))
            {
                Telegram       telegram  = priorityQ.First();
                BaseGameEntity pReciever = EntityManager.Instance.GetEntityFromID(telegram.reciever);

                ConsoleUtils.SetTextColor(ConsoleUtils.ColorConfigs.Telegram);
                Console.WriteLine($"Queued telegram ready for dispatch: Sent to {pReciever.Name}, Message is {Message.GetStringFromMessage(telegram.message)}");

                Discharge(pReciever, telegram);

                priorityQ.Dequeue();
            }
        }
Exemplo n.º 4
0
 //---------------------------- GetEntityFromID ---------------------------
 // This method removes the entity from the Dictionary.
 //------------------------------------------------------------------------
 public void RemoveEntity(BaseGameEntity newEntity)
 {
     entityMap.Remove(newEntity.ID);
 }
Exemplo n.º 5
0
        //---------------------------- RegisterEntity ---------------------------
        // This method adds the entity in the Dictionary
        // with the ID number as key.
        //------------------------------------------------------------------------

        public void RegisterEntity(BaseGameEntity newEntity)
        {
            entityMap.Add(newEntity.ID, newEntity);
        }