completely refactor log generation to avoid duplicate code

This commit is contained in:
2022-02-11 13:01:42 +01:00
parent d79b26e5c9
commit 054376ef63
15 changed files with 342 additions and 643 deletions

View File

@@ -0,0 +1,58 @@
using System.Linq;
using System.Text;
using EDJournal;
namespace EliteBGS.BGS.LogGenerator {
public class CargoSoldFormatter : LogFormatter {
public string GenerateLog(Objective objective) {
StringBuilder builder = new StringBuilder();
SellCargo[] sold = objective.LogEntries
.OfType<SellCargo>()
.ToArray()
;
if (sold == null || sold.Length <= 0) {
return "";
}
// This groups everything together by cargo sold, and then by market sold to.
// Dictionary<string Cargo, Dictionary<string Market, { Market, Amount, Profit }> >
var entries = sold.GroupBy(x => x.Cargo,
(key, cargos) => new {
Cargo = key,
Markets = cargos.GroupBy(y => y.Market,
(market, markets) => new {
Market = market,
Amount = markets.Sum(x => x.Amount),
Profit = markets.Sum(x => x.Profit)
})
}
)
;
foreach (var cargo in entries) {
foreach (var market in cargo.Markets) {
builder.AppendFormat("Sold {0} {1} to the {2}",
market.Amount,
cargo.Cargo,
market.Market
);
if (market.Profit != 0) {
builder.AppendFormat(" ({0} {1})",
Credits.FormatCredits(market.Profit),
market.Profit < 0 ? "loss" : "profit"
);
}
builder.Append("\n");
}
}
builder.AppendFormat("\n");
return builder.ToString();
}
}
}

View File

@@ -0,0 +1,21 @@
using System.Linq;
using EDJournal;
namespace EliteBGS.BGS.LogGenerator {
public class CartographicsFormat : LogFormatter {
public string GenerateLog(Objective objective) {
var total = objective.LogEntries.OfType<Cartographics>();
var pages = total.Count();
long sum = total.Sum(x => (x as Cartographics).TotalSum);
if (pages <= 0 || sum <= 0) {
return "";
}
return string.Format("Sold {0} page(s) worth of universal cartographics\n" +
"(Total value: {1})\n\n",
pages, Credits.FormatCredits(sum)
);
}
}
}

View File

@@ -0,0 +1,4 @@
namespace EliteBGS.BGS.LogGenerator {
class CombatZoneFormat : GenericFormat<CombatZone> {
}
}

View File

@@ -0,0 +1,29 @@
using System.Linq;
using System.Text;
using EDJournal;
namespace EliteBGS.BGS.LogGenerator {
public class FailedMissionFormat : LogFormatter {
public string GenerateLog(Objective objective) {
MissionFailed[] missions = objective.LogEntries.OfType<MissionFailed>().ToArray();
StringBuilder builder = new StringBuilder();
if (missions.Length <= 0) {
return "";
}
foreach (MissionFailed failed in missions) {
MissionFailedEntry f = failed.Failed;
builder.AppendFormat("Failed {0} mission(s) \"{1}\" targeting {2}\n",
failed.Amount,
f.HumanReadableName == null ? f.Name : f.HumanReadableName,
failed.Faction
);
}
builder.Append("\n");
return builder.ToString();
}
}
}

View File

@@ -0,0 +1,4 @@
namespace EliteBGS.BGS.LogGenerator {
public class KillBondsFormat : GenericFormat<FactionKillBonds> {
}
}

View File

@@ -0,0 +1,4 @@
namespace EliteBGS.BGS.LogGenerator {
public class MarketBuyFormat : GenericFormat<BuyCargo> {
}
}

View File

@@ -0,0 +1,18 @@
using System.Linq;
using EDJournal;
namespace EliteBGS.BGS.LogGenerator {
public class MicroResourcesFormat : LogFormatter {
public string GenerateLog(Objective objective) {
var total = objective.LogEntries.OfType<SellMicroResources>();
long sum = total.Sum(x => x.TotalSum);
if (total == null || total.Count() <= 0 || sum <= 0) {
return "";
}
return string.Format("Sold {0} worth of Micro Resources\n",
Credits.FormatCredits(sum));
}
}
}

View File

@@ -0,0 +1,63 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EliteBGS.BGS.LogGenerator {
public class MissionFormat : LogFormatter {
public string GenerateLog(Objective objective) {
Dictionary<string, Dictionary<string, int>> collated = new Dictionary<string, Dictionary<string, int>>();
StringBuilder output = new StringBuilder();
int total_influence = 0;
var missions = objective.LogEntries.OfType<MissionCompleted>();
if (missions == null) {
return "";
}
foreach (MissionCompleted m in missions) {
if (!collated.ContainsKey(m.MissionName)) {
collated[m.MissionName] = new Dictionary<string, int>();
}
if (!collated[m.MissionName].ContainsKey(m.Influence)) {
collated[m.MissionName][m.Influence] = 0;
}
++collated[m.MissionName][m.Influence];
total_influence += m.Influence.Length;
}
foreach (var mission in collated) {
if (objective.Faction != null) {
output.AppendFormat("{0} for {1}\n", mission.Key, objective.Faction);
} else {
output.AppendFormat("{0}\n", mission.Key);
}
output.Append("(");
foreach (var influence in mission.Value.OrderBy(x => x.Key.Length)) {
output.AppendFormat("Inf{0} x{1}, ", influence.Key, influence.Value);
}
output.Remove(output.Length - 2, 2); // remove last ", "
output.Append(")\n\n");
}
var support = objective.LogEntries.OfType<InfluenceSupport>();
foreach (InfluenceSupport inf in support) {
output.Append(inf.ToString());
output.Append("\n");
total_influence += inf.Influence.Length;
}
if (support.Count() > 0) {
output.Append("\n");
}
if (total_influence > 0) {
output.AppendFormat("Total Influence: {0}", total_influence);
}
return output.ToString().Trim();
}
}
}

View File

@@ -0,0 +1,4 @@
namespace EliteBGS.BGS.LogGenerator {
public class MurderFormat : GenericFormat<FoulMurder> {
}
}

View File

@@ -0,0 +1,24 @@
using System.Linq;
using System.Text;
using EDJournal;
namespace EliteBGS.BGS.LogGenerator {
public class VoucherFormat : LogFormatter {
public string GenerateLog(Objective objective) {
StringBuilder builder = new StringBuilder();
var missions = objective.LogEntries.OfType<Vouchers>();
if (missions == null || missions.Count() <= 0) {
return "";
}
foreach (var m in missions) {
builder.AppendFormat("Handed in {0} vouchers for {1}\n", m.Type, m.Faction);
builder.AppendFormat("(Total value: {0})\n", Credits.FormatCredits(m.TotalSum));
builder.AppendFormat("\n");
}
return builder.ToString();
}
}
}