51 lines
1.9 KiB
C#
51 lines
1.9 KiB
C#
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<Experience> ctxUser = dbContext.ExperiencePoints;
|
|
IQueryable<Experience> 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<string>("Experience:PerMsg", DiscordGuildId, "0"));
|
|
}
|
|
else
|
|
{
|
|
usrNewXp = new Experience();
|
|
usrNewXp.Amount = Convert.ToUInt64(ConfigurationRepository.GetValue<string>("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}]>.");
|
|
}
|
|
}
|
|
}
|
|
} |