Files
cruiseSimulator/Assets/Soundings/Runtime/ShoreExcursionInterface.cs

227 lines
11 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace Soundings
{
public sealed partial class SoundingsApp
{
private Voyage shorePlanningQuote;
private Ship shorePlanningShip;
private ShoreOffer[] shoreDraft;
private ShoreCall displayedShoreCall;
private Label shoreReturnClock;
private int displayedShoreSeconds = -1;
private bool ShorePlanningOpen => shorePlanningQuote != null;
private void DrawShorePlanningLink(VisualElement parent, Voyage quote, bool atSea)
{
if (quote == null || ShoreExcursions.Catalog(quote.Destination).Count == 0)
return;
if (atSea)
parent.AddToClassList("shore-passage-actions");
int offers = CurrentShoreOffers(quote)?.Length ?? 0;
string buttonText = atSea || offers == 0 ? "Shore excursions" : $"Shore excursions · {offers} offered";
Button link = Action(buttonText, () => OpenShorePlanning(quote),
atSea ? "shore-passage-link" : "chart-ink-button shore-chart-link", "shore-planning-open");
link.tooltip = $"Set excursion prices and seats for {Market.Ports[quote.Destination].Name}";
parent.Add(link);
}
private void OpenShorePlanning(Voyage quote)
{
IReadOnlyList<ShoreTour> catalog = ShoreExcursions.Catalog(quote.Destination);
shorePlanningQuote = quote;
shorePlanningShip = Ship;
shoreDraft = new ShoreOffer[catalog.Count];
for (int i = 0; i < catalog.Count; i++)
shoreDraft[i] = new ShoreOffer { Tour = i, Price = catalog[i].StandardPrice, Capacity = 0 };
ShoreOffer[] saved = CurrentShoreOffers(quote);
if (saved != null)
foreach (ShoreOffer offer in saved)
shoreDraft[offer.Tour] = new ShoreOffer { Tour = offer.Tour, Price = offer.Price, Capacity = offer.Capacity };
Render();
}
private void ResetShorePlanning()
{
shorePlanningQuote = null;
shorePlanningShip = null;
shoreDraft = null;
}
private void CloseShorePlanning()
{
ResetShorePlanning();
Render();
}
private void DrawShorePlanning(VisualElement parent)
{
Voyage quote = shorePlanningQuote;
if (quote == null || Ship != shorePlanningShip)
return;
for (int i = 0; i < parent.childCount; i++)
parent[i].SetEnabled(false);
var overlay = Add(parent, "shore-planning-overlay");
var sheet = Add(overlay, "shore-planning-sheet");
sheet.name = "shore-planning";
var heading = Add(sheet, "shore-planning-heading");
var place = Add(heading, "shore-planning-place");
AddText(place, Market.Ports[quote.Destination].Name.ToUpperInvariant(), "shore-kicker");
AddDisplay(place, "Shore excursions", "shore-planning-title");
heading.Add(Action("Close Esc", CloseShorePlanning, "shore-planning-close", "shore-planning-close"));
string passengers = quote == underway ? $"{quote.Guests:N0} guests aboard" : $"{quote.Guests:N0} guests expected";
AddText(sheet, $"Set prices and seats before docking. {passengers} · {quote.Weather} forecast.", "shore-planning-intro");
var tours = new ScrollView(ScrollViewMode.Vertical) { horizontalScrollerVisibility = ScrollerVisibility.Hidden };
tours.AddToClassList("shore-tour-list");
sheet.Add(tours);
IReadOnlyList<ShoreTour> catalog = ShoreExcursions.Catalog(quote.Destination);
var forecasts = new Label[catalog.Count];
var offerButtons = new Button[catalog.Count];
var tourCards = new VisualElement[catalog.Count];
Label forecastTotal = null;
for (int i = 0; i < catalog.Count; i++)
{
int index = i;
ShoreTour tour = catalog[index];
var card = tourCards[index] = Add(tours, "shore-tour");
var header = Add(card, "shore-tour-header");
var description = Add(header, "shore-tour-description");
AddDisplay(description, tour.Name, "shore-tour-name");
AddText(description, $"{tour.Hours:0.#} hr · {(tour.Outdoor ? "Outdoors" : "Mostly indoors")}", "shore-tour-facts");
offerButtons[index] = Action("", () =>
{
IntegerField seats = card.Q<IntegerField>($"shore-capacity-{index}");
seats.value = shoreDraft[index].Capacity > 0 ? 0 : Math.Min(24, tour.MaxCapacity);
}, "shore-offer", $"shore-offer-{index}");
header.Add(offerButtons[index]);
AddText(card, tour.Description, "shore-tour-copy");
var controls = Add(card, "shore-tour-controls");
AddShoreNumber(controls, "Price ($)", $"shore-price-{index}", shoreDraft[index].Price, tour.MinPrice, tour.MaxPrice,
price => { shoreDraft[index].Price = price; RefreshForecasts(); });
AddShoreNumber(controls, "Seats", $"shore-capacity-{index}", shoreDraft[index].Capacity, 0, tour.MaxCapacity,
seats => { shoreDraft[index].Capacity = seats; RefreshForecasts(); });
forecasts[index] = AddText(card, "", "shore-forecast");
}
var footer = Add(sheet, "shore-planning-footer");
var estimate = Add(footer, "shore-planning-estimate");
forecastTotal = AddText(estimate, "", "shore-plan-total");
AddText(estimate, "Projected before any late return costs.", "shore-note");
Label error = AddText(sheet, "", "shore-error");
error.style.display = DisplayStyle.None;
footer.Add(Action("Save excursion plan", () =>
{
if (ApplyShoreOffers(quote, ActiveShoreOffers()))
CloseShorePlanning();
else
{
error.text = "This excursion plan is no longer available.";
error.style.display = DisplayStyle.Flex;
}
}, "primary shore-save-plan", "shore-plan-save"));
RefreshForecasts();
void RefreshForecasts()
{
ShoreBooking[] bookings = ShoreExcursions.Forecast(quote, ActiveShoreOffers());
int guests = 0, net = 0;
for (int i = 0; i < forecasts.Length; i++)
{
tourCards[i].EnableInClassList("shore-tour-offered", shoreDraft[i].Capacity > 0);
offerButtons[i].text = shoreDraft[i].Capacity > 0 ? "Remove offer" : "Offer tour";
forecasts[i].text = shoreDraft[i].Capacity > 0 ? "No bookings expected." : $"Set seats to offer this tour · Up to {catalog[i].MaxCapacity:N0}";
}
foreach (ShoreBooking booking in bookings)
{
guests += booking.Guests;
net += booking.Revenue - booking.OperatorCost;
forecasts[booking.Tour].text = $"{booking.Guests:N0} booked / {booking.Demand:N0} interested · "
+ $"{Money(booking.OperatorCost)} operator cost · {booking.DelayRisk}% late risk";
}
forecastTotal.text = $"{guests:N0} expected guests · {Money(net)} projected net";
forecastTotal.EnableInClassList("loss", net < 0);
}
}
private static void AddShoreNumber(VisualElement parent, string label, string id, int current, int minimum, int maximum,
Action<int> apply)
{
var field = new IntegerField(label) { name = id, value = current, tooltip = $"{minimum:N0}–{maximum:N0}" };
Classes(field, "field shore-number");
field.RegisterValueChangedCallback(change => apply(Mathf.Clamp(change.newValue, minimum, maximum)));
field.RegisterCallback<FocusOutEvent>(_ => field.SetValueWithoutNotify(Mathf.Clamp(field.value, minimum, maximum)));
parent.Add(field);
}
private ShoreOffer[] ActiveShoreOffers()
{
return Array.FindAll(shoreDraft, offer => offer.Capacity > 0);
}
private void DrawBerthExcursions(VisualElement parent)
{
displayedShoreCall = Ship?.ShoreCall;
shoreReturnClock = null;
displayedShoreSeconds = -1;
if (displayedShoreCall is not ShoreCall call || call.Phase == ShorePhase.Complete)
return;
var card = Add(parent, "shore-berth");
card.name = "shore-operations";
AddText(card, $"{Market.Ports[call.Port].Name.ToUpperInvariant()} / SHORE OPERATIONS", "shore-kicker");
if (call.Phase == ShorePhase.AwaitingDecision)
{
card.AddToClassList("shore-berth-decision");
AddDisplay(card, "Late shore excursion", "shore-berth-title");
ShoreTour tour = ShoreExcursions.Catalog(call.Port)[call.LateTour];
AddText(card, $"{call.LateGuests:N0} guests are still ashore. The last group is returning from {tour.Name}.",
"shore-berth-detail");
AddShoreDecision(card, "Hold departure", $"{Money(call.HoldCost)} berth costs · About {call.HoldMinutes} min until the group boards.",
true, "shore-departure-hold");
AddShoreDecision(card, "Arrange transport and depart",
$"{Money(call.TransportCost)} transfer cost · Guests are safely transferred and satisfaction falls.",
false, "shore-departure-transport");
return;
}
bool held = call.Phase == ShorePhase.Holding;
AddDisplay(card, held ? "Departure held" : "Guests ashore", "shore-berth-title");
AddText(card, held ? $"Waiting for {call.LateGuests:N0} guests to return."
: $"{call.Guests:N0} guests on shore excursions. Tours must clear before your next departure.", "shore-berth-detail");
shoreReturnClock = AddText(card, "", "shore-berth-clock");
RefreshBerthExcursions();
}
private void AddShoreDecision(VisualElement parent, string name, string detail, bool hold, string id)
{
Button option = Action("", () => DecideShoreDeparture(hold), hold ? "shore-decision shore-decision-hold" : "shore-decision", id);
AddText(option, name, "shore-decision-title");
AddText(option, detail, "shore-decision-detail");
parent.Add(option);
}
private void RefreshBerthExcursions()
{
if (shoreReturnClock == null || displayedShoreCall == null || displayedShoreCall != Ship?.ShoreCall)
return;
int seconds = Mathf.CeilToInt(displayedShoreCall.RemainingSeconds);
if (seconds == displayedShoreSeconds)
return;
displayedShoreSeconds = seconds;
shoreReturnClock.text = $"Return in {seconds / 60}:{seconds % 60:00}";
}
}
}