Exemplo n.º 1
0
 public Room1(int iD, PlayerServer1 player1, PlayerServer1 player2, string mapPlayerRoom)
 {
     this.iD            = iD;
     this.player1       = player1;
     this.player2       = player2;
     this.mapPlayerRoom = mapPlayerRoom;
 }
Exemplo n.º 2
0
        private void createEmptyRoom()
        {
            PlayerServer1 player1         = new PlayerServer1(0, 0, null);
            PlayerServer1 player2         = new PlayerServer1(0, 0, null);
            Room1         room            = new Room1(roomID, player1, player2, "4x4");
            string        tinnhanTaoPhong = "7x" + roomID;

            foreach (Socket socket in roomSocketList)
            {
                socket.Send(Serialize(tinnhanTaoPhong));
            }
            roomList.Add(room);
            roomID++;

            //SyncServer2("5x7x");
        }
Exemplo n.º 3
0
        private void receiveAndAddPlayer(String data, Socket socket)
        {
            int roomID   = int.Parse(data.Substring(0, 1));
            int playerID = int.Parse(data.Substring(1));
            //MessageBox.Show(socket + ": Player Socket");
            PlayerServer1 player = new PlayerServer1(playerID, 0, socket);

            if (roomList[roomID].player1.socket == null)
            {
                roomList[roomID].player1 = player;
            }
            else
            {
                roomList[roomID].player2 = player;
            }

            //MessageBox.Show(roomList[roomID].player1.socket + "Socket Player 1 Receive");
            //MessageBox.Show(roomList[roomID].player2.socket + "Socket Player 2 Receive");

            //SyncServer2("5x2x" + roomID + playerID);
        }
Exemplo n.º 4
0
        /// <summary>
        /// nhận tin nhắn
        /// </summary>
        void Receive(object obj)
        {
            Socket socket = obj as Socket;

            try
            {
                while (true)
                {
                    byte[] dataRcv = new byte[1024 * 5000];
                    socket.Receive(dataRcv);

                    string message = (string)Deserialize(dataRcv);

                    string code = message.Substring(0, 2);
                    string data = message.Substring(2);

                    switch (code)
                    {
                    case "0x":     //New Game
                        newGamePlayer(data);
                        break;

                    case "1x":     //Playing Game
                        playGame(data);
                        break;

                    case "2x":     //Nhận ID người chơi và thêm người chơi vào đúng phòng
                        receiveAndAddPlayer(data, socket);
                        break;

                    case "7x":     //Tạo 1 phòng trống
                        createEmptyRoom();
                        break;

                    case "8x":     //Lấy thông tin người chơi khi vào phòng
                        getInfoPlayer(data);
                        break;

                    case "9x":     //Có thêm thành viên mới
                        roomSocketList.Add(socket);
                        break;

                    case "10":     //Tin nhắn gửi trả lại thông tin từ Server2
                        int player1ID        = int.Parse(data.Substring(0, 2));
                        int player1CountTime = int.Parse(data.Substring(2, 1));
                        int player2ID        = int.Parse(data.Substring(3, 2));
                        int player2CountTime = int.Parse(data.Substring(5));

                        PlayerServer1 player1 = new PlayerServer1(player1ID, player1CountTime, null);
                        PlayerServer1 player2 = new PlayerServer1(player2ID, player2CountTime, null);

                        roomList.Add(new Room1(this.roomID, player1, player2, "4x4"));

                        this.roomID++;
                        break;

                    case "12":
                        int roomID   = int.Parse(data.Substring(0, 1));
                        int playerID = int.Parse(data.Substring(1));

                        PlayerServer1 player;

                        if (roomList[roomID].player1.iD == playerID)
                        {
                            int iD        = roomList[roomID].player1.iD;
                            int countTime = roomList[roomID].player1.countTime;

                            player = new PlayerServer1(iD, countTime, socket);

                            roomList[roomID].player1 = player;

                            roomList[roomID].player1.socket.Send(Serialize("5x"));

                            //SyncServer2("5x2x" + roomID + playerID);
                        }
                        else
                        {
                            int iD        = roomList[roomID].player2.iD;
                            int countTime = roomList[roomID].player2.countTime;

                            player = new PlayerServer1(iD, countTime, socket);

                            roomList[roomID].player2 = player;

                            roomList[roomID].player2.socket.Send(Serialize("5x"));

                            //SyncServer2("5x2x" + roomID + playerID);
                        }
                        break;
                    }

                    lsvMessage.Items.Add(new ListViewItem()
                    {
                        Text = "Connection accepted from " + socket.RemoteEndPoint
                    });
                    AddMessage(message);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error in Receive Server" + ex.ToString());
                //Báo cho server 2 biết là có thằng out
                for (int i = 0; i < roomList.Count; i++)
                {
                    if (roomList[i].player1.socket == socket)
                    {
                        string tinnhanSync = "6x" + i + socket;
                        //SyncServer2(tinnhanSync);
                        AddMessage(tinnhanSync);

                        roomList[i].player1.iD        = 0;
                        roomList[i].player1.countTime = 0;
                        roomList[i].player1.socket    = null;
                    }

                    else if (roomList[i].player2.socket == socket)
                    {
                        string tinnhanSync = "6x" + i + socket;
                        //SyncServer2(tinnhanSync);
                        AddMessage(tinnhanSync);

                        roomList[i].player2.iD        = 0;
                        roomList[i].player2.countTime = 0;
                        roomList[i].player2.socket    = null;
                    }
                }
            }
        }