Esempio n. 1
0
        public async Task <IActionResult> JoinGroup(string roomId, JoinRoomForm joinRoomForm)
        {
            var user = await accountRepository.GetUser(joinRoomForm.UserId);

            if (user == null)
            {
                return(BadRequest(new { message = "Couldn't find the user in the database" }));
            }
            var roomExist = user.Rooms.Find(r => r.Id == roomId);

            if (roomExist != null)
            {
                return(BadRequest(new { message = "Cannot join the group twice" }));
            }

            var newJoinRoom = await userRepository.AddRoomToUser(joinRoomForm.UserId, roomId, joinRoomForm.RoomName);

            await roomRepository.AddUserToRoom(roomId, joinRoomForm.UserId, joinRoomForm.Username);

            var senderConnection = await userConnectedRepository.GetConnectedUserByUserId(joinRoomForm.UserId);

            await hubContext.Groups.AddToGroupAsync(senderConnection.ConnectionId, joinRoomForm.RoomName);

            await hubContext.Clients.Client(senderConnection.ConnectionId).SendAsync(KeyConstants.joinRoomSuccess, newJoinRoom);

            await hubContext.Clients.Group(joinRoomForm.RoomName).SendAsync(KeyConstants.newJoinRoom, senderConnection.ConnectionId, joinRoomForm.UserId);

            return(NoContent());
        }
Esempio n. 2
0
        public async Task JoinRoomAsync(string roomId, string roomName)
        {
            var url = ChatRootUrl + RoomChatRoute + roomId + "/join";

            using var client  = new HttpClient();
            using var request = new HttpRequestMessage(HttpMethod.Post, url);

            var content     = new JoinRoomForm(roomName, ConnectedUser.Id, ConnectedUser.Username);
            var jsonContent = JsonConvert.SerializeObject(content);

            using var stringContent = new StringContent(jsonContent, Encoding.UTF8, "application/json");

            request.Content    = stringContent;
            using var response = await client.SendAsync(request).ConfigureAwait(false);

            await UpdateUserInfoAsync();
        }
Esempio n. 3
0
        private void Form1_Load(object sender, EventArgs e)
        {
            // refresh();
            bool joinSuccessfully = false;

            do
            {
                JoinRoomForm joinRoomForm = new JoinRoomForm();
                if (joinRoomForm.ShowDialog() == DialogResult.OK)
                {
                    PORT = joinRoomForm.PORT;
                    try
                    {
                        client.Connect("127.0.0.1", PORT);
                        joinSuccessfully = true;
                    }
                    catch (Exception e1)
                    {
                        joinSuccessfully = false;
                        MessageBox.Show("Không thể tham gia bàn!\n + Bàn đang chơi\n + Bàn không tồn tại", "Thất bại", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            } while (!joinSuccessfully);

            Thread thread = new Thread(delegate()
            {
                // 1. connect

                stream = client.GetStream();

                Console.WriteLine("Đã kết nối");

                //GET DESK SIZE
                byte[] buffer                 = new byte[client.ReceiveBufferSize];
                int bytesRead                 = stream.Read(buffer, 0, client.ReceiveBufferSize);
                string receivedString         = Encoding.UTF8.GetString(buffer, 0, bytesRead);
                TicTacToeController.DESK_SIZE = Convert.ToInt32(receivedString);
                groupBox2.Invoke((MethodInvoker) delegate()
                {
                    labelDeskSize.Text = TicTacToeController.DESK_SIZE.ToString();
                    labelPort.Text     = PORT.ToString();
                });
                //NEW GAME
                flowLayoutPanel1.Invoke((MethodInvoker) delegate() { newDesk(); });
                while (true)
                {
                    buffer         = new byte[client.ReceiveBufferSize];
                    bytesRead      = stream.Read(buffer, 0, client.ReceiveBufferSize);
                    receivedString = Encoding.UTF8.GetString(buffer, 0, bytesRead);

                    if (receivedString.Contains("!hit"))
                    {
                        receivedString = receivedString.Replace("!hit", "");

                        Console.WriteLine(receivedString);

                        string[] rs      = receivedString.Split(' ');
                        int i            = Convert.ToInt32(rs[0]);
                        int j            = Convert.ToInt32(rs[1]);
                        int playerNumber = Convert.ToInt32(rs[2]);

                        TicTacToeController.hit(i, j, playerNumber);
                        isMoved = false;
                        txtStatus.Invoke((MethodInvoker) delegate()
                        {
                            txtStatus.Text = "ĐẾN LƯỢT BẠN";
                        });
                        if (aTimer.Enabled == false)
                        {
                            aTimer.Start();
                        }
                        labelTime.Invoke((MethodInvoker) delegate() { labelTime.Text = TIME.ToString(); });
                        flowLayoutPanel1.Invoke((MethodInvoker) delegate() { refresh(); });
                    }


                    else if (receivedString.Equals("!endgame"))
                    {
                        if (isMoved)
                        {
                            TicTacToeController.winnerNumber = 1;
                            txtStatus.Invoke((MethodInvoker) delegate() { txtStatus.Text = "HẾT GIỜ! BẠN ĐÃ THAWSNG!"; });
                        }
                        else
                        {
                            TicTacToeController.winnerNumber = 2;
                            txtStatus.Invoke((MethodInvoker) delegate() { txtStatus.Text = "HẾT GIỜ! BẠN ĐÃ THUA!"; });
                        }
                        this.Invoke((MethodInvoker) delegate() { this.Enabled = false; });

                        endGame = true;
                        this.Invoke((MethodInvoker) delegate() { this.Enabled = false; });
                        flowLayoutPanel1.Invoke((MethodInvoker) delegate() { refresh(); });
                        labelTime.Invoke((MethodInvoker) delegate() { labelTime.Text = TIME.ToString(); });
                    }
                    else if (receivedString.Equals("!newgame"))
                    {
                        newGame = true;
                        this.Invoke((MethodInvoker) delegate() { this.Enabled = false; });
                        endGame = true;
                        flowLayoutPanel1.Invoke((MethodInvoker) delegate() { refresh(); });
                        labelTime.Invoke((MethodInvoker) delegate() { labelTime.Text = TIME.ToString(); });
                    }

                    else if (receivedString.Equals("!2win"))
                    {
                        txtStatus.Invoke((MethodInvoker) delegate() { txtStatus.Text = "BẠN ĐÃ THUA!"; });
                        this.Invoke((MethodInvoker) delegate() { this.Enabled = false; });


                        endGame = true;
                        flowLayoutPanel1.Invoke((MethodInvoker) delegate() { refresh(); });
                        labelTime.Invoke((MethodInvoker) delegate() { labelTime.Text = TIME.ToString(); });
                    }
                }



                // 4. close
                stream.Close();
                client.Close();
                //Client nhan message tu server
                //size = sck.Receive(data);

                //}
                ///////////////////////////////////////
            });

            thread.Start();
        }