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

149 lines
4.3 KiB
Text
Raw Permalink Normal View History

@implements IDisposable
@using LANCommander.Launcher.Data.Models
@using LANCommander.Launcher.Models
2024-06-10 23:29:19 -05:00
@using System.Diagnostics
@using LANCommander.SDK.Helpers
@inject SDK.Client Client
2024-06-10 23:29:19 -05:00
@inject GameService GameService
@inject LibraryService LibraryService
@inject InstallService InstallService
2024-06-10 23:29:19 -05:00
@inject ModalService ModalService
2024-08-01 17:27:35 -05:00
@inject MessageService MessageService
2024-07-11 18:32:09 -05:00
@inject MessageBusService MessageBusService
2024-06-10 23:29:19 -05:00
@if (IsRunning)
2024-06-10 23:29:19 -05:00
{
<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
{
<Space Direction="@DirectionVHType.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" />
2024-06-10 23:29:19 -05:00
</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; }
2024-11-09 17:15:04 -06:00
[Parameter] public Models.ListItem LibraryItem { get; set; }
[Parameter] public RenderFragment MenuExtra { get; set; }
2024-06-10 23:29:19 -05:00
Timer StateTimer;
Settings Settings;
bool IsRunning = false;
2024-07-11 18:32:09 -05:00
protected override async Task OnInitializedAsync()
{
StateTimer = new Timer(state => { InvokeAsync(UpdateState); }, null, 0, 250);
Settings = SettingService.GetSettings();
2024-07-11 18:32:09 -05:00
}
2024-06-10 23:29:19 -05:00
protected override async Task OnParametersSetAsync()
{
if (LibraryItem == null && LibraryItemId != Guid.Empty)
2024-11-09 17:15:04 -06:00
LibraryItem = await LibraryService.GetItemAsync(LibraryItemId);
2024-06-10 23:29:19 -05:00
}
async Task UpdateState()
2024-07-11 18:32:09 -05:00
{
if (!IsRunning && Client.Games.IsRunning(LibraryItem.Key))
{
IsRunning = true;
await InvokeAsync(StateHasChanged);
}
else if (IsRunning && !Client.Games.IsRunning(LibraryItem.Key))
2024-07-11 18:32:09 -05:00
{
IsRunning = false;
2024-07-11 18:32:09 -05:00
await InvokeAsync(StateHasChanged);
}
}
2024-06-10 23:29:19 -05:00
async Task Run()
{
var game = LibraryItem.DataItem as Game;
var actions = await Client.Games.GetActionsAsync(game.InstallDirectory, game.Id);
2024-06-10 23:29:19 -05:00
2024-08-01 17:27:35 -05:00
if (actions == null || actions.Count() == 0)
{
MessageService.Error("No actions found!");
return;
}
2024-06-10 23:29:19 -05:00
var primaryActions = actions.Where(a => a.IsPrimaryAction);
2024-08-01 17:27:35 -05:00
if (primaryActions.Count() == 0)
{
MessageService.Error("No primary action found!");
return;
}
2024-06-10 23:29:19 -05:00
if (primaryActions.Count() == 1)
{
var task = GameService.Run(game, primaryActions.First());
2024-06-10 23:29:19 -05:00
await InvokeAsync(StateHasChanged);
await task;
}
else
{
var modalOptions = new ModalOptions()
{
Title = $"Play {game.Title}",
2024-06-10 23:29:19 -05:00
Maximizable = false,
DefaultMaximized = false,
Closable = true,
Footer = null,
Centered = true,
WrapClassName = "game-actions-dialog",
OnCancel = async (e) =>
{
Client.Lobbies.ReleaseSteam();
},
AfterClose = async () =>
{
Client.Lobbies.ReleaseSteam();
}
2024-06-10 23:29:19 -05:00
};
var model = new ActionSelectorDialogOptions
{
Actions = actions.Where(a => a.IsPrimaryAction),
Game = game
2024-06-10 23:29:19 -05:00
};
var modalRef = ModalService.CreateModal<ActionSelectorDialog, ActionSelectorDialogOptions, Process>(modalOptions, model);
}
}
async Task Stop()
{
await Client.Games.Stop(LibraryItem.Key);
2024-06-10 23:29:19 -05:00
await InvokeAsync(StateHasChanged);
}
public void Dispose()
{
StateTimer.Dispose();
}
2024-06-10 23:29:19 -05:00
}