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;
|
2025-01-30 10:11:54 +03:00
|
|
|
using Nebula.Launcher.Views;
|
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-30 10:11:54 +03:00
|
|
|
[ObservableProperty] private bool _isFavoriteMode;
|
|
|
|
|
|
2025-03-14 19:42:55 +03:00
|
|
|
[ObservableProperty] private bool _isFilterVisible;
|
|
|
|
|
|
2025-01-30 20:18:40 +03:00
|
|
|
public ObservableCollection<ServerEntryModelView> Servers { get; }= new();
|
2025-02-02 10:57:29 +03:00
|
|
|
public ObservableCollection<Exception> HubErrors { get; } = new();
|
2025-01-30 20:18:40 +03:00
|
|
|
|
2024-12-27 08:22:17 +03:00
|
|
|
public Action? OnSearchChange;
|
2025-01-30 20:18:40 +03:00
|
|
|
[GenerateProperty] private HubService HubService { get; }
|
2025-01-29 12:32:42 +03:00
|
|
|
[GenerateProperty] private PopupMessageService PopupMessageService { get; }
|
2025-01-30 20:18:40 +03:00
|
|
|
[GenerateProperty] private CancellationService CancellationService { get; }
|
|
|
|
|
[GenerateProperty] private DebugService DebugService { get; }
|
|
|
|
|
[GenerateProperty, DesignConstruct] private ViewHelperService ViewHelperService { get; }
|
|
|
|
|
|
|
|
|
|
private ServerViewContainer ServerViewContainer { get; set; }
|
2025-01-30 10:11:54 +03:00
|
|
|
|
2024-12-27 19:15:33 +03:00
|
|
|
private List<ServerHubInfo> UnsortedServers { get; } = new();
|
2025-03-14 19:42:55 +03:00
|
|
|
|
|
|
|
|
private List<string> _filters = 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-03-12 14:51:47 +03:00
|
|
|
ServerViewContainer = new ServerViewContainer(this, ViewHelperService);
|
2025-02-02 10:57:29 +03:00
|
|
|
HubErrors.Add(new Exception("UVI"));
|
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-03-12 14:51:47 +03:00
|
|
|
ServerViewContainer = new ServerViewContainer(this, ViewHelperService);
|
2025-01-30 10:11:54 +03:00
|
|
|
|
2025-01-14 22:10:16 +03:00
|
|
|
foreach (var info in HubService.ServerList) UnsortedServers.Add(info);
|
|
|
|
|
|
|
|
|
|
HubService.HubServerChangedEventArgs += HubServerChangedEventArgs;
|
2025-01-30 10:11:54 +03:00
|
|
|
HubService.HubServerLoaded += UpdateServerEntries;
|
2025-02-02 10:57:29 +03:00
|
|
|
HubService.HubServerLoadingError += HubServerLoadingError;
|
2024-12-27 08:22:17 +03:00
|
|
|
OnSearchChange += OnChangeSearch;
|
2025-01-12 15:15:01 +03:00
|
|
|
|
2025-01-30 10:11:54 +03:00
|
|
|
if (!HubService.IsUpdating) UpdateServerEntries();
|
2025-01-30 20:18:40 +03:00
|
|
|
UpdateFavoriteEntries();
|
2024-12-27 08:22:17 +03:00
|
|
|
}
|
2025-01-30 10:11:54 +03:00
|
|
|
|
2025-03-14 19:42:55 +03:00
|
|
|
public void OnFilterChanged(string name, bool active)
|
|
|
|
|
{
|
|
|
|
|
DebugService.Debug($"OnFilterChanged: {name} {active}");
|
|
|
|
|
if(active)
|
|
|
|
|
_filters.Add(name);
|
|
|
|
|
else
|
|
|
|
|
_filters.Remove(name);
|
|
|
|
|
|
|
|
|
|
if(IsFavoriteMode)
|
|
|
|
|
{
|
|
|
|
|
UpdateFavoriteEntries();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
UpdateServerEntries();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-02 10:57:29 +03:00
|
|
|
private void HubServerLoadingError(Exception obj)
|
|
|
|
|
{
|
|
|
|
|
HubErrors.Add(obj);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-30 10:11:54 +03:00
|
|
|
private void UpdateServerEntries()
|
|
|
|
|
{
|
2025-03-14 17:08:35 +03:00
|
|
|
foreach(var fav in Servers.ToList()){
|
|
|
|
|
Servers.Remove(fav);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-01 13:13:49 +03:00
|
|
|
Task.Run(() =>
|
2025-01-30 10:11:54 +03:00
|
|
|
{
|
2025-01-30 20:18:40 +03:00
|
|
|
UnsortedServers.Sort(new ServerComparer());
|
2025-03-14 19:42:55 +03:00
|
|
|
foreach (var info in UnsortedServers.Where(CheckServerThink))
|
2025-01-30 10:11:54 +03:00
|
|
|
{
|
2025-02-01 13:13:49 +03:00
|
|
|
var view = ServerViewContainer.Get(info.Address.ToRobustUrl(), info.StatusData);
|
2025-01-30 20:18:40 +03:00
|
|
|
Servers.Add(view);
|
2025-01-30 10:11:54 +03:00
|
|
|
}
|
2025-01-30 20:18:40 +03:00
|
|
|
});
|
2025-01-30 10:11:54 +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;
|
2025-01-30 10:11:54 +03:00
|
|
|
|
2025-01-30 20:18:40 +03:00
|
|
|
if(IsFavoriteMode)
|
|
|
|
|
{
|
|
|
|
|
UpdateFavoriteEntries();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
UpdateServerEntries();
|
|
|
|
|
}
|
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-30 20:18:40 +03:00
|
|
|
if (obj.Action == HubServerChangeAction.Clear)
|
|
|
|
|
{
|
|
|
|
|
UnsortedServers.Clear();
|
|
|
|
|
ServerViewContainer.Clear();
|
|
|
|
|
UpdateFavoriteEntries();
|
|
|
|
|
}
|
2024-12-27 08:22:17 +03:00
|
|
|
}
|
|
|
|
|
|
2025-03-14 19:42:55 +03:00
|
|
|
private bool CheckServerThink(ServerHubInfo hubInfo)
|
2024-12-27 08:22:17 +03:00
|
|
|
{
|
2025-03-14 19:42:55 +03:00
|
|
|
var isNameEqual = string.IsNullOrEmpty(SearchText) || hubInfo.StatusData.Name.ToLower().Contains(SearchText.ToLower());
|
|
|
|
|
|
|
|
|
|
if (_filters.Count == 0) return isNameEqual;
|
|
|
|
|
if(_filters.Select(t=>t.Replace('_',':').Replace("ERPYes","18+")).Any(t=>hubInfo.StatusData.Tags.Contains(t) || hubInfo.InferredTags.Contains(t)))
|
|
|
|
|
return isNameEqual;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
2024-12-27 08:22:17 +03:00
|
|
|
}
|
2024-12-27 19:15:33 +03:00
|
|
|
|
|
|
|
|
public void FilterRequired()
|
|
|
|
|
{
|
2025-03-14 19:42:55 +03:00
|
|
|
IsFilterVisible = !IsFilterVisible;
|
2024-12-27 19:15:33 +03:00
|
|
|
}
|
|
|
|
|
|
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-02-02 10:57:29 +03:00
|
|
|
HubErrors.Clear();
|
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-30 20:18:40 +03:00
|
|
|
public class ServerViewContainer(
|
2025-03-12 14:51:47 +03:00
|
|
|
ServerListViewModel serverListViewModel,
|
2025-01-30 20:18:40 +03:00
|
|
|
ViewHelperService viewHelperService
|
|
|
|
|
)
|
2025-01-30 10:11:54 +03:00
|
|
|
{
|
2025-01-31 22:55:01 +03:00
|
|
|
private readonly Dictionary<string, ServerEntryModelView> _entries = new();
|
2025-01-30 10:11:54 +03:00
|
|
|
|
2025-01-30 20:18:40 +03:00
|
|
|
public void Clear()
|
|
|
|
|
{
|
|
|
|
|
_entries.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-01 13:13:49 +03:00
|
|
|
public ServerEntryModelView Get(RobustUrl url, ServerStatus? serverStatus = null)
|
2025-01-30 10:11:54 +03:00
|
|
|
{
|
2025-02-01 13:13:49 +03:00
|
|
|
ServerEntryModelView? entry;
|
|
|
|
|
|
2025-01-31 22:55:01 +03:00
|
|
|
lock (_entries)
|
2025-01-30 10:11:54 +03:00
|
|
|
{
|
2025-02-01 13:13:49 +03:00
|
|
|
if (_entries.TryGetValue(url.ToString(), out entry))
|
2025-01-31 22:55:01 +03:00
|
|
|
{
|
2025-02-01 13:13:49 +03:00
|
|
|
return entry;
|
2025-01-31 22:55:01 +03:00
|
|
|
}
|
2025-01-30 10:11:54 +03:00
|
|
|
|
2025-02-01 13:13:49 +03:00
|
|
|
entry = viewHelperService.GetViewModel<ServerEntryModelView>().WithData(url, serverStatus);
|
|
|
|
|
|
|
|
|
|
_entries.Add(url.ToString(), entry);
|
2025-01-30 20:18:40 +03:00
|
|
|
}
|
2025-02-01 13:13:49 +03:00
|
|
|
|
2025-01-30 20:18:40 +03:00
|
|
|
entry.OnFavoriteToggle += () =>
|
|
|
|
|
{
|
|
|
|
|
if (entry.IsFavorite) serverListViewModel.RemoveFavorite(entry);
|
|
|
|
|
else serverListViewModel.AddFavorite(entry);
|
|
|
|
|
};
|
2025-01-31 22:55:01 +03:00
|
|
|
|
2025-01-30 10:11:54 +03:00
|
|
|
return entry;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
return Compare(x.Item2, y.Item2);
|
2024-12-21 15:15:04 +03:00
|
|
|
}
|
|
|
|
|
}
|