Creating Experience Handler code to add XP based on Configuration setting.
This commit is contained in:
parent
56edd20562
commit
0ebcfdb605
51
ChaosBot/Discord/Services/ExperienceHandler.cs
Normal file
51
ChaosBot/Discord/Services/ExperienceHandler.cs
Normal file
@ -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<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}]>.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user