public void MakeProgress_GivenAchievementConditionThatNeedsACountOf1_IsCompleted() { // Arrange AchievementCondition ac = new AchievementCondition("", "", 1); IAchievementCondition reportedAchievementConditionProgressChanged = null; int reportedProgressCount = 0; ac.ProgressChanged += delegate(IAchievementCondition iac, AchievementConditionProgressChangedArgs args) { reportedAchievementConditionProgressChanged = iac; reportedProgressCount = args.ProgressCount; }; IAchievementCondition reportedAchievementConditionCompleted = null; ac.ConditionCompleted += delegate(IAchievementCondition iac) { reportedAchievementConditionCompleted = iac; }; // Act ac.MakeProgress(); // Assert Assert.AreEqual(100, ac.Progress); Assert.AreEqual(1, ac.ProgressCount); Assert.AreEqual(ac, reportedAchievementConditionProgressChanged); Assert.AreEqual(1, reportedProgressCount); Assert.AreEqual(ac, reportedAchievementConditionCompleted); }
/// <summary> /// Register an IAchievementCondition /// </summary> /// <remarks>Only registered AchievementConditions can be tracked!</remarks> /// <param name="achievementCondition">The IAchievementCondition that should be registered</param> public void RegisterAchievementCondition(IAchievementCondition achievementCondition) { if (registeredAchievementConditions.All(x => x.UniqueId != achievementCondition.UniqueId)) { registeredAchievementConditions.Add(achievementCondition); } }
/// <summary> /// An achievement /// </summary> /// /// <param name="uniqueId">Applicationwide unique uniqueId of the achievement</param> /// <param name="titel">Applicationwide unique uniqueId of the achievement</param> /// <param name="description">Description of the achievement</param> /// <param name="condition">Condition that must be met to unlock the achievement</param> public Achievement(string uniqueId, string titel, string description, IAchievementCondition condition) : this(uniqueId, titel, description, new List <IAchievementCondition> { condition }) { if (condition == null) { throw new ArgumentNullException(nameof(condition)); } }
/// <summary> /// This method is tied to the ProgressChanged events in the AchievementConditions of this Achievement /// </summary> /// <param name="sender">IAchievementCondition thath fired the event</param> /// <param name="args">Parameters that are important for this event</param> private void ConditionProgressChanged(IAchievementCondition sender, AchievementConditionProgressChangedArgs args) { if (sender.Unlocked) { return; } // Calculate overall progress of Achievement based on progress of AchievementConditions Progress = Conditions.Sum(condition => condition.Progress) / Conditions.Count(); InvokeProgressChanged(Progress); }
public void Clear_GivenAnAchievementWithSeveralConditions_ClearsTheAchievement() { // Arrange Mock <IAchievementCondition> achievementConditionMock1 = new Mock <IAchievementCondition>(); achievementConditionMock1.SetupGet(x => x.UniqueId).Returns("ac1"); achievementConditionMock1.SetupGet(x => x.Progress).Returns(0); Mock <IAchievementCondition> achievementConditionMock2 = new Mock <IAchievementCondition>(); achievementConditionMock2.SetupGet(x => x.UniqueId).Returns("ac2"); achievementConditionMock2.SetupGet(x => x.Progress).Returns(50); IAchievementCondition ac1 = achievementConditionMock1.Object; IAchievementCondition ac2 = achievementConditionMock2.Object; List <IAchievementCondition> achievementConditions = new List <IAchievementCondition> { ac1, ac2 }; Achievement achievement = new Achievement("uniqueId", "titel", "description", achievementConditions); int reportedProgress = 0; Achievement reportedAchievementProgressChanged = null; achievement.ProgressChanged += delegate(Achievement a, AchievementProgressChangedArgs args) { reportedProgress = args.ProgressCount; reportedAchievementProgressChanged = a; }; Achievement reportedAchievementCompleted = null; achievement.AchievementCompleted += delegate(Achievement a) { reportedAchievementCompleted = a; }; // Act achievement.Clear(); achievementConditionMock2.Raise(x => x.ProgressChanged += null, ac2, new AchievementConditionProgressChangedArgs(50)); achievementConditionMock2.Raise(x => x.ConditionCompleted += null, ac2); // Assert Assert.AreEqual(0, achievement.Conditions.Count()); Assert.AreEqual(0, reportedProgress); Assert.AreEqual(null, reportedAchievementProgressChanged); Assert.AreEqual(null, reportedAchievementCompleted); }
public void Constructor_GivenIAchievementConditionNull_ShouldThrowException() { // Arrange IAchievementCondition achievementCondition = null; // Act try { new Achievement("uniqueId", "titel", "description", achievementCondition); Assert.Fail("Soll Exception werfen."); } catch (Exception e) { // Assert ArgumentNullException ane = e as ArgumentNullException; Assert.IsNotNull(ane); Assert.AreEqual("condition", ane.ParamName); } }
public void ConditionProgressChanged_GivenSomeProgress_CalculatesProgressAndFiresProgressChangedEvent( int conditionProgress, int expectedAchievementProgress) { // Arrange Mock <IAchievementCondition> achievementConditionMock1 = new Mock <IAchievementCondition>(); achievementConditionMock1.SetupGet(x => x.UniqueId).Returns("ac1"); achievementConditionMock1.SetupGet(x => x.Progress).Returns(0); Mock <IAchievementCondition> achievementConditionMock2 = new Mock <IAchievementCondition>(); achievementConditionMock2.SetupGet(x => x.UniqueId).Returns("ac2"); achievementConditionMock2.SetupGet(x => x.Progress).Returns(conditionProgress); IAchievementCondition ac1 = achievementConditionMock1.Object; IAchievementCondition ac2 = achievementConditionMock2.Object; List <IAchievementCondition> achievementConditions = new List <IAchievementCondition> { ac1, ac2 }; Achievement achievement = new Achievement("uniqueId", "titel", "description", achievementConditions); int reportedProgress = 0; Achievement reportedAchievement = null; achievement.ProgressChanged += delegate(Achievement a, AchievementProgressChangedArgs args) { reportedProgress = args.ProgressCount; reportedAchievement = a; }; // Act achievementConditionMock2.Raise(x => x.ProgressChanged += null, ac1, new AchievementConditionProgressChangedArgs(conditionProgress)); // Assert Assert.AreEqual(achievement, reportedAchievement); Assert.AreEqual(expectedAchievementProgress, achievement.Progress); Assert.AreEqual(expectedAchievementProgress, reportedProgress); }
public void ConditionProgressChanged_GivenAlreadyUnlockedAchievementCondition_DoNothing() { // Arrange Mock <IAchievementCondition> achievementConditionMock1 = new Mock <IAchievementCondition>(); achievementConditionMock1.SetupGet(x => x.UniqueId).Returns("ac1"); achievementConditionMock1.SetupGet(x => x.Progress).Returns(0); Mock <IAchievementCondition> achievementConditionMock2 = new Mock <IAchievementCondition>(); achievementConditionMock2.SetupGet(x => x.UniqueId).Returns("ac2"); achievementConditionMock2.SetupGet(x => x.Progress).Returns(150); achievementConditionMock2.SetupGet(x => x.Unlocked).Returns(true); IAchievementCondition ac1 = achievementConditionMock1.Object; IAchievementCondition ac2 = achievementConditionMock2.Object; List <IAchievementCondition> achievementConditions = new List <IAchievementCondition> { ac1, ac2 }; Achievement achievement = new Achievement("uniqueId", "titel", "description", achievementConditions); int reportedProgress = 0; Achievement reportedAchievement = null; achievement.ProgressChanged += delegate(Achievement a, AchievementProgressChangedArgs args) { reportedProgress = args.ProgressCount; reportedAchievement = a; }; // Act achievementConditionMock2.Raise(x => x.ProgressChanged += null, ac2, new AchievementConditionProgressChangedArgs(150)); // Assert Assert.AreEqual(0, reportedProgress); Assert.IsNull(reportedAchievement); }
public void ConditionCompleted_RaiseEventOnOnlyConditionAndConditionIsUnlocked_UnlockAchievement() { // Arrange Mock <IAchievementCondition> achievementConditionMock1 = new Mock <IAchievementCondition>(); achievementConditionMock1.SetupGet(x => x.Unlocked).Returns(true); achievementConditionMock1.SetupGet(x => x.UniqueId).Returns("ac1"); IAchievementCondition ac = achievementConditionMock1.Object; Achievement achievement = new Achievement("uniqueId", "titel", "description", ac); List <Achievement> reportedAchievements = new List <Achievement>(); achievement.AchievementCompleted += delegate(Achievement a) { reportedAchievements.Add(a); }; // Act achievementConditionMock1.Raise(x => x.ConditionCompleted += null, ac); // Assert Assert.AreEqual(1, reportedAchievements.Count); Assert.IsTrue(achievement.Unlocked); }
/// <summary> /// An achievement /// </summary> /// /// <param name="uniqueId">Applicationwide unique uniqueId of the achievement</param> /// <param name="titel">Applicationwide unique uniqueId of the achievement</param> /// <param name="description">Description of the achievement</param> /// <param name="condition">Condition that must be met to unlock the achievement</param> public Achievement(string uniqueId, string titel, string description, IAchievementCondition condition) : this(uniqueId, titel, description, new List<IAchievementCondition> { condition }) { if (condition == null) throw new ArgumentNullException(nameof(condition)); }
/// <summary> /// This method is tied to the ProgressChanged events in the AchievementConditions of this Achievement /// </summary> /// <param name="sender">IAchievementCondition thath fired the event</param> /// <param name="args">Parameters that are important for this event</param> private void ConditionProgressChanged(IAchievementCondition sender, AchievementConditionProgressChangedArgs args) { if (sender.Unlocked) return; // Calculate overall progress of Achievement based on progress of AchievementConditions Progress = Conditions.Sum(condition => condition.Progress) / Conditions.Count(); InvokeProgressChanged(Progress); }
/// <summary> /// This method is tied to the ConditionCompleted events in the AchievementConditions of this Achievement /// </summary> /// <param name="achievementCondition">IAchievementCondition that fired the event</param> private void ConditionCompleted(IAchievementCondition achievementCondition) { CheckUnlockStatus(); }
/// <summary> /// Set the madeProgress variable is event is called /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private static void AcProgressChanged(IAchievementCondition sender, AchievementConditionProgressChangedArgs args) { madeProgress = true; }
/// <summary> /// sets completed variable true, when an AchievementCondition is completed /// </summary> /// <param name="achievementCondition"></param> private static void AcConditionCompleted(IAchievementCondition achievementCondition) { completed = true; }
/// <summary> /// Register an IAchievementCondition /// </summary> /// <remarks>Only registered AchievementConditions can be tracked!</remarks> /// <param name="achievementCondition">The IAchievementCondition that should be registered</param> public void RegisterAchievementCondition(IAchievementCondition achievementCondition) { if (registeredAchievementConditions.All(x => x.UniqueId != achievementCondition.UniqueId)) registeredAchievementConditions.Add(achievementCondition); }