public async Task UpdatePercentAsync(ulong userId, uint percent) { await EnsureExistsAsync(userId); var oldToxicity = await GetAsync(userId); await using var context = new RiftContext(); var toxicity = new RiftToxicity { UserId = userId, Percent = percent }; context.Entry(toxicity).Property(x => x.Percent).IsModified = true; if (percent > oldToxicity.Percent) { toxicity.LastIncreased = DateTime.UtcNow; context.Entry(toxicity).Property(x => x.LastIncreased).IsModified = true; } else if (percent < oldToxicity.Percent) { toxicity.LastDecreased = DateTime.UtcNow; context.Entry(toxicity).Property(x => x.LastDecreased).IsModified = true; } await context.SaveChangesAsync(); }
static async Task <bool> EnsureExistsAsync(ulong userId) { if (!await DB.Users.EnsureExistsAsync(userId)) { throw new DatabaseException(nameof(EnsureExistsAsync)); } await using var context = new RiftContext(); if (await context.Toxicity .AsQueryable() .AnyAsync(x => x.UserId == userId)) { return(true); } try { var entry = new RiftToxicity { UserId = userId, LastIncreased = DateTime.MinValue, }; await context.Toxicity.AddAsync(entry); await context.SaveChangesAsync(); return(true); } catch (Exception ex) { RiftBot.Log.Error(ex, $"Failed to check {nameof(EnsureExistsAsync)} for user {userId.ToString()}."); return(false); } }
static async Task ReduceToxicityAsync(RiftToxicity toxicity) { var percent = toxicity.Percent > Settings.Economy.ToxicityWeeklyDropRate ? toxicity.Percent - Settings.Economy.ToxicityWeeklyDropRate : 0u; RiftBot.Log.Warning($"Reduced {toxicity.UserId.ToString()}'s toxicity to {percent.ToString()}% (level {toxicity.Level.ToString()})."); await DB.Toxicity.UpdatePercentAsync(toxicity.UserId, percent); }
static async Task <(RiftToxicity, RiftToxicity)> GetNewToxicityAsync(ulong userId, ToxicitySource source) { var currentToxicity = await DB.Toxicity.GetAsync(userId) ?? new RiftToxicity { UserId = userId, Percent = 0u, }; var newToxicity = new RiftToxicity { UserId = userId, }; uint addend; switch (source) { case ToxicitySource.Warn: addend = 1u; break; case ToxicitySource.Mute: addend = 5u; break; case ToxicitySource.Kick: addend = 20u; break; case ToxicitySource.Ban: addend = 50u; break; default: addend = 0u; break; } newToxicity.Percent = Math.Min(currentToxicity.Percent + addend, 100u); return(currentToxicity, newToxicity); }