Ejemplo n.º 1
0
        public static string RequestAchievements(string apiKey, string userID)
        {
            string response = ResponseTool.CheckAPIKey(apiKey, userAPIKey);

            if (response != null)
            {
                return(response);
            }

            using (var conn = DataManager.CreateConnectionObject())
            {
                conn.Open();
                using (var command = conn.CreateCommand())
                {
                    command.CommandText = string.Format("SELECT ID, AchievementID, PercentComplete FROM Achievements WHERE UserID = '{0}'", userID);
                    using (var reader = command.ExecuteReader())
                    {
                        var webResponse = new WebResponse(ResponseTypes.Succeeded);
                        webResponse.Achievements = new List <WebResponse_Achievement>();
                        while (reader.Read())
                        {
                            var a = new WebResponse_Achievement()
                            {
                                ID              = reader["AchievementID"].ToString(),
                                AchievementID   = reader["AchievementID"].ToString(),
                                PercentComplete = float.Parse(reader["PercentComplete"].ToString())
                            };
                            webResponse.Achievements.Add(a);
                        }

                        return(ResponseTool.GenerateXML(webResponse));
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public static string GetGameList(string apiKey, string clientID)
        {
            string response = ResponseTool.CheckAPIKey(apiKey, clientAPIKey);

            if (response != null)
            {
                return(response);
            }

            using (var conn = DataManager.CreateConnectionObject())
            {
                conn.Open();
                using (var command = conn.CreateCommand())
                {
                    var webResponse = new WebResponse(ResponseTypes.Succeeded);
                    webResponse.Games   = new List <WebResponse_Game>();
                    command.CommandText = string.Format("SELECT ID, Name FROM Games WHERE ClientID = '{0}'", clientID);
                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var game = new WebResponse_Game()
                            {
                                ID   = reader["ID"].ToString(),
                                Name = reader["Name"].ToString()
                            };
                            webResponse.Games.Add(game);
                        }
                    }

                    return(ResponseTool.GenerateXML(webResponse));
                }
            }
        }
Ejemplo n.º 3
0
        public static string CreateUser(string apiKey, string gameID, string username, string password)
        {
            string response = ResponseTool.CheckAPIKey(apiKey, gameAPIKey);

            if (response != null)
            {
                return(response);
            }

            using (var conn = DataManager.CreateConnectionObject())
            {
                conn.Open();
                using (var command = conn.CreateCommand())
                {
                    // make sure username doesn't already exist
                    command.CommandText = string.Format("SELECT ID FROM Users WHERE GameID = '{0}' and Username = '******'", gameID, username);
                    using (var reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            var webResponse = new WebResponse(ResponseTypes.Error)
                            {
                                ErrorMessage = "Username already exists"
                            };
                            return(ResponseTool.GenerateXML(webResponse));
                        }
                    }

                    // create account
                    string passwordEncrypted = SecurityManager.Hash(password);
                    var    userID            = Guid.NewGuid();
                    string values            = string.Format("('{0}', '{1}', '{2}', '{3}', '{4}')", userID, gameID, username, passwordEncrypted, DateTime.UtcNow);
                    command.CommandText = "INSERT INTO Users (ID, GameID, Username, Password, DateCreated) VALUES " + values;
                    if (command.ExecuteNonQuery() == 1)
                    {
                        var webResponse = new WebResponse(ResponseTypes.Succeeded)
                        {
                            UserID   = userID.ToString(),
                            Username = username
                        };
                        return(ResponseTool.GenerateXML(webResponse));
                    }
                    else
                    {
                        var webResponse = new WebResponse(ResponseTypes.Error)
                        {
                            ErrorMessage = "Failed to properly create user"
                        };
                        return(ResponseTool.GenerateXML(webResponse));
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public static string RequestScores(string apiKey, string leaderboardID, string offset, string range, string sortOrder)
        {
            string response = ResponseTool.CheckAPIKey(apiKey, gameAPIKey);

            if (response != null)
            {
                return(response);
            }

            using (var conn = DataManager.CreateConnectionObject())
            {
                conn.Open();
                using (var command = conn.CreateCommand())
                {
                    var webResponse = new WebResponse(ResponseTypes.Succeeded);
                    webResponse.Scores = new List <WebResponse_Score>();

                    // get all leaderboard usersIDs
                    command.CommandText = string.Format("SELECT ID, UserID, Score FROM Scores WHERE LeaderboardID = '{0}' ORDER BY Score {3} OFFSET {1} ROWS FETCH NEXT {2} ROWS ONLY", leaderboardID, offset, range, sortOrder == "Ascending" ? "ASC" : "DESC");
                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            webResponse.Scores.Add(new WebResponse_Score()
                            {
                                ID     = reader["ID"].ToString(),
                                UserID = reader["UserID"].ToString(),
                                Score  = long.Parse(reader["Score"].ToString())
                            });
                        }
                    }

                    // get all usernames for IDs
                    foreach (var score in webResponse.Scores)
                    {
                        command.CommandText = string.Format("SELECT Username FROM Users WHERE ID = '{0}'", score.UserID);
                        using (var reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                score.Username = reader["Username"].ToString();
                            }
                        }
                    }

                    return(ResponseTool.GenerateXML(webResponse));
                }
            }
        }
Ejemplo n.º 5
0
        public static string DeleteGame(string apiKey, string gameID)
        {
            string response = ResponseTool.CheckAPIKey(apiKey, clientAPIKey);

            if (response != null)
            {
                return(response);
            }

            using (var conn = DataManager.CreateConnectionObject())
            {
                conn.Open();
                using (var command = conn.CreateCommand())
                {
                    // get all users
                    command.CommandText = string.Format("SELECT ID FROM Users WHERE GameID = '{0}'", gameID);
                    var userIDs = new List <Guid>();
                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            userIDs.Add(new Guid(reader["ID"].ToString()));
                        }
                    }

                    // delete all user scores & achievemetns
                    foreach (var userID in userIDs)
                    {
                        // scores
                        command.CommandText = string.Format("DELETE FROM Scores WHERE UserID = '{0}'", userID);
                        command.ExecuteNonQuery();

                        // achievements
                        command.CommandText = string.Format("DELETE FROM Achievements WHERE UserID = '{0}'", userID);
                        command.ExecuteNonQuery();
                    }

                    // delete users
                    command.CommandText = string.Format("DELETE FROM Users WHERE GameID = '{0}'", gameID);
                    command.ExecuteNonQuery();

                    // delete game
                    command.CommandText = string.Format("DELETE FROM Games WHERE ID = '{0}'", gameID);
                    var webResponse = new WebResponse(command.ExecuteNonQuery() != 0 ? ResponseTypes.Succeeded : ResponseTypes.Error);
                    return(ResponseTool.GenerateXML(webResponse));
                }
            }
        }
Ejemplo n.º 6
0
        public static string CreateGame(string apiKey, string clientID, string name)
        {
            string response = ResponseTool.CheckAPIKey(apiKey, clientAPIKey);

            if (response != null)
            {
                return(response);
            }

            using (var conn = DataManager.CreateConnectionObject())
            {
                conn.Open();
                using (var command = conn.CreateCommand())
                {
                    // make sure name doesn't already exist
                    command.CommandText = string.Format("SELECT ID FROM Games WHERE Name = '{0}'", name);
                    using (var reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            var webResponse = new WebResponse(ResponseTypes.Error)
                            {
                                ErrorMessage = "Game Name already exists"
                            };
                            return(ResponseTool.GenerateXML(webResponse));
                        }
                    }

                    // create account
                    string values = string.Format("(NEWID(), '{0}', '{1}', '{2}')", clientID, name, DateTime.UtcNow);
                    command.CommandText = "INSERT INTO Games (ID, ClientID, Name, DateCreated) VALUES " + values;
                    if (command.ExecuteNonQuery() == 1)
                    {
                        var webResponse = new WebResponse(ResponseTypes.Succeeded);
                        return(ResponseTool.GenerateXML(webResponse));
                    }
                    else
                    {
                        var webResponse = new WebResponse(ResponseTypes.Error)
                        {
                            ErrorMessage = "Failed to properly create game"
                        };
                        return(ResponseTool.GenerateXML(webResponse));
                    }
                }
            }
        }
Ejemplo n.º 7
0
        public static string Login(string apiKey, string gameID, string username, string password)
        {
            string response = ResponseTool.CheckAPIKey(apiKey, userAPIKey);

            if (response != null)
            {
                return(response);
            }

            using (var conn = DataManager.CreateConnectionObject())
            {
                conn.Open();
                using (var command = conn.CreateCommand())
                {
                    string passwordEncrypted = SecurityManager.Hash(password);
                    command.CommandText = string.Format("SELECT ID, Username FROM Users WHERE GameID = '{0}' and Username = '******' and Password = '******'", gameID, username, passwordEncrypted);
                    using (var reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            var webResponse = new WebResponse(ResponseTypes.Succeeded)
                            {
                                UserID   = reader["ID"].ToString(),
                                Username = reader["Username"].ToString()
                            };
                            return(ResponseTool.GenerateXML(webResponse));
                        }
                        else
                        {
                            var webResponse = new WebResponse(ResponseTypes.Error)
                            {
                                ErrorMessage = "Failed to find user"
                            };
                            return(ResponseTool.GenerateXML(webResponse));
                        }
                    }
                }
            }
        }
Ejemplo n.º 8
0
        public static string ReportScore(string apiKey, string userID, string leaderboardID, string score)
        {
            string response = ResponseTool.CheckAPIKey(apiKey, userAPIKey);

            if (response != null)
            {
                return(response);
            }

            using (var conn = DataManager.CreateConnectionObject())
            {
                conn.Open();
                using (var command = conn.CreateCommand())
                {
                    // check if score already exists
                    command.CommandText = string.Format("SELECT ID, Score FROM Scores WHERE UserID = '{0}' and LeaderboardID = '{1}'", userID, leaderboardID);
                    string id           = null;
                    long   currentScore = 0;
                    using (var reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            id           = reader["ID"].ToString();
                            currentScore = long.Parse(reader["Score"].ToString());
                        }
                    }

                    if (id == null)
                    {
                        // create score
                        string values = string.Format("(NEWID(), '{0}', '{1}', {2}, '{3}')", userID, leaderboardID, score, DateTime.UtcNow);
                        command.CommandText = "INSERT INTO Scores (ID, UserID, LeaderboardID, Score, Date) VALUES " + values;
                        if (command.ExecuteNonQuery() == 1)
                        {
                            var webResponse = new WebResponse(ResponseTypes.Succeeded);
                            return(ResponseTool.GenerateXML(webResponse));
                        }
                        else
                        {
                            var webResponse = new WebResponse(ResponseTypes.Error)
                            {
                                ErrorMessage = "Failed to properly create score"
                            };
                            return(ResponseTool.GenerateXML(webResponse));
                        }
                    }
                    else if (long.Parse(score) > currentScore)
                    {
                        // update existing score
                        command.CommandText = string.Format("UPDATE Scores SET Date = '{0}', Score = {1} WHERE ID = '{2}'", DateTime.UtcNow, score, id);
                        if (command.ExecuteNonQuery() == 1)
                        {
                            var webResponse = new WebResponse(ResponseTypes.Succeeded);
                            return(ResponseTool.GenerateXML(webResponse));
                        }
                        else
                        {
                            var webResponse = new WebResponse(ResponseTypes.Error)
                            {
                                ErrorMessage = "Failed to properly update score"
                            };
                            return(ResponseTool.GenerateXML(webResponse));
                        }
                    }
                    else
                    {
                        // current score already higher
                        var webResponse = new WebResponse(ResponseTypes.Succeeded);
                        return(ResponseTool.GenerateXML(webResponse));
                    }
                }
            }
        }
Ejemplo n.º 9
0
        public static string ReportAchievement(string apiKey, string userID, string achievementID, string percentComplete)
        {
            string response = ResponseTool.CheckAPIKey(apiKey, userAPIKey);

            if (response != null)
            {
                return(response);
            }

            using (var conn = DataManager.CreateConnectionObject())
            {
                conn.Open();
                using (var command = conn.CreateCommand())
                {
                    // check if achievement already exists
                    command.CommandText = string.Format("SELECT ID, PercentComplete FROM Achievements WHERE UserID = '{0}' and AchievementID = '{1}'", userID, achievementID);
                    string id = null;
                    float  currentPercentComplete = 0;
                    using (var reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            id = reader["ID"].ToString();
                            currentPercentComplete = float.Parse(reader["PercentComplete"].ToString());
                        }
                    }

                    if (id == null)
                    {
                        // create achievement
                        string values = string.Format("(NEWID(), '{0}', '{1}', '{2}', '{3}')", userID, achievementID, percentComplete, DateTime.UtcNow);
                        command.CommandText = "INSERT INTO Achievements (ID, UserID, AchievementID, PercentComplete, Date) VALUES " + values;
                        if (command.ExecuteNonQuery() == 1)
                        {
                            var webResponse = new WebResponse(ResponseTypes.Succeeded);
                            return(ResponseTool.GenerateXML(webResponse));
                        }
                        else
                        {
                            var webResponse = new WebResponse(ResponseTypes.Error)
                            {
                                ErrorMessage = "Failed to properly create achievement"
                            };
                            return(ResponseTool.GenerateXML(webResponse));
                        }
                    }
                    else if (float.Parse(percentComplete) > currentPercentComplete)
                    {
                        // update existing achievement
                        command.CommandText = string.Format("UPDATE Achievements SET Date = '{0}', PercentComplete = {1} WHERE ID = '{2}'", DateTime.UtcNow, percentComplete, id);
                        if (command.ExecuteNonQuery() == 1)
                        {
                            var webResponse = new WebResponse(ResponseTypes.Succeeded);
                            return(ResponseTool.GenerateXML(webResponse));
                        }
                        else
                        {
                            var webResponse = new WebResponse(ResponseTypes.Error)
                            {
                                ErrorMessage = "Failed to properly update achievement"
                            };
                            return(ResponseTool.GenerateXML(webResponse));
                        }
                    }
                    else
                    {
                        // current achievement percent already higher
                        var webResponse = new WebResponse(ResponseTypes.Succeeded);
                        return(ResponseTool.GenerateXML(webResponse));
                    }
                }
            }
        }