Skip to content

Commit

Permalink
A few updates (#233)
Browse files Browse the repository at this point in the history
* Improve typings

* Misc minor tweaks
  • Loading branch information
vicb authored Sep 25, 2024
1 parent 1b8ad39 commit ac81c38
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = {
SharedArrayBuffer: 'readonly'
},
parserOptions: {
ecmaVersion: 2018,
ecmaVersion: 2020,
sourceType: 'module'
},
rules: {
Expand Down
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ interface Scoring {
name: string;
code: string;
multiplier: number;
/** Fixed closing distance that is always accepted */
closingDistanceFixed?: number;
/** Closing distance that does not incur any scoring penalty */
closingDistanceFree?: number;
/** Closing distance that is relative to the full triangle length but incurs a penalty */
closingDistanceRelative?: number;
}

interface Opt {
Expand Down
4 changes: 2 additions & 2 deletions scoring-rules.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import * as scoring from './src/scoring.js';
* These are the scoring types
* @enum {object[][]} scoringRules
*
* The differences are mostly in the mutipliers and the way the closing distances of the triangles are calculated
*
* The differences are mostly in the multipliers and the way the closing distances of the triangles are calculated
*
* These are the tools that are already implemented in scoring.js/geom.js:
* @param {function} closingDistance is the triangle closing type, you choose between two types:
* closingWithLimit - closing distance is limited
Expand Down
34 changes: 15 additions & 19 deletions src/scoring.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export function closingPenalty(cd, opt) {

export function closingWithLimit(distance, opt) {
return opt.scoring.rounding(
Math.max(opt.scoring.closingDistanceFixed || 0,
distance * (opt.scoring.closingDistanceRelative || 0)));
Math.max(opt.scoring.closingDistanceFixed ?? 0,
distance * (opt.scoring.closingDistanceRelative ?? 0)));
}

/*eslint no-unused-vars: ["error", { "args": "none" }]*/
Expand All @@ -21,11 +21,7 @@ export function closingWithPenalty(distance, opt) {
}

export function finalRounding(v, opt) {
if (opt.scoring.finalRounding)
return opt.scoring.finalRounding(v);
if (opt.scoring.rounding)
return opt.scoring.rounding(v);
return v;
return opt.scoring.finalRounding?.(v) ?? opt.scoring.rounding?.(v) ?? v;
}

// Upper limit for a 3TP distance flight with 3 TPs in boxes
Expand All @@ -34,7 +30,7 @@ export function boundDistance3Points(ranges, boxes, opt) {
const pout = geom.findFurthestPointInSegment(ranges[2].end, opt.landing, boxes[2], opt);
const maxDistance = opt.scoring.rounding(
geom.maxDistanceNRectangles([pin, boxes[0], boxes[1], boxes[2], pout]));
if (maxDistance < (opt.scoring.minDistance || 0))
if (maxDistance < (opt.scoring.minDistance ?? 0))
return 0;
return finalRounding(maxDistance * opt.scoring.multiplier, opt);
}
Expand All @@ -58,7 +54,7 @@ export function scoreDistance3Points(tp, opt) {
legs[i].finish = all[i+1];
}
distance = finalRounding(distance, opt);
const score = distance >= (opt.scoring.minDistance || 0) ?
const score = distance >= (opt.scoring.minDistance ?? 0) ?
finalRounding(distance * opt.scoring.multiplier, opt) :
0;
return { distance, score, tp: tp, ep: { start: pin, finish: pout }, legs };
Expand Down Expand Up @@ -122,7 +118,7 @@ export function boundOpenTriangle(ranges, boxes, opt) {
opt.scoring.rounding(j.distanceEarth(k)) +
opt.scoring.rounding(k.distanceEarth(i));
});
if (maxTriDistance < (opt.scoring.minDistance || 0))
if (maxTriDistance < (opt.scoring.minDistance ?? 0))
return 0;
if (opt.scoring.minSide !== undefined) {
if (maxFAIDistance(maxTriDistance, boxes, opt) === 0)
Expand Down Expand Up @@ -150,7 +146,7 @@ export function scoreOpenTriangle(tp, opt) {
const d2 = opt.scoring.rounding(tp[2].distanceEarth(tp[0]));
const triDistance = d0 + d1 + d2;

if (triDistance < (opt.scoring.minDistance || 0))
if (triDistance < (opt.scoring.minDistance ?? 0))
return { score: 0 };

if (opt.scoring.minSide !== undefined) {
Expand Down Expand Up @@ -194,7 +190,7 @@ export function boundTriangle(ranges, boxes, opt) {
opt.scoring.rounding(k.distanceEarth(i));
});

if (maxTriDistance < (opt.scoring.minDistance || 0))
if (maxTriDistance < (opt.scoring.minDistance ?? 0))
return 0;
let maxDistance = maxTriDistance;
if (opt.scoring.minSide !== undefined)
Expand Down Expand Up @@ -234,7 +230,7 @@ export function scoreTriangle(tp, opt) {
}

distance = finalRounding(distance, opt);
if (distance < (opt.scoring.minDistance || 0))
if (distance < (opt.scoring.minDistance ?? 0))
return { score: 0 };

if (opt.scoring.minSide !== undefined) {
Expand Down Expand Up @@ -263,7 +259,7 @@ export function scoreTriangle(tp, opt) {
export function boundOutAndReturn2(ranges, boxes, opt) {
const maxDistance = opt.scoring.rounding(geom.maxDistance2Rectangles(boxes)) * 2;

if (maxDistance < (opt.scoring.minDistance || 0))
if (maxDistance < (opt.scoring.minDistance ?? 0))
return 0;

if (ranges[0].end < ranges[1].start) {
Expand All @@ -283,7 +279,7 @@ export function scoreOutAndReturn2(tp, opt) {
const leg = opt.scoring.rounding(tp[0].distanceEarth(tp[1]));
const distance = finalRounding(leg * 2, opt);

if (distance < (opt.scoring.minDistance || 0))
if (distance < (opt.scoring.minDistance ?? 0))
return { score: 0 };

let cp = geom.isTriangleClosed(tp[0].r, tp[1].r, distance, opt);
Expand Down Expand Up @@ -311,7 +307,7 @@ export function boundOutAndReturn1(ranges, boxes, opt) {
);
const maxDistance = opt.scoring.rounding(geom.maxDistance2Rectangles([boxes[1], box2]));

if (maxDistance < (opt.scoring.minDistance || 0))
if (maxDistance < (opt.scoring.minDistance ?? 0))
return 0;

if (ranges[0].end < ranges[2].start) {
Expand Down Expand Up @@ -345,7 +341,7 @@ export function scoreOutAndReturn1(tp, opt) {
const leg = opt.scoring.rounding(tp[1].distanceEarth(tp2));
const distance = finalRounding(leg * 2, opt);

if (distance < (opt.scoring.minDistance || 0))
if (distance < (opt.scoring.minDistance ?? 0))
return { score: 0 };

const closing = opt.scoring.rounding(tp[0].distanceEarth(tp[2]));
Expand Down Expand Up @@ -464,6 +460,6 @@ export function adjustFAICylinders(score, opt) {
break;
}
score.distance = finalRounding(score.distance, opt);
score.score = score.distance >= (opt.scoring.minDistance || 0) ?
finalRounding((score.distance - (score.penalty || 0)) * opt.scoring.multiplier, opt) : 0;
score.score = score.distance >= (opt.scoring.minDistance ?? 0) ?
finalRounding((score.distance - (score.penalty ?? 0)) * opt.scoring.multiplier, opt) : 0;
}

0 comments on commit ac81c38

Please sign in to comment.