/// <summary> /// Loads a profile by reading a text document from the stream reader and returns it /// </summary> /// <param name="sr">StreamReader</param> /// <returns>Profile</returns> public Profile LoadProfile(StreamReader sr) { Profile user = new Profile("temp", "temp"); user.Name = sr.ReadLine(); user.Desc = sr.ReadLine(); user.Exp = sr.ReadInteger(); user.ExpReq = sr.ReadInteger(); user.Level = sr.ReadInteger(); int taskCount = sr.ReadInteger(); int shortCount = sr.ReadInteger(); int longCount = sr.ReadInteger(); for (int i = 0; i < taskCount; i++) { LoadGoal(sr, ref user); } for (int i = 0; i < shortCount; i++) { LoadGoal(sr, ref user); } for (int i = 0; i < longCount; i++) { LoadGoal(sr, ref user); } return user; }
/// <summary> /// Constructor for Perfection Model. Creates a loader and sets user to null. /// </summary> public PerfectionModel() { _user = null; _loader = new Loader(); }
/// <summary> /// Takes in a name and description and uses that to make a profile. Sets a path for the loader based on the name. Returns a string explaining the events /// </summary> /// <param name="name">string</param> /// <param name="desc">string</param> /// <returns>string</returns> public string NewUser(string name, string desc) { _user = new Profile(name, desc); _path = @"profile" + _user.Name + ".txt"; return Environment.NewLine + name + " was created!"; }
/// <summary> /// Attempts to load a profile based on the inputed sting name, returns a string explaining the events. If a profile is loaded, sets it to _user /// </summary> /// <param name="name"></param> /// <returns></returns> public string LoadProfile(string name) { _path = @"profile" + name + ".txt"; if (!File.Exists(_path)) { return Environment.NewLine + name + " profile doesn't exist."; } else { using (StreamReader sr = new StreamReader (_path)) { _user = _loader.LoadProfile(sr); sr.Close(); } } return Environment.NewLine + _user.Name + " was succesfully loaded."; }
/// <summary> /// Loads a goal from a stream reacer and attaches it to the profile /// </summary> /// <param name="sr">StreamReader</param> /// <param name="user">Profile</param> private void LoadGoal(StreamReader sr, ref Profile user) { Goal resultGoal = sr.ReadGoalType(); string goalName = sr.ReadLine(); string goalDesc = sr.ReadLine(); GoalCategory goalCate = sr.ReadCategory(); if (resultGoal is LongTermGoal) { LongTermGoal resultLong = new LongTermGoal(goalName, goalDesc, goalCate, new List<ShortTermGoal>()); resultLong.Completed = sr.ReadBool(); resultLong.ExpVal = sr.ReadInteger(); resultLong.EstHours = sr.ReadInteger(); int reqShort = sr.ReadInteger(); for (int i = 0; i < reqShort; i++) { string reqShortName = sr.ReadLine(); foreach (ShortTermGoal sg in user.ShortGoals) { if (sg.Name == reqShortName) { if (!resultLong.ReqShort.Contains(sg)) { resultLong.ReqShort.Add(sg); break; } } } } resultGoal = resultLong; } else if (resultGoal is ShortTermGoal) { ShortTermGoal resultShort = new ShortTermGoal(goalName, goalDesc, goalCate, new List<TaskGoal>()); resultShort.Completed = sr.ReadBool(); resultShort.ExpVal = sr.ReadInteger(); resultShort.EstHours = sr.ReadInteger(); int reqTasks = sr.ReadInteger(); for (int i = 0; i < reqTasks; i++) { string reqTaskName = sr.ReadLine(); foreach (TaskGoal tg in user.Tasks) { if (tg.Name == reqTaskName) { if (!resultShort.ReqTasks.Contains(tg)) { resultShort.ReqTasks.Add(tg); break; } } } } resultGoal = resultShort; } else { resultGoal = new TaskGoal(goalName, goalDesc, goalCate); resultGoal.Completed = sr.ReadBool(); resultGoal.ExpVal = sr.ReadInteger(); resultGoal.EstHours = sr.ReadInteger(); } user.AddGoal(resultGoal); }