public async Task <MatchDto> SaveMatchPick(string matchId, string username, int pick) { var dto = await GetMatch(matchId); if (dto.PlayerName == username) { dto.PlayerMove.Text = PickDto.ToText(pick); dto.PlayerMove.Value = pick; } else { dto.ChallengerMove.Text = PickDto.ToText(pick); dto.ChallengerMove.Value = pick; } _cacheService.UpdateMatch(dto); if (_constr == null) { return(dto); } var cResponse = await GetContainer(); var result = await cResponse.Container.UpsertItemAsync(dto); return(result.Resource); }
public async Task SaveMatch(PickDto pick, string username, int userPick, Result result) { var dto = MatchDto.FromPickDto(pick); dto.PlayerName = username; dto.PlayerMove.Text = PickDto.ToText(userPick); dto.PlayerMove.Value = userPick; dto.Result.Value = (int)result; dto.Result.Winner = Enum.GetName(typeof(Result), result); if (_constr == null) { _logger.LogInformation("+++ Cosmos constr is null. Doc that would be written is:"); _logger.LogInformation(JsonSerializer.Serialize(dto)); _logger.LogInformation("+++ Nothing was written on Cosmos"); return; } var cResponse = await GetContainer(); var response = await cResponse.Container.CreateItemAsync(dto); if (response.StatusCode != System.Net.HttpStatusCode.OK && response.StatusCode != System.Net.HttpStatusCode.Created) { _logger.LogInformation($"Cosmos save attempt resulted with StatusCode {response.StatusCode}."); } }
public override async Task <GameResponse> DoPlay(GameRequest request, ServerCallContext context) { _logger.LogInformation($"Player {request.Username} picked {PickDto.ToText(request.Pick)} against {request.Challenger}."); var challenger = _challengersService.SelectChallenger(request); var result = new GameResponse() { Challenger = request.Challenger, User = request.Username, UserPick = request.Pick }; var pick = await challenger.Pick(GetContext(context), request.TwitterLogged, request.Username); _logger.LogInformation($"Challenger {result.Challenger} picked {PickDto.ToText(pick.Value)} against {request.Username}."); result.ChallengerPick = pick.Value; result.IsValid = IsValid(pick); result.Result = result.IsValid ? _gameService.Check(result.UserPick, result.ChallengerPick) : Result.Player; _logger.LogInformation($"Result of User {request.Username} vs Challenger {result.Challenger}, winner: {result.Result}"); if (result.IsValid && request.TwitterLogged) { await _resultsDao.SaveMatch(pick, request.Username, request.Pick, result.Result); } if (_playFabService.HasCredentials) { var username = !request.TwitterLogged ? $"${request.Username}" : request.Username; await _playFabService.UpdateStats(username, result.Result == Result.Player); } return(result); }
private static bool IsValid(PickDto pick) { if (pick.Value > 4 || pick.Value < 0) { return(false); } if (pick.Text.ToLowerInvariant() != PickDto.ToText(pick.Value).ToLowerInvariant()) { return(false); } return(true); }