77 lines
2.5 KiB
C#
77 lines
2.5 KiB
C#
using NPinyin;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace AnotherReplayReader
|
|
{
|
|
class PinyinReplayData
|
|
{
|
|
public Replay Replay { get; }
|
|
public string Name { get; }
|
|
public string Map { get; }
|
|
public string Mod { get; }
|
|
public List<string> Players { get; } = new List<string>();
|
|
public List<string> RealNames { get; } = new List<string>();
|
|
public List<string> Factions { get; } = new List<string>();
|
|
|
|
public PinyinReplayData(Replay replay, PlayerIdentity playerIdentity)
|
|
{
|
|
Replay = replay;
|
|
Name = replay.FileName.ToPinyin();
|
|
Map = replay.MapName.ToPinyin();
|
|
Mod = replay.Mod.ModName.ToPinyin();
|
|
foreach (var player in replay.Players)
|
|
{
|
|
void AddIfNotEmpty(List<string> target, string s)
|
|
{
|
|
s = s.ToPinyin();
|
|
if (!string.IsNullOrEmpty(s))
|
|
{
|
|
target.Add(s);
|
|
}
|
|
}
|
|
AddIfNotEmpty(Players, player.PlayerName);
|
|
AddIfNotEmpty(RealNames, playerIdentity.GetRealName(player.PlayerIP));
|
|
AddIfNotEmpty(Factions, ModData.GetFaction(replay.Mod, player.FactionID).Name);
|
|
}
|
|
}
|
|
|
|
public bool MatchPinyin(string pinyin)
|
|
{
|
|
return Name.ContainsIgnoreCase(pinyin)
|
|
|| Players.FindIndex(s => s.ContainsIgnoreCase(pinyin)) != -1
|
|
|| RealNames.FindIndex(s => s.ContainsIgnoreCase(pinyin)) != -1
|
|
|| Map.ContainsIgnoreCase(pinyin)
|
|
|| Mod.ContainsIgnoreCase(pinyin)
|
|
|| Factions.FindIndex(s => s.ContainsIgnoreCase(pinyin)) != -1;
|
|
}
|
|
}
|
|
|
|
static class PinyinExtensions
|
|
{
|
|
public static bool ContainsIgnoreCase(this string self, string s)
|
|
{
|
|
return s != null && self.IndexOf(s, StringComparison.CurrentCultureIgnoreCase) != -1;
|
|
}
|
|
|
|
public static string ToPinyin(this string self)
|
|
{
|
|
string pinyin;
|
|
try
|
|
{
|
|
pinyin = Pinyin.GetPinyin(self);
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
return pinyin.Replace(" ", "");
|
|
}
|
|
|
|
public static PinyinReplayData ToPinyin(this Replay replay, PlayerIdentity playerIdentity)
|
|
{
|
|
return new PinyinReplayData(replay, playerIdentity);
|
|
}
|
|
}
|
|
}
|