public async Task ReportGame(double time, string matchId, string playerId) { var player = _connectionCache[Context.ConnectionId]; var matchOfPlayer = _matchCache.Matches.Single(m => m.MatchId == matchId); if (_timeReported.ContainsKey(matchOfPlayer.MatchId)) { var unfinishedMatchResult = _timeReported[matchOfPlayer.MatchId]; var finishedMatch = unfinishedMatchResult.Finish(player.PlayerId, time); var result = await _httpClient.PutAsJsonAsync($"/matches/{matchOfPlayer.MatchId}?api_key=secret", finishedMatch); var playerIds = new List <string> { playerId, unfinishedMatchResult.FirstPlayerId }; var connectionsFromPlayers = _connectionCache.Where(c => playerIds.Contains(c.Value.PlayerId)).Select(c => c.Key); if (result.IsSuccessStatusCode) { var matchResult = await result.Content.ReadFromJsonAsync <MatchDto>(); _timeReported.Remove(matchOfPlayer.MatchId); await Clients.Clients(connectionsFromPlayers).SendAsync("MatchFinished", matchResult); } else { await Clients.Caller.SendAsync("MatchReportFailed"); } } else { var unfinishedMatchResult = new UnfinishedMatchResult(matchOfPlayer, player.PlayerId, time); _timeReported.Add(matchOfPlayer.MatchId, unfinishedMatchResult); await Clients.Caller.SendAsync("PartialResultReported"); } }
public async Task <ActionResult> ReportMatches([FromBody] List <MatchDto> matchesFound) { foreach (var matchFound in matchesFound) { var playerIds = matchFound.Teams.SelectMany(t => t.Players).Select(p => p.PlayerId); var connectionIds = playerIds.SelectMany(id => _connectionCache.Where(p => p.Value.PlayerId == id).Select(r => r.Key)).ToList(); await _gameHub.Clients.Clients(connectionIds).SendAsync("MatchFound", matchFound); } _matchCache.Add(matchesFound); return(Ok()); }