Archived
1
0

add autocompletion for system names

This commit is contained in:
Florian Stinglmayr 2021-07-09 14:40:27 +02:00
parent fca1e607ec
commit 14c14f7626
8 changed files with 175 additions and 71 deletions

59
EDDB/API.cs Normal file
View File

@ -0,0 +1,59 @@
using System;
using System.IO;
using System.Net;
namespace NonaBGS.EDDB {
public class API {
private static readonly string EDDB_SYSTEMS_ARCHIVE = "https://eddb.io/archive/v6/systems_populated.json";
private string cache_folder = null;
private readonly WebClient client = new WebClient();
private string systems_file = null;
public string SystemsFile => systems_file;
public string Cache {
get => cache_folder;
set => cache_folder = value;
}
public API(string cache_folder) {
Initialise(cache_folder);
}
private void Initialise(string cache_folder) {
this.cache_folder = cache_folder;
systems_file = Path.Combine(this.cache_folder, "systems_populated.json");
client.DownloadDataCompleted += Client_DownloadDataCompleted;
}
private void DownloadFile(string url, string file) {
client.DownloadFileAsync(new Uri(url), file);
}
public void Download(bool force) {
if (!HaveSystemsFile || force) {
DownloadFile(EDDB_SYSTEMS_ARCHIVE, systems_file);
}
}
public void Download() {
Download(false);
}
public bool HaveSystemsFile {
get { return systems_file != null && File.Exists(systems_file); }
}
public PopulatedSystems MakePopulatedSystems() {
if (!HaveSystemsFile) {
throw new InvalidOperationException("no local systems file downloaded");
}
return PopulatedSystems.FromFile(SystemsFile);
}
private void Client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e) {
}
}
}

59
EDDB/PopulatedSystems.cs Normal file
View File

@ -0,0 +1,59 @@
using System.IO;
using System.Linq;
using System;
using System.Globalization;
using Newtonsoft.Json.Linq;
namespace NonaBGS.EDDB {
public class PopulatedSystems {
private string json_file = null;
private JArray root = null;
private string[] system_names = null;
public static PopulatedSystems FromFile(string file) {
PopulatedSystems pop = new PopulatedSystems();
string content = File.ReadAllText(file);
pop.json_file = file;
pop.root = JArray.Parse(content);
return pop;
}
private void MakeSystemNames() {
if (root == null) {
throw new InvalidDataException("no JSON loaded");
}
if (system_names != null && system_names.Length > 0) {
return;
}
var names = root.Children<JObject>().Select(x => x.Value<string>("name"));
system_names = names.ToArray();
}
public string[] SystemNames {
get {
MakeSystemNames();
return system_names;
}
}
public string[] SystemNamesByFilter(string filter) {
MakeSystemNames();
var culture = CultureInfo.InvariantCulture;
return system_names.Where(x => culture.CompareInfo.IndexOf(x, filter, CompareOptions.IgnoreCase) > -1)
.ToArray()
;
}
public string JSONFile {
get => json_file;
}
public JArray Root {
get => root;
}
}
}

View File

@ -3,6 +3,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:abc="http://wpfcontrols.com/"
xmlns:local="clr-namespace:NonaBGS"
xmlns:BGS="clr-namespace:NonaBGS.BGS" xmlns:Util="clr-namespace:NonaBGS.Util" d:DataContext="{d:DesignInstance Type=Util:AppConfig}" x:Name="window" x:Class="NonaBGS.MainWindow"
mc:Ignorable="d"
@ -29,11 +30,11 @@
</Grid.ColumnDefinitions>
<ToolBar VerticalAlignment="Top" Grid.Row="0" Width="Auto" Grid.ColumnSpan="3" Height="Auto" Margin="0,0,0,0" HorizontalAlignment="Left">
<Label Content="System:" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<TextBox x:Name="system" HorizontalAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Center" Margin="0,0,0,3.286" MinWidth="120" VerticalContentAlignment="Center" KeyDown="Filter_KeyDown"/>
<abc:AutoCompleteTextBox x:Name="system" VerticalAlignment="Center" MinWidth="120" MinHeight="22" KeyDown="Filter_KeyDown"/>
<Label Content="Station:" Height="26.2857142857143" VerticalAlignment="Top"/>
<TextBox x:Name="station" Margin="0" TextWrapping="Wrap" VerticalAlignment="Center" MinWidth="120" KeyDown="Filter_KeyDown"/>
<abc:AutoCompleteTextBox x:Name="station" Margin="0" VerticalAlignment="Center" MinWidth="120" MinHeight="22" KeyDown="Filter_KeyDown"/>
<Label Content="Faction:" Height="26.2857142857143" VerticalAlignment="Top"/>
<TextBox x:Name="faction" Margin="0" TextWrapping="Wrap" VerticalAlignment="Center" MinWidth="120" KeyDown="Filter_KeyDown"/>
<abc:AutoCompleteTextBox x:Name="faction" Margin="0" VerticalAlignment="Center" MinWidth="120" MinHeight="22" KeyDown="Filter_KeyDown"/>
<Separator Height="26.2857142857143" Margin="0" VerticalAlignment="Top"/>
<Button x:Name="AddFilter" Content="Add Objective" VerticalAlignment="Stretch" Click="AddFilter_Click" Margin="0,0,0,0.286" RenderTransformOrigin="0.5,0.505"/>
</ToolBar>

View File

@ -1,23 +1,15 @@
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Ookii.Dialogs.Wpf;
using NonaBGS.Journal;
using NonaBGS.BGS;
using NonaBGS.Util;
using NonaBGS.EDDB;
using NonaBGS.UI;
namespace NonaBGS {
/// <summary>
@ -27,7 +19,9 @@ namespace NonaBGS {
private PlayerJournal journal = null;
private Report report = new Report();
private Config config = new Config();
private EDDB eddb = null;
private API api = null;
private PopulatedSystems systems_db = null;
private SystemSuggestionProvider systems_provider = null;
public Config Config => config;
@ -44,7 +38,7 @@ namespace NonaBGS {
report.OnLog += Report_OnLog;
eddb = new EDDB(config.ConfigPath);
api = new API(config.ConfigPath);
journal = new PlayerJournal(config.Global.JournalLocation);
// Set both to now
@ -56,10 +50,18 @@ namespace NonaBGS {
try {
config.LoadObjectives(Report);
RefreshObjectives();
} catch (Exception e) {
Log(e.Message);
}
try {
SyncDatabases();
} catch (Exception) {
/* ignored */
systems_db = api.MakePopulatedSystems();
systems_provider = new SystemSuggestionProvider(systems_db);
system.Provider = systems_provider;
} catch (Exception e) {
Log(e.Message);
}
}
@ -74,12 +76,16 @@ namespace NonaBGS {
log.AppendText(builder.ToString());
}
private void Log(string message) {
Report_OnLog(message);
}
private void SyncDatabases() {
if (!config.Global.UseEDDB) {
return;
}
eddb.Download();
api.Download();
}
private void RefreshObjectives() {

View File

@ -0,0 +1,23 @@
using System;
using System.Collections;
using AutoCompleteTextBox.Editors;
using NonaBGS.EDDB;
namespace NonaBGS.UI {
public class SystemSuggestionProvider : ISuggestionProvider {
private PopulatedSystems systems = null;
public SystemSuggestionProvider(PopulatedSystems systems) {
this.systems = systems;
}
public PopulatedSystems Data {
get => systems;
set => systems = value;
}
public IEnumerable GetSuggestions(string filter) {
return systems.SystemNamesByFilter(filter);
}
}
}

View File

@ -1,48 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Net;
namespace NonaBGS.Util {
public class EDDB {
private static readonly string EDDB_SYSTEMS_ARCHIVE = "https://eddb.io/archive/v6/systems_populated.json";
private string cache_folder = null;
private WebClient client = new WebClient();
private string systems_file = null;
public string SystemsFile => systems_file;
public string Cache {
get => cache_folder;
set => cache_folder = value;
}
public EDDB(string cache_folder) {
this.cache_folder = cache_folder;
Initialise();
}
public void Initialise() {
client.DownloadDataCompleted += Client_DownloadDataCompleted;
}
private void DownloadFile(string url) {
Uri uri = new Uri(url);
string name = Path.GetFileName(uri.AbsolutePath);
systems_file = Path.Combine(this.cache_folder, name);
client.DownloadFileAsync(new Uri(EDDB_SYSTEMS_ARCHIVE), systems_file);
}
public void Download() {
DownloadFile(EDDB_SYSTEMS_ARCHIVE);
}
private void Client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e) {
}
}
}

View File

@ -38,6 +38,9 @@
<StartupObject>NonaBGSApplication</StartupObject>
</PropertyGroup>
<ItemGroup>
<Reference Include="AutoCompleteTextBox, Version=1.1.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\AutoCompleteTextBox.1.1.1\lib\net472\AutoCompleteTextBox.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
@ -70,14 +73,16 @@
<Compile Include="BGS\DiscordLogGenerator.cs" />
<Compile Include="BGS\NonaDiscordLog.cs" />
<Compile Include="BGS\SellMicroResources.cs" />
<Compile Include="EDDB\PopulatedSystems.cs" />
<Compile Include="Journal\EliteDangerous.cs" />
<Compile Include="Journal\MarketSellEntry.cs" />
<Compile Include="Journal\MultiSellExplorationDataEntry.cs" />
<Compile Include="Journal\RedeemVoucherEntry.cs" />
<Compile Include="Journal\SellMicroResourcesEntry.cs" />
<Compile Include="UI\SystemSuggestionProvider.cs" />
<Compile Include="Util\AppConfig.cs" />
<Compile Include="Util\Config.cs" />
<Compile Include="Util\EDDB.cs" />
<Compile Include="EDDB\API.cs" />
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
@ -133,8 +138,6 @@
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<Folder Include="UI\" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AutoCompleteTextBox" version="1.1.1" targetFramework="net472" />
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net472" />
<package id="Ookii.Dialogs.Wpf" version="3.1.0" targetFramework="net472" />
</packages>