Exemple #1
0
        public void Start()
        {
            Stopwatch stopwatch = Stopwatch.StartNew();

            // Start the task that listens to incomming messages and queue them
            messageListenerTask.Start();

            // Send map and their respective position to everyone and start the game
            SignalGameStartToAllActors();

            while (true)
            {
                try
                {
                    // Waits for the next message to arrive
                    Communication.Request request = messageQueue.Take() as Communication.Request;
                    if (request != null)
                    {
                        HandleRequest(request);
                    }
                }
                catch (InvalidOperationException)
                {
                    break; // No more message to receive and the task receiving messages is finished, get out of the infinite while loop
                }
            }

            stopwatch.Stop();
            logger.LogMap(map.ToString());
            logger.LogExecutionTime((int)stopwatch.ElapsedMilliseconds, map.Rats.Count == 0 ? ProcessType.Cat : ProcessType.Rat);
        }
Exemple #2
0
        /// <summary>
        /// Handle a request message
        /// </summary>
        /// <param name="request"></param>
        private void HandleRequest(Communication.Request request)
        {
            ActorProcess sender = GetActorProcessByRank(request.Rank);

            // Death confirmation
            DeathConfirmation deathConfirmation = request as DeathConfirmation;

            if (deathConfirmation != null)
            {
                HandleDeath(deathConfirmation, sender);

                return;
            }

            // Validate that the process is still in the game (playing).
            // This is meant to deny requests that were sent in between the moment the actor was removed from the game and the moment the actor learned about it.
            if (!sender.Playing)
            {
                return; // Ignore the request, the process is no longer playing
            }

            // Move request
            MoveRequest moveRequest = request as MoveRequest;

            if (moveRequest != null)
            {
                HandleMovePlayer(moveRequest, sender);

                return;
            }

            // Meow request
            MeowRequest meowRequest = request as MeowRequest;

            if (meowRequest != null)
            {
                HandleMeow(meowRequest, sender);

                return;
            }
        }