AnotherReplayReader/Window1.xaml.cs
2021-10-19 23:17:30 +02:00

142 lines
4.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AnotherReplayReader.Utils;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web;
using System.Windows;
using static System.Text.Json.JsonSerializer;
namespace AnotherReplayReader
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
internal partial class Window1 : Window
{
private readonly PlayerIdentity _identity;
public Window1(PlayerIdentity identity)
{
InitializeComponent();
_identity = identity;
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<IpAndPlayer>());
}
await _identity.Fetch();
await Display();
}
catch (Exception e)
{
MessageBox.Show(this, $"无法加载IP表{e}");
}
finally
{
_setIPButton.IsEnabled = true;
}
}
private async Task Display(string filter = "", string nameFilter = "")
{
var result = await Task.Run(() =>
{
var pinyin = nameFilter.ToPinyin();
var query = _identity
.AsSortedList()
.Where(x =>
{
if (!x.IpString.Contains(filter))
{
return false;
}
if (x.PinyinId?.ContainsIgnoreCase(pinyin) is true)
{
return true;
}
return x.Id.ContainsIgnoreCase(nameFilter);
});
return new ObservableCollection<IpAndPlayer>(query);
});
_dataGrid.ItemsSource = result;
}
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 static async Task<bool> 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];
var text = HttpUtility.UrlEncode(idText);
var key = HttpUtility.UrlEncode(Auth.GetKey());
var request = WebRequest.Create($"https://lanyi.altervista.org/playertable/playertable.php?do=setIP&ip={ipNum}&id={text}&key={key}");
using var response = await request.GetResponseAsync().ConfigureAwait(false);
using var stream = response.GetResponseStream();
return await DeserializeAsync<bool>(stream).ConfigureAwait(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)
{
}
}
}