// Adds questions to the game state and updates the DB private void PrepGameForStart(M_GameState curGameState) { QuestionDB_Connector dbConnector = new QuestionDB_Connector(); IList <M_QuestionCard> list = null; try { list = dbConnector.PullInQuestions(); } catch (Exception e) { throw new Exception("Couldn't get the questions from the database.", e); } if (list == null) { throw new Exception("The question list is null."); } else { curGameState.GameQuestions = list; curGameState.GenerateNextQuestion(); UpdateGameState(curGameState); } }
//Client[FP] requests continue public void RequestContinueToNextQuestion(string gameKey) { if (gameController.IsRoundOver(gameKey)) { if (gameController.IsGameOver(gameKey)) { Clients.Group(gameKey).DisplayGameStats(gameController.GetGameStats(gameKey)); Groups.Remove(Context.ConnectionId, gameKey); } else { gameController.BeginNewRound(gameKey); M_GameState curGameState = gameController.GetGame(gameKey); Clients.Group(gameKey).DisplayQuestion(curGameState.FocusedPlayerId, curGameState.GetFocusedQuestion()); } } else { gameController.BeginNewQuestion(gameKey); M_GameState curGameState = gameController.GetGame(gameKey); Clients.Group(gameKey).DisplayQuestion(curGameState.FocusedPlayerId, curGameState.GetFocusedQuestion()); } }
// Updates the given game state on the DB private void UpdateGameState(M_GameState curGameState) { var collection = gameDbConnector.GetCollection(); var filter = Builders <M_GameState> .Filter.Eq("_id", curGameState.GameId); collection.ReplaceOne(filter, curGameState); }
// Begin a new question with a new focused player public void BeginNewQuestion(string gameKey) { M_GameState curGameState = GetGame(gameKey); curGameState.ChooseNextFocusedPlayer(); curGameState.GenerateNextQuestion(); UpdateGameState(curGameState); }
// Begin a new round of questions public void BeginNewRound(string gameKey) { M_GameState curGameState = GetGame(gameKey); curGameState.GameRound++; curGameState.FocusedPlayerId = curGameState.GamePlayers.First().PlayerId; curGameState.GenerateNextQuestion(); UpdateGameState(curGameState); }
// Gets all players in the given game public IList <M_Player> GetPlayerList(string gameKey) { try { M_GameState curGameState = GetGame(gameKey); return(curGameState.GamePlayers); } catch (MongoConnectionException ex) { string msg = ex.Message; return(null); } }
// Adds a single player to the given game public void AddPlayerToGame(M_Player myPlayer, string gameKey) { try { M_GameState curGameState = GetGame(gameKey); curGameState.GamePlayers.Add(myPlayer); UpdateGameState(curGameState); } catch (Exception e) { throw new Exception("Couldn't add the player to the game."); } }
// Checks if the answer just given is the last answer of the hand public bool IsLastAnswer(string gameKey) { //Leave this call because it saves an extra search of the DB M_GameState curGameState = GetGame(gameKey); if (curGameState.GetAnswerStats().OtherPlayerAnswers.Count == curGameState.GamePlayers.Count - 1) { return(true); } else { return(false); } }
// Checks to see if round is over public bool IsRoundOver(string gameKey) { //Leave this call because it saves an extra search of the DB M_GameState curGameState = GetGame(gameKey); if (curGameState.FocusedPlayerId.Equals(curGameState.GamePlayers.Last().PlayerId)) { return(true); } else { return(false); } }
//Client requests game start public void RequestStartGame(M_Player myPlayer, string gameKey) { try { bool beginGame = gameController.PlayerIsReadyToStart(myPlayer, gameKey); if (beginGame) { M_GameState curGameState = gameController.GetGame(gameKey); Clients.Group(gameKey).DisplayQuestion(curGameState.FocusedPlayerId, curGameState.GetFocusedQuestion()); } } catch (Exception e) { Clients.Caller.DisplayError("Oops! Something went wrong :( Try that again!", e); } }
// Adds the calling player saying they're ready to begin public bool PlayerIsReadyToStart(M_Player myPlayer, string gameKey) { M_GameState curGameState = GetGame(gameKey); curGameState.StartRequests++; if (curGameState.StartRequests == curGameState.GamePlayers.Count) { PrepGameForStart(curGameState); return(true); } else { UpdateGameState(curGameState); return(false); } }
//Client requests new game public void RequestNewGame(M_Player myPlayer) { var gameState = new M_GameState(); try { gameState = gameController.CreateNewGameState(myPlayer); } catch (Exception e) { Clients.Caller.DisplayError("Oops! Something went wrong :( Try that again!", e); } string gameKey = gameState.GameId; Groups.Add(Context.ConnectionId, gameKey); UpdatePlayerListScreen(gameKey); }
// Creates a new game with a first player public M_GameState CreateNewGameState(M_Player myPlayer) { var collection = gameDbConnector.GetCollection(); M_GameState newGameState = new M_GameState(myPlayer); try { collection.InsertOne(newGameState); return(newGameState); } catch (MongoCommandException ex) { string msg = ex.Message; return(null); } }
// Adds the calling player's answer to the game stats public void AddAnswer(M_PlayerAnswer newAnswer, string gameKey) { M_GameState curGameState = GetGame(gameKey); M_AnswerStats curAnswerStats = curGameState.GetAnswerStats(); if (curAnswerStats.FocusedPlayerAnswer.PlayerAnswer == 0) { curAnswerStats.FocusedPlayerAnswer = newAnswer; } else { curAnswerStats.OtherPlayerAnswers.Add(newAnswer); } curGameState.UpdateAnswerStats(curAnswerStats); UpdateGameState(curGameState); }
// Returns the game stats public IList <M_AnswerStats> GetGameStats(string gameKey) { M_GameState curGameState = GetGame(gameKey); return(curGameState.GameAnswerStats); }