Archived
1
0
This repository has been archived on 2021-10-19. You can view files and clone it, but cannot push or open issues or pull requests.
nonabgs/EDDB/Stations.cs

52 lines
1.5 KiB
C#
Raw Normal View History

using System.Globalization;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Newtonsoft.Json.Linq;
namespace NonaBGS.EDDB {
public class Stations {
private JObject root = null;
private Dictionary<int, List<string>> by_system_id = null;
public JObject JSON => root;
private Stations() {
}
private void Initialise() {
by_system_id = new Dictionary<int, List<string>>();
foreach (var station in root.Properties()) {
int id = int.Parse(station.Name);
var names = root.Value<JArray>(id.ToString()).Values<string>().ToArray();
if (!by_system_id.ContainsKey(id)) {
by_system_id[id] = new List<string>();
}
by_system_id[id].AddRange(names);
}
}
public static Stations FromFile(string filename) {
string alltext = File.ReadAllText(filename);
Stations stations = new Stations {
root = JObject.Parse(alltext),
};
stations.Initialise();
return stations;
}
public string[] StationNamesBySystemId(int systemid, string filter) {
if (!by_system_id.ContainsKey(systemid)) {
return new string[0];
}
return by_system_id[systemid]
.Where(x => CultureInfo.InvariantCulture.CompareInfo.IndexOf(x, filter, CompareOptions.IgnoreCase) > -1)
.ToArray()
;
}
}
}