Example #1
0
        public FighterLevel GetLevel(FighterExperience fighterExperience)
        {
            var fighterLevel = new FighterLevel
            {
                Level = 1,
            };

            long neededExp  = 0;
            bool levelFound = false;

            do
            {
                var levelExp = GetNeededExperience(fighterLevel.Level);
                neededExp += levelExp;

                if (neededExp <= fighterExperience.Points)
                {
                    fighterLevel.Level += 1;
                }
                else
                {
                    levelFound = true;
                    fighterLevel.RequiredExperience = levelExp;
                    fighterLevel.Experience         = levelExp + (fighterExperience.Points - neededExp);
                }
            }while (!levelFound);

            return(fighterLevel);
        }
Example #2
0
        public async Task AddExperienceAsync(FighterExperience fighterExperience, FighterContribution contribution, double mutliplicator)
        {
            var ids = new Guid[] { fighterExperience.FighterId }.Union(contribution.Kills).Union(contribution.Assists).ToList();
            var fighters = await context.Fighters.Where(o => ids.Contains(o.Id)).ToListAsync();

            var fighter    = fighters.First(o => o.Id == fighterExperience.FighterId);
            var experience = 100;

            foreach (var killFighterId in contribution.Kills)
            {
                experience += GetExperience(fighters, fighter, killFighterId, KillExperience);
            }

            foreach (var assistFighterId in contribution.Assists)
            {
                experience += GetExperience(fighters, fighter, assistFighterId, AssistExperience);
            }

            fighterExperience.Points    += experience + (int)(experience * mutliplicator);
            fighterExperience.LastUpdate = dateTimeProvider.Now;
        }
        public async Task Process(Match match, IEnumerable <FighterContribution> contributions)
        {
            foreach (var contribution in contributions)
            {
                var fighter = await databaseContext.Fighters
                              .AsTracking()
                              .FirstOrDefaultAsync(o => o.Id == contribution.FighterId);

                if (fighter == null)
                {
                    continue;
                }

                var fighterExperience = await databaseContext.FighterExperiences
                                        .AsTracking()
                                        .FirstOrDefaultAsync(o => o.FighterId == contribution.FighterId);

                if (fighterExperience == null)
                {
                    fighterExperience = new FighterExperience()
                    {
                        FighterId = contribution.FighterId,
                    };
                    databaseContext.FighterExperiences.Add(fighterExperience);
                }

                var levelBefore = fighterExperienceCalculator.GetLevel(fighterExperience);
                await fighterExperienceCalculator.AddExperienceAsync(fighterExperience, contribution, 0);

                var levelAfter = fighterExperienceCalculator.GetLevel(fighterExperience);

                for (int i = levelBefore.Level; i < levelAfter.Level; i++)
                {
                    databaseContext.AddRange(fighterLevelUpHandler.Up(fighter));
                }
            }

            await databaseContext.SaveChangesAsync();
        }