public Response TryANewGuess(string gameKey, string guess) { if (string.IsNullOrEmpty(gameKey) || string.IsNullOrEmpty(guess)) { return(new Response("Please provide a valid game key and a colors guess.")); } guess = guess.ToUpper(); GameSession session = GameSessionFactory.GetSession(gameKey); Player player = session.GetPlayer(gameKey); Response response; try { Validate(session, player, guess); } catch (GameException ex) { response = new Response("Unable to evaluate your guess: " + ex.Message); return(response); } Player opponent = session.Players.FirstOrDefault(x => x.GameKey != player.GameKey); if (opponent != null) { //avoid a second asynchronous call if (player.TotalGuesses > opponent.TotalGuesses) { return(new Response("Please wait until your opponent submit his guess.")); } } Result guessEvaluation = EvaluateGuess(session.GetColors(), guess); player.AddResult(guessEvaluation); player.TotalGuesses += 1; player.Solved = guessEvaluation.Exact == session.GetColors().Length; if (opponent != null) { if (player.TotalGuesses != opponent.TotalGuesses) { //wait until the opponent submit his guess WaitUntilBothPlayersHaveGuessed(session, player, opponent); } } if (player.Solved) { session.SetWinner(player); } response = new Response(_colors, player, session); return(response); }
public Response JoinAMultiplayerMatch(string hostPlayer, string userName) { if (string.IsNullOrEmpty(hostPlayer) || string.IsNullOrEmpty(userName)) { return(new Response("Please provide a valid host player and your player name.")); } Response response; try { var session = GameSessionFactory.GetSession(hostPlayer, userName); response = new Response(_colors, session.Players[1].GameKey, true); } catch (GameSessionException ex) { response = new Response(ex); } return(response); }
public Response StartNewGame(string userName, bool multiplayer) { if (string.IsNullOrEmpty(userName)) { return(new Response("Please provide a valid player name.")); } Response response; try { var session = GameSessionFactory.GetSession(_colors, userName, multiplayer); response = new Response(_colors, session.Players[0].GameKey, multiplayer); } catch (GameSessionException ex) { response = new Response(ex); } return(response); }