/// <summary> /// Gather the information on a particular username and save it to the database. /// This is a temporary funtion /// TODO: Replace this with a simple web api lookup and return /// </summary> /// <param name="username">The username</param> /// <returns>A list of Skills</returns> public static List <SkillData> GetSkills(string username) { RunescapeDataContext db = new RunescapeDataContext(); Player playerToFind = db.Players.Where(a => a.RS_Username == username).FirstOrDefault(); // TODO: Find player based on login info with GetSkills(void) List <SkillData> toReturn = new List <SkillData>(); // Create empty list UpdateSkills(username); // If the player exists in the database if (playerToFind != null) { // Gather the info for each skill foreach (Skill skill in db.Skills) { string skillName = skill.Name; PlayerSkill ps = db.PlayerSkills.Where(a => a.PlayerID == playerToFind.PlayerID && a.SkillID == skill.SkillID).FirstOrDefault(); SkillData sD = new SkillData(); sD.level = (int)ps.Level; sD.experience = (int)ps.Exp; sD.rank = (int)ps.Rank; sD.questreq = (int)skill.MinimumQuestRequirement; sD.diaryreq = (int)skill.MinimumDiaryRequirement; sD.name = skill.Name; sD.skill = skill; //Quest progress sD.questprogress = sD.level / sD.questreq; sD.diaryprogress = sD.level / sD.diaryreq; sD.levelprogress = 100; // Default value is 100% for Overall if (sD.name != "Overall" && sD.level < 99) { Level nl = db.Levels.Where(a => a.Value == sD.level + 1).FirstOrDefault(); Level ll = db.Levels.Where(a => a.Value == sD.level).FirstOrDefault(); sD.levelprogress = Math.Round(((decimal)sD.experience - ll.Experience) / ((decimal)nl.Experience - ll.Experience) * 100, 2); if (sD.levelprogress > 100) { sD.levelprogress = 100; } } toReturn.Add(sD); // Add the skill to the output list } return(toReturn); } // Player does not exist, will need to create it else { PlayersHelper.CreatePlayer(username); return(GetSkills(username)); // Recursive call on the newly created data } }
public static bool UpdateSkills(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" }; Player playerToUpdate = db.Players.Where(a => a.RS_Username == username).FirstOrDefault(); if (playerToUpdate != null) { // 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(';'); // Update 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]; Skill sk = db.Skills.Where(a => a.Name == skillName).FirstOrDefault(); PlayerSkill ps = db.PlayerSkills.Where(a => a.PlayerID == playerToUpdate.PlayerID && a.SkillID == sk.SkillID).FirstOrDefault(); if (ps == null) { ps.PlayerID = playerToUpdate.PlayerID; ps.SkillID = sk.SkillID; } ps.Rank = Convert.ToInt32(stats[0]); ps.Exp = Convert.ToInt32(stats[2]); ps.Level = Convert.ToInt32(stats[1]); db.SubmitChanges(); skillIndex++; } return(true); } // Player does not exist, so create him/her else { return(PlayersHelper.CreatePlayer(username)); } }
/// <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); } }