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() .Where(x => x.IsEnabled) .ToArray() ; if (sold == null || sold.Length <= 0) { return ""; } // This groups everything together by cargo sold, and then by market sold to. // Dictionary > 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(); } } }