public MainMenuPageViewModel(GameModel gameModel, Func <GameModel, GamePage> createGamePage, Func <GameModel, WaitGamePage> createWaitGamePage, IClient client) { this.gameModel = gameModel; this.createGamePage = createGamePage; this.client = client; PageEnabled = true; CreateRoomWithSinglePlayer = new Command(async _ => { PageEnabled = false; var createRoomRequest = new CreateRoomRequestDto { PlayerName = gameModel.PlayerName, RoomType = RoomTypeDto.SinglePlayer }; var room = await client.CreateRoomAsync(createRoomRequest, gameModel.PlayerId).ConfigureAwait(true); gameModel.RoomId = room.RoomId; await Application.Current.MainPage.Navigation.PushModalAsync(createWaitGamePage(gameModel)).ConfigureAwait(true); }); CreateRoomWithTwoPlayers = new Command(async _ => { PageEnabled = false; var createRoomRequest = new CreateRoomRequestDto { PlayerName = gameModel.PlayerName, RoomType = RoomTypeDto.TwoPlayers }; var room = await client.CreateRoomAsync(createRoomRequest, gameModel.PlayerId).ConfigureAwait(true); gameModel.RoomId = room.RoomId; await Application.Current.MainPage.Navigation.PushModalAsync(createWaitGamePage(gameModel)).ConfigureAwait(true); }); RestartGame = new Command(_ => { var application = (App)Application.Current; application.BeginGame(); }); RefreshRooms = new Command(async _ => await RefreshRoomsAsync().ConfigureAwait(true)); RefreshRooms.Execute(null); }
public async Task <CreateRoomResponseDto> CreateRoomAsync(CreateRoomRequestDto parameters, Guid playerId) { var content = new StringContent(JsonConvert.SerializeObject(parameters), Encoding.UTF8, "application/json"); var response = await httpClient.PostAsync($"/v2/rooms?playerId={playerId}", content).ConfigureAwait(false); var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false); logger.Info($"{nameof(CreateRoomAsync)}:Response:{json}"); return(JsonConvert.DeserializeObject <CreateRoomResponseDto>(json)); }
private async Task <RoomDto> CreateRoomAsync() { var createRoomRequest = new CreateRoomRequestDto { PlayerName = playerName }; var createdRoom = await client.CreateRoomAsync(createRoomRequest, playerId).ConfigureAwait(false); logger.Info($"Создана комната {createdRoom.RoomId}, ожидаем соперника"); while (true) { var room = await client.GetRoomAsync(createdRoom.RoomId, playerId).ConfigureAwait(false); if (room.Status == RoomStatusDto.Ready) { logger.Info($"В комнату {room.Id} подключился соперник {room.Players.First(x => x.Id != playerId).Name}"); return(room); } await Task.Delay(1000).ConfigureAwait(false); } }
public Task <CreateRoomResponseDto> CreateRoomAsync(CreateRoomRequestDto parameters, Guid playerId) => RetryAsync(x => client.CreateRoomAsync(x.parameters, x.playerId), (parameters, playerId));