diff --git a/Lieb/Data/RaidService.cs b/Lieb/Data/RaidService.cs index e222b13..a682df3 100644 --- a/Lieb/Data/RaidService.cs +++ b/Lieb/Data/RaidService.cs @@ -1,4 +1,5 @@ -using Lieb.Models.GuildWars2.Raid; +using Lieb.Models; +using Lieb.Models.GuildWars2.Raid; using Microsoft.EntityFrameworkCore; namespace Lieb.Data @@ -148,7 +149,7 @@ namespace Lieb.Data public async Task SignUp(int raidId, int liebUserId, int guildWars2AccountId, int plannedRoleId, SignUpType signUpType) { - if (!IsSignUpAllowed(liebUserId, plannedRoleId, signUpType)) + if (!await IsRoleSignUpAllowed(raidId, liebUserId, plannedRoleId, signUpType, true, new List())) { return; } @@ -195,7 +196,7 @@ namespace Lieb.Data public async Task ChangeSignUpType(int raidId, int liebUserId, int plannedRoleId, SignUpType signUpType) { - if (!IsSignUpAllowed(liebUserId, plannedRoleId, signUpType)) + if (!await IsRoleSignUpAllowed(raidId, liebUserId, plannedRoleId, signUpType, true, new List())) { return; } @@ -223,7 +224,7 @@ namespace Lieb.Data } } - public bool IsSignUpAllowed(int liebUserId, int plannedRoleId, SignUpType signUpType) + public bool IsRoleSignUpAllowed(int liebUserId, int plannedRoleId, SignUpType signUpType) { if(signUpType == SignUpType.Backup || signUpType == SignUpType.Flex || signUpType == SignUpType.SignedOff) { @@ -246,9 +247,14 @@ namespace Lieb.Data return signUps.Where(s => s.LiebUserId == liebUserId && s.SignUpType == SignUpType.SignedUp).Any(); } - public async Task IsSignUpAllowed(int raidId, int liebUserId, int plannedRoleId, SignUpType signUpType, bool moveFlexUser, List checkedRoleIds) + public bool IsRoleSignUpAllowed(int raidId, int liebUserId, int plannedRoleId, SignUpType signUpType, bool moveFlexUser) { - if (IsSignUpAllowed(liebUserId, plannedRoleId, signUpType)) + return IsRoleSignUpAllowed(raidId, liebUserId, plannedRoleId, signUpType, moveFlexUser, new List()).Result; + } + + private async Task IsRoleSignUpAllowed(int raidId, int liebUserId, int plannedRoleId, SignUpType signUpType, bool moveFlexUser, List checkedRoleIds) + { + if (IsRoleSignUpAllowed(liebUserId, plannedRoleId, signUpType)) return true; if (checkedRoleIds == null) @@ -271,7 +277,7 @@ namespace Lieb.Data foreach (RaidSignUp signUp in raid.SignUps.Where(s => s.LiebUserId == userId && s.SignUpType == SignUpType.Flex)) { if (!checkedRoleIds.Contains(signUp.PlannedRaidRoleId) - && await IsSignUpAllowed(raidId, userId, signUp.PlannedRaidRoleId, SignUpType.SignedUp, moveFlexUser, checkedRoleIds)) + && await IsRoleSignUpAllowed(raidId, userId, signUp.PlannedRaidRoleId, SignUpType.SignedUp, moveFlexUser, checkedRoleIds)) { if (moveFlexUser) { @@ -283,5 +289,52 @@ namespace Lieb.Data } return false; } + + public bool IsRaidSignUpAllowed(int liebUserId, 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; + } + LiebUser? user = context.LiebUsers + .Include(u => u.RoleAssignments) + .ThenInclude(a => a.LiebRole) + .Include(u => u.GuildWars2Accounts) + .ThenInclude(a => a.EquippedBuilds) + .AsNoTracking() + .FirstOrDefault(r => r.LiebUserId == liebUserId); + if (user == null) + { + errorMessage = "User not found."; + return false; + } + + DateTime freeForAllTime = raid.FreeForAllDate.Date + raid.FreeForAllTime.TimeOfDay; + if (!string.IsNullOrEmpty(raid.RequiredRole) && !user.RoleAssignments.Where(a => a.LiebRole.RoleName == raid.RequiredRole).Any() || freeForAllTime > DateTimeOffset.Now) + { + errorMessage = $"The raid is still locked for {raid.RequiredRole}."; + return false; + } + + if (user.GuildWars2Accounts.Count == 0) + { + errorMessage = "No Guild Wars 2 account found."; + return false; + } + + if (raid.RaidType != RaidType.Planned && !user.GuildWars2Accounts.Where(a => a.EquippedBuilds.Count > 0).Any()) + { + errorMessage = "No equipped Guild Wars 2 build found."; + return false; + } + + return true; + } } } diff --git a/Lieb/Pages/Raids/RaidDetails.razor b/Lieb/Pages/Raids/RaidDetails.razor index e5d40c2..1c22b12 100644 --- a/Lieb/Pages/Raids/RaidDetails.razor +++ b/Lieb/Pages/Raids/RaidDetails.razor @@ -40,82 +40,82 @@ @{ - ulong discordId = ulong.Parse(@context.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value); - LiebUser user = UserService.GetLiebUser(discordId); - RaidSignUp userRole = _raid.SignUps.Where(s => s.LiebUserId == user.LiebUserId).FirstOrDefault(); - bool isSignedUp = userRole != null; - DateTime flexTime = _raid.FreeForAllDate.Date + _raid.FreeForAllTime.TimeOfDay; - bool isSignUoAllowed = !_raid.IsRandomized && (user.RoleAssignments.FirstOrDefault(a => a.LiebRole.RoleName == _raid.RequiredRole) != null || flexTime < DateTime.Now); - isSignUoAllowed = isSignUoAllowed && user.GuildWars2Accounts.Count > 0 && (_raid.RaidType == RaidType.Planned || user.GuildWars2Accounts.FirstOrDefault()?.EquippedBuilds.Count > 0); + ulong discordId = ulong.Parse(@context.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value); + LiebUser user = UserService.GetLiebUser(discordId); + RaidSignUp userRole = _raid.SignUps.Where(s => s.LiebUserId == user.LiebUserId).FirstOrDefault(); + bool isSignedUp = userRole != null; + bool isRaidSignUpAllowed = RaidService.IsRaidSignUpAllowed(user.LiebUserId, _raid.RaidId, out string errorMessage); + } + + + + @foreach (var role in _raid.Roles) + { + RaidSignUp[] signUps = _raid.SignUps.Where(s => s.PlannedRaidRoleId == role.PlannedRaidRoleId).ToArray(); + int usedSpots = signUps.Where(s => s.SignUpType == SignUpType.SignedUp).Count(); -
- - @foreach (var role in _raid.Roles) - { - Models.GuildWars2.Raid.RaidSignUp[] signUps = _raid.SignUps.Where(s => s.PlannedRaidRoleId == role.PlannedRaidRoleId).ToArray(); - int usedSpots = signUps.Where(s => s.SignUpType == SignUpType.SignedUp).Count(); - - - @if (isSignUoAllowed) - { - @if (@usedSpots < @role.Spots) - { - - - } - else - { - - - } - - } - - - - @foreach (var signUp in signUps) + + @if (isRaidSignUpAllowed) { - @if(signUp.SignUpType != SignUpType.SignedOff) + @if (RaidService.IsRoleSignUpAllowed(_raid.RaidId, user.LiebUserId, role.PlannedRaidRoleId, SignUpType.SignedUp, false)) { - - @{bool isUser = isSignedUp && userRole.PlannedRaidRole.PlannedRaidRoleId == role.PlannedRaidRoleId && signUp.LiebUserId == user.LiebUserId;} - @if (isSignUoAllowed) - { - - - @if(isUser) - { - - } - else - { - - } - } - @{string signUpStatus = string.Empty;} - @if (signUp.SignUpType != SignUpType.SignedUp) signUpStatus = $" - {signUp.SignUpType}"; + + + } + else + { + + + } + + + } + + - @if (isUser) + @foreach (var signUp in signUps) + { + @if(signUp.SignUpType != SignUpType.SignedOff) + { + + @{bool isUser = isSignedUp && userRole.PlannedRaidRole.PlannedRaidRoleId == role.PlannedRaidRoleId && signUp.LiebUserId == user.LiebUserId;} + @if (isRaidSignUpAllowed) + { + + + + @if(isUser) { - + } else { - + } - - } + } + @{string signUpStatus = string.Empty;} + @if (signUp.SignUpType != SignUpType.SignedUp) signUpStatus = $" - {signUp.SignUpType}"; + + @if (isUser) + { + + } + else + { + + } + } } - -
@role.Name: @role.Description (@usedSpots /@role.Spots)
@role.Name: @role.Description (@usedSpots /@role.Spots)
@signUp.LiebUser.Name - @signUpStatus @signUp.LiebUser.Name (@signUp.GuildWars2Account.AccountName) @signUpStatus
@signUp.LiebUser.Name + @signUpStatus @signUp.LiebUser.Name (@signUp.GuildWars2Account.AccountName) @signUpStatus
- } + } + +
@@ -165,41 +165,18 @@ [Parameter] public Raid _raid { get; set; } - async Task SignUpClicked(PlannedRaidRole role, LiebUser liebUser, bool isSignedUp) - { - if(isSignedUp) - { - await RaidService.ChangeSignUpType(_raid.RaidId, liebUser.LiebUserId, role.PlannedRaidRoleId, SignUpType.SignedUp); - } - else - { - await RaidService.SignUp(_raid.RaidId, liebUser.LiebUserId, liebUser.GuildWars2Accounts.FirstOrDefault().GuildWars2AccountId, role.PlannedRaidRoleId, SignUpType.SignedUp); - } - _raid = RaidService.GetRaid(_raid.RaidId); - } + [Parameter] + public LiebUser? _user { get; set; } - async Task BackupClicked(PlannedRaidRole role, LiebUser liebUser, bool isSignedUp) + async Task SignUpClicked(PlannedRaidRole role, LiebUser liebUser, bool isSignedUp, SignUpType signUpType) { if(isSignedUp) { - await RaidService.ChangeSignUpType(_raid.RaidId, liebUser.LiebUserId, role.PlannedRaidRoleId, SignUpType.Backup); + await RaidService.ChangeSignUpType(_raid.RaidId, liebUser.LiebUserId, role.PlannedRaidRoleId, signUpType); } else { - await RaidService.SignUp(_raid.RaidId, liebUser.LiebUserId, liebUser.GuildWars2Accounts.FirstOrDefault().GuildWars2AccountId, role.PlannedRaidRoleId, SignUpType.Backup); - } - _raid = RaidService.GetRaid(_raid.RaidId); - } - - async Task MaybeClicked(PlannedRaidRole role, LiebUser liebUser, bool isSignedUp) - { - if(isSignedUp) - { - await RaidService.ChangeSignUpType(_raid.RaidId, liebUser.LiebUserId, role.PlannedRaidRoleId, SignUpType.Maybe); - } - else - { - await RaidService.SignUp(_raid.RaidId, liebUser.LiebUserId, liebUser.GuildWars2Accounts.FirstOrDefault().GuildWars2AccountId, role.PlannedRaidRoleId, SignUpType.Maybe); + await RaidService.SignUp(_raid.RaidId, liebUser.LiebUserId, liebUser.GuildWars2Accounts.FirstOrDefault().GuildWars2AccountId, role.PlannedRaidRoleId, signUpType); } _raid = RaidService.GetRaid(_raid.RaidId); } diff --git a/Lieb/Pages/Raids/RaidOverview.razor b/Lieb/Pages/Raids/RaidOverview.razor index 18226f0..ef42f75 100644 --- a/Lieb/Pages/Raids/RaidOverview.razor +++ b/Lieb/Pages/Raids/RaidOverview.razor @@ -1,6 +1,11 @@ @page "/raidoverview" @using Lieb.Data +@using System.Security.Claims +@using Lieb.Models +@using Lieb.Models.GuildWars2.Raid @inject RaidService RaidService +@inject UserService UserService +@inject AuthenticationStateProvider AuthenticationStateProvider

RaidOverview

@@ -15,19 +20,28 @@ -@foreach (var raid in raids) { +@foreach (var raid in _raids) {
- + } @code { - private List raids; + private List _raids; + private LiebUser? _user; protected override async Task OnInitializedAsync() { - raids = RaidService.GetRaids(); + var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); + + if (authState.User.Identity.IsAuthenticated) + { + ulong discordId = ulong.Parse(authState.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value); + _user = UserService.GetLiebUserSmall(discordId); + } + + _raids = RaidService.GetRaids(); } }