311 lines
16 KiB
C#
311 lines
16 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UIElements;
|
||
|
||
namespace Soundings
|
||
{
|
||
public sealed partial class SoundingsApp
|
||
{
|
||
private bool ledgerAccounts;
|
||
private float ledgerRecordOffset;
|
||
|
||
private void DrawFinance(VisualElement pageRoot)
|
||
{
|
||
var workspace = Add(pageRoot, "log-workspace");
|
||
var reader = new ScrollView(ScrollViewMode.Vertical)
|
||
{
|
||
verticalScrollerVisibility = ScrollerVisibility.Auto
|
||
};
|
||
reader.AddToClassList("log-reader");
|
||
workspace.Add(reader);
|
||
|
||
var folio = Add(reader, "log-folio");
|
||
var head = Add(folio, "log-heading");
|
||
AddDisplay(head, "Captain’s log", "log-title");
|
||
var tabs = Add(head, "log-tabs");
|
||
AddLogTab(tabs, "Voyage records", false, "log-voyages");
|
||
AddLogTab(tabs, "Company accounts", true, "log-accounts");
|
||
|
||
var binding = Add(folio, "log-binding");
|
||
var sheets = Add(binding, "log-sheets");
|
||
var left = Add(sheets, "log-leaf log-left-leaf");
|
||
var spine = Add(sheets, "log-spine");
|
||
spine.pickingMode = PickingMode.Ignore;
|
||
var right = Add(sheets, "log-leaf log-right-leaf");
|
||
|
||
if (ledgerAccounts)
|
||
DrawAccounts(left, right);
|
||
else
|
||
DrawVoyageRecords(left, right);
|
||
}
|
||
|
||
private void AddLogTab(VisualElement parent, string label, bool accounts, string id)
|
||
{
|
||
bool selected = ledgerAccounts == accounts;
|
||
Button button = Action(label, () =>
|
||
{
|
||
if (ledgerAccounts == accounts)
|
||
return;
|
||
ledgerAccounts = accounts;
|
||
Render();
|
||
}, selected ? "log-tab log-tab-active" : "log-tab", id);
|
||
parent.Add(button);
|
||
}
|
||
|
||
private void DrawVoyageRecords(VisualElement register, VisualElement entry)
|
||
{
|
||
AddText(register, "VOYAGE REGISTER", "log-small-title");
|
||
AddDisplay(register, "Passages", "log-sheet-title");
|
||
AddText(register, $"{company.Voyages:N0} completed", "log-secondary");
|
||
if (Ship?.Itinerary is { HasNext: true, CompletedStops: > 0 } itinerary)
|
||
register.Add(Action($"Plan next leg · {Market.Ports[itinerary.NextPort].Name}", OpenVoyageChart,
|
||
"log-paper-action log-next-leg", "log-next-leg"));
|
||
AddText(register, company.Flagship
|
||
? "Flagship charter complete · Bermuda to Lisbon"
|
||
: "Flagship charter · Bermuda to Lisbon",
|
||
"log-note " + (company.Flagship ? "log-positive" : "log-charter"));
|
||
if (!company.Flagship)
|
||
AddText(register, "Ocean Class hull · 320 berths · 160 show seats", "log-charter-terms");
|
||
|
||
if (company.History.Count == 0)
|
||
{
|
||
DrawEmptyLog(register, entry);
|
||
return;
|
||
}
|
||
|
||
var tableHead = Add(register, "log-index-heading");
|
||
AddText(tableHead, "DAY", "log-index-day");
|
||
AddText(tableHead, "PASSAGE", "log-index-passage");
|
||
AddText(tableHead, "NET PROFIT", "log-index-amount");
|
||
|
||
selectedVoyage = Mathf.Clamp(selectedVoyage, 0, company.History.Count - 1);
|
||
var recordList = new ScrollView(ScrollViewMode.Vertical) { verticalScrollerVisibility = ScrollerVisibility.Auto };
|
||
recordList.AddToClassList("log-index");
|
||
register.Add(recordList);
|
||
for (int i = 0; i < company.History.Count; i++)
|
||
{
|
||
int record = i;
|
||
Voyage trip = company.History[i];
|
||
string route = $"{Market.Ports[trip.Origin].Name} to {Market.Ports[trip.Destination].Name}";
|
||
Button item = Action("", () =>
|
||
{
|
||
ledgerRecordOffset = recordList.verticalScroller.value;
|
||
selectedVoyage = record;
|
||
Render();
|
||
}, i == selectedVoyage ? "log-entry log-entry-active" : "log-entry", "voyage-" + i);
|
||
item.tooltip = route + " aboard " + trip.ShipName;
|
||
AddText(item, trip.Day.ToString(), "log-index-day");
|
||
var line = Add(item, "log-index-passage");
|
||
AddText(line, route, "log-entry-route");
|
||
AddText(line, trip.ShipName, "log-entry-ship");
|
||
AddText(item, Money(trip.NetIncome), "log-index-amount " + LogAmountColor(trip.NetIncome));
|
||
recordList.Add(item);
|
||
}
|
||
|
||
recordList.verticalScroller.valueChanged += offset => ledgerRecordOffset = offset;
|
||
if (ledgerRecordOffset > 0)
|
||
{
|
||
float restore = ledgerRecordOffset;
|
||
recordList.schedule.Execute(() => recordList.scrollOffset = new Vector2(0, restore));
|
||
}
|
||
if (company.Voyages > company.History.Count)
|
||
AddText(register, $"Showing the latest {company.History.Count} voyages", "log-note");
|
||
|
||
DrawVoyageEntry(entry, company.History[selectedVoyage]);
|
||
}
|
||
|
||
private void DrawEmptyLog(VisualElement register, VisualElement entry)
|
||
{
|
||
var ruling = Add(register, "log-unused-lines");
|
||
ruling.pickingMode = PickingMode.Ignore;
|
||
ruling.generateVisualContent += DrawLedgerRuling;
|
||
|
||
var empty = Add(entry, "log-empty");
|
||
var compass = Add(empty, "log-compass");
|
||
compass.pickingMode = PickingMode.Ignore;
|
||
compass.generateVisualContent += DrawLedgerCompass;
|
||
AddDisplay(empty, "Awaiting first arrival", "log-sheet-title log-centered");
|
||
AddText(empty, "Complete a sailing to record its passengers, costs and earnings.", "log-note log-centered");
|
||
if (Ship != null)
|
||
empty.Add(Action("Set course", OpenVoyageChart, "log-paper-action", "log-set-course"));
|
||
else
|
||
empty.Add(Action("Visit shipyard", () => ChangePage(2), "log-paper-action", "log-visit-yard"));
|
||
}
|
||
|
||
private void DrawVoyageEntry(VisualElement leaf, Voyage trip)
|
||
{
|
||
string leg = trip.ItineraryLeg > 0 ? $" · ITINERARY LEG {trip.ItineraryLeg} OF {trip.ItineraryLegs}" : "";
|
||
AddText(leaf, $"ARRIVED ON DAY {trip.Day}{leg}", "log-small-title");
|
||
string origin = Market.Ports[trip.Origin].Name;
|
||
string destinationName = Market.Ports[trip.Destination].Name;
|
||
AddDisplay(leaf, $"{origin} to {destinationName}", "log-sheet-title log-route-title");
|
||
AddText(leaf, $"{trip.ShipName} · {trip.Miles:N0} nm over {trip.Days} days", "log-secondary");
|
||
AddText(leaf, $"{trip.Guests:N0} passengers · {trip.FuelBurn:N0} t of fuel consumed", "log-secondary");
|
||
if (trip.Onboard != null)
|
||
{
|
||
AddText(leaf, $"Guest survey {trip.Onboard.Happiness}/100 · Health {trip.Onboard.Health}/100 · {trip.Weather} seas", "log-note");
|
||
AddText(leaf, $"{trip.Onboard.Provisions:N0} portions left · {trip.Onboard.WaterPercent}% freshwater · {trip.Onboard.SewagePercent}% treatment load", "log-secondary");
|
||
if (trip.Onboard.LaundryCleaned > 0 || trip.Onboard.LaundryBacklog > 0)
|
||
AddText(leaf, $"Laundry · {trip.Onboard.LaundryCleaned:N0} loads processed · {trip.Onboard.LaundryBacklog:N0} still waiting", "log-secondary");
|
||
if (trip.Onboard.CrewCalls > 0)
|
||
AddText(leaf, $"Crew calls answered · {trip.Onboard.CrewCalls}", "log-secondary");
|
||
}
|
||
|
||
var statement = Add(leaf, "log-statement");
|
||
LogLine(statement, "Ticket revenue", Money(trip.TicketSales - trip.SuitePremium));
|
||
if (trip.SuiteGuests > 0)
|
||
LogLine(statement, $"Suite premium · {trip.SuiteGuests:N0} guests", Money(trip.SuitePremium));
|
||
LogLine(statement, "Onboard revenue", Money(trip.OnboardSales));
|
||
if (trip.Onboard?.ServiceSales is int[] sales && sales.Length > (int)OnboardService.Retail
|
||
&& sales[(int)OnboardService.Retail] > 0)
|
||
AddText(statement, $"Includes {Money(sales[(int)OnboardService.Retail])} in shop sales", "log-secondary");
|
||
if (trip.Onboard?.Expenses > 0)
|
||
LogLine(statement, "Onboard operating costs", Money(-trip.Onboard.Expenses));
|
||
if (trip.Onboard?.ServiceSales is int[] takings && takings.Length > (int)OnboardService.Casino
|
||
&& takings[(int)OnboardService.Casino] > 0)
|
||
AddText(statement, $"Casino · {Money(takings[(int)OnboardService.Casino])} in stakes · {Money(trip.Onboard.CasinoPayouts)} paid out", "log-secondary");
|
||
LogLine(statement, "Crew payroll", Money(-trip.Crew));
|
||
LogLine(statement, "Catering", Money(-trip.Catering));
|
||
if (trip.Plan != null)
|
||
{
|
||
LogLine(statement, $"Provisions · {trip.Plan.FoodUnits:N0} portions", Money(-trip.Plan.FoodCost));
|
||
if (trip.Plan.ProgramCost > 0)
|
||
LogLine(statement, PortPlanning.ProgramLabel(trip.Plan.Program), Money(-trip.Plan.ProgramCost));
|
||
}
|
||
LogLine(statement, "Port dues", Money(-trip.PortFees));
|
||
LogLine(statement, "Fuel expense", Money(-trip.FuelExpense));
|
||
LogLine(statement, "Loan interest", Money(-trip.LoanInterest));
|
||
LogLine(statement, "Principal repaid", Money(-trip.Principal));
|
||
if (trip.ShoreReport is ShoreReport shore)
|
||
{
|
||
LogLine(statement, $"Shore excursion sales · {shore.Guests:N0} guests", Money(shore.Revenue));
|
||
LogLine(statement, "Local operators", Money(-shore.OperatorCost));
|
||
if (shore.Resolution != ShoreLateResolution.None)
|
||
{
|
||
string response = shore.Resolution == ShoreLateResolution.Held ? "Held at berth" : "Onward transport";
|
||
LogLine(statement, $"{response} · {shore.LateGuests:N0} guests", Money(-shore.ExtraCost));
|
||
}
|
||
}
|
||
else if (trip.ShoreCallId > 0 && company.Fleet.Exists(ship => ship.ShoreCall?.CallId == trip.ShoreCallId))
|
||
AddText(statement, "Shore excursion accounts are still open.", "log-secondary");
|
||
|
||
var balances = Add(leaf, "log-totals");
|
||
LogLine(balances, "Net voyage profit", Money(trip.NetIncome), LogAmountColor(trip.NetIncome));
|
||
LogLine(balances, trip.ShoreReport != null ? "Cash change including shore call" : "Cash change on arrival",
|
||
Money(trip.CashChange), LogAmountColor(trip.CashChange));
|
||
}
|
||
|
||
private void DrawAccounts(VisualElement left, VisualElement right)
|
||
{
|
||
AddText(left, $"COMPANY POSITION / DAY {company.Day}", "log-small-title");
|
||
AddDisplay(left, "Balances", "log-sheet-title");
|
||
|
||
var balances = Add(left, "log-statement log-account-statement");
|
||
LogLine(balances, "Treasury", Money(company.Cash), "log-positive");
|
||
if (company.AvailableCash < company.Cash)
|
||
{
|
||
LogLine(balances, "Held for active voyages", Money(company.Cash - company.AvailableCash));
|
||
LogLine(balances, "Available to spend", Money(company.AvailableCash), "log-positive");
|
||
}
|
||
LogLine(balances, "Outstanding debt", Money(company.Debt));
|
||
LogLine(balances, "Lifetime voyage profit", Money(company.LifetimeProfit), LogAmountColor(company.LifetimeProfit));
|
||
LogLine(balances, "Completed voyages", company.Voyages.ToString("N0"));
|
||
AddText(left, "Voyage profit includes operating expenses and interest. Hull purchases and port permits are tracked as company outlays.", "log-note log-account-note");
|
||
|
||
AddText(right, "CREDIT ACCOUNT", "log-small-title");
|
||
AddDisplay(right, "Financing", "log-sheet-title");
|
||
|
||
var terms = Add(right, "log-statement log-account-statement");
|
||
if (company.Debt > 0)
|
||
{
|
||
LogLine(terms, "Interest rate", "13% APR");
|
||
LogLine(terms, "Principal per sailing day", Money(850));
|
||
|
||
int payment = Mathf.Min(company.Debt, 25000);
|
||
Button pay = Action($"Repay {Money(payment)} in principal",
|
||
() => Apply(company.PayDownDebt(), "Principal payment posted."), "log-paper-action", "pay-debt");
|
||
pay.SetEnabled(company.AvailableCash >= payment);
|
||
right.Add(pay);
|
||
if (!pay.enabledSelf)
|
||
AddText(right, $"You need {Money(payment)} in available cash to make this payment.", "log-note");
|
||
}
|
||
else
|
||
AddText(terms, "No outstanding loan.", "log-note");
|
||
|
||
if (!company.Bridge)
|
||
return;
|
||
|
||
var bridge = Add(right, "log-bridge");
|
||
AddText(bridge, "BRIDGE CREDIT", "log-small-title");
|
||
Button draw = Action("Draw $125,000",
|
||
() => Apply(company.DrawBridge(), "Bridge loan posted."), "log-paper-action", "draw-bridge");
|
||
draw.SetEnabled(company.CanDrawBridge);
|
||
bridge.Add(draw);
|
||
|
||
if (!company.CanDrawBridge)
|
||
AddText(bridge, "Available when spendable cash falls below $100,000.", "log-note");
|
||
}
|
||
|
||
private static void LogLine(VisualElement parent, string label, string value, string color = "")
|
||
{
|
||
var row = Add(parent, "log-line");
|
||
AddText(row, label, "log-line-description");
|
||
AddText(row, value, "log-line-value " + color);
|
||
}
|
||
|
||
private static string LogAmountColor(long amount) => amount >= 0 ? "log-positive" : "log-negative";
|
||
|
||
private static void DrawLedgerRuling(MeshGenerationContext context)
|
||
{
|
||
Rect area = context.visualElement.contentRect;
|
||
Painter2D rule = context.painter2D;
|
||
rule.strokeColor = new Color(.44f, .37f, .28f, .24f);
|
||
rule.lineWidth = 1;
|
||
rule.BeginPath();
|
||
for (int y = 23; y < area.height; y += 24)
|
||
{
|
||
rule.MoveTo(new Vector2(0, y));
|
||
rule.LineTo(new Vector2(area.width, y));
|
||
}
|
||
rule.Stroke();
|
||
}
|
||
|
||
private static void DrawLedgerCompass(MeshGenerationContext context)
|
||
{
|
||
Vector2 center = context.visualElement.contentRect.center;
|
||
Painter2D pen = context.painter2D;
|
||
pen.strokeColor = new Color(.45f, .34f, .22f, .56f);
|
||
pen.lineWidth = 1;
|
||
pen.BeginPath();
|
||
for (int i = 0; i < 32; i++)
|
||
{
|
||
float angle = i * Mathf.PI / 16;
|
||
Vector2 point = center + new Vector2(Mathf.Cos(angle), Mathf.Sin(angle)) * 40;
|
||
if (i == 0)
|
||
pen.MoveTo(point);
|
||
else
|
||
pen.LineTo(point);
|
||
}
|
||
pen.ClosePath();
|
||
pen.Stroke();
|
||
|
||
const float arm = 48;
|
||
const float shoulder = 9;
|
||
pen.BeginPath();
|
||
pen.MoveTo(center + Vector2.up * arm);
|
||
pen.LineTo(center + new Vector2(shoulder, shoulder));
|
||
pen.LineTo(center + Vector2.right * arm);
|
||
pen.LineTo(center + new Vector2(shoulder, -shoulder));
|
||
pen.LineTo(center + Vector2.down * arm);
|
||
pen.LineTo(center + new Vector2(-shoulder, -shoulder));
|
||
pen.LineTo(center + Vector2.left * arm);
|
||
pen.LineTo(center + new Vector2(-shoulder, shoulder));
|
||
pen.ClosePath();
|
||
pen.MoveTo(center + Vector2.up * arm);
|
||
pen.LineTo(center + Vector2.down * arm);
|
||
pen.MoveTo(center + Vector2.left * arm);
|
||
pen.LineTo(center + Vector2.right * arm);
|
||
pen.Stroke();
|
||
}
|
||
}
|
||
}
|