Exemplo n.º 1
0
        //Creates a new pending game with the user as the first player
        public string CreatePendingGame(JoinGameInfo user, SqlConnection conn, SqlTransaction trans)
        {
            using (SqlCommand command = new SqlCommand("insert into Games (Player1, TimeLimit, GameStatus) output inserted.GameID values (@Player1, @TimeLimit, @GameStatus)",
                                                       conn, trans))
            {
                command.Parameters.AddWithValue("@Player1", user.UserToken);
                command.Parameters.AddWithValue("@TimeLimit", user.TimeLimit);
                command.Parameters.AddWithValue("@GameStatus", "pending");

                string s = command.ExecuteScalar().ToString();
                return(s);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Determines how to recieve the incoming request and what type of object needs to be created.
        /// </summary>
        /// <param name="ss"></param>
        /// <param name="contentLength"></param>
        /// <param name="methodName"></param>
        private string GetObject(String s)
        {
            switch (methodName)
            {
            // Used for creating a user.
            case "CreateUser":
                UserInfo        user   = JsonConvert.DeserializeObject <UserInfo>(s);
                UserTokenObject token  = service.CreateUser(user);
                string          result =
                    JsonConvert.SerializeObject(token,
                                                new JsonSerializerSettings {
                    NullValueHandling = NullValueHandling.Ignore
                });
                return(result);

            // Used for joing a game.
            case "JoinGame":
                JoinGameInfo info = JsonConvert.DeserializeObject <JoinGameInfo>(s);
                GameiD       id   = service.JoinGame(info);
                string       gID  = JsonConvert.SerializeObject(id,
                                                                new JsonSerializerSettings {
                    NullValueHandling = NullValueHandling.Ignore
                });
                return(gID);

            // Used for cancelling a join request.
            case "CancelJoinRequest":
                Cancel cancel = JsonConvert.DeserializeObject <Cancel>(s);
                service.CancelJoinRequest(cancel);
                return("");

            // Used for playing a word
            case "PlayWord":
                WordCheck word       = JsonConvert.DeserializeObject <WordCheck>(s);
                WordScore score      = service.PlayWord(gameID, word);
                string    wordPlayed =
                    JsonConvert.SerializeObject(score,
                                                new JsonSerializerSettings {
                    NullValueHandling = NullValueHandling.Ignore
                });

                return(wordPlayed);

            default:
                return("");
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Join a game.
        ///
        /// (1) If UserToken is invalid, TimeLimit is less than 5, or greater than 120,
        ///     responds with status 403 (Forbidden).
        /// (2) Otherwise, if UserToken is already a player in the pending game, responds
        ///     with status 409 (Conflict).
        /// (3) Otherwise, if there is already one player in the pending game, adds UserToken
        ///     as the second player. The pending game becomes active. The active game's time limit
        ///     is the integer average of the time limits requested by the two players. Returns
        ///     the new active game's GameID (which should be the same as the old pending game's
        ///     GameID). Responds with status 201 (Created).
        /// (4) Otherwise, adds UserToken as the first player of the pending game, and the
        ///     TimeLimit as the pending game's requested time limit. Returns the pending game's
        ///     GameID. Responds with status 202 (Accepted).
        /// </summary>
        /// <param name="game">Contains the body of the request</param>
        /// <returns>The created game's ID. Null if invalid userToken or timelimit.</returns>
        public JoinGameReturnInfo JoinGame(JoinGameInfo game)
        {
            // Check valid time limit, and ensure that the usertoken is in the Users DB
            if (game.TimeLimit < 5 || game.TimeLimit > 120 || IsUserTokenInvalid(game.UserToken))
            {
                SetStatus(Forbidden);
                return(null);
            }

            // Else if already a part of a pending game, set status to conflict and
            // return null
            if (UserIsInPendingGame(game.UserToken))
            {
                SetStatus(Conflict);
                return(null);
            }

            // Else if there is pending game that this player is not a part of, adds UserToken
            // as the second player. The pending game becomes active. The active game's time limit
            // is the integer average of the time limits requested by the two players. Returns
            // the new active game's GameID (which should be the same as the old pending game's
            // GameID). Responds with status 201 (Created).
            int foundPendingGameID = FindPendingGame(game.UserToken);

            if (foundPendingGameID != -1)
            {
                int activeGameID = MakeAnActiveGame(game.UserToken, game.TimeLimit, foundPendingGameID);

                SetStatus(Created);
                JoinGameReturnInfo info = new JoinGameReturnInfo();
                info.GameID = activeGameID.ToString();
                return(info);
            }

            // Otherwise, add this player as the first player of the pending game, and set the
            // TimeLimit as the pending game's requested time limit. Set the status and return
            // the pending game's GameID.
            else
            {
                int pendingGameID = CreatePendingGame(game.UserToken, game.TimeLimit);
                SetStatus(Accepted);
                JoinGameReturnInfo info = new JoinGameReturnInfo();
                info.GameID = pendingGameID.ToString();
                return(info);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Join a game
        ///
        /// If UserToken is invalid, TimeLimit <
        /// 5, or TimeLimit > 120, responds with
        /// status 403 (Forbidden).
        ///
        /// Otherwise, if UserToken is already a
        /// player in the pending game, responds
        /// with status 409 (Conflict).
        ///
        /// Otherwise, if there is already one player
        /// in the pending game, adds UserToken as the
        /// second player. The pending game becomes active
        /// and a new pending game with no players is
        /// created. The active game's time limit is the
        /// integer average of the time limits requested by
        /// the two players. Returns the new active game's
        /// GameID (which should be the same as the old pending
        /// game's GameID). Responds with status 201 (Created).
        ///
        /// Otherwise, adds UserToken as the first player of the
        /// pending game, and the TimeLimit as the pending game's
        /// requested time limit. Returns the pending game's GameID.
        /// Responds with status 202 (Accepted).
        /// </summary>
        /// <param name="UserToken"></param>
        /// <param name="TimeLimit"></param>
        /// <returns></returns>
        public GameiD JoinGame(JoinGameInfo user)
        {
            try {
                GameiD id = new GameiD();
                if (user.UserToken == null)
                {
                    SetStatus(Forbidden);
                    return(null);
                }

                if (user.TimeLimit < 5 || user.TimeLimit > 120)
                {
                    SetStatus(Forbidden);
                    return(null);
                }

                using (SqlConnection conn = new SqlConnection(BoggleDB))
                {
                    conn.Open();

                    using (SqlTransaction trans = conn.BeginTransaction())
                    {
                        int time   = 0;
                        int gameID = 0;
                        //Check for a pending game
                        using (SqlCommand command = new SqlCommand("select GameID, Player1, TimeLimit from Games where Player2 is null",
                                                                   conn, trans))
                        {
                            //reads, checks for a pending game by seeing if there are rows returned.
                            //if there aren't any rows returned, that means there are no pending games
                            //so we create one with the player1 and timelimit added.
                            using (SqlDataReader reader = command.ExecuteReader())
                            {
                                //make sure a row was returned, if not means there is no
                                //pending game with a first player, create one.
                                if (!reader.HasRows)
                                {
                                    //close reader - error if we don't
                                    reader.Close();

                                    //create a new game with the player and get the auto generated
                                    //gameID back
                                    id.GameID = CreatePendingGame(user, conn, trans);

                                    //commit transaction and return id
                                    trans.Commit();
                                    SetStatus(Accepted);
                                    return(id);
                                }

                                //read the first null game and get the first players time request
                                //as well as that games ID
                                if (reader.Read())
                                {
                                    time   = reader.GetInt32(2);
                                    gameID = reader.GetInt32(0);
                                }

                                //check if player is already in the game...
                                if (reader.GetString(1).Equals(user.UserToken))
                                {
                                    SetStatus(Conflict);
                                    return(null);
                                }
                            }
                        }


                        //Since we have the first players time request and game id we update the database
                        //with the second player updated time, a new board and the current time.
                        using (SqlCommand command = new SqlCommand("update Games set Player2 = @Player2, TimeLimit = @TimeLimit, Board = @Board, StartTime = @StartTime, GameStatus = @GameStatus where GameID = @GameID",
                                                                   conn, trans))
                        {
                            command.Parameters.AddWithValue("@Player2", user.UserToken);
                            command.Parameters.AddWithValue("@TimeLimit", (time + user.TimeLimit) / 2);
                            command.Parameters.AddWithValue("@Board", new BoggleBoard().ToString());
                            command.Parameters.AddWithValue("@StartTime", DateTime.Now.TimeOfDay);
                            command.Parameters.AddWithValue("@GameID", gameID);
                            command.Parameters.AddWithValue("@GameStatus", "active");

                            //if there were no rows affected then I set status to badrequest
                            //so there is no continuation
                            if (command.ExecuteNonQuery() == 0)
                            {
                                SetStatus(BadRequest);
                                return(null);
                            }
                        }

                        //Otherwise sets status
                        id.GameID = gameID.ToString();
                        SetStatus(Created);
                        trans.Commit();
                        return(id);
                    }
                }
            }
            catch (Exception)
            {
                SetStatus(Forbidden);
                return(null);
            }
        }