diff --git a/ChaosBot/Discord/Services/ExperienceHandler.cs b/ChaosBot/Discord/Services/ExperienceHandler.cs new file mode 100644 index 0000000..9b74856 --- /dev/null +++ b/ChaosBot/Discord/Services/ExperienceHandler.cs @@ -0,0 +1,51 @@ +using System; +using System.Linq; +using System.Reflection; +using ChaosBot.Models; +using ChaosBot.Repositories; +using Microsoft.EntityFrameworkCore; +using NLog; + +namespace ChaosBot.Discord.Services +{ + public class ExperienceHandler + { + private static readonly ILogger _logger = Program.Logger; + + public static async void addXP(ulong DiscordGuildId, ulong DiscordUserId) + { + try + { + using (ChaosbotContext dbContext = new ChaosbotContext()) + { + IQueryable ctxUser = dbContext.ExperiencePoints; + IQueryable usrXp = ctxUser + .Where(p => p.DiscordGuildId.Equals(DiscordGuildId)) + .Where(p => p.DiscordUserId.Equals(DiscordUserId)); + + Experience usrNewXp; + if (usrXp.Any()) + { + usrNewXp = usrXp.First(); + usrNewXp.Amount = usrNewXp.Amount + Convert.ToUInt64(ConfigurationRepository.GetValue("Experience:PerMsg", DiscordGuildId, "0")); + } + else + { + usrNewXp = new Experience(); + usrNewXp.Amount = Convert.ToUInt64(ConfigurationRepository.GetValue("Experience:PerMsg", DiscordGuildId, "0")); + } + usrNewXp.DiscordGuildId = DiscordGuildId; + usrNewXp.DiscordUserId = DiscordUserId; + + await dbContext.ExperiencePoints.Upsert(usrNewXp) + .On(x => new { x.DiscordGuildId, x.DiscordUserId}).RunAsync(); + } + } + catch (Exception ex) + { + _logger.Error( + $"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + } + } +} \ No newline at end of file