Refactor the levelup code to a more sensible order of operations
This commit is contained in:
parent
ad0ea126e8
commit
8bdacb5328
@ -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 = 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 = usrXp.First();
|
||||
}
|
||||
|
||||
// We don't want to update more than once every minute
|
||||
// We want to throttle gaining experience
|
||||
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);
|
||||
usrNewXp.Amount += Convert.ToUInt64(new Random().Next(15, 26));
|
||||
|
||||
if(newLevel > usrNewXp.Level)
|
||||
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();
|
||||
}
|
||||
else
|
||||
|
||||
if (newLevel > oldLevel)
|
||||
{
|
||||
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");
|
||||
// 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))
|
||||
@ -76,15 +81,19 @@ namespace ChaosBot.Discord.Services
|
||||
}
|
||||
}
|
||||
|
||||
if (ConfigSet != "false")
|
||||
ISocketMessageChannel messageChannel;
|
||||
if (channelToSendIn != "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>");
|
||||
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);
|
||||
ulong curLevel = usrExperience.Level;
|
||||
ulong curXP = usrExperience.Amount;
|
||||
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}]>.");
|
||||
}
|
||||
|
||||
if (curXP > nextLevelXP)
|
||||
return curLevel + 1;
|
||||
return curLevel;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user