Esempio n. 1
0
        protected override async Task OnInitializedAsync()
        {
            string          userName = HttpContextAccessor.HttpContext.User.Identity.Name;
            RoomResponseDTO roomResponse;

            if (RoomId == null)
            {
                roomResponse = await RoomService.CreateAndJoinPartyRoomAsync(userName);

                if (roomResponse.IsNew)
                {
                    await GameService.AddRoomToGame(roomResponse.Room.Id);
                }
                else
                {
                    if (roomResponse.Room.RoomType != RoomType.Party.ToString())
                    {
                        if (roomResponse.Room.RoomType == RoomType.Practice.ToString())
                        {
                            NavigationManager.NavigateTo("/practice");
                        }
                        else
                        {
                            NavigationManager.NavigateTo("/room");
                        }
                    }
                }
            }
            else
            {
                var urlDecoded = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(RoomId));
                var decrypted  = StringCipher.Decrypt(urlDecoded, "gsdoifjdsoi");
                int.TryParse(decrypted, out var roomId);
                roomResponse = await RoomService.JoinPartyRoomAsync(userName, roomId);

                if (roomResponse.Room.RoomType != RoomType.Party.ToString())
                {
                    if (roomResponse.Room.RoomType == RoomType.Practice.ToString())
                    {
                        NavigationManager.NavigateTo("/practice");
                    }
                    else
                    {
                        NavigationManager.NavigateTo("/public");
                    }
                }
            }
            ChangeDurationOfToast    = 5;
            ShowToastForStartingGame = true;
            Room = new RoomVM(roomResponse.Room);
            ShowToastForOnePlayer = Room.RoomPlayers.Count() == 1 ? true : false;
            Username   = userName;
            RoomPlayer = Room.RoomPlayers.FirstOrDefault(rp => rp.UserName == userName);
            if (RoomPlayer.IsRoomAdmin)
            {
                var encrypted  = StringCipher.Encrypt(Room.Id + "", "gsdoifjdsoi");
                var urlEncoded = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(encrypted));
                JoinLink = NavigationManager.Uri + "/" + urlEncoded;
                JoinLink = HtmlEncoder.Default.Encode(JoinLink);
            }
            hubConnection = new HubConnectionBuilder()
                            .WithUrl(NavigationManager.ToAbsoluteUri("/whoIsFasterSignalRHub"), conf =>
            {
                conf.HttpMessageHandlerFactory = (x) => new HttpClientHandler
                {
                    ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator,
                };
            })
                            .Build();

            hubConnection.On <string>("ReceiveRoom", (roomObject) =>
            {
                RoomDTO room = JsonSerializer.Deserialize <RoomDTO>(roomObject);
                var player   = room.RoomPlayers.FirstOrDefault(rp => rp.UserName == Username);

                if (player != null)
                {
                    Room       = new RoomVM(room);
                    RoomPlayer = new RoomPlayerVM(player);
                    if (Room.HasFinished)
                    {
                        hubConnection.DisposeAsync();
                    }
                    if (CurrentTextIndex == 0)
                    {
                        CurrentTextIndex = RoomPlayer.CurrentTextIndex;
                    }
                    StateHasChanged();
                }
            });

            hubConnection.On <string>("LeaveRoom", (message) =>
            {
                NavigationManager.NavigateTo("/");
            });

            await hubConnection.StartAsync();

            await EventService.AddConnectionToSignalRGroup(hubConnection.ConnectionId, Room.Id.ToString());
        }
        protected override async Task OnInitializedAsync()
        {
            DisableStartButton       = false;
            ChangeDurationOfToast    = 3;
            ShowToastForStartingGame = true;
            string userName     = HttpContextAccessor.HttpContext.User.Identity.Name;
            var    roomResponse = await RoomService.CreateAndJoinPracticeRoomAsync(userName);

            if (roomResponse.IsNew)
            {
                await GameService.AddRoomToGame(roomResponse.Room.Id);
            }
            else
            {
                if (roomResponse.Room.RoomType != RoomType.Practice.ToString())
                {
                    if (roomResponse.Room.RoomType == RoomType.Party.ToString())
                    {
                        NavigationManager.NavigateTo("/party");
                    }
                    else
                    {
                        NavigationManager.NavigateTo("/room");
                    }
                }
            }
            Room = new RoomVM(roomResponse.Room);
            ShowToastForOnePlayer = Room.RoomPlayers.Count() == 1 ? true : false;
            Username      = userName;
            RoomPlayer    = Room.RoomPlayers.FirstOrDefault(rp => rp.UserName == userName);
            hubConnection = new HubConnectionBuilder()
                            .WithUrl(NavigationManager.ToAbsoluteUri("/whoIsFasterSignalRHub"), conf =>
            {
                conf.HttpMessageHandlerFactory = (x) => new HttpClientHandler
                {
                    ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator,
                };
            })
                            .Build();

            hubConnection.On <string>("ReceiveRoom", (roomObject) =>
            {
                RoomDTO room = JsonSerializer.Deserialize <RoomDTO>(roomObject);
                var player   = room.RoomPlayers.FirstOrDefault(rp => rp.UserName == Username);

                if (player != null)
                {
                    Room       = new RoomVM(room);
                    RoomPlayer = new RoomPlayerVM(player);
                    if (Room.HasFinished)
                    {
                        hubConnection.DisposeAsync();
                    }
                    if (CurrentTextIndex == 0)
                    {
                        CurrentTextIndex = RoomPlayer.CurrentTextIndex;
                    }
                    StateHasChanged();
                }
            });

            await hubConnection.StartAsync();

            await EventService.AddConnectionToSignalRGroup(hubConnection.ConnectionId, Room.Id.ToString());
        }