Example #1
0
            public static List <QuestData> GetQuests(string username)
            {
                ToolsDataContext db     = new ToolsDataContext();
                Player           p      = db.Players.Where(a => a.RS_Username == username).FirstOrDefault();
                List <QuestData> output = new List <QuestData>();

                // If the player does not exist, create one
                if (p == null)
                {
                    p = NewPlayer(username);
                }

                foreach (Quest q in db.Quests)
                {
                    QuestData qd = new QuestData();
                    qd.quest = q;

                    PlayerQuest questStatus = db.PlayerQuests.Where(a => a.PlayerID == p.PlayerID && a.QuestID == q.QuestID).FirstOrDefault();
                    if (questStatus != null)
                    {
                        qd.status = questStatus.Status;
                    }
                    else
                    {
                        questStatus          = new PlayerQuest();
                        questStatus.QuestID  = q.QuestID;
                        questStatus.PlayerID = p.PlayerID;
                        questStatus.Status   = false;
                        db.PlayerQuests.InsertOnSubmit(questStatus);
                    }
                    output.Add(qd);
                }
                db.SubmitChanges();

                return(output);
            }
Example #2
0
 partial void DeletePlayerQuest(PlayerQuest instance);
Example #3
0
 partial void UpdatePlayerQuest(PlayerQuest instance);
Example #4
0
 partial void InsertPlayerQuest(PlayerQuest instance);
Example #5
0
 private void detach_PlayerQuests(PlayerQuest entity)
 {
     this.SendPropertyChanging();
     entity.Player = null;
 }
Example #6
0
 private void attach_PlayerQuests(PlayerQuest entity)
 {
     this.SendPropertyChanging();
     entity.Player = this;
 }
Example #7
0
        /// <summary>
        /// Create a Player entry in the Runescape database containing the username, skills, and blank quest progress
        /// Skills are retrieved dynamically via the Runescape.com api
        /// </summary>
        /// <param name="username">The Runescape username to create</param>
        /// <returns>Whether the Player was successfully created</returns>
        public static bool CreatePlayer(string username)
        {
            RunescapeDataContext db = new RunescapeDataContext();

            // The order that the skills will be recieved by Runscape API. THIS ORDER MATTERS
            string[] order = { "Overall", "Attack", "Defense", "Strength", "Hitpoints", "Ranged", "Prayer", "Magic", "Cooking", "Woodcutting", "Fletching", "Fishing", "Firemaking", "Crafting", "Smithing", "Mining", "Herblore", "Agility", "Thieving", "Slayer", "Farming", "Runecraft", "Hunter", "Construction" };

            // Check if the player exists already
            // TODO: Alter so that it instead looks up the current user's player and replaces them with the new one
            Player playerToCreate = db.Players.Where(a => a.RS_Username == "username").FirstOrDefault();

            // The player does not exist, proceed with the creation
            if (playerToCreate == null)
            {
                //Create the new user
                playerToCreate             = new Player();
                playerToCreate.RS_Username = username;
                playerToCreate.Admin       = false;
                db.Players.InsertOnSubmit(playerToCreate);
                db.SubmitChanges();

                // Create quests for the user
                foreach (Quest quest in db.Quests.ToList())
                {
                    PlayerQuest pq = new PlayerQuest();
                    pq.Status   = false;
                    pq.PlayerID = playerToCreate.PlayerID;
                    pq.QuestID  = quest.QuestID;
                    db.PlayerQuests.InsertOnSubmit(pq);
                }

                // Get skill data from Runescape
                WebClient client     = new WebClient();
                string[]  jsonResult = client.DownloadString("http://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player=" + username.ToLower()).Replace('\n', ';').Split(';');

                // Create skills for the user
                int skillIndex = 0;
                foreach (string skillName in order)
                {
                    string[] stats      = jsonResult[skillIndex].Split(',');
                    string   skillRank  = stats[0],
                             skillLevel = stats[1],
                             skillExp   = stats[2];

                    PlayerSkill ps = new PlayerSkill();
                    ps.SkillID  = db.Skills.Where(a => a.Name == skillName).FirstOrDefault().SkillID;
                    ps.Rank     = Convert.ToInt32(stats[0]);
                    ps.Exp      = Convert.ToInt32(stats[2]);
                    ps.Level    = Convert.ToInt32(stats[1]);
                    ps.PlayerID = playerToCreate.PlayerID;

                    db.PlayerSkills.InsertOnSubmit(ps);
                    skillIndex++;
                }



                db.SubmitChanges();
                return(true);
            }

            // If the player exists, it doesn't need to be created, make sure they have stats
            else
            {
                // Check the skills
                foreach (Skill skill in db.Skills.ToList())
                {
                    PlayerSkill ps = db.PlayerSkills.Where(a => a.PlayerID == playerToCreate.PlayerID && a.SkillID == skill.SkillID).FirstOrDefault();

                    // The skill does not exist for the player; create it
                    if (ps == null)
                    {
                        ps.PlayerID = playerToCreate.PlayerID;
                        ps.SkillID  = skill.SkillID;
                        ps.Rank     = 0;
                        ps.Level    = 1;
                        ps.Exp      = 1; // Set these stats to 1 until the next update
                        db.PlayerSkills.InsertOnSubmit(ps);
                    }
                }

                // Check the quests
                foreach (Quest quest in db.Quests.ToList())
                {
                    PlayerQuest pq = db.PlayerQuests.Where(a => a.PlayerID == playerToCreate.PlayerID && a.QuestID == quest.QuestID).FirstOrDefault();

                    // The quest does not exist for the player; create it
                    if (pq == null)
                    {
                        pq.PlayerID = playerToCreate.PlayerID;
                        pq.QuestID  = quest.QuestID;
                        pq.Status   = false;
                        db.PlayerQuests.InsertOnSubmit(pq);
                    }
                }

                db.SubmitChanges();
                return(true);
            }
        }