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));
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();
// We don't want to update more than once every minute
if (DateTime.Now < usrNewXp.LastUpdated.AddMinutes(1)) return;
usrNewXp.Amount = usrNewXp.Amount + Convert.ToUInt64(new Random().Next(15, 26));
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;
usrNewXp = new Experience
{
Amount = 0,
DiscordGuildId = context.Guild.Id,
DiscordUserId = context.User.Id,
LastUpdated = DateTime.UnixEpoch,
Level = 0
};
await dbContext.ExperiencePoints.Upsert(usrNewXp)
.On(x => new {x.DiscordGuildId, x.DiscordUserId}).RunAsync();
}
else
{
usrNewXp = new Experience();
usrNewXp.Amount = Convert.ToUInt64(new Random().Next(15, 26));
usrNewXp.DiscordGuildId = context.Guild.Id;
usrNewXp.DiscordUserId = context.User.Id;
usrNewXp.LastUpdated = DateTime.Now;
usrNewXp.Level = 1;
await dbContext.ExperiencePoints.Upsert(usrNewXp)
.On(x => new { x.DiscordGuildId, x.DiscordUserId}).RunAsync();
string ConfigSet = ConfigurationRepository.GetValue<string>("LevelUp:Channel", context.Guild.Id, "false");
usrNewXp = usrXp.First();
}
// We want to throttle gaining experience
if (DateTime.Now < usrNewXp.LastUpdated.AddMinutes(1)) return;
usrNewXp.LastUpdated = DateTime.Now;
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)
.On(x => new {x.DiscordGuildId, x.DiscordUserId}).RunAsync();
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}>";
if (!ConfigurationRepository.GetValue<bool>("LevelUp:MentionUser", context.Guild.Id, true))
@ -75,16 +80,20 @@ namespace ChaosBot.Discord.Services
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 1 <:wot:740387232514572310>");
ISocketMessageChannel messageChannel;
if (channelToSendIn != "false")
{
ulong channelId = Convert.ToUInt64(channelToSendIn.Substring(2, channelToSendIn.Length - 3));
messageChannel = context.Guild.GetTextChannel(channelId);
}
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;
try
{
// var nextLevelXP = 1 * usrExperience.Level * (2 * usrExperience.Level * usrExperience.Level + 27 * usrExperience.Level + 91);
var nextLevelXP = 5 * usrExperience.Level ^ 3 + 95 * usrExperience.Level;
Console.WriteLine(nextLevelXP);
if (usrExperience.Amount > nextLevelXP)
{
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}]>.");
}
ulong curLevel = usrExperience.Level;
ulong curXP = usrExperience.Amount;
var nextLevelXP = 5 * usrExperience.Level ^ 3 + 95 * usrExperience.Level;
if (curXP > nextLevelXP)
return curLevel + 1;
return curLevel;
}
}