Exemple #1
0
        /// <summary>
        /// THIS IS ONE OF THE MESSAGE DESIGN PATTERN METHODS.
        /// This method sends the message to the other thread to be dealt with.
        /// </summary>
        /// <param name="otherShip"></param>
        private void SendOtherShipAction(SpaceShip otherShip)
        {
            try
            {
                MessageDTO messageDto = InteractWithOtherShip?.Invoke(otherShip.name) ?? new MessageDTO()
                {
                    intent = -1
                };

                if (messageDto.intent != -1)
                {
                    shipThatWasMessaged = otherShip.name;
                    string message = JsonConvert.SerializeObject(messageDto);
                    otherShip.RecieveOtherShipAction(message);
                    // The ship waiting on the other ship to respond will busy wait to deal with any interactions.
                    shipStatus = ShipStatuses.Waiting;
                }
            }
            catch (NullReferenceException)
            {
                // During the exiting of the code, the interactions will become null.
                // Thus there will be a null referece exception thrown.
                // This is because a thread is relying on a resource that is becoming removed when exiting.
                return;
            }
        }
Exemple #2
0
        /// <summary>
        /// THIS IS ONE OF THE MESSAGE DESIGN PATTERN METHODS.
        /// This sends the response of the ship to the thread that concerns the other ship.
        /// </summary>
        /// <param name="messageDto">
        /// message to respond to.
        /// </param>
        private void RespondToOtherShipAction(MessageDTO messageDto)
        {
            try
            {
                SpaceShip otherShip = Board.GetSpaceShip(messageDto.fromShipName);

                if (otherShip == null)
                {
                    return;
                }

                if (shipThatWasMessaged == otherShip.name)
                {
                    shipStatus = ShipStatuses.Working;
                }

                if (reactionDictionary.Keys.Contains((Intents)messageDto.intent))
                {
                    MessageDTO responseDto = reactionDictionary[(Intents)messageDto.intent]?.Invoke(messageDto) ?? new MessageDTO()
                    {
                        intent = -1
                    };

                    if (responseDto.intent == -1 || responseDto.intent == (int)Intents.Destroy)
                    {
                        shipStatus = ShipStatuses.Working;
                    }

                    string responseString = JsonConvert.SerializeObject(responseDto);

                    otherShip.RecieveOtherShipAction(responseString);
                }
                else if (messageDto.intent == -1)
                {
                    shipStatus = ShipStatuses.Working;
                }
                else
                {
                    string noInteractionMessage = JsonConvert.SerializeObject(new MessageDTO()
                    {
                        intent       = (int)Intents.StopInteraction,
                        fromShipName = name,
                        tradeObject  = null
                    });
                    otherShip.RecieveOtherShipAction(noInteractionMessage);
                }
            }
            catch (ArgumentException)
            {// safely catch if we can no longer respond to the other ship. So we can carry onwards.
                Console.WriteLine("The ship " + messageDto.fromShipName + " was not found when we went to respond to it.");
            }
            catch (ShipDestroyedException)
            {
                Console.WriteLine("The ship " + name + " has been destroyed!");
            }
        }