Exemple #1
0
        private void listenThread()
        {
            while (true)
            {
                Socket socket = tcpModel.listen();
                Player player = new Player(socket);

                players.Add(player);
                rtxtLog.Text += $"{player.getName()} join game.\n";
                player.sendData("user " + player.getID().ToString());

                Thread thread = new Thread(reciveDataThread);
                thread.Start(player);
            }
        }
Exemple #2
0
        private void reciveDataThread(object obj)
        {
            Player player = obj as Player;
            string name   = player.getName();

            try
            {
                while (true)
                {
                    string data = player.receiveData();
                    rtxtLog.Text += $"From {name}: {data}\n";
                    rtxtLog.Text += "\n";

                    string[] fixData = data.Split(' ');
                    switch (fixData[0])
                    {
                    case "create":    //yêu cầu tạo phòng
                        Room newRoom = new Room(int.Parse(fixData[2]), rooms.Count);
                        newRoom.addPlayer(player);
                        rooms.Add(newRoom);
                        break;

                    case "join":    // yêu cầu chơi phòng ngẫu nhiên
                        join(player);
                        break;

                    case "winner":    //client thắng gửi, kết thúc ván
                        rooms[player.getIDRoom()].endGame(data, player);
                        break;

                    case "pop":
                        rooms[player.getIDRoom()].mergeCard(data);
                        rooms[player.getIDRoom()].sendCardToPlayer(data, player);
                        break;

                    case "miss":    //client bỏ lượt
                        rooms[player.getIDRoom()].sendCardToPlayer(data, player);
                        break;

                    case "close":
                        rooms[player.getIDRoom()].deletePlayer(player);
                        player.closeConnection();
                        break;

                    case "lose":
                        rooms[player.getIDRoom()].mergeCard(data);
                        break;

                    case "chat":
                        data = data.Substring(5);
                        rooms[player.getIDRoom()].chat(data, player);
                        break;

                    case "quit":
                        rooms[player.getIDRoom()].deletePlayer(player);
                        break;

                    case "startplay":
                        rooms[player.getIDRoom()].startGame();
                        break;

                    default:
                        break;
                    }
                }
            }
            catch (Exception)
            {
                player.closeConnection();
                rtxtLog.Text += $"Close connection from {name}.\n";
            }
        }