コード例 #1
0
        /// <summary>
        /// Moves the player.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="isSinglePlayer">if set to <c>true</c> [is single player].</param>
        public void MovePlayer(Key key, bool isSinglePlayer)
        {
            bool isFinish = MoveOneStep(key, isSinglePlayer);

            // goal position
            if (isFinish)
            {
                RecieveInfo recieveInfo = new RecieveInfo("", false, "win");
                reciveFromServer.Invoke(this, new Recive(recieveInfo.typeCommand, recieveInfo.result));
            }
        }
コード例 #2
0
        /// <summary>
        /// Connects the client.
        /// </summary>
        public void ConnectClient()
        {
            bool isConected = true;

            while (isConected)
            {
                // wait for a message
                while (message.CompareTo("") == 0)
                {
                }

                Task task = new Task(() =>
                {
                    connectionAlive  = true;
                    TcpClient client = new TcpClient();
                    IPEndPoint ep    = new IPEndPoint(IPAddress.Parse(ServerIP), ServerPort);

                    // comnect:
                    try
                    {
                        client.Connect(ep);
                    }
                    catch (SocketException e)
                    {
                        connection.Invoke(this, new Recive("connection", "can't find server"));
                        isConected = false;
                        return;
                    }

                    using (NetworkStream stream = client.GetStream())
                        using (StreamReader reader = new StreamReader(stream))
                            using (StreamWriter writer = new StreamWriter(stream))
                            {
                                while (connectionAlive)
                                {
                                    string[] arr      = message.Split(' ');
                                    string commandKey = arr[0];

                                    SendAndRecieve.Send(writer, message);

                                    message = "";

                                    // check whats next (close connection or continue)
                                    if (commands.ContainsKey(commandKey) || updateCommands.ContainsKey(commandKey))
                                    {
                                        System.EventHandler <Recive> temp          = (commands.ContainsKey(commandKey)) ? reciveFromServer : updateFromServer;
                                        Dictionary <string, ICommand> tempCommands = (commands.ContainsKey(commandKey)) ? commands : updateCommands;

                                        string[] arguments      = arr.Skip(1).ToArray();
                                        command                 = tempCommands[commandKey];
                                        RecieveInfo recieveInfo = command.Execute(arguments, this, client);
                                        connectionAlive         = recieveInfo.connectionAlive;

                                        Task viewTask = new Task(() =>
                                        {
                                            if (!recieveInfo.typeCommand.Equals("close"))
                                            {
                                                temp.Invoke(this, new Recive(recieveInfo.typeCommand, recieveInfo.result));
                                            }
                                        });
                                        viewTask.Start();
                                    }
                                    if (connectionAlive)
                                    {
                                        // NEED other input
                                        while (message.CompareTo("") == 0)
                                        {
                                        }
                                    }
                                }
                            }
                    client.Close();
                    message = "";
                });
                task.Start();
                task.Wait();
            }
        }