changing the sign up type is now possible with 1 click in the bot
This commit is contained in:
parent
058cc89cbc
commit
e675323adc
7 changed files with 155 additions and 40 deletions
|
@ -25,17 +25,17 @@ namespace DiscordBot.CommandHandlers
|
||||||
switch(ids[0])
|
switch(ids[0])
|
||||||
{
|
{
|
||||||
case Constants.ComponentIds.SIGN_UP_BUTTON:
|
case Constants.ComponentIds.SIGN_UP_BUTTON:
|
||||||
|
await SignUpClicked(component, false, SharedConstants.SIGNED_UP);
|
||||||
|
break;
|
||||||
case Constants.ComponentIds.MAYBE_BUTTON:
|
case Constants.ComponentIds.MAYBE_BUTTON:
|
||||||
RaidMessage.ButtonParameters signUpParameters = RaidMessage.ParseButtonId(component.Data.CustomId);
|
await SignUpClicked(component, false, SharedConstants.MAYBE);
|
||||||
await _handlerFunctions.SelectRole(component, signUpParameters.RaidId, signUpParameters.ButtonType, false, component.User.Id, 0);
|
|
||||||
break;
|
break;
|
||||||
case Constants.ComponentIds.BACKUP_BUTTON:
|
case Constants.ComponentIds.BACKUP_BUTTON:
|
||||||
RaidMessage.ButtonParameters backupParameters = RaidMessage.ParseButtonId(component.Data.CustomId);
|
await SignUpClicked(component, true, SharedConstants.BACKUP);
|
||||||
await _handlerFunctions.SelectRole(component, backupParameters.RaidId, backupParameters.ButtonType, true, component.User.Id, 0);
|
|
||||||
break;
|
break;
|
||||||
case Constants.ComponentIds.FLEX_BUTTON:
|
case Constants.ComponentIds.FLEX_BUTTON:
|
||||||
RaidMessage.ButtonParameters flexParameters = RaidMessage.ParseButtonId(component.Data.CustomId);
|
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);
|
await _handlerFunctions.SelectRole(component, flexParameters.RaidId, flexParameters.ButtonType, true, component.User.Id, 0);
|
||||||
}
|
}
|
||||||
|
@ -66,5 +66,36 @@ namespace DiscordBot.CommandHandlers
|
||||||
break;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -67,7 +67,7 @@ namespace DiscordBot.Services
|
||||||
return new Tuple<bool, string>(true, string.Empty);
|
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);
|
var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME);
|
||||||
|
|
||||||
|
@ -75,12 +75,9 @@ namespace DiscordBot.Services
|
||||||
|
|
||||||
if (httpResponseMessage.IsSuccessStatusCode)
|
if (httpResponseMessage.IsSuccessStatusCode)
|
||||||
{
|
{
|
||||||
using var contentStream =
|
return await httpResponseMessage.Content.ReadAsStringAsync();
|
||||||
await httpResponseMessage.Content.ReadAsStreamAsync();
|
|
||||||
|
|
||||||
return await JsonSerializer.DeserializeAsync<bool>(contentStream, _serializerOptions);
|
|
||||||
}
|
}
|
||||||
return false;
|
return string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<ApiRole>> GetRoles(int raidId, ulong userId)
|
public async Task<List<ApiRole>> GetRoles(int raidId, ulong userId)
|
||||||
|
@ -124,6 +121,21 @@ namespace DiscordBot.Services
|
||||||
await SendSignUp(signUp, "DiscordBot/SignOff");
|
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)
|
private async Task<bool> SendSignUp(ApiSignUp signUp, string requestUri)
|
||||||
{
|
{
|
||||||
var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME);
|
var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME);
|
||||||
|
|
|
@ -49,10 +49,22 @@ namespace Lieb.Controllers
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Route("[action]/{raidId}/{userId}")]
|
[Route("[action]/{raidId}/{userId}")]
|
||||||
public bool IsUserSignedUp(int raidId, ulong userId)
|
public string IsUserSignedUp(int raidId, ulong userId)
|
||||||
{
|
{
|
||||||
Raid raid = _raidService.GetRaid(raidId);
|
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]
|
[HttpGet]
|
||||||
|
@ -173,6 +185,30 @@ namespace Lieb.Controllers
|
||||||
return true;
|
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]
|
[HttpPost]
|
||||||
[Route("[action]")]
|
[Route("[action]")]
|
||||||
public async Task<ActionResult> CreateAccount(ApiRaid.Role.User user)
|
public async Task<ActionResult> CreateAccount(ApiRaid.Role.User user)
|
||||||
|
|
|
@ -151,7 +151,8 @@ namespace Lieb.Data
|
||||||
Roles = new[] { new RaidRole(){
|
Roles = new[] { new RaidRole(){
|
||||||
Description = "WupWup",
|
Description = "WupWup",
|
||||||
Name = "Ups",
|
Name = "Ups",
|
||||||
Spots = 10
|
Spots = 10,
|
||||||
|
IsRandomSignUpRole = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -114,24 +114,41 @@ namespace Lieb.Data
|
||||||
.Include(r => r.SignUps)
|
.Include(r => r.SignUps)
|
||||||
.FirstOrDefault(r => r.RaidId == raidId);
|
.FirstOrDefault(r => r.RaidId == raidId);
|
||||||
if(raid == null) return false;
|
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))
|
if (!IsRoleSignUpAllowed(raidId, liebUserId, plannedRoleId, signUpType, true))
|
||||||
{
|
{
|
||||||
return false;
|
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();
|
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())
|
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);
|
await ChangeAccount(raidId, liebUserId, guildWars2AccountId, false);
|
||||||
}
|
}
|
||||||
else if (!signUps.Where(r => r.RaidRoleId == plannedRoleId).Any())
|
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);
|
RaidSignUp signUp = new RaidSignUp(raidId, liebUserId, guildWars2AccountId, plannedRoleId, signUpType);
|
||||||
context.RaidSignUps.Add(signUp);
|
context.RaidSignUps.Add(signUp);
|
||||||
await context.SaveChangesAsync();
|
await context.SaveChangesAsync();
|
||||||
await LogSignUp(signUp, user.Name, signedUpByUserId);
|
await LogSignUp(signUp, userName, signedUpByUserId);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
user.LastSignUpAt = DateTime.UtcNow;
|
|
||||||
await context.SaveChangesAsync();
|
|
||||||
await _discordService.PostRaidMessage(raidId);
|
|
||||||
return true;
|
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();
|
using var context = _contextFactory.CreateDbContext();
|
||||||
if(signUpType == SignUpType.Flex) return false;
|
if(signUpType == SignUpType.Flex) return false;
|
||||||
|
@ -184,8 +198,7 @@ namespace Lieb.Data
|
||||||
context.RaidSignUps.Add(signUp);
|
context.RaidSignUps.Add(signUp);
|
||||||
}
|
}
|
||||||
await context.SaveChangesAsync();
|
await context.SaveChangesAsync();
|
||||||
await LogSignUp(signUp, context.LiebUsers.FirstOrDefault(u => u.Id == liebUserId)?.Name, signedUpByUserId);
|
await LogSignUp(signUp, userName, signedUpByUserId);
|
||||||
await _discordService.PostRaidMessage(raidId);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -281,16 +294,8 @@ namespace Lieb.Data
|
||||||
//change to new role
|
//change to new role
|
||||||
if (signUp != null)
|
if (signUp != null)
|
||||||
{
|
{
|
||||||
if(raidType == RaidType.Planned)
|
signUp.RaidRoleId = plannedRoleId;
|
||||||
{
|
if(!signUp.IsExternalUser)
|
||||||
signUp.RaidRoleId = plannedRoleId;
|
|
||||||
}
|
|
||||||
signUp.SignUpType = signUpType;
|
|
||||||
if(signUp.IsExternalUser)
|
|
||||||
{
|
|
||||||
await LogSignUp(signUp, signUp.ExternalUserName);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
await LogSignUp(signUp, signUp.LiebUser.Name);
|
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)
|
public bool IsRoleSignUpAllowed(int raidId, ulong liebUserId, int plannedRoleId, SignUpType signUpType, bool moveFlexUser)
|
||||||
{
|
{
|
||||||
using var context = _contextFactory.CreateDbContext();
|
using var context = _contextFactory.CreateDbContext();
|
||||||
|
@ -376,7 +402,7 @@ namespace Lieb.Data
|
||||||
{
|
{
|
||||||
if (moveFlexUser)
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -138,7 +138,11 @@
|
||||||
{
|
{
|
||||||
if(_raid.SignUps.Where(s => s.LiebUserId == _liebUserId).Any() && signUpType != SignUpType.Flex)
|
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
|
else
|
||||||
{
|
{
|
||||||
|
|
|
@ -2,6 +2,11 @@ namespace SharedClasses.SharedModels
|
||||||
{
|
{
|
||||||
public class SharedConstants
|
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 class SlashCommands
|
||||||
{
|
{
|
||||||
public const string RAID = "raid";
|
public const string RAID = "raid";
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue