Updated Xp module

This commit is contained in:
Toastie (DCS Team) 2024-07-15 15:45:33 +12:00
parent b2b8e4c3d3
commit 624171c35f
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
3 changed files with 53 additions and 37 deletions

View file

@ -113,16 +113,18 @@ public partial class Xp
{ {
var lvl = new LevelStats(club.Xp); var lvl = new LevelStats(club.Xp);
var allUsers = club.Members.OrderByDescending(x => var allUsers = club.Members.OrderByDescending(x =>
{ {
var l = new LevelStats(x.TotalXp).Level; var l = new LevelStats(x.TotalXp).Level;
if (club.OwnerId == x.Id) if (club.OwnerId == x.Id)
return int.MaxValue; return int.MaxValue;
if (x.IsClubAdmin) if (x.IsClubAdmin)
return (int.MaxValue / 2) + l; return (int.MaxValue / 2) + l;
return l; return l;
}) })
.ToList(); .ToList();
var rank = await _service.GetClubRankAsync(club.Id);
await Response() await Response()
.Paginated() .Paginated()
.Items(allUsers) .Items(allUsers)
@ -135,6 +137,7 @@ public partial class Xp
.WithDescription(GetText(strs.level_x(lvl.Level + $" ({club.Xp} xp)"))) .WithDescription(GetText(strs.level_x(lvl.Level + $" ({club.Xp} xp)")))
.AddField(GetText(strs.desc), .AddField(GetText(strs.desc),
string.IsNullOrWhiteSpace(club.Description) ? "-" : club.Description) string.IsNullOrWhiteSpace(club.Description) ? "-" : club.Description)
.AddField(GetText(strs.rank), $"#{rank}", true)
.AddField(GetText(strs.owner), club.Owner.ToString(), true) .AddField(GetText(strs.owner), club.Owner.ToString(), true)
// .AddField(GetText(strs.level_req), club.MinimumLevelReq.ToString(), true) // .AddField(GetText(strs.level_req), club.MinimumLevelReq.ToString(), true)
.AddField(GetText(strs.members), .AddField(GetText(strs.members),

View file

@ -23,22 +23,22 @@ public class ClubService : IEService, IClubService
{ {
if (!CheckClubName(clubName)) if (!CheckClubName(clubName))
return ClubCreateResult.NameTooLong; return ClubCreateResult.NameTooLong;
//must be lvl 5 and must not be in a club already //must be lvl 5 and must not be in a club already
await using var uow = _db.GetDbContext(); await using var uow = _db.GetDbContext();
var du = uow.GetOrCreateUser(user); var du = uow.GetOrCreateUser(user);
var xp = new LevelStats(du.TotalXp); var xp = new LevelStats(du.TotalXp);
if (xp.Level < 5) if (xp.Level < 5)
return ClubCreateResult.InsufficientLevel; return ClubCreateResult.InsufficientLevel;
if (du.ClubId is not null) if (du.ClubId is not null)
return ClubCreateResult.AlreadyInAClub; return ClubCreateResult.AlreadyInAClub;
if (await uow.Set<ClubInfo>().AnyAsyncEF(x => x.Name == clubName)) if (await uow.Set<ClubInfo>().AnyAsyncEF(x => x.Name == clubName))
return ClubCreateResult.NameTaken; return ClubCreateResult.NameTaken;
du.IsClubAdmin = true; du.IsClubAdmin = true;
du.Club = new() du.Club = new()
{ {
@ -53,7 +53,7 @@ public class ClubService : IEService, IClubService
return ClubCreateResult.Success; return ClubCreateResult.Success;
} }
public OneOf<ClubInfo, ClubTransferError> TransferClub(IUser from, IUser newOwner) public OneOf<ClubInfo, ClubTransferError> TransferClub(IUser from, IUser newOwner)
{ {
using var uow = _db.GetDbContext(); using var uow = _db.GetDbContext();
@ -62,7 +62,7 @@ public class ClubService : IEService, IClubService
if (club is null || club.Owner.UserId != from.Id) if (club is null || club.Owner.UserId != from.Id)
return ClubTransferError.NotOwner; return ClubTransferError.NotOwner;
if (!club.Members.Contains(newOwnerUser)) if (!club.Members.Contains(newOwnerUser))
return ClubTransferError.TargetNotMember; return ClubTransferError.TargetNotMember;
@ -72,22 +72,22 @@ public class ClubService : IEService, IClubService
uow.SaveChanges(); uow.SaveChanges();
return club; return club;
} }
public async Task<ToggleAdminResult> ToggleAdminAsync(IUser owner, IUser toAdmin) public async Task<ToggleAdminResult> ToggleAdminAsync(IUser owner, IUser toAdmin)
{ {
if (owner.Id == toAdmin.Id) if (owner.Id == toAdmin.Id)
return ToggleAdminResult.CantTargetThyself; return ToggleAdminResult.CantTargetThyself;
await using var uow = _db.GetDbContext(); await using var uow = _db.GetDbContext();
var club = uow.Set<ClubInfo>().GetByOwner(owner.Id); var club = uow.Set<ClubInfo>().GetByOwner(owner.Id);
var adminUser = uow.GetOrCreateUser(toAdmin); var adminUser = uow.GetOrCreateUser(toAdmin);
if (club is null) if (club is null)
return ToggleAdminResult.NotOwner; return ToggleAdminResult.NotOwner;
if(!club.Members.Contains(adminUser)) if (!club.Members.Contains(adminUser))
return ToggleAdminResult.TargetNotMember; return ToggleAdminResult.TargetNotMember;
var newState = adminUser.IsClubAdmin = !adminUser.IsClubAdmin; var newState = adminUser.IsClubAdmin = !adminUser.IsClubAdmin;
await uow.SaveChangesAsync(); await uow.SaveChangesAsync();
return newState ? ToggleAdminResult.AddedAdmin : ToggleAdminResult.RemovedAdmin; return newState ? ToggleAdminResult.AddedAdmin : ToggleAdminResult.RemovedAdmin;
@ -99,17 +99,17 @@ public class ClubService : IEService, IClubService
var member = uow.Set<ClubInfo>().GetByMember(user.Id); var member = uow.Set<ClubInfo>().GetByMember(user.Id);
return member; return member;
} }
public async Task<SetClubIconResult> SetClubIconAsync(ulong ownerUserId, string url) public async Task<SetClubIconResult> SetClubIconAsync(ulong ownerUserId, string url)
{ {
if (url is not null) if (url is not null)
{ {
using var http = _httpFactory.CreateClient(); using var http = _httpFactory.CreateClient();
using var temp = await http.GetAsync(url, HttpCompletionOption.ResponseHeadersRead); using var temp = await http.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
if (!temp.IsImage()) if (!temp.IsImage())
return SetClubIconResult.InvalidFileType; return SetClubIconResult.InvalidFileType;
if (temp.GetContentLength() > 5.Megabytes()) if (temp.GetContentLength() > 5.Megabytes())
return SetClubIconResult.TooLarge; return SetClubIconResult.TooLarge;
} }
@ -134,6 +134,18 @@ public class ClubService : IEService, IClubService
return club is not null; return club is not null;
} }
public async Task<int> GetClubRankAsync(int clubId)
{
await using var uow = _db.GetDbContext();
var rank = await uow.Clubs
.ToLinqToDBTable()
.Where(x => x.Xp > (uow.Clubs.First(c => c.Id == clubId).Xp))
.CountAsyncLinqToDB();
return rank + 1;
}
public ClubApplyResult ApplyToClub(IUser user, ClubInfo club) public ClubApplyResult ApplyToClub(IUser user, ClubInfo club)
{ {
using var uow = _db.GetDbContext(); using var uow = _db.GetDbContext();
@ -144,10 +156,10 @@ public class ClubService : IEService, IClubService
// or doesn't min minumum level requirement, can't apply // or doesn't min minumum level requirement, can't apply
if (du.ClubId is not null) if (du.ClubId is not null)
return ClubApplyResult.AlreadyInAClub; return ClubApplyResult.AlreadyInAClub;
if (club.Bans.Any(x => x.UserId == du.Id)) if (club.Bans.Any(x => x.UserId == du.Id))
return ClubApplyResult.Banned; return ClubApplyResult.Banned;
if (club.Applicants.Any(x => x.UserId == du.Id)) if (club.Applicants.Any(x => x.UserId == du.Id))
return ClubApplyResult.AlreadyApplied; return ClubApplyResult.AlreadyApplied;
@ -162,7 +174,7 @@ public class ClubService : IEService, IClubService
return ClubApplyResult.Success; return ClubApplyResult.Success;
} }
public ClubAcceptResult AcceptApplication(ulong clubOwnerUserId, string userName, out DiscordUser discordUser) public ClubAcceptResult AcceptApplication(ulong clubOwnerUserId, string userName, out DiscordUser discordUser)
{ {
discordUser = null; discordUser = null;
@ -188,7 +200,7 @@ public class ClubService : IEService, IClubService
uow.SaveChanges(); uow.SaveChanges();
return ClubAcceptResult.Accepted; return ClubAcceptResult.Accepted;
} }
public ClubDenyResult RejectApplication(ulong clubOwnerUserId, string userName, out DiscordUser discordUser) public ClubDenyResult RejectApplication(ulong clubOwnerUserId, string userName, out DiscordUser discordUser)
{ {
discordUser = null; discordUser = null;
@ -201,9 +213,9 @@ public class ClubService : IEService, IClubService
club.Applicants.FirstOrDefault(x => x.User.ToString().ToUpperInvariant() == userName.ToUpperInvariant()); club.Applicants.FirstOrDefault(x => x.User.ToString().ToUpperInvariant() == userName.ToUpperInvariant());
if (applicant is null) if (applicant is null)
return ClubDenyResult.NoSuchApplicant; return ClubDenyResult.NoSuchApplicant;
club.Applicants.Remove(applicant); club.Applicants.Remove(applicant);
discordUser = applicant.User; discordUser = applicant.User;
uow.SaveChanges(); uow.SaveChanges();
return ClubDenyResult.Rejected; return ClubDenyResult.Rejected;
@ -220,7 +232,7 @@ public class ClubService : IEService, IClubService
using var uow = _db.GetDbContext(); using var uow = _db.GetDbContext();
var du = uow.GetOrCreateUser(user, x => x.Include(u => u.Club)); var du = uow.GetOrCreateUser(user, x => x.Include(u => u.Club));
if (du.Club is null) if (du.Club is null)
return ClubLeaveResult.NotInAClub; return ClubLeaveResult.NotInAClub;
if (du.Club.OwnerId == du.Id) if (du.Club.OwnerId == du.Id)
return ClubLeaveResult.OwnerCantLeave; return ClubLeaveResult.OwnerCantLeave;
@ -306,7 +318,7 @@ public class ClubService : IEService, IClubService
return ClubUnbanResult.Success; return ClubUnbanResult.Success;
} }
public ClubKickResult Kick(ulong kickerId, string userName, out ClubInfo club) public ClubKickResult Kick(ulong kickerId, string userName, out ClubInfo club)
{ {
using var uow = _db.GetDbContext(); using var uow = _db.GetDbContext();
@ -342,14 +354,14 @@ public class ClubService : IEService, IClubService
{ {
if (!CheckClubName(clubName)) if (!CheckClubName(clubName))
return ClubRenameResult.NameTooLong; return ClubRenameResult.NameTooLong;
await using var uow = _db.GetDbContext(); await using var uow = _db.GetDbContext();
var club = uow.Set<ClubInfo>().GetByOwnerOrAdmin(userId); var club = uow.Set<ClubInfo>().GetByOwnerOrAdmin(userId);
if (club is null) if (club is null)
return ClubRenameResult.NotOwnerOrAdmin; return ClubRenameResult.NotOwnerOrAdmin;
if (await uow.Set<ClubInfo>().AnyAsyncEF(x => x.Name == clubName)) if (await uow.Set<ClubInfo>().AnyAsyncEF(x => x.Name == clubName))
return ClubRenameResult.NameTaken; return ClubRenameResult.NameTaken;

View file

@ -6,7 +6,7 @@ namespace EllieBot.Modules.Xp.Services;
public interface IClubService public interface IClubService
{ {
Task<ClubCreateResult> CreateClubAsync(IUser user, string clubName); Task<ClubCreateResult> CreateClubAsync(IUser user, string clubName);
OneOf<ClubInfo,ClubTransferError> TransferClub(IUser from, IUser newOwner); OneOf<ClubInfo, ClubTransferError> TransferClub(IUser from, IUser newOwner);
Task<ToggleAdminResult> ToggleAdminAsync(IUser owner, IUser toAdmin); Task<ToggleAdminResult> ToggleAdminAsync(IUser owner, IUser toAdmin);
ClubInfo? GetClubByMember(IUser user); ClubInfo? GetClubByMember(IUser user);
Task<SetClubIconResult> SetClubIconAsync(ulong ownerUserId, string? url); Task<SetClubIconResult> SetClubIconAsync(ulong ownerUserId, string? url);
@ -23,6 +23,7 @@ public interface IClubService
ClubKickResult Kick(ulong kickerId, string userName, out ClubInfo club); ClubKickResult Kick(ulong kickerId, string userName, out ClubInfo club);
List<ClubInfo> GetClubLeaderboardPage(int page); List<ClubInfo> GetClubLeaderboardPage(int page);
Task<ClubRenameResult> RenameClubAsync(ulong userId, string clubName); Task<ClubRenameResult> RenameClubAsync(ulong userId, string clubName);
Task<int> GetClubRankAsync(int clubId);
} }
public enum ClubApplyResult public enum ClubApplyResult