LANCommander/LANCommander.Launcher/UI/Components/PlayButton.razor

174 lines
No EOL
5 KiB
Text

@implements IDisposable
@using LANCommander.Launcher.Data.Models
@using LANCommander.Launcher.Models
@using System.Diagnostics
@using LANCommander.SDK.Helpers
@inject SDK.Client Client
@inject GameService GameService
@inject LibraryService LibraryService
@inject InstallService InstallService
@inject ModalService ModalService
@inject MessageService MessageService
@inject MessageBusService MessageBusService
@if (State == PlayButtonState.Running)
{
<Popconfirm Title="Do you want to stop this game?" OkText="Yes" OnConfirm="() => Stop()">
<Button Type="ButtonType.Primary" Size="ButtonSize.Large" Icon="@IconType.Outline.CaretRight">Running</Button>
</Popconfirm>
}
else if (State == PlayButtonState.Starting)
{
<Button Type="ButtonType.Primary" Size="ButtonSize.Large" Loading="true">Starting</Button>
}
else if (State == PlayButtonState.Stopping)
{
<Button Type="ButtonType.Primary" Size="ButtonSize.Large" Loading="true">Stopping</Button>
}
else
{
<Space Direction="SpaceDirection.Horizontal">
<SpaceItem>
<Button Type="ButtonType.Primary" Size="ButtonSize.Large" Icon="@IconType.Outline.CaretRight" OnClick="() => Run()">Play</Button>
</SpaceItem>
<SpaceItem>
<Dropdown>
<Overlay>
<Menu>
<LibraryItemContextMenu Model="LibraryItem" MenuExtra="MenuExtra" />
</Menu>
</Overlay>
<ChildContent>
<Button Type="@ButtonType.Default" Size="ButtonSize.Large" Icon="@IconType.Outline.Ellipsis" />
</ChildContent>
</Dropdown>
</SpaceItem>
</Space>
}
@code {
[Parameter] public Guid LibraryItemId { get; set; }
[Parameter] public Models.ListItem LibraryItem { get; set; }
[Parameter] public RenderFragment MenuExtra { get; set; }
Timer StateTimer;
Settings Settings;
PlayButtonState State = PlayButtonState.Idle;
public enum PlayButtonState
{
Idle,
Starting,
Running,
Stopping
}
protected override async Task OnInitializedAsync()
{
StateTimer = new Timer(state => { InvokeAsync(UpdateState); }, null, 0, 250);
Settings = SettingService.GetSettings();
}
protected override async Task OnParametersSetAsync()
{
if (LibraryItem == null && LibraryItemId != Guid.Empty)
LibraryItem = await LibraryService.GetItemAsync(LibraryItemId);
}
async Task UpdateState()
{
if (Client.Games.IsRunning(LibraryItem.Key))
{
await ChangeState(PlayButtonState.Running);
}
else if (State == PlayButtonState.Running && !Client.Games.IsRunning(LibraryItem.Key))
{
await ChangeState(PlayButtonState.Idle);
}
}
async Task ChangeState(PlayButtonState state)
{
State = state;
await InvokeAsync(StateHasChanged);
}
async Task Run()
{
var game = LibraryItem.DataItem as Game;
var actions = await Client.Games.GetActionsAsync(game.InstallDirectory, game.Id);
await ChangeState(PlayButtonState.Starting);
if (actions == null || actions.Count() == 0)
{
MessageService.Error("No actions found!");
await ChangeState(PlayButtonState.Idle);
return;
}
var primaryActions = actions.Where(a => a.IsPrimaryAction);
if (primaryActions.Count() == 0)
{
MessageService.Error("No primary action found!");
await ChangeState(PlayButtonState.Idle);
return;
}
if (primaryActions.Count() == 1)
{
var task = GameService.Run(game, primaryActions.First());
await InvokeAsync(StateHasChanged);
await task;
}
else
{
var modalOptions = new ModalOptions()
{
Title = $"Play {game.Title}",
Maximizable = false,
DefaultMaximized = false,
Closable = true,
MaskClosable = true,
Footer = null,
Centered = true,
WrapClassName = "game-actions-dialog",
AfterClose = async () =>
{
Client.Lobbies.ReleaseSteam();
await ChangeState(PlayButtonState.Idle);
}
};
var model = new ActionSelectorDialogOptions
{
Actions = actions.Where(a => a.IsPrimaryAction),
Game = game
};
var modalRef = ModalService.CreateModal<ActionSelectorDialog, ActionSelectorDialogOptions, Process>(modalOptions, model);
}
}
async Task Stop()
{
await ChangeState(PlayButtonState.Stopping);
await Client.Games.Stop(LibraryItem.Key);
await ChangeState(PlayButtonState.Idle);
}
public void Dispose()
{
StateTimer.Dispose();
}
}