From e7d10caf4ea87473e639fa6656207ec6da6a21e6 Mon Sep 17 00:00:00 2001 From: Sean Stoves Date: Sat, 8 Aug 2020 23:54:27 -0400 Subject: [PATCH] Sending Channel to parsers for Reply on level Up, Adding Leveling Formula --- ChaosBot/Discord/Services/CommandHandler.cs | 6 ++-- .../Discord/Services/ExperienceHandler.cs | 28 ++++++++++++++++++- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/ChaosBot/Discord/Services/CommandHandler.cs b/ChaosBot/Discord/Services/CommandHandler.cs index d00c5a3..d5b292b 100644 --- a/ChaosBot/Discord/Services/CommandHandler.cs +++ b/ChaosBot/Discord/Services/CommandHandler.cs @@ -59,12 +59,12 @@ namespace ChaosBot.Discord.Services if (!(message.HasMentionPrefix(_client.CurrentUser, ref argPos) || message.HasStringPrefix(prefix, ref argPos))) { - ExperienceHandler.addXP(context.Guild.Id, context.User.Id); + ExperienceHandler.addXP(context.Guild.Id, context.User.Id, context.Channel); return; } - + if(Convert.ToBoolean(ConfigurationRepository.GetValue("Experience:Commands", context.Guild.Id, "false"))) - ExperienceHandler.addXP(context.Guild.Id, context.User.Id); + ExperienceHandler.addXP(context.Guild.Id, context.User.Id, context.Channel); await _commands.ExecuteAsync(context, argPos, _services); } diff --git a/ChaosBot/Discord/Services/ExperienceHandler.cs b/ChaosBot/Discord/Services/ExperienceHandler.cs index 44d913b..941f53b 100644 --- a/ChaosBot/Discord/Services/ExperienceHandler.cs +++ b/ChaosBot/Discord/Services/ExperienceHandler.cs @@ -1,8 +1,10 @@ using System; using System.Linq; using System.Reflection; +using System.Threading.Tasks; using ChaosBot.Models; using ChaosBot.Repositories; +using Discord.WebSocket; using Microsoft.EntityFrameworkCore; using NLog; @@ -12,7 +14,7 @@ namespace ChaosBot.Discord.Services { private static readonly ILogger _logger = Program.Logger; - public static async void addXP(ulong DiscordGuildId, ulong DiscordUserId) + public static async void addXP(ulong DiscordGuildId, ulong DiscordUserId, ISocketMessageChannel Channel) { try { @@ -35,6 +37,7 @@ namespace ChaosBot.Discord.Services usrNewXp.DiscordGuildId = DiscordGuildId; usrNewXp.DiscordUserId = DiscordUserId; usrNewXp.LastUpdated = DateTime.Now; + usrNewXp.Level = await checkLevel(usrNewXp, Channel); await dbContext.ExperiencePoints.Upsert(usrNewXp) .On(x => new {x.DiscordGuildId, x.DiscordUserId}).RunAsync(); @@ -59,5 +62,28 @@ namespace ChaosBot.Discord.Services $"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>."); } } + + public static async Task checkLevel(Experience usrExperience, ISocketMessageChannel Channel) + { + ulong curLevel = 0; + + try + { + ulong nextLevelXP = 6 * (usrExperience.Level / 2) + 48 * usrExperience.Level + 123; + + if (usrExperience.Amount >= nextLevelXP) + { + curLevel = usrExperience.Level + 1; + await Channel.SendMessageAsync($"Congratulations <@{usrExperience.DiscordUserId}>, You have reached {curLevel}. Congratulations!"); + } + } + catch (Exception ex) + { + _logger.Error( + $"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + + return curLevel; + } } } \ No newline at end of file