示例#1
0
 /// <summary>
 /// Cancels a JoinGame request.
 /// </summary>
 public void CancelJoinRequest(UserID UserToken, out HttpStatusCode status)
 {
     if (!UserIDs.ContainsKey(UserToken.UserToken) || UserToken.UserToken != CurrentPendingGame.Player1Token)
     {
         status = Forbidden;
     }
     else
     {
         CurrentPendingGame.Player1Token = null;
         status = OK;
     }
 }
 /// <summary>
 /// Cancels a JoinGame request.
 /// </summary>
 public void CancelJoinRequest(UserID UserToken)
 {
     if (!UserIDs.ContainsKey(UserToken.UserToken) || UserToken.UserToken != CurrentPendingGame.Player1Token)
     {
         SetStatus(Forbidden);
     }
     else
     {
         CurrentPendingGame.Player1Token = null;
         SetStatus(OK);
     }
 }
示例#3
0
        /// <summary>
        /// Cancels a join request
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public void CancelJoinRequest(UserID user)
        {
            UserInfo info = GetUserInfo(user.UserToken);

            if (info.Nickname == null)
            {
                SetStatus(Forbidden);
                return;
            }

            bool valid = false;

            if (info.GameID == null)
            {
                SetStatus(Forbidden);
                return;
            }

            lock (sync)
            {
                if (ContainsGame(info.GameID))
                {
                    GameStatusThing tmp = GetGameStatus(info.GameID);
                    if (tmp.GameState == "pending")
                    {
                        valid = tmp.Player1Hidden.UserToken == user.UserToken;
                        if (valid)
                        {
                            DeleteGame(info.GameID);
                            SetUserGameID(user.UserToken, null);
                        }
                    }
                }
            }

            if (valid)
            {
                SetStatus(OK);
            }
            else
            {
                SetStatus(Forbidden);
            }
        }
示例#4
0
        public void Cancel(UserID UserToken)
        {
            if (UserToken.UserToken == null)
            {
                SetStatus(Forbidden);
                return;
            }
            using (SqlConnection conn = new SqlConnection(BoggleDB))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    //Checking if UserToken exists in Users table.
                    using (SqlCommand command = new SqlCommand("select UserID from Users where UserID = @UserID", conn, trans))
                    {
                        command.Parameters.AddWithValue("@UserID", UserToken.UserToken);
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            if (!reader.HasRows)
                            {
                                SetStatus(Forbidden);
                                trans.Commit();
                                return;
                            }
                        }
                    }

                    //Checking if the UserToken is already in the pending game.
                    using (SqlCommand command = new SqlCommand("delete from Games where Player1 = @UserID and Player2 is null", conn, trans))
                    {
                        command.Parameters.AddWithValue("@UserID", UserToken.UserToken);
                        if (command.ExecuteNonQuery() == 0)
                        {
                            SetStatus(Forbidden);
                        }
                        else
                        {
                            SetStatus(OK);
                        }
                        trans.Commit();
                    }
                }
            }
        }
        /// <summary>
        /// Given the proper parameters, it runs the correct method in our BoggleServer and sends the response message.
        /// This is essentially covering the functionality of IBoggleService from before.
        /// </summary>
        /// <param name="Type"></param>
        /// <param name="GameID"></param>
        /// <param name="IsBrief"></param>
        /// <param name="content"></param>
        private void ParseMessage(string Type, string Url, string GameID, string IsBrief, dynamic content)
        {
            HttpStatusCode status;

            // Each method we call should return the object we to JSON encoded

            if (Type == "POST")
            {
                // CreateUser
                if (Url == "users")
                {
                    UserID ReturnID = server.CreateUser(content, out status);
                    CompileMessage(status, ReturnID);
                }
                // JoinGame
                else if (Url == "games")
                {
                    GameIDReturn IDReturn = server.JoinGame(content, out status);
                    CompileMessage(status, IDReturn);
                }
            }
            else if (Type == "PUT")
            {
                // CancelJoinRequest
                if (Url == "games" && GameID == string.Empty)
                {
                    server.CancelJoinRequest(content, out status);
                    CompileMessage(status, null);
                }
                // PlayWord
                else
                {
                    ScoreReturn Score = server.PlayWord(content, GameID, out status);
                    CompileMessage(status, Score);
                }
            }
            // GetGameStatus
            else
            {
                Game CurrentGame = new Game();
                CurrentGame = server.GetGameStatus(GameID, IsBrief, out status);
                CompileMessage(status, CurrentGame);
            }
        }
示例#6
0
        /// <summary>
        /// Creates a new user
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public UserID CreateUser(UserInfo user)
        {
            if (user.Nickname == null || user.Nickname.Trim().Length == 0 || user.Nickname.ToCharArray()[0] == '#')
            {
                SetStatus(Forbidden);
                return(null);
            }

            using (SqlConnection conn = new SqlConnection(BoggleDB))
            {
                // Connections must be opened
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    using (SqlCommand command = new SqlCommand("INSERT INTO Users (UserID, Nickname) VALUES(@UserID, @Nickname)", conn, trans))
                    {
                        string userID = Guid.NewGuid().ToString();
                        UserID id     = new UserID();
                        id.UserToken = userID;

                        // This is where the placeholders are replaced.
                        command.Parameters.AddWithValue("@UserID", userID);
                        command.Parameters.AddWithValue("@Nickname", user.Nickname.Trim());

                        // This executes the command within the transaction over the connection.  The number of rows
                        // that were modified is returned.  Perhaps I should check and make sure that 1 is returned
                        // as expected.
                        int tmp = command.ExecuteNonQuery();
                        SetStatus(Created);

                        // Immediately before each return that appears within the scope of a transaction, it is
                        // important to commit the transaction.  Otherwise, the transaction will be aborted and
                        // rolled back as soon as control leaves the scope of the transaction.
                        trans.Commit();
                        return(id);
                    }
                }
            }
        }
示例#7
0
        public UserID Register(RegisterRequest Nickname)
        {
            if (Nickname.Nickname == null || Nickname.Nickname.Trim().Length == 0 || Nickname.Nickname.Trim().Length > 50)
            {
                SetStatus(Forbidden);
                return(null);
            }

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

                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    using (SqlCommand command =
                               new SqlCommand("insert into Users (UserID, Nickname) values(@UserID, @Nickname)",
                                              conn,
                                              trans))
                    {
                        string userID = Guid.NewGuid().ToString();

                        command.Parameters.AddWithValue("@UserID", userID);
                        command.Parameters.AddWithValue("@Nickname", Nickname.Nickname.Trim());

                        command.ExecuteNonQuery();
                        SetStatus(Created);

                        trans.Commit();
                        UserID UserID = new UserID();
                        UserID.UserToken = userID;
                        SetStatus(Created);
                        return(UserID);
                    }
                }
            }
        }