changing the sign up type is now possible with 1 click in the bot

This commit is contained in:
Sarah Faey 2022-12-11 01:10:50 +01:00
parent 058cc89cbc
commit e675323adc
7 changed files with 155 additions and 40 deletions

View file

@ -25,17 +25,17 @@ namespace DiscordBot.CommandHandlers
switch(ids[0])
{
case Constants.ComponentIds.SIGN_UP_BUTTON:
await SignUpClicked(component, false, SharedConstants.SIGNED_UP);
break;
case Constants.ComponentIds.MAYBE_BUTTON:
RaidMessage.ButtonParameters signUpParameters = RaidMessage.ParseButtonId(component.Data.CustomId);
await _handlerFunctions.SelectRole(component, signUpParameters.RaidId, signUpParameters.ButtonType, false, component.User.Id, 0);
await SignUpClicked(component, false, SharedConstants.MAYBE);
break;
case Constants.ComponentIds.BACKUP_BUTTON:
RaidMessage.ButtonParameters backupParameters = RaidMessage.ParseButtonId(component.Data.CustomId);
await _handlerFunctions.SelectRole(component, backupParameters.RaidId, backupParameters.ButtonType, true, component.User.Id, 0);
await SignUpClicked(component, true, SharedConstants.BACKUP);
break;
case Constants.ComponentIds.FLEX_BUTTON:
RaidMessage.ButtonParameters flexParameters = RaidMessage.ParseButtonId(component.Data.CustomId);
if(await _httpService.IsUserSignedUp(flexParameters.RaidId, component.User.Id))
if(!string.IsNullOrEmpty(await _httpService.IsUserSignedUp(flexParameters.RaidId, component.User.Id)))
{
await _handlerFunctions.SelectRole(component, flexParameters.RaidId, flexParameters.ButtonType, true, component.User.Id, 0);
}
@ -66,5 +66,36 @@ namespace DiscordBot.CommandHandlers
break;
}
}
public async Task SignUpClicked(SocketMessageComponent component, bool allRoles, string signUpType)
{
RaidMessage.ButtonParameters parameters = RaidMessage.ParseButtonId(component.Data.CustomId);
string currentSignUp = await _httpService.IsUserSignedUp(parameters.RaidId, component.User.Id);
if(string.IsNullOrEmpty(currentSignUp) || currentSignUp == signUpType)
{
await _handlerFunctions.SelectRole(component, parameters.RaidId, parameters.ButtonType, allRoles, component.User.Id, 0);
}
else
{
ApiSignUp signUp = new ApiSignUp()
{
raidId = parameters.RaidId,
userId = component.User.Id
};
switch(signUpType)
{
case SharedConstants.SIGNED_UP:
await _httpService.ChangeToSignUp(signUp);
break;
case SharedConstants.MAYBE:
await _httpService.ChangeToMaybe(signUp);
break;
case SharedConstants.BACKUP:
await _httpService.ChangeToBackup(signUp);
break;
}
await component.RespondAsync("Sign up type changed.", ephemeral: true);
}
}
}
}

View file

@ -67,7 +67,7 @@ namespace DiscordBot.Services
return new Tuple<bool, string>(true, string.Empty);
}
public async Task<bool> IsUserSignedUp(int raidId, ulong userId)
public async Task<string> IsUserSignedUp(int raidId, ulong userId)
{
var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME);
@ -75,12 +75,9 @@ namespace DiscordBot.Services
if (httpResponseMessage.IsSuccessStatusCode)
{
using var contentStream =
await httpResponseMessage.Content.ReadAsStreamAsync();
return await JsonSerializer.DeserializeAsync<bool>(contentStream, _serializerOptions);
return await httpResponseMessage.Content.ReadAsStringAsync();
}
return false;
return string.Empty;
}
public async Task<List<ApiRole>> GetRoles(int raidId, ulong userId)
@ -124,6 +121,21 @@ namespace DiscordBot.Services
await SendSignUp(signUp, "DiscordBot/SignOff");
}
public async Task<bool> ChangeToSignUp(ApiSignUp signUp)
{
return await SendSignUp(signUp, "DiscordBot/ChangeSignUpTypeToSignUp");
}
public async Task<bool> ChangeToMaybe(ApiSignUp signUp)
{
return await SendSignUp(signUp, "DiscordBot/ChangeSignUpTypeToMaybe");
}
public async Task<bool> ChangeToBackup(ApiSignUp signUp)
{
return await SendSignUp(signUp, "DiscordBot/ChangeSignUpTypeToBackup");
}
private async Task<bool> SendSignUp(ApiSignUp signUp, string requestUri)
{
var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME);

View file

@ -49,10 +49,22 @@ namespace Lieb.Controllers
[HttpGet]
[Route("[action]/{raidId}/{userId}")]
public bool IsUserSignedUp(int raidId, ulong userId)
public string IsUserSignedUp(int raidId, ulong userId)
{
Raid raid = _raidService.GetRaid(raidId);
return raid.SignUps.Where(s => s.LiebUserId == userId && s.SignUpType != SignUpType.Flex && s.SignUpType != SignUpType.SignedOff).Any();
RaidSignUp signUp = raid.SignUps.FirstOrDefault(s => s.LiebUserId == userId && s.SignUpType != SignUpType.Flex);
if(signUp == null) return string.Empty;
switch(signUp.SignUpType)
{
case SignUpType.SignedUp:
return SharedConstants.SIGNED_UP;
case SignUpType.Maybe:
return SharedConstants.MAYBE;
case SignUpType.Backup:
return SharedConstants.BACKUP;
}
return string.Empty;
}
[HttpGet]
@ -173,6 +185,30 @@ namespace Lieb.Controllers
return true;
}
[HttpPost]
[Route("[action]")]
public async Task<bool> ChangeSignUpTypeToSignUp(ApiSignUp signUp)
{
await _raidService.ChangeSignUpType(signUp.raidId, signUp.userId, SignUpType.SignedUp, true);
return true;
}
[HttpPost]
[Route("[action]")]
public async Task<bool> ChangeSignUpTypeToMaybe(ApiSignUp signUp)
{
await _raidService.ChangeSignUpType(signUp.raidId, signUp.userId, SignUpType.Maybe, true);
return true;
}
[HttpPost]
[Route("[action]")]
public async Task<bool> ChangeSignUpTypeToBackup(ApiSignUp signUp)
{
await _raidService.ChangeSignUpType(signUp.raidId, signUp.userId, SignUpType.Backup, true);
return true;
}
[HttpPost]
[Route("[action]")]
public async Task<ActionResult> CreateAccount(ApiRaid.Role.User user)

View file

@ -151,7 +151,8 @@ namespace Lieb.Data
Roles = new[] { new RaidRole(){
Description = "WupWup",
Name = "Ups",
Spots = 10
Spots = 10,
IsRandomSignUpRole = true
}
}
};

View file

@ -114,24 +114,41 @@ namespace Lieb.Data
.Include(r => r.SignUps)
.FirstOrDefault(r => r.RaidId == raidId);
if(raid == null) return false;
if(raid.RaidType != RaidType.Planned)
LiebUser user = context.LiebUsers.FirstOrDefault(l => l.Id == liebUserId);
if(user == null) return false;
bool result = false;
if(raid.RaidType == RaidType.Planned)
{
return await SignUpRandom(raidId, liebUserId, guildWars2AccountId, signUpType, signedUpByUserId);
result = await SignUpPlanned(raidId, liebUserId, guildWars2AccountId, plannedRoleId, signUpType, user.Name, signedUpByUserId);
}
else
{
result = await SignUpRandom(raidId, liebUserId, guildWars2AccountId, signUpType, user.Name, signedUpByUserId);
}
user.LastSignUpAt = DateTime.UtcNow;
await context.SaveChangesAsync();
await _discordService.PostRaidMessage(raidId);
return result;
}
private async Task<bool> SignUpPlanned(int raidId, ulong liebUserId, int guildWars2AccountId, int plannedRoleId, SignUpType signUpType, string userName, ulong signedUpByUserId = 0)
{
if (!IsRoleSignUpAllowed(raidId, liebUserId, plannedRoleId, signUpType, true))
{
return false;
}
LiebUser user = context.LiebUsers.FirstOrDefault(l => l.Id == liebUserId);
using var context = _contextFactory.CreateDbContext();
if(user == null) return false;
List<RaidSignUp> signUps = context.RaidSignUps.Where(r => r.RaidId == raidId && r.LiebUserId == liebUserId).ToList();
if (signUpType != SignUpType.Flex && signUps.Where(r => r.SignUpType != SignUpType.Flex).Any())
{
await ChangeSignUpType(raidId, liebUserId, plannedRoleId, signUpType, raid.RaidType, false);
await ChangeSignUpType(raidId, liebUserId, signUpType, false);
await ChangeRole(raidId, liebUserId, plannedRoleId, signUpType, RaidType.Planned, false);
await ChangeAccount(raidId, liebUserId, guildWars2AccountId, false);
}
else if (!signUps.Where(r => r.RaidRoleId == plannedRoleId).Any())
@ -139,19 +156,16 @@ namespace Lieb.Data
RaidSignUp signUp = new RaidSignUp(raidId, liebUserId, guildWars2AccountId, plannedRoleId, signUpType);
context.RaidSignUps.Add(signUp);
await context.SaveChangesAsync();
await LogSignUp(signUp, user.Name, signedUpByUserId);
await LogSignUp(signUp, userName, signedUpByUserId);
}
else
{
return false;
}
user.LastSignUpAt = DateTime.UtcNow;
await context.SaveChangesAsync();
await _discordService.PostRaidMessage(raidId);
return true;
}
public async Task<bool> SignUpRandom(int raidId, ulong liebUserId, int guildWars2AccountId, SignUpType signUpType, ulong signedUpByUserId = 0)
private async Task<bool> SignUpRandom(int raidId, ulong liebUserId, int guildWars2AccountId, SignUpType signUpType, string userName, ulong signedUpByUserId = 0)
{
using var context = _contextFactory.CreateDbContext();
if(signUpType == SignUpType.Flex) return false;
@ -184,8 +198,7 @@ namespace Lieb.Data
context.RaidSignUps.Add(signUp);
}
await context.SaveChangesAsync();
await LogSignUp(signUp, context.LiebUsers.FirstOrDefault(u => u.Id == liebUserId)?.Name, signedUpByUserId);
await _discordService.PostRaidMessage(raidId);
await LogSignUp(signUp, userName, signedUpByUserId);
return true;
}
return false;
@ -258,9 +271,9 @@ namespace Lieb.Data
}
}
public async Task ChangeSignUpType(int raidId, ulong liebUserId, int plannedRoleId, SignUpType signUpType, RaidType raidType, bool postChanges = true)
public async Task ChangeRole(int raidId, ulong liebUserId, int plannedRoleId, SignUpType signUpType, RaidType raidType, bool postChanges = true)
{
if (!IsRoleSignUpAllowed(raidId, liebUserId, plannedRoleId, signUpType, true))
if (raidType != RaidType.Planned || !IsRoleSignUpAllowed(raidId, liebUserId, plannedRoleId, signUpType, true))
{
return;
}
@ -280,17 +293,9 @@ namespace Lieb.Data
//change to new role
if (signUp != null)
{
if(raidType == RaidType.Planned)
{
signUp.RaidRoleId = plannedRoleId;
}
signUp.SignUpType = signUpType;
if(signUp.IsExternalUser)
{
await LogSignUp(signUp, signUp.ExternalUserName);
}
else
if(!signUp.IsExternalUser)
{
await LogSignUp(signUp, signUp.LiebUser.Name);
}
@ -302,6 +307,27 @@ namespace Lieb.Data
}
}
public async Task ChangeSignUpType(int raidId, ulong liebUserId, SignUpType signUpType, bool postChanges = true)
{
using var context = _contextFactory.CreateDbContext();
RaidSignUp? signUp = context.RaidSignUps
.Include(s => s.LiebUser)
.FirstOrDefault(x => x.RaidId == raidId && x.LiebUserId == liebUserId
&& x.SignUpType != SignUpType.Flex);
if (signUp != null && IsRoleSignUpAllowed(raidId, liebUserId, signUp.RaidRoleId, signUpType, true))
{
signUp.SignUpType = signUpType;
await LogSignUp(signUp, signUp.LiebUser.Name);
context.SaveChanges();
}
if(postChanges)
{
await _discordService.PostRaidMessage(raidId);
}
}
public bool IsRoleSignUpAllowed(int raidId, ulong liebUserId, int plannedRoleId, SignUpType signUpType, bool moveFlexUser)
{
using var context = _contextFactory.CreateDbContext();
@ -376,7 +402,7 @@ namespace Lieb.Data
{
if (moveFlexUser)
{
await ChangeSignUpType(raid.RaidId, userId, signUp.RaidRoleId, SignUpType.SignedUp, raid.RaidType, false);
await ChangeRole(raid.RaidId, userId, signUp.RaidRoleId, SignUpType.SignedUp, raid.RaidType, false);
}
return true;
}

View file

@ -138,7 +138,11 @@
{
if(_raid.SignUps.Where(s => s.LiebUserId == _liebUserId).Any() && signUpType != SignUpType.Flex)
{
await RaidService.ChangeSignUpType(_raid.RaidId, _liebUserId, role.RaidRoleId, signUpType, _raid.RaidType);
if(_raid.RaidType == RaidType.Planned)
{
await RaidService.ChangeRole(_raid.RaidId, _liebUserId, role.RaidRoleId, signUpType, _raid.RaidType, false);
}
await RaidService.ChangeSignUpType(_raid.RaidId, _liebUserId, signUpType);
}
else
{

View file

@ -2,6 +2,11 @@ namespace SharedClasses.SharedModels
{
public class SharedConstants
{
public const string SIGNED_UP = "signedUp";
public const string MAYBE = "maybe";
public const string BACKUP = "backup";
public const string FLEX = "flex";
public class SlashCommands
{
public const string RAID = "raid";