1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2024-10-17 03:42:50 +02:00
Minecraft-Phantom-Panel/Web/Phantom.Web/Pages/Instances.razor

106 lines
4.0 KiB
Plaintext

@page "/instances"
@attribute [Authorize(Permission.ViewInstancesPolicy)]
@using System.Collections.Immutable
@using Phantom.Common.Data.Web.Instance
@using Phantom.Common.Data.Web.Users
@using Phantom.Web.Services.Agents
@using Phantom.Web.Services.Authorization
@using Phantom.Web.Services.Instances
@inherits PhantomComponent
@inject AgentManager AgentManager
@inject InstanceManager InstanceManager
<h1>Instances</h1>
<PermissionView Permission="Permission.CreateInstances">
<a href="instances/create" class="btn btn-primary" role="button">New Instance</a>
</PermissionView>
<Table TItem="Instance" Items="instances" ItemUrl="@(static instance => "instances/" + instance.InstanceGuid)">
<HeaderRow>
<Column Width="40%">Agent</Column>
<Column Width="40%">Name</Column>
<Column MinWidth="215px">Status</Column>
<Column Class="text-center" MinWidth="120px">Players</Column>
<Column Width="20%">Version</Column>
<Column Class="text-center" MinWidth="110px">Server Port</Column>
<Column Class="text-center" MinWidth="110px">Rcon Port</Column>
<Column Class="text-end" MinWidth="90px">Memory</Column>
<Column MinWidth="75px">Actions</Column>
</HeaderRow>
<ItemRow Context="instance">
@{ var configuration = instance.Configuration; }
<Cell>
<p class="fw-semibold">@(agentNamesByGuid.TryGetValue(configuration.AgentGuid, out var name) ? name : string.Empty)</p>
<small class="font-monospace text-uppercase">@configuration.AgentGuid.ToString()</small>
</Cell>
<Cell>
<p class="fw-semibold">@configuration.InstanceName</p>
<small class="font-monospace text-uppercase">@instance.InstanceGuid.ToString()</small>
</Cell>
<Cell>
<InstanceStatusText Status="instance.Status" />
</Cell>
<Cell class="text-center">
@if (instance.PlayerCounts is var (online, maximum)) {
<p class="font-monospace">@online.ToString() / @maximum.ToString()</p>
}
else {
<p class="font-monospace">-</p>
}
</Cell>
<Cell>@configuration.MinecraftServerKind @configuration.MinecraftVersion</Cell>
<Cell class="text-center">
<p class="font-monospace">@configuration.ServerPort.ToString()</p>
</Cell>
<Cell class="text-center">
<p class="font-monospace">@configuration.RconPort.ToString()</p>
</Cell>
<Cell class="text-end">
<p class="font-monospace">@configuration.MemoryAllocation.InMegabytes.ToString() MB</p>
</Cell>
<Cell>
<a href="instances/@instance.InstanceGuid.ToString()" class="btn btn-info btn-sm">Detail</a>
</Cell>
</ItemRow>
<NoItemsRow>
No instances found.
</NoItemsRow>
</Table>
@code {
private ImmutableDictionary<Guid, string> agentNamesByGuid = ImmutableDictionary<Guid, string>.Empty;
private ImmutableArray<Instance>? instances;
protected override void OnInitialized() {
AgentManager.AgentsChanged.Subscribe(this, agents => {
this.agentNamesByGuid = agents.ToImmutableDictionary(static agent => agent.AgentGuid, static agent => agent.Configuration.AgentName);
InvokeAsync(StateHasChanged);
});
}
protected override async Task OnInitializedAsync() {
var authenticatedUser = await GetAuthenticatedUser();
if (authenticatedUser == null) {
instances = ImmutableArray<Instance>.Empty;
return;
}
InstanceManager.InstancesChanged.Subscribe(this, instances => {
this.instances = instances.Values
.Where(instance => authenticatedUser.Info.HasAccessToAgent(instance.Configuration.AgentGuid))
.OrderBy(instance => agentNamesByGuid.TryGetValue(instance.Configuration.AgentGuid, out var agentName) ? agentName : string.Empty)
.ThenBy(static instance => instance.Configuration.InstanceName)
.ToImmutableArray();
InvokeAsync(StateHasChanged);
});
}
protected override void OnDisposed() {
AgentManager.AgentsChanged.Unsubscribe(this);
InstanceManager.InstancesChanged.Unsubscribe(this);
}
}