Skip to content

Commit

Permalink
misc: changes to IFleetCarrier interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ROMVoid95 committed Nov 2, 2023
1 parent fc994cb commit ba6cc82
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 152 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ tscg {
dependencies {
compileOnly 'io.javalin:javalin:5.6.2'
compileOnly 'com.squareup.okhttp3:okhttp:4.11.0'
compileOnly('net.dv8tion:JDA:5.0.0-beta.17') { exclude module: 'opus-java' }
implementation 'org.apache.commons:commons-lang3:3.13.0'
implementation 'org.tinylog:tinylog-api:2.6.2'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.3'
Expand Down
122 changes: 122 additions & 0 deletions src/main/java/space/tscg/api/carrier/Builder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package space.tscg.api.carrier;

import java.util.ArrayList;
import java.util.List;

import elite.dangerous.capi.FleetCarrierData;
import elite.dangerous.capi.modal.fleetcarrier.Modules;
import elite.dangerous.capi.modal.fleetcarrier.Modules.EliteModule;
import elite.dangerous.capi.modal.fleetcarrier.Ships;
import elite.dangerous.capi.modal.fleetcarrier.Ships.EliteShip;
import elite.dangerous.fdevid.Outfitting;
import elite.dangerous.fdevid.Shipyard;
import space.tscg.database.entity.CarrierMarket;
import space.tscg.database.entity.CarrierMarket.CarrierModule;
import space.tscg.database.entity.CarrierMarket.CarrierShip;
import space.tscg.database.entity.CarrierServices;
import space.tscg.database.entity.CarrierServices.Service;
import space.tscg.database.entity.CarrierServices.TaxableService;
import space.tscg.database.entity.FleetCarrier;

final class Builder {
/**
* Creates a FleetCarrier instance from the data returned from Frontier's CAPI /fleetcarrier endpoint
*
* @param data the CAPIFleetCarrier
* @return a new FleetCarrier instance
*/
static FleetCarrier build(FleetCarrierData data) {
var services = data.getServicesCrew();
var taxation = data.getFinance().getServiceTaxation();

var moduleList = buildModuleList(data.getModules());
var shipList = buildShipList(data.getShips());

var cs = CarrierServices.Builder();
if(services.isRefuelInstalled())
cs.refuel(TaxableService.Creator().enabled(!services.isRefuelSuspended()).tax(taxation.refuel).build());
if(services.isRepairInstalled())
cs.repair(TaxableService.Creator().enabled(!services.isRepairSuspended()).tax(taxation.repair).build());
if(services.isRearmInstalled())
cs.armoury(TaxableService.Creator().enabled(!services.isRearmSuspended()).tax(taxation.rearm).build());
if(services.isRedemptionOfficeInstalled())
cs.redemptionOffice(Service.Creator().enabled(!services.isRedemptionOfficeSuspended()).build());
if(services.isShipyardInstalled())
cs.shipyard(TaxableService.Creator().enabled(!services.isShipyardSuspended()).tax(taxation.shipyard).build());
if(services.isOutfittingInstalled())
cs.outfitting(TaxableService.Creator().enabled(!services.isOutfittingSuspended()).tax(taxation.outfitting).build());
if(services.isBlackmarketInstalled())
cs.secureWarehouse(Service.Creator().enabled(!services.isBlackmarketSuspended()).build());
if(services.isUniversalCartographicsInstalled())
cs.universalCartographics(Service.Creator().enabled(!services.isUniversalCartographicsSuspended()).build());
if(services.isBartenderInstalled())
cs.concourseBar(Service.Creator().enabled(!services.isBartenderSuspended()).build());
if(services.isVistaGenomicsInstalled())
cs.vistaGenomics(Service.Creator().enabled(!services.isVistaGenomicsSuspended()).build());
if(services.isPioneerSuppliesInstalled())
cs.pioneerSupplies(TaxableService.Creator().enabled(!services.isPioneerSuppliesSuspended()).tax(taxation.pioneersupplies).build());

return FleetCarrier.Builder()
.id(data.getCarrierId())
.callsign(data.getCallsign())
.name(data.getName())
.system(data.getCurrentStarSystem())
.fuel(data.getFuel())
.services(cs.build())
.orders(data.getOrders())
.market(CarrierMarket.Builder().ships(shipList).modules(moduleList).build())
.build();
}

private static List<ICarrierModule> buildModuleList(Modules modules)
{
List<ICarrierModule> list = new ArrayList<>();
if(modules.isEmpty())
{
return list;
} else {
for(EliteModule module : modules)
{
Outfitting.Module m = Outfitting.getFromId(module.getId());

var builder = CarrierModule.Builder();
if(m.getGuidance() != null)
builder.guidance(m.getGuidance());
if(m.getMount() != null)
builder.mount(m.getMount());

builder.fdevId(module.getId());
builder.stock(module.getStock());
builder.cost(module.getCost());
builder.category(m.getCategory());
builder.moduleClass(m.getModuleClass());
builder.name(m.getName());
builder.rating(m.getRating());

list.add(builder.build());
}

return list;
}
}

private static List<ICarrierShip> buildShipList(Ships ships)
{
List<ICarrierShip> list = new ArrayList<>();
if(ships.getShipyard_list().isEmpty())
{
return list;
} else {
for(EliteShip ship: ships.getShipyard_list())
{
list.add(CarrierShip.Builder()
.fdevId(ship.getId())
.cost(ship.getBaseValue())
.name(Shipyard.getFromId(ship.getId()).getName())
.build());
}

return list;
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/space/tscg/api/carrier/ICarrierServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import space.tscg.api.Diffable;
import space.tscg.database.entity.CarrierServices;

/**
* Represents the collection of Services a FleetCarrier can have
*/
@JsonDeserialize(as = CarrierServices.class)
public interface ICarrierServices
public interface ICarrierServices extends Diffable<ICarrierServices>
{

/**
Expand Down
31 changes: 30 additions & 1 deletion src/main/java/space/tscg/api/carrier/IFleetCarrier.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package space.tscg.api.carrier;

import java.util.Optional;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import elite.dangerous.capi.FleetCarrierData;
import elite.dangerous.capi.modal.fleetcarrier.Orders;
import net.dv8tion.jda.api.entities.UserSnowflake;
import space.tscg.api.Diffable;
import space.tscg.api.database.DbEntity;
import space.tscg.database.DefinedTable;
import space.tscg.database.defined.TSCGDatabase;
import space.tscg.database.entity.FleetCarrier;
import space.tscg.database.entity.TSCGMember;

/**
* Represents very basic information about a Fleet Carrier, all implementations should be able to return
* all the information contained in this interface.
*/
@JsonDeserialize(as = FleetCarrier.class)
public interface IFleetCarrier
public interface IFleetCarrier extends DbEntity, Diffable<IFleetCarrier>
{
/**
* Returns the carrier's id. This is the Market ID for the carrier
Expand Down Expand Up @@ -67,4 +76,24 @@ public interface IFleetCarrier
* @return ICarrierMarket instance
*/
ICarrierMarket getMarket();

static IFleetCarrier fromFleetCarrierData(FleetCarrierData data)
{
return Builder.build(data);
}

static Optional<IFleetCarrier> fromId(String id)
{
return Optional.ofNullable(TSCGDatabase.instance().get(DefinedTable.CARRIERS, id, IFleetCarrier.class));
}

static IFleetCarrier fromMmember(TSCGMember member)
{
return TSCGDatabase.instance().get(DefinedTable.CARRIERS, member.getElite().getCarrierId(), IFleetCarrier.class);
}

static IFleetCarrier fromUserSnowflake(UserSnowflake snowflake)
{
return TSCGDatabase.instance().get(DefinedTable.CARRIERS, TSCGMember.fromUserSnowflake(snowflake).getElite().getCarrierId(), IFleetCarrier.class);
}
}
3 changes: 2 additions & 1 deletion src/main/java/space/tscg/api/carrier/IService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import space.tscg.api.Diffable;
import space.tscg.database.entity.CarrierServices.Service;

/**
* A toggleable service that is accessible on fleetcarriers
*/
@JsonDeserialize(as = Service.class)
public interface IService
public interface IService extends Diffable<IService>
{
/**
* If this service is installed and publically available
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/space/tscg/api/carrier/ITaxableService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import space.tscg.api.Diffable;
import space.tscg.database.entity.CarrierServices.TaxableService;

/**
* A tax-rate configurable service that is accessible on fleetcarriers
*/
@JsonDeserialize(as = TaxableService.class)
public interface ITaxableService
public interface ITaxableService extends Diffable<ITaxableService>
{
/**
* If this service is installed and publically available
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/space/tscg/database/entity/CarrierServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import lombok.Builder;
import lombok.Getter;
import lombok.extern.jackson.Jacksonized;
import space.tscg.api.Diffable;
import space.tscg.api.carrier.ICarrierServices;
import space.tscg.api.carrier.IService;
import space.tscg.api.carrier.ITaxableService;
Expand All @@ -20,7 +19,7 @@
@Getter
@Builder(builderMethodName = "Builder")
@Jacksonized
public class CarrierServices implements ICarrierServices, Diffable<CarrierServices>
public class CarrierServices implements ICarrierServices
{
@Builder.Default
private TaxableService refuel = null;
Expand Down Expand Up @@ -133,7 +132,7 @@ public boolean isPioneerSuppliesInstalled()
}

@Override
public DiffMap diff(CarrierServices other)
public DiffMap diff(ICarrierServices other)
{
var diff = DiffMap.Builder();
if(this.refuel != null)
Expand Down Expand Up @@ -165,11 +164,11 @@ public DiffMap diff(CarrierServices other)
@Getter
@Builder(builderMethodName = "Creator")
@Jacksonized
public static class Service implements IService, Diffable<Service> {
public static class Service implements IService {
private boolean enabled;

@Override
public DiffMap diff(Service other) {
public DiffMap diff(IService other) {
return DiffMap
.Builder()
.append("active", this.isEnabled(), other.isEnabled())
Expand All @@ -180,12 +179,12 @@ public DiffMap diff(Service other) {
@Getter
@Builder(builderMethodName = "Creator")
@Jacksonized
public static class TaxableService implements ITaxableService, Diffable<TaxableService> {
public static class TaxableService implements ITaxableService {
private boolean enabled;
private int tax;

@Override
public DiffMap diff(TaxableService other) {
public DiffMap diff(ITaxableService other) {
return DiffMap
.Builder()
.append("enabled", this.isEnabled(), other.isEnabled())
Expand Down
Loading

0 comments on commit ba6cc82

Please sign in to comment.