Lieb-Website/Lieb/Pages/Raids/RaidRoles.razor
2022-03-14 22:21:10 +01:00

108 lines
4.9 KiB
Text

@using Lieb.Data
@using Lieb.Models
@using Lieb.Models.GuildWars2
@using Lieb.Models.GuildWars2.Raid
@inject RaidService RaidService
<table class="table">
<tbody>
@{
RaidSignUp userRole = _raid.SignUps.Where(s => s.LiebUserId == _liebUserId).FirstOrDefault();
bool isSignedUp = userRole != null;
}
@foreach (PlannedRaidRole role in _raid.Roles.OrderBy(r => r.PlannedRaidRoleId))
{
RaidSignUp[] signUps = _raid.SignUps.Where(s => s.PlannedRaidRoleId == role.PlannedRaidRoleId).ToArray();
int usedSpots = signUps.Where(s => s.SignUpType == SignUpType.SignedUp).Count();
<tr>
<td><b>@role.Name</b> (@usedSpots /@role.Spots) <br> @role.Description </td>
<td>
<table>
@foreach (var signUp in signUps)
{
@if(signUp.SignUpType != SignUpType.SignedOff)
{
<tr>
@{bool isUser = isSignedUp && userRole.PlannedRaidRole.PlannedRaidRoleId == role.PlannedRaidRoleId && signUp.LiebUserId == _liebUserId;}
@{string signUpStatus = string.Empty;}
@if (signUp.SignUpType != SignUpType.SignedUp) signUpStatus = $" - {signUp.SignUpType}";
@if (isUser && _usableAccounts.Count > 1)
{
<td>@signUp.LiebUser.Name
<select value=@signUp.GuildWars2AccountId @onchange="args => ChangeAccount(_user, args)">
@foreach (var account in _usableAccounts)
{
<option value=@account.GuildWars2AccountId>@account.AccountName</option>
}
</select> @signUpStatus </td>
}
else
{
<td>@signUp.LiebUser.Name (@signUp.GuildWars2Account.AccountName) @signUpStatus</td>
}
</tr>
}
}
</table>
</td>
@if(_liebUserId > 0)
{
bool notIsRoleSignUpAllowed = !RaidService.IsRoleSignUpAllowed(_raid.RaidId, _user.LiebUserId, role.PlannedRaidRoleId, SignUpType.SignedUp, false);
bool notIsBackupAllowed = _raid.RaidType != RaidType.Planned && notIsRoleSignUpAllowed;
<td><button @onclick="() => SignUpClicked(role, _user, isSignedUp, SignUpType.SignedUp)" disabled="@notIsRoleSignUpAllowed">Sign Up</button></td>
<td><button @onclick="() => SignUpClicked(role, _user, isSignedUp, SignUpType.Maybe)" disabled="@notIsRoleSignUpAllowed">Maybe</button></td>
<td><button @onclick="() => SignUpClicked(role, _user, isSignedUp, SignUpType.Backup)" disabled="@notIsBackupAllowed">Backup</button></td>
@if (isSignedUp && userRole.SignUpType != SignUpType.SignedOff && _raid.RaidType == RaidType.Planned)
{
<td><button @onclick="() => SignUpClicked(role, _user, isSignedUp, SignUpType.Flex)">Flex</button></td>
}
}
</tr>
}
</tbody>
</table>
@code {
[Parameter]
public Raid _raid { get; set; }
[Parameter]
public LiebUser? _user { get; set; }
private int _liebUserId { get; set; } = -1;
private List<GuildWars2Account> _usableAccounts;
protected override async Task OnParametersSetAsync()
{
if (_user != null)
{
_usableAccounts = _user.GuildWars2Accounts.Where(a => a.EquippedBuilds.Count > 0).ToList();
_liebUserId = _user.LiebUserId;
}
}
async Task SignUpClicked(PlannedRaidRole role, LiebUser liebUser, bool isSignedUp, SignUpType signUpType)
{
if(isSignedUp && signUpType != SignUpType.Flex)
{
await RaidService.ChangeSignUpType(_raid.RaidId, liebUser.LiebUserId, role.PlannedRaidRoleId, signUpType);
}
else
{
await RaidService.SignUp(_raid.RaidId, liebUser.LiebUserId, _usableAccounts.FirstOrDefault().GuildWars2AccountId, role.PlannedRaidRoleId, signUpType);
}
_raid = RaidService.GetRaid(_raid.RaidId);
}
async Task ChangeAccount(LiebUser liebUser, ChangeEventArgs e)
{
int accountId = int.Parse(e.Value.ToString());
await RaidService.ChangeAccount(_raid.RaidId, liebUser.LiebUserId, accountId);
_raid = RaidService.GetRaid(_raid.RaidId);
}
}