Skip to content

Commit

Permalink
Add map bans WebUI page
Browse files Browse the repository at this point in the history
  • Loading branch information
matte-ek committed Jul 27, 2023
1 parent e761f68 commit 4dffc32
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 5 deletions.
17 changes: 17 additions & 0 deletions BanchoMultiplayerBot.Database/Repositories/MapBanRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ public async Task<bool> IsMapBanned(int? beatmapSetId, int? beatmapId)
.AnyAsync();
}

public async Task<IReadOnlyList<MapBan>> GetAll()
{
return await _botDbContext.MapBans.AsNoTracking().ToListAsync();
}

public async Task RemoveAsync(MapBan mapBan)
{
var entity = await _botDbContext.MapBans.FirstOrDefaultAsync(x => x.Id == mapBan.Id);

if (entity == null)
{
return;
}

_botDbContext.Remove(entity);
}

public async Task Save()
{
await _botDbContext.SaveChangesAsync();
Expand Down
38 changes: 38 additions & 0 deletions BanchoMultiplayerBot.Host.Web/Pages/Dialogs/AddMapBanDialog.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
@using BanchoMultiplayerBot.Data
@using BanchoMultiplayerBot.Database.Models

<MudDialog>
<TitleContent>
<MudText Typo="Typo.h6">
Add Map Ban
</MudText>
</TitleContent>
<DialogContent>
<MudTextField @bind-Value="MapBan.BeatmapId" Label="Map Id" Required="false"/>
<MudTextField @bind-Value="MapBan.BeatmapSetId" Label="Map Set Id" Required="false"/>
</DialogContent>
<DialogActions>
<MudButton OnClick="Cancel">Cancel</MudButton>
<MudButton Color="Color.Primary" OnClick="OkButtonPressed">OK</MudButton>
</DialogActions>
</MudDialog>

@code {

[CascadingParameter]
MudDialogInstance MudDialog { get; set; }

[Parameter]
public MapBan MapBan { get; set; } = null!;

private void Cancel()
{
MudDialog.Cancel();
}

private void OkButtonPressed()
{
MudDialog.Close(DialogResult.Ok(MapBan));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
@implements IDisposable
@inject IDialogService DialogService
@page "/settings/map-bans"
@using BanchoMultiplayerBot.Database.Models
@using BanchoMultiplayerBot.Database.Repositories
@using BanchoMultiplayerBot.Host.Web.Pages.Dialogs

<PageTitle>Map Bans</PageTitle>

<MudText Typo="Typo.h4" GutterBottom="true">Map Bans</MudText>

<MudForm @ref="_form">
<MudTable Class="pa-0 ma-2" Items="@_mapBans" Hover="true" Breakpoint="Breakpoint.Sm">
<ColGroup>
<col style="width: 50%;"/>
<col style="width: 40px;"/>
<col style="width: 120px;"/>
</ColGroup>
<HeaderContent>
<MudTh>Map Set Id</MudTh>
<MudTh>Map Id</MudTh>
<MudTh>Options</MudTh>
</HeaderContent>
<RowTemplate>
<MudTd DataLabel="Map Set Id">@(context.BeatmapSetId?.ToString() ?? "N/A")</MudTd>
<MudTd DataLabel="Map Id">@(context.BeatmapId?.ToString() ?? "N/A")</MudTd>
<MudTd DataLabel="Options">
<MudIconButton Icon="@Icons.Material.Filled.Delete" Title="Remove" OnClick="(() => RemoveBan(context))"/>
</MudTd>
</RowTemplate>
</MudTable>
<MudPaper Class="pa-2 ma-2">
<MudButton Variant="Variant.Outlined" Color="Color.Primary" OnClick="@AddBan">Add New</MudButton>
</MudPaper>
</MudForm>

@code {
private readonly DialogOptions _maxWidthOptions = new DialogOptions() { MaxWidth = MaxWidth.Medium, FullWidth = true };
private MudForm? _form;
private List<MapBan>? _mapBans;

protected override async void OnInitialized()
{
await UpdateMapBans();
}

private async Task UpdateMapBans()
{
using var mapBanRepository = new MapBanRepository();

_mapBans = (await mapBanRepository.GetAll()).ToList();
}

private async void AddBan()
{
var mapBan = new MapBan();

var parameters = new DialogParameters { ["MapBan"] = mapBan };

var dialog = DialogService.Show<AddMapBanDialog>("AddMapBan", parameters, _maxWidthOptions);
var result = await dialog.Result;

if (result.Cancelled)
{
return;
}

if (mapBan.BeatmapId == null &&
mapBan.BeatmapSetId == null)
{
return;
}

using var mapBanRepository = new MapBanRepository();

await mapBanRepository.AddMapBan(mapBan.BeatmapSetId, mapBan.BeatmapId);
await mapBanRepository.Save();
await UpdateMapBans();

// Force update the page in case Blazor doesn't update automatically
await InvokeAsync(StateHasChanged).ConfigureAwait(false);
}

private async void RemoveBan(MapBan mapBan)
{
using var mapBanRepository = new MapBanRepository();

await mapBanRepository.RemoveAsync(mapBan);
await mapBanRepository.Save();

await UpdateMapBans();

// Force update the page in case Blazor doesn't update automatically
await InvokeAsync(StateHasChanged).ConfigureAwait(false);
}

public void Dispose()
{
}

}
9 changes: 4 additions & 5 deletions BanchoMultiplayerBot.Host.Web/Shared/NavMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

{
<MudNavLink Href="@($"lobby/{i}")" Match="NavLinkMatch.All" Icon="@Icons.Material.Filled.Tag">@lobby.Configuration.Name</MudNavLink>

i++;
}
}
Expand All @@ -20,6 +20,8 @@
<MudNavGroup Title="Settings" Icon="@Icons.Material.Filled.Settings">
<MudNavLink Href="settings/general" Icon="@Icons.Material.Filled.Build">General</MudNavLink>
<MudNavLink Href="settings/announcements" Icon="@Icons.Material.Filled.Campaign">Announcements</MudNavLink>
<MudNavLink Href="settings/map-bans" Icon="@Icons.Material.Filled.DoNotDisturb">Map Bans</MudNavLink>
<MudNavLink Href="settings/player-bans" Icon="@Icons.Material.Filled.PersonRemove">Player Bans</MudNavLink>
<MudNavLink Href="settings/admins" Icon="@Icons.Material.Filled.Security">Administrators</MudNavLink>
<MudNavLink Href="settings/auth" Icon="@Icons.Material.Filled.Lock">Authentication</MudNavLink>
</MudNavGroup>
Expand All @@ -39,10 +41,7 @@

public async void OnLobbiesUpdated()
{
await InvokeAsync(() =>
{
StateHasChanged();
});
await InvokeAsync(() => { StateHasChanged(); });
}

}

0 comments on commit 4dffc32

Please sign in to comment.