fixed Add external user command

This commit is contained in:
Sarah Faey 2022-11-15 22:11:21 +01:00
parent a504925752
commit f40903851e
9 changed files with 122 additions and 39 deletions

View file

@ -65,6 +65,7 @@ namespace DiscordBot.CommandHandlers
roleId = roleId roleId = roleId
}; };
await _httpService.SignUp(signUpExternal); await _httpService.SignUp(signUpExternal);
await modal.RespondAsync($"signed up {userName}", ephemeral: true);
break; break;
} }
} }

View file

@ -97,6 +97,12 @@ namespace DiscordBot.CommandHandlers
List<ApiRole> roles = await _httpService.GetRoles(raidId, user.Id); List<ApiRole> roles = await _httpService.GetRoles(raidId, user.Id);
if(await _httpService.DoesUserExist(user.Id)) if(await _httpService.DoesUserExist(user.Id))
{ {
Tuple<bool, string> signUpAllowed = await _httpService.IsSignUpAllowed(raidId, user.Id, true);
if(!signUpAllowed.Item1)
{
await command.RespondAsync(signUpAllowed.Item2, ephemeral: true);
return;
}
await command.RespondAsync("Please choose a role.", components: SignUpMessage.buildMessage(roles, raidId, Constants.ComponentIds.SIGN_UP_BUTTON, false, user.Id) , ephemeral: true); await command.RespondAsync("Please choose a role.", components: SignUpMessage.buildMessage(roles, raidId, Constants.ComponentIds.SIGN_UP_BUTTON, false, user.Id) , ephemeral: true);
} }
else else
@ -117,6 +123,13 @@ namespace DiscordBot.CommandHandlers
private async Task SignUpExternalUser(SocketSlashCommand command, int raidId, List<ApiRole> roles) private async Task SignUpExternalUser(SocketSlashCommand command, int raidId, List<ApiRole> roles)
{ {
Tuple<bool, string> signUpAllowed = await _httpService.IsExternalSignUpAllowed(raidId);
if(!signUpAllowed.Item1)
{
await command.RespondAsync(signUpAllowed.Item2, ephemeral: true);
return;
}
var signUpSelect = new SelectMenuBuilder() var signUpSelect = new SelectMenuBuilder()
.WithPlaceholder("Select an option") .WithPlaceholder("Select an option")
.WithCustomId($"{Constants.ComponentIds.SIGN_UP_EXTERNAL_DROP_DOWN}-{raidId}") .WithCustomId($"{Constants.ComponentIds.SIGN_UP_EXTERNAL_DROP_DOWN}-{raidId}")

View file

@ -37,11 +37,26 @@ namespace DiscordBot.Services
return false; return false;
} }
public async Task<Tuple<bool, string>> IsSignUpAllowed(int raidId, ulong userId) public async Task<Tuple<bool, string>> IsSignUpAllowed(int raidId, ulong userId, bool ignoreRole = false)
{ {
var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME); var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME);
var httpResponseMessage = await httpClient.GetAsync($"DiscordBot/IsSignUpAllowed/{raidId}/{userId}"); var httpResponseMessage = await httpClient.GetAsync($"DiscordBot/IsSignUpAllowed/{raidId}/{userId}/{ignoreRole}");
if (!httpResponseMessage.IsSuccessStatusCode)
{
ProblemDetails problemDetails = await httpResponseMessage.Content.ReadFromJsonAsync<ProblemDetails>(_serializerOptions) ?? new ProblemDetails();
string errorMessage = string.IsNullOrEmpty(problemDetails.Detail) ? string.Empty : problemDetails.Detail;
return new Tuple<bool, string>(false, errorMessage);
}
return new Tuple<bool, string>(true, string.Empty);
}
public async Task<Tuple<bool, string>> IsExternalSignUpAllowed(int raidId)
{
var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME);
var httpResponseMessage = await httpClient.GetAsync($"DiscordBot/IsExternalSignUpAllowed/{raidId}");
if (!httpResponseMessage.IsSuccessStatusCode) if (!httpResponseMessage.IsSuccessStatusCode)
{ {

View file

@ -35,10 +35,21 @@ namespace Lieb.Controllers
} }
[HttpGet] [HttpGet]
[Route("[action]/{raidId}/{userId}")] [Route("[action]/{raidId}/{userId}/{ignoreRole}")]
public ActionResult IsSignUpAllowed(int raidId, ulong userId) public ActionResult IsSignUpAllowed(int raidId, ulong userId, bool ignoreRole)
{ {
if(!_raidService.IsRaidSignUpAllowed(userId, raidId, out string errorMessage)) if(!_raidService.IsRaidSignUpAllowed(userId, raidId, out string errorMessage, ignoreRole))
{
return Problem(errorMessage);
}
return Ok();
}
[HttpGet]
[Route("[action]/{raidId}")]
public ActionResult IsExternalSignUpAllowed(int raidId)
{
if(!_raidService.IsExternalSignUpAllowed(raidId, out string errorMessage))
{ {
return Problem(errorMessage); return Problem(errorMessage);
} }

View file

@ -159,11 +159,11 @@ namespace Lieb.Data
var signUps = new RaidSignUp[] var signUps = new RaidSignUp[]
{ {
new RaidSignUp{GuildWars2AccountId = linaith.GuildWars2AccountId, LiebUserId = users[0].Id, RaidRoleId = ele.RaidRoleId, RaidId = raid.RaidId, SignUpType = SignUpType.SignedUp }, new RaidSignUp(raid.RaidId, users[0].Id, linaith.GuildWars2AccountId, ele.RaidRoleId, SignUpType.SignedUp),
new RaidSignUp{GuildWars2AccountId = hierpiepts.GuildWars2AccountId, LiebUserId = users[1].Id, RaidRoleId = flexTest1.RaidRoleId, RaidId = raid.RaidId, SignUpType = SignUpType.SignedUp }, new RaidSignUp(raid.RaidId, users[1].Id, hierpiepts.GuildWars2AccountId, flexTest1.RaidRoleId, SignUpType.SignedUp),
new RaidSignUp{GuildWars2AccountId = bloodseeker.GuildWars2AccountId, LiebUserId = users[2].Id, RaidRoleId = flexTest2.RaidRoleId, RaidId = raid.RaidId, SignUpType = SignUpType.SignedUp }, new RaidSignUp(raid.RaidId, users[2].Id, bloodseeker.GuildWars2AccountId, flexTest2.RaidRoleId, SignUpType.SignedUp),
new RaidSignUp{GuildWars2AccountId = hierpiepts.GuildWars2AccountId, LiebUserId = users[1].Id, RaidRoleId = flexTest2.RaidRoleId, RaidId = raid.RaidId, SignUpType = SignUpType.Flex }, new RaidSignUp(raid.RaidId, users[1].Id, hierpiepts.GuildWars2AccountId, flexTest2.RaidRoleId, SignUpType.Flex),
new RaidSignUp{GuildWars2AccountId = bloodseeker.GuildWars2AccountId, LiebUserId = users[2].Id, RaidRoleId = flexTest3.RaidRoleId, RaidId = raid.RaidId, SignUpType = SignUpType.Flex } new RaidSignUp(raid.RaidId, users[2].Id, bloodseeker.GuildWars2AccountId, flexTest3.RaidRoleId, SignUpType.Flex)
}; };
context.RaidSignUps.AddRange(signUps); context.RaidSignUps.AddRange(signUps);

View file

@ -240,7 +240,7 @@ namespace Lieb.Data
AccountName = signUp.GuildWars2Account.AccountName, AccountName = signUp.GuildWars2Account.AccountName,
Status = status, Status = status,
UserName = signUp.LiebUser.Name, UserName = signUp.LiebUser.Name,
UserId = signUp.LiebUserId UserId = signUp.LiebUserId.Value
}); });
} }
} }
@ -274,9 +274,9 @@ namespace Lieb.Data
apiReminder.UserIds = new List<ulong>(); apiReminder.UserIds = new List<ulong>();
foreach(RaidSignUp signUp in raid.SignUps) foreach(RaidSignUp signUp in raid.SignUps)
{ {
if(signUp.LiebUserId > 0) if(signUp.LiebUserId.HasValue)
{ {
apiReminder.UserIds.Add(signUp.LiebUserId); apiReminder.UserIds.Add(signUp.LiebUserId.Value);
} }
} }
return apiReminder; return apiReminder;

View file

@ -104,9 +104,9 @@ namespace Lieb.Data
Dictionary<ulong, GuildWars2Build> signedUpUsers= new Dictionary<ulong, GuildWars2Build>(); Dictionary<ulong, GuildWars2Build> signedUpUsers= new Dictionary<ulong, GuildWars2Build>();
foreach (RaidSignUp signUp in raid.SignUps) foreach (RaidSignUp signUp in raid.SignUps)
{ {
if (signUp.GuildWars2Account.EquippedBuilds.Count > 0) if (!signUp.IsExternalUser && signUp.GuildWars2Account.EquippedBuilds.Count > 0)
{ {
signedUpUsers.Add(signUp.LiebUserId, signUp.GuildWars2Account.EquippedBuilds.ToList()[_random.Next(signUp.GuildWars2Account.EquippedBuilds.Count - 1)].GuildWars2Build); signedUpUsers.Add(signUp.LiebUserId.Value, signUp.GuildWars2Account.EquippedBuilds.ToList()[_random.Next(signUp.GuildWars2Account.EquippedBuilds.Count - 1)].GuildWars2Build);
} }
} }
BalanceRoles(raid, signedUpUsers); BalanceRoles(raid, signedUpUsers);

View file

@ -117,14 +117,7 @@ namespace Lieb.Data
} }
else if (!signUps.Where(r => r.RaidRoleId == plannedRoleId).Any()) else if (!signUps.Where(r => r.RaidRoleId == plannedRoleId).Any())
{ {
RaidSignUp signUp = new RaidSignUp() RaidSignUp signUp = new RaidSignUp(raidId, liebUserId, guildWars2AccountId, plannedRoleId, signUpType);
{
GuildWars2AccountId = guildWars2AccountId,
RaidId = raidId,
LiebUserId = liebUserId,
RaidRoleId = plannedRoleId,
SignUpType = signUpType
};
string userName = context.LiebUsers.FirstOrDefault(l => l.Id == liebUserId)?.Name; string userName = context.LiebUsers.FirstOrDefault(l => l.Id == liebUserId)?.Name;
context.RaidSignUps.Add(signUp); context.RaidSignUps.Add(signUp);
await context.SaveChangesAsync(); await context.SaveChangesAsync();
@ -142,13 +135,7 @@ namespace Lieb.Data
using var context = _contextFactory.CreateDbContext(); using var context = _contextFactory.CreateDbContext();
RaidSignUp signUp = new RaidSignUp() RaidSignUp signUp = new RaidSignUp(raidId, userName, plannedRoleId, signUpType);
{
RaidId = raidId,
ExternalUserName = userName,
RaidRoleId = plannedRoleId,
SignUpType = signUpType
};
context.RaidSignUps.Add(signUp); context.RaidSignUps.Add(signUp);
await context.SaveChangesAsync(); await context.SaveChangesAsync();
await LogSignUp(signUp, userName, signedUpByUserId); await LogSignUp(signUp, userName, signedUpByUserId);
@ -235,7 +222,14 @@ namespace Lieb.Data
{ {
signUp.RaidRoleId = plannedRoleId; signUp.RaidRoleId = plannedRoleId;
signUp.SignUpType = signUpType; signUp.SignUpType = signUpType;
await LogSignUp(signUp, signUp.LiebUser.Name); if(signUp.IsExternalUser)
{
await LogSignUp(signUp, signUp.ExternalUserName);
}
else
{
await LogSignUp(signUp, signUp.LiebUser.Name);
}
} }
context.SaveChanges(); context.SaveChanges();
if(postChanges) if(postChanges)
@ -337,7 +331,7 @@ namespace Lieb.Data
return false; return false;
} }
public bool IsRaidSignUpAllowed(ulong liebUserId, int raidId, out string errorMessage) public bool IsRaidSignUpAllowed(ulong liebUserId, int raidId, out string errorMessage, bool ignoreRole = false)
{ {
errorMessage = string.Empty; errorMessage = string.Empty;
using var context = _contextFactory.CreateDbContext(); using var context = _contextFactory.CreateDbContext();
@ -374,7 +368,7 @@ namespace Lieb.Data
return false; return false;
} }
if (!string.IsNullOrEmpty(raid.RequiredRole) if (!ignoreRole && !string.IsNullOrEmpty(raid.RequiredRole)
&& !user.RoleAssignments.Where(a => a.LiebRole.RoleName == raid.RequiredRole).Any() && !user.RoleAssignments.Where(a => a.LiebRole.RoleName == raid.RequiredRole).Any()
&& raid.FreeForAllTimeUTC.UtcDateTime > DateTimeOffset.UtcNow) && raid.FreeForAllTimeUTC.UtcDateTime > DateTimeOffset.UtcNow)
{ {
@ -392,9 +386,37 @@ namespace Lieb.Data
return true; return true;
} }
public bool IsExternalSignUpAllowed(int raidId, out string errorMessage)
{
errorMessage = string.Empty;
using var context = _contextFactory.CreateDbContext();
Raid? raid = context.Raids
.AsNoTracking()
.FirstOrDefault(r => r.RaidId == raidId);
if(raid == null)
{
errorMessage = "Raid not found.";
return false;
}
if (raid.RaidType != RaidType.Planned)
{
errorMessage = "Random raids need an Account with equipped builds.";
return false;
}
if(raid.EndTimeUTC < DateTimeOffset.UtcNow)
{
errorMessage = $"The raid already ended.";
return false;
}
return true;
}
private async Task LogSignUp(RaidSignUp signUp, string userName, ulong signedUpBy = 0) private async Task LogSignUp(RaidSignUp signUp, string userName, ulong signedUpBy = 0)
{ {
ulong userId = signedUpBy > 0 ? signedUpBy : signUp.LiebUserId; ulong userId = signedUpBy > 0 ? signedUpBy : signUp.LiebUserId.Value;
RaidLog log = RaidLog.CreateSignUpLog(userId, signUp, userName); RaidLog log = RaidLog.CreateSignUpLog(userId, signUp, userName);
using var context = _contextFactory.CreateDbContext(); using var context = _contextFactory.CreateDbContext();

View file

@ -12,18 +12,39 @@
public class RaidSignUp public class RaidSignUp
{ {
public int RaidSignUpId { get; set; } public int RaidSignUpId { get; set; }
public bool IsExternalUser {get { return LiebUserId == 0;}} public bool IsExternalUser {get { return LiebUserId == null;}}
public int RaidId { get; set; } public int RaidId { get; set; }
public ulong LiebUserId { get; set; } public ulong? LiebUserId { get; set; }
public int GuildWars2AccountId { get; set; } public int? GuildWars2AccountId { get; set; }
public int RaidRoleId { get; set; } public int RaidRoleId { get; set; }
public string ExternalUserName {get; set;} = string.Empty; public string ExternalUserName {get; set;} = string.Empty;
public SignUpType SignUpType { get; set; } public SignUpType SignUpType { get; set; }
public Raid Raid { get; set; } public Raid Raid { get; set; }
public LiebUser LiebUser { get; set; } public LiebUser? LiebUser { get; set; }
public GuildWars2Account GuildWars2Account { get; set; } public GuildWars2Account? GuildWars2Account { get; set; }
public RaidRole RaidRole { get; set; } public RaidRole RaidRole { get; set; }
public RaidSignUp(int raidId, ulong userId, int gw2AccountId, int roleId, SignUpType signUpType)
{
RaidId = raidId;
LiebUserId = userId;
GuildWars2AccountId = gw2AccountId;
RaidRoleId = roleId;
SignUpType = signUpType;
}
public RaidSignUp(int raidId, string userName, int roleId, SignUpType signUpType)
{
RaidId = raidId;
RaidRoleId = roleId;
SignUpType = signUpType;
ExternalUserName = userName;
}
private RaidSignUp()
{
}
} }
} }