2024-12-21 15:15:04 +03:00
|
|
|
using System;
|
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;
|
2024-12-22 16:38:47 +03:00
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
using Nebula.Launcher.Models;
|
|
|
|
|
using Nebula.Launcher.Services;
|
2024-12-21 15:15:04 +03:00
|
|
|
using Nebula.Launcher.ViewHelper;
|
|
|
|
|
using Nebula.Launcher.Views.Pages;
|
|
|
|
|
|
|
|
|
|
namespace Nebula.Launcher.ViewModels;
|
|
|
|
|
|
|
|
|
|
[ViewRegister(typeof(ServerListView))]
|
2024-12-22 16:38:47 +03:00
|
|
|
public partial class ServerListViewModel : ViewModelBase
|
2024-12-21 15:15:04 +03:00
|
|
|
{
|
2024-12-27 08:22:17 +03:00
|
|
|
public ObservableCollection<ServerHubInfo> ServerInfos { get; } = new();
|
|
|
|
|
|
|
|
|
|
public Action? OnSearchChange;
|
|
|
|
|
|
|
|
|
|
[ObservableProperty] private string _searchText;
|
2024-12-22 16:38:47 +03:00
|
|
|
|
|
|
|
|
[ObservableProperty]
|
2024-12-22 21:38:19 +03:00
|
|
|
private ServerHubInfo? _selectedListItem;
|
2024-12-26 09:49:01 +03:00
|
|
|
|
|
|
|
|
private List<ServerHubInfo> UnsortedServers { get; } = new List<ServerHubInfo>();
|
2024-12-22 21:38:19 +03:00
|
|
|
|
|
|
|
|
//Design think
|
2024-12-22 16:38:47 +03:00
|
|
|
public ServerListViewModel()
|
|
|
|
|
{
|
2024-12-23 20:45:34 +03:00
|
|
|
ServerInfos.Add(new ServerHubInfo("ss14://localhost",new ServerStatus("Nebula","TestCraft", ["16+","RU"], "super", 12,55,1,false,DateTime.Now, 20),[]));
|
2024-12-22 16:38:47 +03:00
|
|
|
}
|
2024-12-22 21:38:19 +03:00
|
|
|
|
|
|
|
|
//real think
|
2024-12-22 16:38:47 +03:00
|
|
|
public ServerListViewModel(IServiceProvider serviceProvider, HubService hubService) : base(serviceProvider)
|
|
|
|
|
{
|
|
|
|
|
hubService.HubServerChangedEventArgs += HubServerChangedEventArgs;
|
2024-12-27 08:22:17 +03:00
|
|
|
OnSearchChange += OnChangeSearch;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnChangeSearch()
|
|
|
|
|
{
|
|
|
|
|
SortServers();
|
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);
|
2024-12-22 16:38:47 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
foreach (var info in obj.Items)
|
|
|
|
|
{
|
2024-12-26 09:49:01 +03:00
|
|
|
UnsortedServers.Remove(info);
|
2024-12-22 16:38:47 +03:00
|
|
|
}
|
|
|
|
|
}
|
2024-12-26 09:49:01 +03:00
|
|
|
|
2024-12-27 08:22:17 +03:00
|
|
|
SortServers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SortServers()
|
|
|
|
|
{
|
2024-12-26 09:49:01 +03:00
|
|
|
ServerInfos.Clear();
|
|
|
|
|
UnsortedServers.Sort(new ServerComparer());
|
2024-12-27 08:22:17 +03:00
|
|
|
foreach (var server in UnsortedServers.Where(CheckServerThink))
|
2024-12-26 09:49:01 +03:00
|
|
|
{
|
2024-12-27 08:22:17 +03:00
|
|
|
ServerInfos.Add(server);
|
2024-12-26 09:49:01 +03:00
|
|
|
}
|
|
|
|
|
}
|
2024-12-27 08:22:17 +03:00
|
|
|
|
|
|
|
|
private bool CheckServerThink(ServerHubInfo hubInfo)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(SearchText)) return true;
|
|
|
|
|
return hubInfo.StatusData.Name.ToLower().Contains(SearchText.ToLower());
|
|
|
|
|
}
|
2024-12-26 09:49:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ServerComparer : IComparer<ServerHubInfo>
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
return y.StatusData.Players.CompareTo(x.StatusData.Players);
|
|
|
|
|
|
2024-12-21 15:15:04 +03:00
|
|
|
}
|
|
|
|
|
}
|