Exemplo n.º 1
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.º 2
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);
            }
        }