private void btnCreateRoom_Click(object sender, EventArgs e)
        {
            // showing the new room window
            CreateRoom   createRoom = new CreateRoom();
            DialogResult result     = createRoom.ShowDialog();

            // checking on the returned form value
            if (result == DialogResult.OK)
            {
                // creating a new room by sending a request to the server
                Player.msgType = "Create Room";
                Room room = new Room(Player);
                room.MsgType  = "Create Room";
                room.RoomId   = Guid.NewGuid().ToString("N");
                room.RoomName = createRoom.RoomName;
                string data = JsonConvert.SerializeObject(room);
                sendBuffer = Encoding.Default.GetBytes(data);
                Player.PlayerSocket.BeginSend(sendBuffer, 0, sendBuffer.Length, SocketFlags.None, SendData_callback, null);

                // recieving the updated rooms data from the server
                Player.PlayerSocket.BeginReceive(recBuffer, 0, recBuffer.Length, SocketFlags.None, RecieveData, null);
                this.Hide();
                GameFrm game = new GameFrm(Player, room, this);
                game.Show();
            }
        }
        private void RecieveData(IAsyncResult result)
        {
            int amountOfdata = Player.PlayerSocket.EndReceive(result);
            //byte[] temp = new byte[amountOfdata];
            //File.WriteAllBytes("fle.txt", recBuffer);
            //temp = ArrayCopy(recBuffer, temp);
            //Array.Copy(recBuffer, 0, temp, 0, temp.Length);
            string dataInString = Encoding.Default.GetString(recBuffer);

            //MessageBox.Show(dataInString);

            // checking what type of response has the server sent
            if (dataInString.Contains("Create Room"))
            {
                List <Room> rooms = JsonConvert.DeserializeObject <List <Room> >(dataInString);
                Rooms = rooms;
                lsRooms.Items.Clear();
                foreach (Room room in Rooms)
                {
                    lsRooms.Items.Add(room.RoomName + " " + room.RoomId);
                }
            }
            else if (dataInString.Contains("yes join"))
            {
                JoinMsg joinMsg = JsonConvert.DeserializeObject <JoinMsg>(dataInString);
                Room    room    = null;
                foreach (Room r in Rooms)
                {
                    if (r.RoomId == joinMsg.RoomId)
                    {
                        room = r;
                        break;
                    }
                }
                this.Hide();
                GameFrm gameFrm = new GameFrm(joinMsg.SenderPlayer, room, this);
                gameFrm.Show();
            }
            else
            {
                string recData = Encoding.Default.GetString(recBuffer);

                if (recData.Contains("room"))
                {
                    // deserialzing the data as a room list object
                    // recieving rooms
                    Rooms = JsonConvert.DeserializeObject <List <Room> >(recData);

                    foreach (Room room in Rooms)
                    {
                        lsRooms.Items.Add(room.RoomName + " " + room.RoomId);
                    }
                }
            }
            recBuffer = new byte[4048];
            Player.PlayerSocket.BeginReceive(recBuffer, 0, recBuffer.Length, SocketFlags.None, RecieveData, null);
        }
示例#3
0
        private void btnJoin_Click(object sender, EventArgs e)
        {
            // sending a special message to the server
            // to indicate that the player wants to join a room
            string joinData = JoinRoom();

            recBuffer = Encoding.Default.GetBytes(joinData);

            ///// =?>
            PlayerSocket.BeginSend(recBuffer, 0, recBuffer.Length, SocketFlags.None, SendJoinData, null);

            this.Hide();

            GameFrm game = new GameFrm(PlayerSocket, this);

            game.Show();
        }
示例#4
0
        private void btnCreateRoom_Click(object sender, EventArgs e)
        {
            // showing the new room window
            CreateRoom   createRoom = new CreateRoom();
            DialogResult result     = createRoom.ShowDialog();

            // checking on the returned form value
            if (result == DialogResult.OK)
            {
                // creating a new room by sending a request to the server
                string data = CreateRoom(createRoom.criteria + "," + createRoom.RoomName + "," + PlayerId);
                sendBuffer = Encoding.Default.GetBytes(data);
                PlayerSocket.BeginSend(sendBuffer, 0, sendBuffer.Length, SocketFlags.None, SendData_callback, null);

                // recieving the updated rooms data from the server
                PlayerSocket.BeginReceive(recBuffer, 0, recBuffer.Length, SocketFlags.None, RecieveData, null);
                this.Hide();
                GameFrm game = new GameFrm(PlayerSocket, this);
                game.Show();
            }
        }