Esempio n. 1
0
        /// <summary>
        /// Remove a client from all of the data in the connector.
        /// </summary>
        /// <param name="client">
        /// The client that we wish to remove.
        /// </param>
        public void RemoveClient(IClient client)
        {
            Player      p    = connector.GetPlayer(client);
            ISearchGame game = connector.GetGame(p);

            // remove the clients player from his current game
            game?.RemovePlayer(p);
            // delete the clients player from the server
            connector.DeleteClient(client);
            // delete the game if the forfeit of the player made it end
            if (!ReferenceEquals(game, null) && game.HasEnded())
            {
                connector.DeleteGame(game);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Start the game with the given name,number of rows,number of columns and creator.
        /// </summary>
        /// <param name="name">
        /// The name of the game.
        /// </param>
        /// <param name="rows">
        /// Number of rows.
        /// </param>
        /// <param name="cols">
        /// Number of columns.
        /// </param>
        /// <param name="creator">
        /// The creator of this game.
        /// </param>
        /// <returns>
        /// The SearchGame that was started.
        /// </returns>
        public ISearchGame StartGame(string name, int rows, int cols, IClient creator)
        {
            /*
             * If the game already exists, the user didn't used this command as he should and won't be getting a game.
             * If we would join the client anyway, we will ignore his requst to "rows", "cols" and maybe he does't want
             * to play in a different size maze(it's like the difference between requesting a 100 pieces puzzle and
             * getting a 2000 pieces puzzle).
             * If a game with that name already exists, We will generate a new game for the client if and only if
             * he's the only player is the exitising game or, the exisiting game has ended.
             */
            bool        toCreate = true;
            Player      p = connector.GetPlayer(creator);
            ISearchGame g1 = connector.GetGame(p), g = null;

            if (!ReferenceEquals(null, g1))
            {
                toCreate = g1.HasEnded() || (g1.NumOfPlayer == 1 && g1.GetPlayers().Contains(p));
            }
            if (connector.ContainsGame(name))
            {
                g = connector.GetGame(name);

                /* g.NumOfPlayer > 0 always because the server know only multiplayer games
                 * replace the existing game if it has ended or the only player in it in the
                 * one who asks to replace it.*/
                toCreate &= (g.HasEnded() || (g.NumOfPlayer == 1 && g.GetPlayers().Contains(p)));
            }
            if (toCreate)
            {
                // delete exitsing games
                connector.DeleteGame(g);
                try
                {
                    connector.DeleteGame(g1);
                } catch (Exception)
                {
                    //The game with this name was deleted
                }
                // Create a game with this name.
                ISearchGame game = GenerateNewGame(name, rows, cols);
                connector.AddGame(game);
                connector.AddClientToGame(creator, game);
                game.AddPlayer(connector.GetPlayer(creator));
                return(game);
            }
            return(null);
        }