public static void CleanseTFEnergies(Player player, decimal bonusPercentageFromBuffs) { ITFEnergyRepository repo = new EFTFEnergyRepository(); IEnumerable <Models.TFEnergy> mydbEnergies = repo.TFEnergies.Where(e => e.PlayerId == player.Id).ToList(); foreach (var energy in mydbEnergies) { energy.Amount -= bonusPercentageFromBuffs; if (energy.Amount > 0) { repo.SaveTFEnergy(energy); } else { repo.DeleteTFEnergy(energy.Id); } } }
private static (decimal, decimal) AccumulateTFEnergy(Player victim, Player attacker, SkillViewModel skill, decimal modifier) { ITFEnergyRepository repo = new EFTFEnergyRepository(); // assert modifier is never negative (reduced TF damage instead of adding it) if (modifier < 0) { modifier = 0; } // crunch down any old TF Energies into one public energy var energiesOnPlayer = repo.TFEnergies.Where(e => e.PlayerId == victim.Id && e.FormSourceId == skill.StaticSkill.FormSourceId).ToList(); var energiesEligibleForDelete = new List <Models.TFEnergy>(); decimal mergeUpEnergyAmt = 0; decimal sharedEnergyAmt = 0; foreach (var e in energiesOnPlayer) { var minutesAgo = Math.Abs(Math.Floor(e.Timestamp.Subtract(DateTime.UtcNow).TotalMinutes)); if (e.CasterId == null) { sharedEnergyAmt += e.Amount; } else if (minutesAgo > 180) { mergeUpEnergyAmt += e.Amount; energiesEligibleForDelete.Add(e); } } // if the amount of old energies is greater than 0, write up a new one and save it as 'public domain' TF Energy if (mergeUpEnergyAmt > 0) { var publicEnergy = repo.TFEnergies.FirstOrDefault(e => e.PlayerId == victim.Id && e.FormSourceId == skill.StaticSkill.FormSourceId && e.CasterId == null); if (publicEnergy == null) { var cmd = new CreateTFEnergy { PlayerId = victim.Id, Amount = mergeUpEnergyAmt, CasterId = null, FormSourceId = skill.StaticSkill.FormSourceId }; DomainRegistry.Repository.Execute(cmd); } else { publicEnergy.Amount += mergeUpEnergyAmt; repo.SaveTFEnergy(publicEnergy); } foreach (var e in energiesEligibleForDelete) { repo.DeleteTFEnergy(e.Id); } } // get the amount of TF Energy the attacker has on the player var energyFromMe = repo.TFEnergies.FirstOrDefault(e => e.PlayerId == victim.Id && e.FormSourceId == skill.StaticSkill.FormSourceId && e.CasterId == attacker.Id); var tfeGain = skill.StaticSkill.TFPointsAmount * modifier; if (energyFromMe == null) { var cmd = new CreateTFEnergy { PlayerId = victim.Id, Amount = tfeGain, CasterId = attacker.Id, FormSourceId = skill.StaticSkill.FormSourceId }; DomainRegistry.Repository.Execute(cmd); // create an old entity just so it doesn't break functionality below energyFromMe = new Models.TFEnergy { PlayerId = victim.Id, FormSourceId = skill.StaticSkill.FormSourceId.Value, CasterId = attacker.Id, Amount = tfeGain }; } else { energyFromMe.Amount += tfeGain; energyFromMe.Timestamp = DateTime.UtcNow; repo.SaveTFEnergy(energyFromMe); } var totalAfter = energyFromMe.Amount + sharedEnergyAmt + mergeUpEnergyAmt; var totalBefore = totalAfter - tfeGain; return(totalBefore, totalAfter); }