2022-11-24 19:38:19 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using EliteBGS.LogGenerator;
|
2023-06-18 15:22:32 +02:00
|
|
|
|
using System.Reflection;
|
2022-11-24 19:38:19 +01:00
|
|
|
|
|
|
|
|
|
namespace EliteBGS;
|
|
|
|
|
|
|
|
|
|
public class DiscordLogGenerator {
|
|
|
|
|
protected List<LogFormatter> formatters = new List<LogFormatter>() {
|
|
|
|
|
new MissionFormat(),
|
|
|
|
|
new MurderFormat(),
|
|
|
|
|
new VoucherFormat(),
|
2022-11-25 14:48:59 +01:00
|
|
|
|
new ThargoidFormatter(),
|
2022-11-24 19:38:19 +01:00
|
|
|
|
new CombatZoneFormat(),
|
|
|
|
|
new KillBondsFormat(),
|
|
|
|
|
new CartographicsFormat(),
|
|
|
|
|
new MicroResourcesFormat(),
|
|
|
|
|
new MarketBuyFormat(),
|
|
|
|
|
new CargoSoldFormatter(),
|
2023-03-02 20:10:49 +01:00
|
|
|
|
new VistaGenomicsFormat(),
|
2022-11-24 19:38:19 +01:00
|
|
|
|
new SearchAndRescueFormat(),
|
|
|
|
|
};
|
|
|
|
|
|
2023-06-18 15:22:32 +02:00
|
|
|
|
protected virtual string GetToolVersion() {
|
|
|
|
|
Version v = Assembly.GetCallingAssembly().GetName().Version;
|
|
|
|
|
string ver;
|
|
|
|
|
if (v == null) {
|
|
|
|
|
ver = "v?.?.?";
|
|
|
|
|
} else {
|
|
|
|
|
ver = "v" + v.ToString(3);
|
|
|
|
|
}
|
|
|
|
|
return string.Format("EliteBGS {0}", ver);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-18 15:33:07 +02:00
|
|
|
|
protected virtual DateTime? GetDateOfEarliestEntry(Objective objective) {
|
|
|
|
|
var it = objective
|
2023-06-18 15:22:32 +02:00
|
|
|
|
.Transactions
|
2023-06-18 15:33:07 +02:00
|
|
|
|
.OrderBy(x => x.CompletedAtDateTime)
|
|
|
|
|
.FirstOrDefault()
|
2023-06-18 15:22:32 +02:00
|
|
|
|
;
|
2023-06-18 15:33:07 +02:00
|
|
|
|
if (it != null) {
|
|
|
|
|
return it.CompletedAtDateTime;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2023-06-18 15:22:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-18 15:33:07 +02:00
|
|
|
|
protected virtual DateTime? GetDateOfLatestEntry(Objective objective) {
|
|
|
|
|
var it = objective
|
2023-06-18 15:22:32 +02:00
|
|
|
|
.Transactions
|
2023-06-18 15:33:07 +02:00
|
|
|
|
.OrderByDescending(x => x.CompletedAtDateTime)
|
|
|
|
|
.FirstOrDefault()
|
2023-06-18 15:22:32 +02:00
|
|
|
|
;
|
2023-06-18 15:33:07 +02:00
|
|
|
|
if (it != null) {
|
|
|
|
|
return it.CompletedAtDateTime;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2023-06-18 15:22:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-23 21:44:55 +01:00
|
|
|
|
protected virtual string GenerateSummary(Objective objective) {
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
foreach (var formatter in formatters) {
|
|
|
|
|
string summary = "";
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
summary = formatter.GenerateSummary(objective);
|
|
|
|
|
} catch (NotImplementedException) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(summary)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sb.Length > 0) {
|
2023-04-19 10:13:02 +02:00
|
|
|
|
sb.Append("; ");
|
2023-02-23 21:44:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sb.Append(summary);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 19:38:19 +01:00
|
|
|
|
protected virtual string GenerateHeader() {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual string GenerateFooter() {
|
|
|
|
|
return "\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual string GenerateObjectiveHeader(Objective objective) {
|
|
|
|
|
StringBuilder log = new StringBuilder();
|
|
|
|
|
|
2022-11-25 14:53:04 +01:00
|
|
|
|
string location;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(objective.System) && !string.IsNullOrEmpty(objective.Faction)) {
|
|
|
|
|
location = string.Format("{0}, {1}", objective.System, objective.Faction);
|
|
|
|
|
} else if (!string.IsNullOrEmpty(objective.System)) {
|
|
|
|
|
location = objective.System;
|
|
|
|
|
} else {
|
|
|
|
|
location = "Unknown Location";
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-29 16:39:20 +01:00
|
|
|
|
int legacycount = objective.Transactions
|
|
|
|
|
.Where(x => x.IsLegacy)
|
|
|
|
|
.Count()
|
|
|
|
|
;
|
|
|
|
|
|
2023-02-23 21:44:55 +01:00
|
|
|
|
string summary = GenerateSummary(objective);
|
|
|
|
|
|
2023-06-18 15:33:07 +02:00
|
|
|
|
var earliest = GetDateOfEarliestEntry(objective);
|
|
|
|
|
var latest = GetDateOfLatestEntry(objective);
|
|
|
|
|
if (earliest != null && latest != null) {
|
|
|
|
|
log.AppendFormat("**Date:** {0} - {1}\n",
|
|
|
|
|
GetDateOfEarliestEntry(objective),
|
|
|
|
|
GetDateOfLatestEntry(objective)
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-11-25 14:53:04 +01:00
|
|
|
|
log.AppendFormat("**Target:** {0}\n", location);
|
2023-02-23 21:44:55 +01:00
|
|
|
|
if (!string.IsNullOrEmpty(summary)) {
|
2024-05-02 20:26:14 +02:00
|
|
|
|
log.AppendFormat("**Summary:** {0}\n", summary);
|
2023-02-23 21:44:55 +01:00
|
|
|
|
}
|
2022-11-29 16:39:20 +01:00
|
|
|
|
if (legacycount > 0) {
|
2022-12-07 23:00:38 +01:00
|
|
|
|
log.AppendFormat("**Warning:** Some actions were performed on ED Legacy\n");
|
2022-11-29 16:39:20 +01:00
|
|
|
|
}
|
2022-11-24 19:38:19 +01:00
|
|
|
|
log.AppendLine("```");
|
|
|
|
|
|
|
|
|
|
return log.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual string GenerateObjectiveFooter(Objective objective) {
|
|
|
|
|
return "```\n";
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-07 23:00:38 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Is called to do final adjustments to the log body of a given objective.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="objective">Objective in question.</param>
|
|
|
|
|
/// <param name="log">Final log as generated.</param>
|
|
|
|
|
/// <returns>The transformed log.</returns>
|
|
|
|
|
protected virtual string TransformFinalLogForObjective(Objective objective, string log) {
|
|
|
|
|
return log;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-02 20:07:10 +02:00
|
|
|
|
public virtual string Name {
|
|
|
|
|
get { return "GenericLog"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual string BotHeader() {
|
|
|
|
|
var sb = new StringBuilder();
|
2024-05-02 20:26:14 +02:00
|
|
|
|
sb.AppendFormat("**Bot-Header:** {0}; {1}\n", GetToolVersion(), this.Name);
|
2024-05-02 20:07:10 +02:00
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 19:38:19 +01:00
|
|
|
|
public virtual string GenerateDiscordLog(Report report) {
|
|
|
|
|
StringBuilder log = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
if (report == null) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var objectives = report.Objectives
|
|
|
|
|
.Where(x => x.IsEnabled && x.Transactions.Count() > 0)
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
if (objectives == null || objectives.Count() <= 0) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-02 20:07:10 +02:00
|
|
|
|
log.AppendFormat("{0}", BotHeader());
|
2022-12-07 23:00:38 +01:00
|
|
|
|
log.AppendFormat("{0}", GenerateHeader());
|
2022-11-24 19:38:19 +01:00
|
|
|
|
|
|
|
|
|
foreach (Objective objective in objectives) {
|
|
|
|
|
StringBuilder objlog = new StringBuilder();
|
|
|
|
|
|
2022-12-07 23:00:38 +01:00
|
|
|
|
log.AppendFormat("{0}", GenerateObjectiveHeader(objective));
|
2022-11-24 19:38:19 +01:00
|
|
|
|
|
|
|
|
|
foreach (LogFormatter formatter in formatters) {
|
|
|
|
|
string text = formatter.GenerateLog(objective);
|
|
|
|
|
text = text.Trim();
|
|
|
|
|
if (!string.IsNullOrEmpty(text)) {
|
|
|
|
|
objlog.AppendFormat("{0}\n\n", text);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-07 23:00:38 +01:00
|
|
|
|
string finallog = objlog.ToString().Trim();
|
|
|
|
|
finallog = TransformFinalLogForObjective(objective, finallog);
|
|
|
|
|
|
|
|
|
|
log.AppendFormat("{0}\n", finallog);
|
2022-11-24 19:38:19 +01:00
|
|
|
|
|
2022-12-07 23:00:38 +01:00
|
|
|
|
log.AppendFormat("{0}", GenerateObjectiveFooter(objective));
|
2022-11-24 19:38:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-07 23:00:38 +01:00
|
|
|
|
log.AppendFormat("{0}", GenerateFooter());
|
2022-11-24 19:38:19 +01:00
|
|
|
|
|
|
|
|
|
return log.ToString().Trim();
|
|
|
|
|
}
|
2024-09-17 19:53:17 +02:00
|
|
|
|
|
|
|
|
|
public virtual string[] SplitLog(string log, int maxcount = 2000) {
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected string[] SplitLogWithHeader(string log, string header, int maxcount = 2000) {
|
|
|
|
|
string[] lines = log.Split("\n");
|
|
|
|
|
List<string> chunks = new();
|
|
|
|
|
string chunk = string.Empty;
|
2024-09-17 19:54:35 +02:00
|
|
|
|
|
|
|
|
|
// Optimisation
|
|
|
|
|
if (log.Length <= maxcount) {
|
|
|
|
|
return new string[] { log };
|
|
|
|
|
}
|
2024-09-17 19:53:17 +02:00
|
|
|
|
|
|
|
|
|
// First split the log into its headers
|
|
|
|
|
// skip first bot header line
|
|
|
|
|
for (int i = 1; i < lines.Length; i++) {
|
|
|
|
|
string line = lines[i];
|
|
|
|
|
|
|
|
|
|
if (line.StartsWith(header) && !string.IsNullOrEmpty(chunk)) {
|
|
|
|
|
chunks.Add(chunk.Trim());
|
|
|
|
|
chunk = string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chunk = chunk + "\n" + line;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int curchunk = 0;
|
|
|
|
|
string botheader = BotHeader().Trim() + "\n";
|
2024-09-18 21:10:41 +02:00
|
|
|
|
// Leave room for botheader and some leeway
|
2024-09-17 19:53:17 +02:00
|
|
|
|
int maxlength = (2000 - botheader.Length - 10);
|
|
|
|
|
|
|
|
|
|
// Then try to collate chunks
|
|
|
|
|
for (curchunk = 0; curchunk < chunks.Count; ++curchunk) {
|
|
|
|
|
int count = chunks[curchunk].Length;
|
|
|
|
|
while (count < maxlength && (curchunk+1) < chunks.Count) {
|
|
|
|
|
count += chunks[curchunk + 1].Length + 2;
|
|
|
|
|
if (count < maxlength) {
|
|
|
|
|
chunks[curchunk] += "\n";
|
|
|
|
|
chunks[curchunk] += chunks[curchunk + 1];
|
|
|
|
|
chunks.RemoveAt(curchunk + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Readd bott headers
|
|
|
|
|
for (int i = 0; i < chunks.Count; i++) {
|
|
|
|
|
chunks[i] = chunks[i].Insert(0, botheader);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 21:10:41 +02:00
|
|
|
|
// Now finally check if any one chunk is bigger than max length
|
|
|
|
|
for (int i = 0; i < chunks.Count; i++) {
|
|
|
|
|
if (chunks[i].Length > maxcount) {
|
|
|
|
|
throw new ApplicationException(
|
|
|
|
|
string.Format("a chunk turned out bigger than {0}", maxcount));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 19:53:17 +02:00
|
|
|
|
return chunks.ToArray();
|
|
|
|
|
}
|
2022-11-24 19:38:19 +01:00
|
|
|
|
}
|