Пример #1
0
        public async Task NewBotGame(GameModel gamesettings)
        {
            gamesettings = await _service.GetBotGameBoardAsync();

            await Clients.All.SendAsync(ClientEndpoints.NewBotGame, gamesettings);
        }
Пример #2
0
 public async Task SendMessage(GameModel gamesettings)
 {
     await Clients.All.SendAsync(ClientEndpoints.ReceiveMessage, gamesettings);
 }
Пример #3
0
        protected override async Task OnInitializedAsync()
        {
            _gamesettings = await GameModelService.GetHumanGameBoardAsync();

            _hubConnection = new HubConnectionBuilder()
                             .WithUrl(NavigationManager.ToAbsoluteUri("/gameHub"))
                             .Build();

            _hubConnection.On <GameModel>(ClientEndpoints.ReceiveMessage, (gamesettings) =>
            {
                _gamesettings = gamesettings;
                StateHasChanged();
            });

            _hubConnection.On <GameModel>(ClientEndpoints.EndTurn, async(gamesettings) =>
            {
                if (gamesettings.Botturn)
                {
                    await this.Write($"Turn {gamesettings.TurnCount} - Human Turn Over");
                }
                else
                {
                    await this.Write($"Turn {gamesettings.TurnCount} - Bot Turn Over.");
                }

                await this.Write(Newtonsoft.Json.JsonConvert.SerializeObject(gamesettings.Game, Newtonsoft.Json.Formatting.None));

                if (!gamesettings.Gameover)
                {
                    _gamesettings = gamesettings;
                    StateHasChanged();
                    if (_gamesettings.Botturn)
                    {
                        await DoBotTurn();
                    }
                }
            });

            _hubConnection.On <GameModel>(ClientEndpoints.NewHumanGame, (gamesettings) =>
            {
                _gamesettings = gamesettings;
                StateHasChanged();
            });

            _hubConnection.On <GameModel>(ClientEndpoints.NewBotGame, async(gamesettings) =>
            {
                this.messages.Clear();
                await this.Write($"Starting New Bot Game");
                _gamesettings = gamesettings;
                StateHasChanged();
            });

            _hubConnection.On <GameModel>(ClientEndpoints.GameOver, async(gamesettings) =>
            {
                await this.Write($"Turn {gamesettings.TurnCount} - Game Over.");
                await this.Write(Newtonsoft.Json.JsonConvert.SerializeObject(gamesettings.Game, Newtonsoft.Json.Formatting.None));
                _gamesettings = gamesettings;
                StateHasChanged();
            });


            await _hubConnection.StartAsync();
        }