Skip to content

Commit

Permalink
Validate that waypoint source is carrier location when setting waypoi…
Browse files Browse the repository at this point in the history
…nts for orbiting carrier
  • Loading branch information
SpacialCircumstances committed Jul 5, 2024
1 parent 686362f commit 2e3107d
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions server/services/waypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default class WaypointService {
}

async saveWaypoints(game: Game, player: Player, carrierId: DBObjectId, waypoints: CarrierWaypointBase[], looped: boolean) {
let carrier = this.carrierService.getById(game, carrierId);
const carrier = this.carrierService.getById(game, carrierId);

if (!carrier) {
throw new ValidationError(`Could not find carrier with id ${carrierId}`);
Expand All @@ -61,8 +61,8 @@ export default class WaypointService {
return await this.saveWaypointsForCarrier(game, player, carrier, waypoints, looped);
}

async saveWaypointsForCarrier(game: Game, player: Player, carrier: Carrier, waypoints: CarrierWaypointBase[], looped: boolean, writeToDB: boolean = true) {
if (looped == null) {
async saveWaypointsForCarrier(game: Game, player: Player, carrier: Carrier, waypoints: CarrierWaypointBase[], looped: boolean | null, writeToDB: boolean = true) {
if (looped === null) {
looped = false;
}

Expand All @@ -81,8 +81,8 @@ export default class WaypointService {
// If the carrier is currently in transit then double check that the first waypoint
// matches the source and destination.
if (!carrier.orbiting) {
let currentWaypoint = carrier.waypoints[0];
let newFirstWaypoint = waypoints[0];
const currentWaypoint = carrier.waypoints[0];
const newFirstWaypoint = waypoints[0];

if (!newFirstWaypoint
|| currentWaypoint.source.toString() !== newFirstWaypoint.source.toString()
Expand All @@ -93,16 +93,20 @@ export default class WaypointService {
if (+newFirstWaypoint.delayTicks) {
throw new ValidationError('The first waypoint cannot have delay ticks if mid-flight.');
}
} else {
if (carrier.orbiting.toString() !== waypoints[0].source.toString()) {
throw new ValidationError('The source star does not match the carrier location.');
}
}

// Validate new waypoints.
for (let i = 0; i < waypoints.length; i++) {
let waypoint = waypoints[i];
const waypoint = waypoints[i];

let sourceStar = this.starService.getById(game, waypoint.source);
let destinationStar = this.starService.getById(game, waypoint.destination);
const sourceStar = this.starService.getById(game, waypoint.source);
const destinationStar = this.starService.getById(game, waypoint.destination);

let sourceStarName = sourceStar == null ? 'Unknown' : sourceStar.name; // Could be travelling from a destroyed star.
const sourceStarName = sourceStar == null ? 'Unknown' : sourceStar.name; // Could be travelling from a destroyed star.

// Make sure the user isn't being a dumbass.
waypoint.actionShips = waypoint.actionShips || 0;
Expand Down Expand Up @@ -185,11 +189,11 @@ export default class WaypointService {
}

_waypointRouteIsWithinHyperspaceRange(game: Game, carrier: Carrier, waypoint: CarrierWaypointBase) {
let sourceStar = this.starService.getById(game, waypoint.source);
let destinationStar = this.starService.getById(game, waypoint.destination);
const sourceStar = this.starService.getById(game, waypoint.source);
const destinationStar = this.starService.getById(game, waypoint.destination);

// Stars may have been destroyed.
if (sourceStar == null || destinationStar == null) {
if (!sourceStar || !destinationStar) {
return false;
}

Expand Down

0 comments on commit 2e3107d

Please sign in to comment.