Beispiel #1
0
        public void LevelUp(IUnitOfWork unitOfWork, IStatApplier statApplier, Player player, AssignmentType completedAssignment)
        {
            var currentLevel = player.Level;
            var nextLevel = unitOfWork.LevelRepository.Get((level => level.LevelNumber == currentLevel + 1), null, "").FirstOrDefault();
            // TODO: Make several games on one API ? Check if right game fds
            if (nextLevel == null) return; // Max level reached

            // Check if experience reached
            if (player.Experience < nextLevel.ExperienceReq) return; // Not enough experience

            // Check general stats
            foreach (var generalStatAmountReq in nextLevel.GeneralStatsReq)
            {
                var playerGeneralStatAmount =
                    player.GeneralStats.FirstOrDefault(
                        u =>
                            u.StatEffect.AffectedStatBonus ==
                            generalStatAmountReq.StatEffect.AffectedStatBonus);
                if (playerGeneralStatAmount == null) throw new Exception("Database error, player has no such general stat entry");

                var res = generalStatAmountReq.CompareTo(playerGeneralStatAmount);
                if (res == 1)
                    return; // No level up
            }

            // Level up reached! DING DING DING
            player.Level += 1;
            player.JobPoints += nextLevel.JobPointReward;
            statApplier.ApplyGeneralStats(player, nextLevel.GeneralStatRewards);
        }
        public List<Achievement> CheckAchievements(IStatApplier statApplier, IUnitOfWork unitOfWork, Player player)
        {
            // Retrieve all achievements, which the player did not already achieve
            var unachievedAchievements = unitOfWork.AchievementRepository.Get(ach => player.Achievements.TrueForAll(playerAch => playerAch.Id != ach.Id), null, "");

            var resList = new List<Achievement>();
            // Check for achieved achievements
            foreach (var unachievedAchievement in unachievedAchievements)
            {
                // Check for assignments completed of certain types
                if (!PassedAssignmentTypes(player, unachievedAchievement)) break; // Break if achievement failed here
                // Check for general stat requirements
                if (!PassedGeneralStats(player, unachievedAchievement)) break; // Break if achievement failed here
                // Check for Job Point purchase requirements
                if (!PassedJobPointPurchases(player, unachievedAchievement)) break; // Break if achievement failed here

                // Achievement gotten!
                player.Achievements.Add(unachievedAchievement);
                statApplier.ApplyJobPoints(player, unachievedAchievement.JobPointReward);
                statApplier.ApplyGeneralStats(player, unachievedAchievement.GeneralStatRewards);
                resList.Add(unachievedAchievement);
            }

            return resList; // Return all newly achieved achievements
        }
 private bool PassedJobPointPurchases(Player player, Achievement achievementToCheck)
 {
     foreach (var jpProductReq in achievementToCheck.JobPointProductReqs)
     {
         var fds = player.JobPointPurchases.FirstOrDefault(purch => purch.Product == jpProductReq);
         if (fds == null) return false; // Didn't purchase a required product
     }
     return true;
 }
 private bool PassedAssignmentTypes(Player player, Achievement achievementToCheck)
 {
     foreach (var assTypeReq in achievementToCheck.AssignmentTypeReqs)
     {
         var assignmentsOfType = player.AssignmentsCompleted.FindAll(ass => ass.Type.NameOrCode == assTypeReq.AssignmentType.NameOrCode);
         if (assignmentsOfType.Count != assTypeReq.AmountReq) return false; // Not enough assignments of some type
     }
     return true;
 }
        private bool PassedGeneralStats(Player player, Achievement achievementToCheck)
        {
            foreach (var genStatReq in achievementToCheck.GeneralStatReqs)
            {
                var playerGeneralStatAmount =
                player.GeneralStats.FirstOrDefault(
                    u =>
                        u.StatEffect.AffectedStatBonus ==
                        genStatReq.StatEffect.AffectedStatBonus);
                if (playerGeneralStatAmount == null) throw new Exception("Database error, player has no such general stat entry");

                var res = genStatReq.CompareTo(playerGeneralStatAmount);
                if (res == 1)
                    return false; // General stat not high enough
            }
            return true;
        }
        public List<Achievement> CompleteTask(Player player, AssignmentType assignmentTypeCompleted)
        {
            // Apply general stats - bonuses from here included henceforth
            _statApplier.ApplyGeneralStats(player, assignmentTypeCompleted.GeneralStatRewards);

            // Apply experience TODO: ExperienceApplier, JobPointApplier - Combine to StatApplier
            _statApplier.ApplyExperience(player, assignmentTypeCompleted.ExpReward);

            // Check level up
            _levelUpChecker.LevelUp(_unitOfWork, _statApplier, player, assignmentTypeCompleted); // Levels up, assigns rewards if achieved

            // Add assignment to assignment list
            player.AssignmentsCompleted.Add(new Assignment{Type = assignmentTypeCompleted});

            // Check for achievements TODO: (Return achievementGet ??? fds)
            var achievements = _achievementChecker.CheckAchievements(_statApplier, _unitOfWork, player);

            return achievements;
        }