public async Task <IActionResult> RemoveChallenge(string challengeId) { string accessToken = await HttpContext.GetToken(); var session = await sessionService.GetSession(accessToken); if (session == null) { return(Unauthorized(new { message = "Session expired. Please login again." })); } try { var challenge = gameQueueService.GetSentChallenges(session.UserId).SingleOrDefault(q => q.Id == challengeId); var result = gameQueueService.RemoveQueue(challengeId); if (result && challenge != null) { var challenged = ClientHandler.FindByUserName(challenge.Player2); if (challenged != null) { await gameHubContext.Clients.Client(challenged.ConnectionId).ChallengeCanceled(challengeId); } } return(Ok(result)); } catch (Exception ex) { await log.ErrorAsync("Error in QueueGame()", ex); return(BadRequest(new { title = ex.GetType().ToString(), details = ex.StackTrace, message = ex.Message })); } }
public async Task <IActionResult> ChallengeGame([FromBody] ChallengeGameRequest request) { string accessToken = await HttpContext.GetToken(); var session = await sessionService.GetSession(accessToken); if (session == null) { return(Unauthorized(new { message = "Session expired. Please login again." })); } try { if (request.BoardId == 0) { var b = await GetDefaultBoard(); request.BoardId = b.Id; } var lexicon = await lexiconService.GetDictionary(request.Language); if (lexicon == null) { throw new ApplicationException("Invalid Language"); } var board = await GetBoard(request.BoardId); if (board == null) { throw new ApplicationException("Invalid Board"); } var player2 = await userRepository.Get(request.Challenged); if (player2 == null) { throw new ApplicationException("Invalid Opposer"); } var challenge = await gameService.ChallengeGame(lexicon.Language, board.Id, session.UserId, request.Challenged); var challenged = ClientHandler.FindByUserName(request.Challenged); if (challenged != null) { await gameHubContext.Clients.Client(challenged.ConnectionId).NewChallenge(challenge); } return(Ok(challenge)); } catch (Exception ex) { await log.ErrorAsync("Error in QueueGame()", ex); return(BadRequest(new { title = ex.GetType().ToString(), details = ex.StackTrace, message = ex.Message })); } }
public async Task <IActionResult> AcceptChallenge([FromBody] ChallengeAcceptRequest request) { string accessToken = await HttpContext.GetToken(); var session = await sessionService.GetSession(accessToken); if (session == null) { return(Unauthorized(new { message = "Session expired. Please login again." })); } try { var queue = gameQueueService.GetQueue(request.ChallengeId); if (queue == null) { throw new ApplicationException("Invalid Challenge Id"); } var game = await gameService.AcceptChallenge(request.ChallengeId, request.Accept); var challenger = ClientHandler.FindByUserName(queue.Player1); if (challenger != null) { await gameHubContext.Clients.Client(challenger.ConnectionId).ChallengeAccepted(queue.Id, request.Accept, game?.Id); } if (game != null) { return(Ok(game)); } return(NoContent()); } catch (Exception ex) { await log.ErrorAsync("Error in QueueGame()", ex); return(BadRequest(new { title = ex.GetType().ToString(), details = ex.StackTrace, message = ex.Message })); } }