Refactor the levelup code to a more sensible order of operations

This commit is contained in:
Daniel_I_Am 2020-08-11 03:57:38 +02:00
parent ad0ea126e8
commit 8bdacb5328
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84

View File

@ -29,42 +29,47 @@ namespace ChaosBot.Discord.Services
.Where(p => p.DiscordUserId.Equals(context.User.Id)); .Where(p => p.DiscordUserId.Equals(context.User.Id));
Experience usrNewXp; Experience usrNewXp;
if (usrXp.Any())
// Ensure there's an entry in the database, even if this is the first message ever sent
if (!usrXp.Any())
{ {
usrNewXp = usrXp.First(); usrNewXp = new Experience
{
// We don't want to update more than once every minute Amount = 0,
if (DateTime.Now < usrNewXp.LastUpdated.AddMinutes(1)) return; DiscordGuildId = context.Guild.Id,
DiscordUserId = context.User.Id,
usrNewXp.Amount = usrNewXp.Amount + Convert.ToUInt64(new Random().Next(15, 26)); LastUpdated = DateTime.UnixEpoch,
Level = 0
usrNewXp.DiscordGuildId = context.Guild.Id; };
usrNewXp.DiscordUserId = context.User.Id;
usrNewXp.LastUpdated = DateTime.Now;
usrNewXp.Level = usrNewXp.Level;
ulong newLevel = await checkLevel(usrNewXp, context);
if(newLevel > usrNewXp.Level)
usrNewXp.Level = newLevel;
await dbContext.ExperiencePoints.Upsert(usrNewXp) await dbContext.ExperiencePoints.Upsert(usrNewXp)
.On(x => new {x.DiscordGuildId, x.DiscordUserId}).RunAsync(); .On(x => new {x.DiscordGuildId, x.DiscordUserId}).RunAsync();
} }
else else
{ {
usrNewXp = new Experience(); usrNewXp = usrXp.First();
usrNewXp.Amount = Convert.ToUInt64(new Random().Next(15, 26)); }
usrNewXp.DiscordGuildId = context.Guild.Id; // We want to throttle gaining experience
usrNewXp.DiscordUserId = context.User.Id; if (DateTime.Now < usrNewXp.LastUpdated.AddMinutes(1)) return;
usrNewXp.LastUpdated = DateTime.Now; usrNewXp.LastUpdated = DateTime.Now;
usrNewXp.Level = 1;
usrNewXp.Amount += Convert.ToUInt64(new Random().Next(15, 26));
ulong oldLevel = usrNewXp.Level;
ulong newLevel = await CheckLevel(usrNewXp, context);
if (newLevel > oldLevel)
usrNewXp.Level = newLevel;
await dbContext.ExperiencePoints.Upsert(usrNewXp) await dbContext.ExperiencePoints.Upsert(usrNewXp)
.On(x => new { x.DiscordGuildId, x.DiscordUserId}).RunAsync(); .On(x => new {x.DiscordGuildId, x.DiscordUserId}).RunAsync();
string ConfigSet = ConfigurationRepository.GetValue<string>("LevelUp:Channel", context.Guild.Id, "false"); if (newLevel > oldLevel)
{
// The user has leveled up, we can send a message
string channelToSendIn =
ConfigurationRepository.GetValue<string>("LevelUp:Channel", context.Guild.Id, "false");
string mentionString = $"<@{context.User.Id}>"; string mentionString = $"<@{context.User.Id}>";
if (!ConfigurationRepository.GetValue<bool>("LevelUp:MentionUser", context.Guild.Id, true)) if (!ConfigurationRepository.GetValue<bool>("LevelUp:MentionUser", context.Guild.Id, true))
@ -76,15 +81,19 @@ namespace ChaosBot.Discord.Services
} }
} }
if (ConfigSet != "false") ISocketMessageChannel messageChannel;
if (channelToSendIn != "false")
{ {
ulong channelId = Convert.ToUInt64(channelToSendIn.Substring(2, channelToSendIn.Length - 3));
ulong channelId = Convert.ToUInt64(ConfigSet.Substring(2, ConfigSet.Length - 3)); messageChannel = context.Guild.GetTextChannel(channelId);
await context.Guild.GetTextChannel(channelId).SendMessageAsync(
$"Grats {mentionString}! You have reached level 1 <:wot:740387232514572310>");
} }
else else
await context.Channel.SendMessageAsync($"Grats {mentionString}! You have reached level 1! <:wot:740387232514572310>"); {
messageChannel = context.Channel;
}
await messageChannel.SendMessageAsync(
$"Grats {mentionString}! You have reached level {newLevel}! <:wot:740387232514572310>");
} }
} }
} }
@ -95,47 +104,14 @@ namespace ChaosBot.Discord.Services
} }
} }
public static async Task<ulong> checkLevel(Experience usrExperience, SocketCommandContext context) public static async Task<ulong> CheckLevel(Experience usrExperience, SocketCommandContext context)
{ {
ulong curLevel = 1; ulong curLevel = usrExperience.Level;
ulong curXP = usrExperience.Amount;
try
{
// var nextLevelXP = 1 * usrExperience.Level * (2 * usrExperience.Level * usrExperience.Level + 27 * usrExperience.Level + 91);
var nextLevelXP = 5 * usrExperience.Level ^ 3 + 95 * usrExperience.Level; var nextLevelXP = 5 * usrExperience.Level ^ 3 + 95 * usrExperience.Level;
Console.WriteLine(nextLevelXP); if (curXP > nextLevelXP)
if (usrExperience.Amount > nextLevelXP) return curLevel + 1;
{
curLevel = usrExperience.Level + 1;
string ConfigSet = ConfigurationRepository.GetValue<string>("LevelUp:Channel", usrExperience.DiscordGuildId, "false");
string mentionString = $"<@{context.User.Id}>";
if (!ConfigurationRepository.GetValue<bool>("LevelUp:MentionUser", context.Guild.Id, true))
{
mentionString = context.User.Username;
if (context.User is IGuildUser guildUser)
{
mentionString = guildUser.Nickname ?? mentionString;
}
}
if (ConfigSet != "false")
{
ulong channelId = Convert.ToUInt64(ConfigSet.Substring(2, ConfigSet.Length - 3));
await context.Guild.GetTextChannel(channelId).SendMessageAsync(
$"Grats {mentionString}! You have reached level {curLevel} <:wot:740387232514572310>");
}
else
await context.Channel.SendMessageAsync($"Grats {mentionString}! You have reached level {curLevel} <:wot:740387232514572310>");
}
}
catch (Exception ex)
{
_logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
}
return curLevel; return curLevel;
} }
} }