2024-12-21 15:15:04 +03:00
|
|
|
using System;
|
2025-01-28 19:59:35 +03:00
|
|
|
using System.Collections;
|
2024-12-26 09:49:01 +03:00
|
|
|
using System.Collections.Generic;
|
2024-12-22 16:38:47 +03:00
|
|
|
using System.Collections.ObjectModel;
|
2024-12-27 08:22:17 +03:00
|
|
|
using System.Linq;
|
2025-01-12 15:15:01 +03:00
|
|
|
using System.Threading.Tasks;
|
2024-12-22 16:38:47 +03:00
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
2025-01-14 22:10:16 +03:00
|
|
|
using Nebula.Launcher.Services;
|
2025-01-29 12:32:42 +03:00
|
|
|
using Nebula.Launcher.ViewModels.Popup;
|
2024-12-21 15:15:04 +03:00
|
|
|
using Nebula.Launcher.Views.Pages;
|
2025-01-05 17:05:23 +03:00
|
|
|
using Nebula.Shared.Models;
|
|
|
|
|
using Nebula.Shared.Services;
|
2025-01-28 19:59:35 +03:00
|
|
|
using Nebula.Shared.Utils;
|
2024-12-21 15:15:04 +03:00
|
|
|
|
2025-01-14 22:10:16 +03:00
|
|
|
namespace Nebula.Launcher.ViewModels.Pages;
|
2024-12-21 15:15:04 +03:00
|
|
|
|
2025-01-05 17:05:23 +03:00
|
|
|
[ViewModelRegister(typeof(ServerListView))]
|
2025-01-14 22:10:16 +03:00
|
|
|
[ConstructGenerator]
|
2025-01-29 12:32:42 +03:00
|
|
|
public partial class ServerListViewModel : ViewModelBase, IViewModelPage
|
2024-12-21 15:15:04 +03:00
|
|
|
{
|
2025-01-14 22:10:16 +03:00
|
|
|
[ObservableProperty] private string _searchText = string.Empty;
|
2024-12-27 08:22:17 +03:00
|
|
|
|
2025-01-29 12:32:42 +03:00
|
|
|
[ObservableProperty] private bool _isFavoriteMode;
|
|
|
|
|
|
2024-12-27 08:22:17 +03:00
|
|
|
public Action? OnSearchChange;
|
2025-01-14 22:10:16 +03:00
|
|
|
[GenerateProperty] private HubService HubService { get; } = default!;
|
2025-01-29 12:32:42 +03:00
|
|
|
[GenerateProperty] private PopupMessageService PopupMessageService { get; }
|
2025-01-22 21:06:05 +03:00
|
|
|
[GenerateProperty, DesignConstruct] private ViewHelperService ViewHelperService { get; } = default!;
|
2025-01-29 12:32:42 +03:00
|
|
|
public ObservableCollection<ServerEntryModelView> SortedServers { get; } = new();
|
2024-12-27 19:15:33 +03:00
|
|
|
private List<ServerHubInfo> UnsortedServers { get; } = new();
|
2025-01-14 22:10:16 +03:00
|
|
|
|
2024-12-22 21:38:19 +03:00
|
|
|
//Design think
|
2025-01-14 22:10:16 +03:00
|
|
|
protected override void InitialiseInDesignMode()
|
2024-12-22 16:38:47 +03:00
|
|
|
{
|
2025-01-28 19:59:35 +03:00
|
|
|
FavoriteVisible = true;
|
|
|
|
|
SortedFavoriteServers.Add(GetServerEntryModelView(("ss14://localhost".ToRobustUrl(),
|
|
|
|
|
new ServerStatus("Nebula", "TestCraft", ["16+", "RU"], "super", 12, 55, 1, false, DateTime.Now, 20))));
|
2025-01-29 12:32:42 +03:00
|
|
|
SortedServers.Add(CreateServerView(new ServerHubInfo("ss14://localhost",
|
2025-01-14 22:10:16 +03:00
|
|
|
new ServerStatus("Nebula", "TestCraft", ["16+", "RU"], "super", 12, 55, 1, false, DateTime.Now, 20), [])));
|
2025-01-29 12:32:42 +03:00
|
|
|
SortedServers.Add(CreateServerView(new ServerHubInfo("ss14://localhost",
|
2025-01-16 21:07:50 +03:00
|
|
|
new ServerStatus("Nebula", "TestCraft", ["16+", "RU"], "super", 12, 55, 1, false, DateTime.Now, 20), [])));
|
2025-01-29 12:32:42 +03:00
|
|
|
SortedServers.Add(CreateServerView(new ServerHubInfo("ss14://localhost",
|
2025-01-16 21:07:50 +03:00
|
|
|
new ServerStatus("Nebula", "TestCraft", ["16+", "RU"], "super", 12, 55, 1, false, DateTime.Now, 20), [])));
|
2024-12-22 16:38:47 +03:00
|
|
|
}
|
2025-01-14 22:10:16 +03:00
|
|
|
|
2024-12-22 21:38:19 +03:00
|
|
|
//real think
|
2025-01-14 22:10:16 +03:00
|
|
|
protected override void Initialise()
|
2024-12-22 16:38:47 +03:00
|
|
|
{
|
2025-01-14 22:10:16 +03:00
|
|
|
foreach (var info in HubService.ServerList) UnsortedServers.Add(info);
|
|
|
|
|
|
|
|
|
|
HubService.HubServerChangedEventArgs += HubServerChangedEventArgs;
|
2025-01-27 15:55:30 +03:00
|
|
|
HubService.HubServerLoaded += SortServers;
|
2024-12-27 08:22:17 +03:00
|
|
|
OnSearchChange += OnChangeSearch;
|
2025-01-12 15:15:01 +03:00
|
|
|
|
2025-01-14 22:10:16 +03:00
|
|
|
if (!HubService.IsUpdating) SortServers();
|
2025-01-28 19:59:35 +03:00
|
|
|
FetchFavorite();
|
2024-12-27 08:22:17 +03:00
|
|
|
}
|
2025-01-27 15:55:30 +03:00
|
|
|
|
2024-12-27 08:22:17 +03:00
|
|
|
private void OnChangeSearch()
|
|
|
|
|
{
|
2025-01-27 15:55:30 +03:00
|
|
|
if(string.IsNullOrEmpty(SearchText)) return;
|
2024-12-27 08:22:17 +03:00
|
|
|
SortServers();
|
2025-01-28 19:59:35 +03:00
|
|
|
SortFavorite();
|
2024-12-22 16:38:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HubServerChangedEventArgs(HubServerChangedEventArgs obj)
|
2024-12-21 15:15:04 +03:00
|
|
|
{
|
2024-12-22 16:38:47 +03:00
|
|
|
if (obj.Action == HubServerChangeAction.Add)
|
|
|
|
|
foreach (var info in obj.Items)
|
2024-12-26 09:49:01 +03:00
|
|
|
UnsortedServers.Add(info);
|
2025-01-14 22:10:16 +03:00
|
|
|
if (obj.Action == HubServerChangeAction.Remove)
|
2024-12-22 16:38:47 +03:00
|
|
|
foreach (var info in obj.Items)
|
2024-12-26 09:49:01 +03:00
|
|
|
UnsortedServers.Remove(info);
|
2025-01-14 22:10:16 +03:00
|
|
|
if (obj.Action == HubServerChangeAction.Clear) UnsortedServers.Clear();
|
2024-12-27 08:22:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SortServers()
|
|
|
|
|
{
|
2025-01-12 15:15:01 +03:00
|
|
|
Task.Run(() =>
|
2024-12-26 09:49:01 +03:00
|
|
|
{
|
2025-01-29 12:32:42 +03:00
|
|
|
SortedServers.Clear();
|
2025-01-12 15:15:01 +03:00
|
|
|
UnsortedServers.Sort(new ServerComparer());
|
2025-01-29 12:32:42 +03:00
|
|
|
foreach (var server in UnsortedServers.Where(a => CheckServerThink(a.StatusData))) SortedServers.Add(CreateServerView(server));
|
2025-01-12 15:15:01 +03:00
|
|
|
});
|
2024-12-26 09:49:01 +03:00
|
|
|
}
|
2024-12-27 08:22:17 +03:00
|
|
|
|
2025-01-28 19:59:35 +03:00
|
|
|
private bool CheckServerThink(ServerStatus hubInfo)
|
2024-12-27 08:22:17 +03:00
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(SearchText)) return true;
|
2025-01-28 19:59:35 +03:00
|
|
|
return hubInfo.Name.ToLower().Contains(SearchText.ToLower());
|
2024-12-27 08:22:17 +03:00
|
|
|
}
|
2024-12-27 19:15:33 +03:00
|
|
|
|
|
|
|
|
private ServerEntryModelView CreateServerView(ServerHubInfo serverHubInfo)
|
|
|
|
|
{
|
2025-01-28 19:59:35 +03:00
|
|
|
var svn = ViewHelperService.GetViewModel<ServerEntryModelView>().WithData(serverHubInfo);
|
|
|
|
|
svn.OnFavoriteToggle += () => AddFavorite(svn);
|
2025-01-05 17:05:23 +03:00
|
|
|
return svn;
|
2024-12-27 19:15:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void FilterRequired()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-29 12:32:42 +03:00
|
|
|
public void AddFavoriteRequired()
|
|
|
|
|
{
|
|
|
|
|
var p = ViewHelperService.GetViewModel<AddFavoriteViewModel>();
|
|
|
|
|
PopupMessageService.Popup(p);
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-27 19:15:33 +03:00
|
|
|
public void UpdateRequired()
|
|
|
|
|
{
|
2025-01-14 22:10:16 +03:00
|
|
|
Task.Run(HubService.UpdateHub);
|
2024-12-27 19:15:33 +03:00
|
|
|
}
|
2025-01-29 12:32:42 +03:00
|
|
|
|
|
|
|
|
public void OnPageOpen(object? args)
|
|
|
|
|
{
|
|
|
|
|
if (args is bool fav)
|
|
|
|
|
{
|
|
|
|
|
IsFavoriteMode = fav;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-26 09:49:01 +03:00
|
|
|
}
|
|
|
|
|
|
2025-01-28 19:59:35 +03:00
|
|
|
public class ServerComparer : IComparer<ServerHubInfo>, IComparer<ServerStatus>, IComparer<(RobustUrl,ServerStatus)>
|
2024-12-26 09:49:01 +03:00
|
|
|
{
|
|
|
|
|
public int Compare(ServerHubInfo? x, ServerHubInfo? y)
|
|
|
|
|
{
|
|
|
|
|
if (ReferenceEquals(x, y))
|
|
|
|
|
return 0;
|
|
|
|
|
if (ReferenceEquals(null, y))
|
|
|
|
|
return 1;
|
|
|
|
|
if (ReferenceEquals(null, x))
|
|
|
|
|
return -1;
|
|
|
|
|
|
2025-01-28 19:59:35 +03:00
|
|
|
return Compare(x.StatusData, y.StatusData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Compare(ServerStatus? x, ServerStatus? y)
|
|
|
|
|
{
|
|
|
|
|
if (ReferenceEquals(x, y))
|
|
|
|
|
return 0;
|
|
|
|
|
if (ReferenceEquals(null, y))
|
|
|
|
|
return 1;
|
|
|
|
|
if (ReferenceEquals(null, x))
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
return y.Players.CompareTo(x.Players);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Compare((RobustUrl, ServerStatus) x, (RobustUrl, ServerStatus) y)
|
|
|
|
|
{
|
|
|
|
|
if (ReferenceEquals(x, y))
|
|
|
|
|
return 0;
|
|
|
|
|
if (ReferenceEquals(null, y))
|
|
|
|
|
return 1;
|
|
|
|
|
if (ReferenceEquals(null, x))
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
return Compare(x.Item2, y.Item2);
|
2024-12-21 15:15:04 +03:00
|
|
|
}
|
|
|
|
|
}
|