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); }
public async Task SaveMatch(PickDto pick, string username, int userPick, GameApi.Proto.Result result) { var dto = MatchDto.FromPickDto(pick); dto.PlayerLogged = false; dto.PlayerName = username; dto.PlayerMove.Text = ToText(userPick); dto.PlayerMove.Value = userPick; dto.Result.Value = (int)result; dto.Result.Winner = Enum.GetName(typeof(GameApi.Proto.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 client = new CosmosClient(_constr); var db = client.GetDatabase(DatabaseName); var cprops = new ContainerProperties() { Id = "results", PartitionKeyPath = "/playerName" }; var cResponse = await db.CreateContainerIfNotExistsAsync(cprops); 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 IHttpActionResult CreatePicks(PickDto newPick) { var player = _context.Players.Single( p => p.Id == newPick.PlayerId); var characters = _context.Characters.Where( c => newPick.CharacterIds.Contains(c.Id)).ToList(); foreach (var character in characters) { if (character.isAvailable == 0) { return(BadRequest("Character is not available.")); } character.isAvailable = 0; var pick = new Pick { Player = player, Character = character, DatePicked = DateTime.Now }; _context.Picks.Add(pick); } _context.SaveChanges(); return(Ok()); }
public static MatchDto FromPickDto(PickDto pick) { var dto = new MatchDto(); dto.Challenger.Name = pick.Player; dto.Challenger.Type = pick.PlayerType; dto.ChallengerMove.Text = pick.Text; dto.ChallengerMove.Value = pick.Value; return(dto); }
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); }