LANCommander/LANCommander.Server/UI/Pages/Settings/Integrations/Steam.razor

152 lines
4.7 KiB
Text

@page "/Settings/Integrations/Steam"
@using LANCommander.Server.Settings
@using LANCommander.Server.UI.Pages.Settings.Components
@using LANCommander.Steam.Abstractions
@using LANCommander.Steam.Enums
@using LANCommander.Steam.Models
@using Microsoft.Extensions.Options
@inject ISteamCmdService SteamCmdService
@inject ISteamCmdProfileStore SteamCmdProfileStore
@inject IOptions<Settings> Settings
@inject SettingsProvider<Settings> SettingsProvider
@inject IMessageService MessageService
@inject ILogger<Beacon> Logger
@attribute [Authorize(Roles = RoleService.AdministratorRoleName)]
<PageHeader Title="Steam">
<PageHeaderExtra>
<Button OnClick="Save" Type="@ButtonType.Primary">Save</Button>
</PageHeaderExtra>
</PageHeader>
<PageContent>
<Form Model="Settings.Value" Layout="@FormLayout.Vertical">
<FormItem Label="SteamCMD Path">
<FilePicker @bind-Value="SteamCmdService.ExecutablePath" Title="Choose SteamCMD Executable Path" Root="@_rootPath">
<After>
<Tooltip Title="Auto Detect">
<Button OnClick="AutoDetectPath" Icon="@IconType.Outline.Search"/>
</Tooltip>
</After>
</FilePicker>
</FormItem>
</Form>
<h2>Profiles</h2>
<Flex Direction="FlexDirection.Vertical" Gap="FlexGap.Large">
<Collapse>
@foreach (var profile in _profiles)
{
<Panel Header="@(String.IsNullOrWhiteSpace(profile.Username) ? "New Profile" : profile.Username)">
<ExtraTemplate>
<Flex Gap="FlexGap.Small">
<Button Type="ButtonType.Text" Icon="@IconType.Outline.Close" Size="ButtonSize.Small" Danger OnClick="() => RemoveProfile(profile)"/>
</Flex>
</ExtraTemplate>
<ChildContent>
<SteamProfileEditor
@bind-Username="profile.Username"
@bind-InstallDirectory="profile.InstallDirectory"
OnAuthenticated="Save"/>
</ChildContent>
</Panel>
}
</Collapse>
<Flex Justify="FlexJustify.End">
<Button OnClick="AddProfile">Add Profile</Button>
</Flex>
</Flex>
</PageContent>
@code {
string _rootPath = Path.GetPathRoot(Directory.GetCurrentDirectory());
SteamCmdConnectionStatus _connectionStatus;
List<SteamCmdProfile> _profiles = [];
string _username;
string _password;
bool _processingLogin;
protected override async Task OnInitializedAsync()
{
_profiles = (await SteamCmdService.GetProfilesAsync()).ToList();
if (!_profiles.Any())
AddProfile();
}
void AddProfile()
{
_profiles.Add(new SteamCmdProfile());
}
async Task RemoveProfile(SteamCmdProfile profile)
{
if (!string.IsNullOrWhiteSpace(profile.Username))
{
var logoutMessage = new MessageConfig
{
Content = "Logging out of Steam...",
Duration = 0,
Key = Guid.NewGuid().ToString()
};
MessageService.Loading(logoutMessage);
await SteamCmdService.LogoutAsync(profile.Username);
await SteamCmdService.DeleteProfileAsync(profile.Username);
logoutMessage.Content = "Successfully logged out from Steam!";
logoutMessage.Duration = 3;
_profiles = (await SteamCmdService.GetProfilesAsync()).ToList();
MessageService.Success(logoutMessage);
}
if (!_profiles.Any())
AddProfile();
}
async Task Save()
{
try
{
foreach (var profile in _profiles)
{
await SteamCmdService.SaveProfileAsync(profile);
}
SettingsProvider.Update(s => s.Steam.Path = SteamCmdService.ExecutablePath);
MessageService.Success("Settings saved!");
}
catch (Exception ex)
{
MessageService.Error("An unknown error occurred.");
Logger.LogError(ex, "An unknown error occurred.");
}
}
async Task AutoDetectPath()
{
var path = await SteamCmdService.AutoDetectSteamCmdPathAsync();
if (!string.IsNullOrWhiteSpace(path))
{
SteamCmdService.ExecutablePath = path;
SettingsProvider.Update(s =>
{
s.Steam.Path = path;
});
}
else
MessageService.Error("Could not automatically find SteamCMD! Is it installed?");
}
}