Esempio n. 1
0
        //internal method for catching exception when failed to send data to client
        private void CatchConnectionException(Exception ex, int playerNumber)
        {
            var logger = NLog.LogManager.GetCurrentClassLogger();

            logger.Error($"Couldn't obtain response from client with player number {playerNumber.ToString()}: {ex.Message}.");

            //Send info to clients about ending the game, because of no some
            //player did not answer
            InfoSenderClass Sender = new InfoSenderClass();

            Sender.DeleteTheRoom(ClientInfoType.LostConnectionToClient);
        }
        //Joining to the room timeout time passed
        private static void JoiningTimeoutTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            var logger = NLog.LogManager.GetCurrentClassLogger();

            logger.Info("Time for joining the room just passed. Start sending info to clients.");

            //Send info to clients
            InfoSenderClass Sender = new InfoSenderClass();

            Sender.DeleteTheRoom(ClientInfoType.JoiningTimeout);

            DispodeJoiningTimer();
        }
        //Jreadiness to sart the game timeout time passed
        private static void ReadinessTimeoutTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            var logger = NLog.LogManager.GetCurrentClassLogger();

            logger.Info("Time for confirming readiness to play just passed. Start sending info to clients.");

            //Send info to clients
            InfoSenderClass Sender = new InfoSenderClass();

            Sender.DeleteTheRoom(ClientInfoType.ReadinessTimeout);

            //stop the timer
            DispodeReadinessTimer();
        }
        //method called when some user changes readiness to play status.
        public static void FireUpStartingNewGameAlgorithm(SynchronizationContext synchCont)
        {
            //assign synchronization context
            synchronizationContext = synchCont;

            //controrlling timeout measuring options
            (_, bool allPlayersReady) = CheckIfPlayersAreReady();
            if (allPlayersReady)
            {
                StopAllTimeoutTimers();
            }

            //check if there is as many players as in current game setup data
            bool allPlayersAreAlive = false;

            if (CheckIfRoomIsFull())
            {
                InfoSenderClass Sender = new InfoSenderClass();
                allPlayersAreAlive = Sender.CheckAllivenessOfCurrentPlayers();
            }

            var logger = NLog.LogManager.GetCurrentClassLogger();

            logger.Info(string.Format("While starting new game. Readiness of players: {0}, aliveness pf all players: {1}.",
                                      allPlayersReady.ToString(), allPlayersAreAlive.ToString()));

            //now really start the game
            if (allPlayersReady && allPlayersAreAlive)
            {
                Task.Run(() => StartTheGame(synchCont));
            }
            //or send info to client about error of no connection to some client
            else if (allPlayersReady && !allPlayersAreAlive)
            {
                InfoSenderClass Sender = new InfoSenderClass();
                Sender.DeleteTheRoom(ClientInfoType.LostConnectionToClient);
            }
        }
        //if game did not start properly, send info to players about deletion
        private static void SendLostConnectionRoomDeletedInfo()
        {
            InfoSenderClass Sender = new InfoSenderClass();

            Sender.DeleteTheRoom(ClientInfoType.LostConnectionToClient);
        }