Example #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;
            }
        }
Example #2
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Name,AlertTime,Color,Priority")] ShipStatuses shipStatuses)
        {
            if (id != shipStatuses.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(shipStatuses);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ShipStatusesExists(shipStatuses.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(shipStatuses));
        }
Example #3
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!");
            }
        }
Example #4
0
        public async Task <IActionResult> Create([Bind("Id,Name,AlertTime,Color,Priority")] ShipStatuses shipStatuses)
        {
            if (ModelState.IsValid)
            {
                _context.Add(shipStatuses);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(shipStatuses));
        }
Example #5
0
        /// <summary>
        /// Main loop for each thread.
        /// This method will decide how each ship responds.
        /// And once the thread exits this thread, the thread ends.
        /// </summary>
        public void Think()
        {
            while (shipStatus != ShipStatuses.Destroyed)
            {
                // If we need to interact, interact.
                if (messageQueue.TryDequeue(out string message))
                {
                    // We busy wait so that the responses can get proccessed fully.
                    shipStatus = ShipStatuses.Waiting;

                    MessageDTO messageDto = JsonConvert.DeserializeObject <MessageDTO>(message);
                    RespondToOtherShipAction(messageDto);
                }
                else if (shipStatus != ShipStatuses.Waiting)
                {
                    Move();
                }
            }
        }
Example #6
0
 /// <summary>
 /// Method to handle the destruction of this ship.
 /// </summary>
 public void DestroyShip()
 {
     Board.DestroySpaceShip(this);
     shipStatus = ShipStatuses.Destroyed;
 }