using AnotherReplayReader.Utils;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Windows;
namespace AnotherReplayReader
{
///
/// Window1.xaml 的交互逻辑
///
internal partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
Refresh(true);
}
private async void Refresh(bool showCached)
{
_setIPButton.IsEnabled = false;
try
{
var loading = new IpAndPlayer[] { new() { Ip = 0, Id = "正在加载..." } };
_dataGrid.ItemsSource = loading;
if (showCached)
{
await Display();
_dataGrid.ItemsSource = loading.Concat(_dataGrid.ItemsSource.Cast());
}
await Display();
}
catch (Exception e)
{
MessageBox.Show(this, $"无法加载IP表:{e}");
}
finally
{
_setIPButton.IsEnabled = true;
}
}
private async Task Display(string filter = "", string nameFilter = "")
{
}
private async void OnClick(object sender, RoutedEventArgs e)
{
_setIPButton.IsEnabled = false;
try
{
var ipText = _ipField.Text;
if (!IPAddress.TryParse(ipText, out var ip))
{
MessageBox.Show(this, "IP 格式不正确");
return;
}
var idText = _idField.Text;
if (string.IsNullOrWhiteSpace(idText))
{
var choice = MessageBox.Show(this, "没有输入任何关于该玩家的说明,是否继续?", "注意", MessageBoxButton.OKCancel);
if (choice != MessageBoxResult.OK)
{
return;
}
}
var result = await UpdateIpTable(ip, idText);
if (!result)
{
MessageBox.Show(this, "设置 IP 表失败");
}
Refresh(false);
}
catch (Exception exception)
{
MessageBox.Show(this, $"设置 IP 表时发生错误。\r\n{exception}");
}
finally
{
_setIPButton.IsEnabled = true;
}
}
private async Task UpdateIpTable(IPAddress ip, string idText)
{
var bytes = ip.GetAddressBytes();
var ipNum = (uint)bytes[0] * 256 * 256 * 256 + bytes[1] * 256 * 256 + bytes[2] * 256 + bytes[3];
return false;
}
private async void OnIpFieldChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
var ipText = _ipField.Text;
var idText = _idField.Text;
await Display(ipText, idText);
}
private void OnDataGridMouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
}
}
}