Ejemplo n.º 1
0
        /// <summary>
        /// Execute.
        /// </summary>
        /// <param name="args">The arguments.</param>
        /// <param name="model"></param>
        /// <param name="client">The client.</param>
        /// <returns></returns>
        public RecieveInfo Execute(string[] args, IServerModel model, TcpClient client = null)
        {
            this.client = client;
            NetworkStream stream = client.GetStream();
            StreamReader  reader = new StreamReader(stream);
            string        result = SendAndRecieve.RecieveInfo(reader);

            client.GetStream().Flush();

            // the first finished before the other joined
            if (result.Equals("aborted"))
            {
                return(new RecieveInfo("close", false, "close"));
            }
            // if the message is invalid
            else if (!result.Equals("invalid command"))
            {
                // save start and end point
                Maze maze = Maze.FromJSON(result);
                model.CurrentRow      = maze.InitialPos.Row;
                model.CurrentCol      = maze.InitialPos.Col;
                model.OtherCurrentRow = maze.InitialPos.Row;
                model.OtherCurrentCol = maze.InitialPos.Col;
                model.Maze            = maze;
                model.GameName        = maze.Name;

                model.EndRow = maze.GoalPos.Row;
                model.EndCol = maze.GoalPos.Col;

                Game.server = client;
                Game.StartGame(model);
            }
            return(new RecieveInfo(result, true, "start"));
        }
        public RecieveInfo Execute(string[] args, IServerModel model, TcpClient client = null)
        {
            NetworkStream stream = client.GetStream();
            StreamReader  reader = new StreamReader(stream);
            string        result = SendAndRecieve.RecieveInfo(reader);

            client.GetStream().Flush();
            client.GetStream().Close();
            client.Close();
            result = result.Substring(1, result.Length - 2);
            if (result != "")
            {
                List <string> temp = new List <string>();
                List <string> arr  = result.Split(',').ToList();
                foreach (string s in arr)
                {
                    temp.Add(s.Substring(1, s.Length - 2));
                }
                model.GameMultiPlayerList = temp;
            }
            else
            {
                model.GameMultiPlayerList = new List <string>();
            }

            return(new RecieveInfo(result, false, "list"));
        }
        /// <summary>
        /// Execute.
        /// </summary>
        /// <param name="args">The arguments.</param>
        /// <param name="model"></param>
        /// <param name="client">The client.</param>
        /// <returns></returns>
        public RecieveInfo Execute(string[] args, IServerModel model, TcpClient client = null)
        {
            NetworkStream stream = client.GetStream();
            StreamReader  reader = new StreamReader(stream);
            string        result = SendAndRecieve.RecieveInfo(reader);

            client.GetStream().Flush();

            if (!result.Equals("invalid command"))
            {
                // save start and end point
                Maze maze = Maze.FromJSON(result);
                model.CurrentRow      = maze.InitialPos.Row;
                model.CurrentCol      = maze.InitialPos.Col;
                model.OtherCurrentRow = maze.InitialPos.Row;
                model.OtherCurrentCol = maze.InitialPos.Col;
                model.Maze            = maze;
                model.GameName        = maze.Name;

                // update end point
                model.EndRow = maze.GoalPos.Row;
                model.EndCol = maze.GoalPos.Col;
            }
            Game.server = client;
            Game.StartGame(model);
            return(new RecieveInfo(result, true, "join"));
        }
        /// <summary>
        /// Execute.
        /// </summary>
        /// <param name="args">The arguments.</param>
        /// <param name="model"></param>
        /// <param name="client">The client.</param>
        /// <returns></returns>
        public RecieveInfo Execute(string[] args, IServerModel model, TcpClient client = null)
        {
            NetworkStream stream = client.GetStream();
            StreamReader  reader = new StreamReader(stream);
            string        result = SendAndRecieve.RecieveInfo(reader);

            client.GetStream().Flush();
            client.GetStream().Close();
            client.Close();

            return(new RecieveInfo(result, false, "solve"));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Starts the game.
        /// </summary>
        public static void StartGame(IServerModel model)
        {
            gameAlive = true;
            new Task(() =>
            {
                // wait for join
                NetworkStream stream = server.GetStream();
                StreamReader reader  = new StreamReader(stream);

                while (gameAlive)
                {
                    string result = SendAndRecieve.RecieveInfo(reader);

                    if (result != "")
                    {
                        JObject jsonResult = JObject.Parse(result);

                        // if it's the end of the game:
                        if ((string)jsonResult[""] == "game closed")
                        {
                            gameAlive = false;
                            model.GameOver();
                            model.InitializeGame();
                        }
                        else
                        {
                            // play one move
                            Play(jsonResult, model);
                        }
                    }
                    else
                    {
                        // model.GameOver();
                        gameAlive = false;
                    }
                }
            }).Start();
        }