EliteBGS/MainWindow.xaml.cs

319 lines
10 KiB
C#

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Ookii.Dialogs.Wpf;
using EDJournal;
using EliteBGS.BGS;
using EliteBGS.Util;
namespace EliteBGS {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
private PlayerJournal journal = null;
private Report report = new Report();
private Config config = new Config();
public Config Config => config;
public Report Report => report;
private LoadEntriesWindow loadentries = null;
private static readonly List<DiscordLogGenerator> logtypes = new List<DiscordLogGenerator>() {
new NonaDiscordLog(),
new GenericDiscordLog(),
};
public MainWindow() {
InitializeComponent();
try {
config.LoadGlobal();
} catch (Exception) {
/* ignored */
}
report.OnLog += Report_OnLog;
foreach (DiscordLogGenerator type in logtypes) {
LogType.Items.Add(type);
}
string lastused = config.Global.LastUsedDiscordTemplate;
int lastindex = logtypes.FindIndex(x => x.ToString() == lastused);
if (lastindex > -1) {
LogType.SelectedIndex = lastindex;
} else {
LogType.SelectedIndex = 0;
}
journal = new PlayerJournal(config.Global.JournalLocation);
// Set both to now
startdate.SelectedDate = DateTime.Now;
enddate.SelectedDate = DateTime.Now;
journallocation.Text = Config.Global.JournalLocation;
try {
config.LoadObjectives(Report);
RefreshObjectives();
} catch (Exception e) {
Log(e.Message);
}
}
private void Loadentries_EntriesLoaded(List<Entry> lines) {
try {
report.Scan(lines);
RefreshObjectives();
} catch (Exception exception) {
Log("Something went terribly wrong while parsing the E:D player journal.");
Log("Please send this to CMDR Hekateh:");
Log(exception.ToString());
}
}
private void Report_OnLog(string message) {
StringBuilder builder = new StringBuilder();
builder.Append(DateTime.Now.ToString());
builder.Append(": ");
builder.Append(message);
builder.Append("\n");
log.AppendText(builder.ToString());
}
private void Log(string message) {
Report_OnLog(message);
}
private void RefreshObjectives() {
entries.Items.Clear();
if (report.Objectives == null) {
return;
}
foreach (Objective obj in report.Objectives) {
entries.Items.Add(obj);
obj.IsExpanded = obj.ManuallyAdded || obj.IsExpanded;
obj.IsEnabled = obj.ManuallyAdded || obj.IsEnabled;
}
}
private void ParseJournal_Click(object sender, RoutedEventArgs e) {
try {
bool collate = (this.collate.IsChecked ?? true);
journal.Open(); // Load all files
var start = startdate.SelectedDate ?? DateTime.Now;
var end = enddate.SelectedDate ?? DateTime.Now;
report.Scan(journal, start, end, collate);
RefreshObjectives();
} catch (Exception exception) {
Log("Something went terribly wrong while parsing the E:D player journal.");
Log("Please send this to CMDR Hekateh:");
Log(exception.ToString());
}
}
private void AddObjective() {
Objective objective = new Objective {
System = system.Text,
Faction = faction.Text,
Station = station.Text,
ManuallyAdded = true,
};
if (!objective.IsValid) {
return;
}
if (report.AddObjective(objective)) {
RefreshObjectives();
config.SaveObjectives(Report);
}
}
private void AddFilter_Click(object sender, RoutedEventArgs e) {
AddObjective();
}
private void GenerateDiscord_Click(object sender, RoutedEventArgs e) {
try {
DiscordLogGenerator discord = LogType.SelectedItem as DiscordLogGenerator;
string report = discord.GenerateDiscordLog(Report);
DiscordLog.Text = report;
} catch (Exception exception) {
Log("Something went terribly wrong while generating the Discord log.");
Log("Please send this to CMDR Hekateh:");
Log(exception.ToString());
}
}
private void RemoveCurrentObjective() {
if (entries.SelectedItem == null) {
return;
}
object obj = entries.SelectedItem;
bool removed = false;
if (obj.GetType() == typeof(Objective)) {
removed = report.Objectives.Remove(obj as Objective);
} else if (obj.GetType() == typeof(LogEntry) ||
obj.GetType().IsSubclassOf(typeof(LogEntry))) {
foreach (Objective parent in report.Objectives) {
if (parent.LogEntries.Remove(obj as LogEntry)) {
removed = true;
}
}
}
if (removed) {
RefreshObjectives();
config.SaveObjectives(Report);
}
}
private void entries_KeyUp(object sender, KeyEventArgs e) {
if (e.Key == Key.Delete) {
RemoveCurrentObjective();
}
}
private void browsejournallocation_Click(object sender, RoutedEventArgs e) {
var dialog = new VistaFolderBrowserDialog();
if ((bool)!dialog.ShowDialog()) {
return;
}
Config.Global.JournalLocation = dialog.SelectedPath;
journallocation.Text = Config.Global.JournalLocation;
journal = new PlayerJournal(config.Global.JournalLocation);
}
private void Filter_KeyDown(object sender, KeyEventArgs e) {
if (e.Key == Key.Enter) {
AddObjective();
}
}
/// <summary>
/// Gets the currently selected objective, even if a log entry in said objective
/// is selected instead. If nothing is selected, returns null.
/// </summary>
/// <returns></returns>
private Objective GetSelectedObjective() {
var obj = entries.SelectedItem;
if (obj == null) {
return null;
}
if (obj.GetType() == typeof(Objective)) {
return obj as Objective;
}
// Some form of entry perhaps?
if (obj.GetType().IsSubclassOf(typeof(LogEntry))) {
LogEntry entry = obj as LogEntry;
Objective objective = entries.Items
.OfType<Objective>()
.First(x => x.LogEntries.Contains(entry))
;
return objective;
}
return null;
}
private void AddCombatZone_Click(object sender, RoutedEventArgs e) {
Objective objective = GetSelectedObjective();
if (objective == null) {
return;
}
CombatZoneDialog dialog = new CombatZoneDialog() { Owner = this };
if (!(dialog.ShowDialog() ?? false)) {
return;
}
CombatZone zone = new CombatZone {
ManuallyAdded = true,
Faction = objective.Faction,
System = objective.System,
Station = objective.Station,
Grade = dialog.Grade,
Type = dialog.Type,
Amount = dialog.Amount
};
objective.LogEntries.Add(zone);
RefreshObjectives();
}
private void AdjustProfit_Click(object sender, RoutedEventArgs e) {
if (entries.SelectedItem == null || entries.SelectedItem.GetType() != typeof(SellCargo)) {
return;
}
SellCargo sell = entries.SelectedItem as SellCargo;
AdjustProfitWindow adjust = new AdjustProfitWindow() { Owner = this };
adjust.Profit.Text = sell.Profit.ToString();
if (!(adjust.ShowDialog() ?? false)) {
return;
}
if (int.TryParse(adjust.Profit.Text, out int newprofit)) {
sell.Profit = newprofit;
RefreshObjectives();
}
}
private void LogType_SelectionChanged(object sender, SelectionChangedEventArgs e) {
if (LogType.SelectedItem == null) {
return;
}
string template = LogType.SelectedItem.ToString();
config.Global.LastUsedDiscordTemplate = template;
}
private void ManuallyParse_Click(object sender, RoutedEventArgs e) {
if (loadentries != null) {
loadentries.Show();
return;
}
loadentries = new LoadEntriesWindow();
loadentries.Closed += Loadentries_Closed;
loadentries.EntriesLoaded += Loadentries_EntriesLoaded;
loadentries.Show();
}
private void Loadentries_Closed(object sender, EventArgs e) {
loadentries = null;
}
private void window_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
loadentries?.Close();
loadentries = null;
}
}
}