public async Task <JsonResult> Get() { JsonResult response; try { GameStatusResponse gameStatus = await gameApiService.GetCurrentStatus(); gameStatus.RequestId = requestContext.RequestId; response = new JsonResult(gameStatus) { StatusCode = 200 }; } catch (Exception) { logger.LogError($"Error Occurred while fetching current gamestatus"); response = new JsonResult(new GameStatusResponse { RequestId = requestContext.RequestId, Err = new Error { Message = "Internal Server Error", Description = "Server Failed to fetch Current Gamestatus" } }) { StatusCode = 500 }; } return(response); }
public GameStatusResponse GetGameStatus(string gameId) { var gameStatus = GameKeeper.GetGameState(gameId); var response = new GameStatusResponse(gameStatus); return(response); }
/// <summary> /// Get the current status of the game / web application. Used when a user reconnects /// back to the web application in order to the front end to be updated with the current /// game / application state so the front end can redirect the user accordingly. /// </summary> /// <param name="player">The player making the request.</param> /// <returns> /// A GameStatusResponse object which outlines the GameState, /// if the player has votes to complete, if the player has any new notifications /// and the most recent player record. NULL if an error occurred. /// </returns> public Response <GameStatusResponse> GetGameStatus(Player player) { StoredProcedure = "usp_GetGameStatus"; Response <GameStatusResponse> response = null; try { //Create the connection and command for the stored procedure using (Connection = new SqlConnection(ConnectionString)) { using (Command = new SqlCommand(StoredProcedure, Connection)) { //Add the procedure input and output params AddParam("playerID", player.PlayerID); AddOutput("hasVotesToComplete", SqlDbType.Bit); AddOutput("hasNotifications", SqlDbType.Bit); AddDefaultParams(); //Perform the procedure and get the result RunReader(); Player updatedPlayer = null; while (Reader.Read()) { updatedPlayer = new ModelFactory(Reader).PlayerFactory(true); if (player == null) { return(new Response <GameStatusResponse>("An error occurred while trying to build the Player model.", ErrorCodes.BUILD_MODEL_ERROR)); } } Reader.Close(); //Format the results into a response object ReadDefaultParams(); response = new Response <GameStatusResponse>(null, ErrorMSG, Result); //If the result is not an error build the GameStatus object and assign it to the response if (!IsError) { bool hasVotesToComplete = Convert.ToBoolean(Command.Parameters["@hasVotesToComplete"].Value); bool hasNotifications = Convert.ToBoolean(Command.Parameters["@hasNotifications"].Value); GameStatusResponse gsr = new GameStatusResponse(hasVotesToComplete, hasNotifications, updatedPlayer); response.Data = gsr; } return(response); } } } //A database exception was thrown, return an error response catch { return(Response <GameStatusResponse> .DatabaseErrorResponse()); } }
public void GameStatusResponse_Correct_ObjectCreated() { // Arrange var gameStatusResponse = new GameStatusResponse { Status = this.gameStatus, Error = this.errorOk, IsSuccessful = this.isSuccessful }; // Act // Assert Assert.Equal(this.gameStatus, gameStatusResponse.Status); Assert.Equal(this.errorOk, gameStatusResponse.Error); Assert.Equal(this.isSuccessful, gameStatusResponse.IsSuccessful); }
public GameStatusResponse GetStatusResponse(string gameId, string playerId) { var status = GetStatus(gameId); if (status == null) { return(null); } var gameStatus = GameStatusResponse.FromStatus(status); gameStatus.IsCurrentChoice = status.ChoosingPlayerId == playerId; gameStatus.RemainingQuestions = RemainingQuestions(gameId); // Return previous choice if (!gameStatus.IsCurrentChoice) { gameStatus.ChoiceA = _playData.GetPreviousChoiceA(); gameStatus.ChoiceB = _playData.GetPreviousChoiceB(); } else if (gameStatus.ChoiceA == null || gameStatus.ChoiceB == null) { // TODO: Something with EF not populating the object, can look at it later var choiceA = _gameContext.Answers.Find(status.ChoiceAId); var choiceB = _gameContext.Answers.Find(status.ChoiceBId); gameStatus.ChoiceA = AnswerResponse.FromAnswer(choiceA); gameStatus.ChoiceB = AnswerResponse.FromAnswer(choiceB); } if (gameStatus.ChoosingPlayer == null) { var player = _gameContext.Players.Find(status.ChoosingPlayerId); if (player != null) { gameStatus.ChoosingPlayer = PlayerResponse.FromPlayer(player); gameStatus.ChoosingPlayer.IsChoosing = true; } } return(gameStatus); }
public virtual async Task <GameStatusResponse> GetCurrentStatus() { try { GameStatusResponse response = new GameStatusResponse() { RequestId = _requestContext.RequestId }; var currentPhase = await _context.Phases.Include(p => p.Round).OrderByDescending(p => p.TimeStamp).FirstOrDefaultAsync(); if (currentPhase == null) { response.Err = new Error { Message = "Try after some time", Description = "Game is not running" }; } else { var roundConfig = await _context.RoundConfigs.Where(rc => rc.Id == currentPhase.Round.RoundNumber).SingleOrDefaultAsync(); var participants = await _context.Participants.Include(p => p.Team).Where(p => p.RoundId.CompareTo(currentPhase.RoundId) == 0).ToDictionaryAsync(p => p.TeamId); var scores = await(from t in (from s in _context.Scores.Where(s => s.GameId.CompareTo(currentPhase.GameId) == 0) group s by s.TeamId into scoreGroup select new { TeamId = scoreGroup.Key, TotalScore = scoreGroup.Sum(sg => sg.PointsScored) }) join c in (from k in _context.Scores.Where(s => s.GameId.CompareTo(currentPhase.GameId) == 0) group k by new { k.TeamId, k.RoundId } into scoreGroup select new { TeamId = scoreGroup.Key.TeamId, scoreGroup.Key.RoundId, CurrentScore = scoreGroup.Sum(sg => sg.PointsScored) }) on t.TeamId equals c.TeamId select new { TeamId = t.TeamId, RoundId = c.RoundId, CurrentScore = c.CurrentScore, TotalScore = t.TotalScore }).Where(q => q.RoundId.CompareTo(currentPhase.RoundId) == 0).ToDictionaryAsync(sg => sg.TeamId); var participantsList = new List <TeamData>(); foreach (var key in participants.Keys) { bool inScore = scores.ContainsKey(key); participantsList.Add(new TeamData { TeamId = key, CurrentScore = inScore ? scores[key].CurrentScore : 0, TotalScore = inScore ? scores[key].TotalScore : 0, IsAlive = participants[key].IsAlive, IsRobot = participants[key].Team.IsRobot }); } response.Data = new GameStatusResponseData { RoundId = currentPhase.Round.RoundId, GameId = currentPhase.Round.GameId, RoundNumber = currentPhase.Round.RoundNumber, SecretLength = roundConfig?.SecretLength, Participants = participantsList, Status = currentPhase.PhaseType.ToString() }; } return(response); } catch (Exception ex) { _logger.LogError($"Fetching current gamestatus failed, {ex}"); throw new ServerSideException("Failed in GetCurrentStatus()", ex); } }