Yes, I had a bit more time today as both my regular gaming sessions got cancelled. Life and family happens. And I wanted to get the software to a better place than I left it yesterday. And I needed to get off the couch. I did finish putting in the world details, and then I refactored the planet controller. I was specifically calling the trade classification service which takes in a planet's UWP and spits out a list of trade classifications. We really only use the name, and, as I mentioned in the last post, we're going to be adding more planet details beyond the trade classifications. Such as the world details for gravity, surface area, etc.
I added a "Planet details service" that will return a dictionary, basically the title and a list of strings. For our trade classifications, dictionary is "Trade Classifications", list of trade classification names. But it will also return (eventually) things like "World details": "gravity 1.125", "escape velocity 12 m/s" and so on. I updated the view to basically take that list and the "title" is a <h5> header and then list the details.
This means we can add whatever we want to the details and the view will just show it automatically. Pretty easy and generic. Later on thinking we could even add formatting info somehow.
The new service:
using TravSystem.Models;
namespace TravSystem.Services; public class PlanetDetailsService : IPlanetDetailsService { private readonly ITradeClassiificationService _tradeClassificationService; public PlanetDetailsService(ITradeClassiificationService tradeClassificationService) { _tradeClassificationService = tradeClassificationService; } public async Task<Dictionary<string, List<string>>> GetDetails(TPlanet planet) { Dictionary<string, List<string>> results = new Dictionary<string, List<string>>(); List<TradeClassification> trades = await loadTradeClassifications(planet); results.Add("Trade Classifications", trades.Select(x => x.Name).ToList()) ; return results; } private async Task<List<TradeClassification>> loadTradeClassifications(TPlanet planet) { List<TradeClassification> tradeClassifications = await _tradeClassificationService.FindTradeClassifications(planet.UWP); if (tradeClassifications.Count == 0) tradeClassifications = new List<TradeClassification>() { new TradeClassification() { Name = "none" } }; return tradeClassifications; } }
And then the controller just does:and then the view:// GET: TPlanets/Details/5 public async Task<IActionResult> Details(int? id) { if (id == null) { return NotFound(); } var tPlanet = await _repo.GetByID(id.Value); if (tPlanet == null) { return NotFound(); } ViewBag.Details = await _detailsService.GetDetails(tPlanet); return View(tPlanet); }
@{ ViewData["Title"] = "Details"; var details = ViewBag.Details as Dictionary<string, List<string>>; } .... @if (details != null) { foreach (var kvp in details) { <div class="mb-3"> <h5>@kvp.Key</h5> <div>@string.Join(" ", kvp.Value)</div> </div> } }and voila, details!
While working on this, somewhere along the line I wiped out my trade classifications data. I put it all back in using the T5 book. But I did not use commas, and that broke the service. I started putting in commas between each value, then the lightbulb went off - this is pseudohex code, so 1 character per value. I updated the service so that for each line if there are commas, it will split that way, else will split per character. I do need to verify it is working correctly but it does save on some data entry. But it is all or nothing.
![]() |
ugly mix but allowed |
And yes, halfway through February already. Tempus fugit. And fugits faster as we get older.


No comments:
Post a Comment