Skip to content

Commit

Permalink
Added new ExperienceConfig model
Browse files Browse the repository at this point in the history
  • Loading branch information
FeroxFoxxo committed Mar 4, 2024
1 parent e8e9fd6 commit 8bc3dc5
Show file tree
Hide file tree
Showing 12 changed files with 417 additions and 55 deletions.
8 changes: 4 additions & 4 deletions backend/Levels/Commands/Xp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@ public async Task RankCommand(

if (roleTarget != null)
{
guildlevelconfig.Levels.FirstOrDefault(e =>
found = guildlevelconfig.Levels.Any(e =>
{
if (e.Value.Contains(roleTarget.Id))
{
found = true;
roleTargetLevel = e.Key;
return true;
}
Expand Down Expand Up @@ -145,10 +144,11 @@ private static string LevelDataExpression(LevelType type, CalculatedGuildUserLev
private static string LevelTargetExpression(long currentXp, long targetXp, GuildLevelConfig config)
{
string textExpr;

try
{
var textTime = TimeSpan.FromMinutes((targetXp - currentXp) /
((config.MinimumTextXpGiven + config.MaximumTextXpGiven) >> 1));
((config.Experience.MinimumTextXpGiven + config.Experience.MaximumTextXpGiven) >> 1));
textExpr = textTime.Humanize(2, minUnit: TimeUnit.Minute);
}
catch
Expand All @@ -160,7 +160,7 @@ private static string LevelTargetExpression(long currentXp, long targetXp, Guild
try
{
var voiceTime = TimeSpan.FromMinutes((targetXp - currentXp) /
((config.MinimumVoiceXpGiven + config.MaximumVoiceXpGiven) >> 1));
((config.Experience.MinimumVoiceXpGiven + config.Experience.MaximumVoiceXpGiven) >> 1));
voiceExpr = voiceTime.Humanize(2, minUnit: TimeUnit.Minute);
}
catch
Expand Down
24 changes: 14 additions & 10 deletions backend/Levels/Controllers/LevelsConfigController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Bot.Services;
using Discord;
using Levels.Data;
using Levels.DTOs;
using Levels.Models;
using Microsoft.AspNetCore.Mvc;

Expand Down Expand Up @@ -33,7 +34,7 @@ public async Task<IActionResult> GetConfig([FromRoute] ulong guildId)
}

[HttpPut]
public async Task<IActionResult> PutConfig([FromRoute] ulong guildId, [FromBody] GuildLevelConfig config)
public async Task<IActionResult> PutConfig([FromRoute] ulong guildId, [FromBody] GuildLevelConfigDto config)
{
var identity = await SetupAuthentication();

Expand All @@ -54,17 +55,22 @@ public async Task<IActionResult> PutConfig([FromRoute] ulong guildId, [FromBody]
"The IDs supplied in the request route and in the object in the request body do not match.");

var validationErrors = ValidateConfig(config);

if (validationErrors is not null)
return validationErrors;

var existing = await _levelsConfigRepository.GetOrCreateConfig(guildId);

existing.Coefficients = config.Coefficients;
existing.XpInterval = config.XpInterval;
existing.MinimumTextXpGiven = config.MinimumTextXpGiven;
existing.MaximumTextXpGiven = config.MaximumTextXpGiven;
existing.MinimumVoiceXpGiven = config.MinimumVoiceXpGiven;
existing.MaximumVoiceXpGiven = config.MaximumVoiceXpGiven;

existing.Experience = new ExperienceConfig() {
MaximumTextXpGiven = config.MaximumTextXpGiven,
MaximumVoiceXpGiven = config.MaximumVoiceXpGiven,
MinimumTextXpGiven = config.MinimumTextXpGiven,
MinimumVoiceXpGiven = config.MinimumVoiceXpGiven
};

existing.VoiceXpRequiredMembers = config.VoiceXpRequiredMembers;
existing.VoiceXpCountMutedMembers = config.VoiceXpCountMutedMembers;

Expand All @@ -84,11 +90,11 @@ public async Task<IActionResult> PutConfig([FromRoute] ulong guildId, [FromBody]
existing.Levels = config.Levels;
existing.LevelUpMessageOverrides = config.LevelUpMessageOverrides;

await _levelsConfigRepository.UpdateConfig(config);
await _levelsConfigRepository.UpdateConfig(existing);
return Ok();
}

private IActionResult ValidateConfig(GuildLevelConfig config)
private BadRequestObjectResult ValidateConfig(GuildLevelConfigDto config)
{
if (config.Coefficients.Length is < 2 or > 10)
return BadRequest("Leveling coefficients must have between 2 and 10 elements!");
Expand All @@ -112,19 +118,17 @@ private IActionResult ValidateConfig(GuildLevelConfig config)
{
var found = false;
foreach (var roleIds in config.Levels.Values)
{
if (roleIds.Contains(config.NicknameDisabledReplacement))
{
found = true;
break;
}
}

if (!found) return BadRequest("Nickname Disabled Replacement must be a level role.");
}

return string.IsNullOrWhiteSpace(config.LevelUpTemplate)
? BadRequest("Level Up Template must not be empty.")
: config.LevelUpTemplate.Length > 250 ? BadRequest("The Length of Level Up Template may not exceed 200 characters.") : (IActionResult)null;
: config.LevelUpTemplate.Length > 250 ? BadRequest("The Length of Level Up Template may not exceed 200 characters.") : null;
}
}
6 changes: 3 additions & 3 deletions backend/Levels/Controllers/LevelsImageServerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ public IActionResult GetDefaultImage([FromRoute] string fileName)
return SendImage(fileInfo);
}

private IActionResult SendImage(UploadedFile fileInfo)
private FileContentResult SendImage(UploadedFile fileInfo)
{
HttpContext.Response.Headers.Add("Content-Disposition", fileInfo.ContentDisposition.ToString());
HttpContext.Response.Headers.Add("Content-Type", fileInfo.ContentType);
HttpContext.Response.Headers.ContentType = fileInfo.ContentType;
HttpContext.Response.Headers.ContentDisposition = fileInfo.ContentDisposition.ToString();

return File(fileInfo.FileContent, fileInfo.ContentType);
}
Expand Down
32 changes: 32 additions & 0 deletions backend/Levels/DTOs/GuildLevelConfigDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Discord;
using System.ComponentModel.DataAnnotations;

namespace Levels.DTOs;

public class GuildLevelConfigDto
{
public ulong Id { get; set; }

public float[] Coefficients { get; set; }
public int XpInterval { get; set; }
public int MinimumTextXpGiven { get; set; }
public int MaximumTextXpGiven { get; set; }
public int MinimumVoiceXpGiven { get; set; }
public int MaximumVoiceXpGiven { get; set; }
public string LevelUpTemplate { get; set; }
public ulong VoiceLevelUpChannel { get; set; }
public ulong TextLevelUpChannel { get; set; }

public ulong[] DisabledXpChannels { get; set; }
public bool HandleRoles { get; set; }
public bool SendTextLevelUps { get; set; }
public bool SendVoiceLevelUps { get; set; }
public bool VoiceXpCountMutedMembers { get; set; }
public int VoiceXpRequiredMembers { get; set; }

public ulong NicknameDisabledRole { get; set; }
public ulong NicknameDisabledReplacement { get; set; }

public Dictionary<int, ulong[]> Levels { get; set; }
public Dictionary<int, string> LevelUpMessageOverrides { get; set; }
}
14 changes: 13 additions & 1 deletion backend/Levels/Data/LevelsDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ public override void OverrideModelCreating(ModelBuilder modelBuilder)
.Property(e => e.LevelUpMessageOverrides)
.HasConversion(new DictionaryDataConverter<int, string>(),
new DictionaryDataComparer<int, string>());

modelBuilder
.Entity<GuildLevelConfig>()
.Property(e => e.Experience)
.HasConversion(new JsonDataConverter<ExperienceConfig>(),
new JsonDataComparer<ExperienceConfig>());

modelBuilder
.Entity<GuildLevelConfig>()
.Property(e => e.ExperienceOverrides)
.HasConversion(new DictionaryDataConverter<ulong, ExperienceConfig>(),
new DictionaryDataComparer<ulong, ExperienceConfig>());
}

private static bool CheckNullAndReport([NotNullWhen(false)] object o, string name)
Expand Down Expand Up @@ -145,7 +157,7 @@ public async Task UpdateGuildLevelConfig()
{
if (CheckNullAndReport(GuildLevelConfigs, "GuildLevelsConfigs"))
return;
//GuildLevelConfigs.Update(guildLevelConfig);

await SaveChangesAsync();
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8bc3dc5

Please sign in to comment.