Exemplo n.º 1
0
        /// <summary>
        /// Execution of the Start command based on the given arguments
        /// and the client that sent it.
        /// </summary>
        /// <param name="args">
        /// The arguments of this command.
        /// </param>
        /// <param name="client">
        /// The client which sent this request.
        /// </param>
        public void Execute(string[] args, IClient client)
        {
            string      name = args[0];
            int         rows = int.Parse(args[1]);
            int         cols = int.Parse(args[2]);
            ISearchGame game = model.StartGame(name, rows, cols, client);

            if (!ReferenceEquals(game, null))
            {
                // set condition to start the game
                game.SetStartWhenTrue((g) => (g.NumOfPlayer >= 2));

                // send the client the game when it starts
                game.TellMeWhenTheGameStarts += () => client.SendResponse(game.ToJSON());
            }
            else
            {
                if (!ReferenceEquals(model.GetGameByName(name), null))
                {
                    client.SendResponse(@"A game with that name already exists and 
                        cannot be replaced at this point.");
                }
                else
                {
                    client.SendResponse(@"You're already a part of a game.
                        Please close that game and try again.");
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Execution of the join command based on the given arguments and the client
        /// that sent this command.
        /// </summary>
        /// <param name="args">
        /// The arguments of the command.
        /// </param>
        /// <param name="client">
        /// The client that sent this command.
        /// </param>
        public void Execute(string[] args, IClient client)
        {
            string name = args[0];

            try
            {
                if (model.Join(name, client))
                {
                    // get the game
                    ISearchGame game = model.GetGameByName(name);

                    // register a notifier to tell the client the game has started
                    game.TellMeWhenTheGameStarts += () => client.SendResponse(game.ToJSON());

                    // check if we can start the game
                    if (game.CanStart())
                    {
                        game.MakePlayersNotifyEachOtherAboutTheirMoves((move) =>
                        {
                            // create the notification message about the move
                            if (move.ToString().ToLower() == "exit")
                            {
                                //Exit message.
                                return("exit");
                            }
                            JObject playObj = new JObject
                            {
                                ["Name"]      = game.Name,
                                ["Direction"] = move.ToString()
                            };
                            return(playObj.ToString());
                        });
                        try
                        {
                            game.Start();
                        }
                        catch
                        {
                            client.SendResponse("Failed To join. Something is wrong with the game setting.");
                        }
                    }
                }
                else
                {
                    client.SendResponse(@"Falied to join. You already a part of an existing game.
                        \r\n Please close that game and try again.");
                }
            }
            catch //(System.NullReferenceException)
            {
                client.SendResponse("Falied to join. No such game.");
            }
        }