Skip to content

Commit

Permalink
Update to new return type for rules
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeelio committed Sep 24, 2024
1 parent 225c20f commit 6c66ae9
Show file tree
Hide file tree
Showing 20 changed files with 178 additions and 82 deletions.
40 changes: 30 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,23 +336,43 @@ A rule also has several components:

### Returning a `RuleValue`

The return value is an tuple, with one mandatory value, and 2 optional values:
The return value has multiple possible values, sending on

```typescript
export type RuleValue = undefined | boolean | [string?, number?]
```
If a rule is passing/hasn't been violated:
```typescript
return
return false
```

If a rule has been violated:

```typescript
return true
```
RuleValue = [boolean, message, points]

Or, if you want to return a custom message:

```typescript
return ['message']
```

Or, if you want to return a message and points:

```typescript
// The true/false MUST be there, it's if the rule was violated or not
return [true]
return ['message', points]
```

// Return a message along with the state. If not set or null, it will
// use `this.meta.message` as the default
return [true, message]
If you want to return just the points, you can return:

// This returns that it was violated, uses the default message, but changes
// the points returned to -10
return [true, null, -10]
```typescript
return ['', points]
```

`points` and `message` are optional - if omitted, they're pulled from the `meta` block

### Helper Methods
44 changes: 44 additions & 0 deletions src/aircraft/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { AircraftConfigSimType, AircraftFeature, FeatureType } from '../defs'
import {
AircraftConfig,
FeatureAddresses,
FeatureState,
FlapNames,
Meta,
} from '../interface/aircraft'

export default class Example extends AircraftConfig {
meta: Meta = {
id: 'example',
name: 'example',
sim: AircraftConfigSimType.MsFs,
enabled: true,
priority: 2,
}

features: FeatureAddresses = {
[AircraftFeature.BeaconLights]: {
example_lvar: FeatureType.Int,
},
}

flapNames: FlapNames = {
0: 'UP',
1: 'CONF 1',
}

/**
*
* @param {string} title The title of the aircraft, lowercased
* @param {string=} icao The ICAO of the aircraft. Might not be available
* @param {string=} config_path Path to the aircraft config. Might not be there
* @return {boolean}
*/
match(title: string, icao: string, config_path: string): boolean {
return ['example', 'aircraft'].every((w) => title.includes(w))
}

beaconLights(): FeatureState {
return null
}
}
6 changes: 3 additions & 3 deletions src/rules/beacon_lights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ export default class BeaconLights implements Rule {

violated(pirep: Pirep, data: Telemetry, previousData?: Telemetry): RuleValue {
if (!Acars.IsFeatureEnabled(AircraftFeature.BeaconLights)) {
return [false]
return false
}

return Acars.ViolatedAfterDelay(
this.meta.id,
this.meta.delay_time,
(): RuleValue => {
if (!pirep.isInActiveState) {
return [false]
return false
}

return data.anyEnginesRunning && !data.beaconLights ? [true] : [false]
return data.anyEnginesRunning && !data.beaconLights
},
)
}
Expand Down
27 changes: 13 additions & 14 deletions src/rules/excess_bank.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Excess bank angle
*/
import { AircraftFeature, PirepState } from '../defs'
import { PirepState } from '../defs'
import { Meta, Rule, RuleValue } from '../types/rule'
import { Pirep, Telemetry } from '../types/types'

Expand All @@ -27,20 +27,19 @@ export default class ExcessBank implements Rule {

violated(pirep: Pirep, data: Telemetry, previousData?: Telemetry): RuleValue {
if (data.onGround) {
return [false]
return
}

return Acars.ViolatedAfterDelay(this.meta.id, this.meta.delay_time, () => {
// +Bank is right, -Bank is left
const value =
data.bank < -1 * this.meta.parameter! ||
data.bank > this.meta.parameter!

if (value) {
return [true]
} else {
return [false]
}
})
return Acars.ViolatedAfterDelay(
this.meta.id,
this.meta.delay_time,
(): RuleValue => {
// +Bank is right, -Bank is left
return (
data.bank < -1 * this.meta.parameter! ||
data.bank > this.meta.parameter!
)
},
)
}
}
4 changes: 2 additions & 2 deletions src/rules/excess_gforce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export default class ExcessGForce implements Rule {

violated(pirep: Pirep, data: Telemetry, previousData?: Telemetry): RuleValue {
if (data.onGround) {
return [false]
return
}

return [data.gForce >= this.meta.parameter!]
return data.gForce >= this.meta.parameter!
}
}
8 changes: 4 additions & 4 deletions src/rules/excess_pitch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ export default class ExcessPitch implements Rule {

violated(pirep: Pirep, data: Telemetry, previousData?: Telemetry): RuleValue {
if (data.onGround) {
return [false]
return
}

return [
return (
data.pitch < -1 * this.meta.parameter! ||
data.pitch > this.meta.parameter!,
]
data.pitch > this.meta.parameter!
)
}
}
6 changes: 3 additions & 3 deletions src/rules/excess_taxi_speed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ export default class ExcessTaxiSpeed implements Rule {
this.meta.delay_time,
(): RuleValue => {
if (!data.onGround) {
return [false]
return
}

// If they're on a runway, ignore any taxi speed warnings, might be taking off
if (data.approachingRunway != null || data.runway != null) {
return [false]
return
}

return [data.groundSpeed.Knots > this.meta.parameter!]
return data.groundSpeed.Knots > this.meta.parameter!
},
)
}
Expand Down
23 changes: 13 additions & 10 deletions src/rules/fuel_refilled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,23 @@ export default class FuelRefilled implements Rule {

violated(pirep: Pirep, data: Telemetry, previousData?: Telemetry): RuleValue {
if (data.onGround || previousData == null) {
return [false]
return
}

if (data.fuelQuantity.Pounds > previousData.fuelQuantity.Pounds) {
return [false]
return
}

return [
Acars.NumberOverPercent(
data.fuelQuantity.Pounds,
previousData.fuelQuantity.Pounds,
10,
),
`Fuel Refilled: Current: ${data.fuelQuantity.Pounds}, previous=${previousData.fuelQuantity.Pounds}`,
]
const violated = Acars.NumberOverPercent(
data.fuelQuantity.Pounds,
previousData.fuelQuantity.Pounds,
10,
)

if (violated) {
return [
`Fuel Refilled: Current: ${data.fuelQuantity.Pounds}, previous=${previousData.fuelQuantity.Pounds}`,
]
}
}
}
8 changes: 4 additions & 4 deletions src/rules/hard_landing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ export default class HandLandings implements Rule {
meta: Meta = {
id: 'HARD_LANDING',
name: 'Hard Landing',
enabled: true, // Default, will change depending on airline config
enabled: true,
message: 'Hard Landing',
states: [PirepState.Final, PirepState.Landed],
repeatable: false,
cooldown: 60,
max_count: 3,
points: -1,
parameter: 300, // default, gets overwritten from remote
parameter: 500,
}

violated(pirep: Pirep, data: Telemetry, previousData?: Telemetry): RuleValue {
const absRate = Math.abs(pirep.landingRate.FeetPerMinute)
const absParam = Math.abs(this.meta.parameter!)
if (absRate < absParam) {
return [false]
return
}

console.log(
`Hard landing violation, rate=${absRate}, threshold=${absParam}`,
)

return [true]
return true
}
}
6 changes: 3 additions & 3 deletions src/rules/lights_off_during_taxi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default class LightsOffDuringTaxi implements Rule {

violated(pirep: Pirep, data: Telemetry, previousData?: Telemetry): RuleValue {
if (!Acars.IsFeatureEnabled(AircraftFeature.LandingLights)) {
return [false]
return
}

return Acars.ViolatedAfterDelay(
Expand All @@ -30,10 +30,10 @@ export default class LightsOffDuringTaxi implements Rule {
(): RuleValue => {
// Ignore landing lights being turned on
if (data.approachingRunway != null || data.runway != null) {
return [false]
return
}

return [data.landingLights]
return data.landingLights
},
)
}
Expand Down
8 changes: 5 additions & 3 deletions src/rules/lights_over_10k.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export default class LightsOver10K implements Rule {

violated(pirep: Pirep, data: Telemetry, previousData?: Telemetry): RuleValue {
if (!Acars.IsFeatureEnabled(AircraftFeature.LandingLights)) {
return [false]
return
}

if (data.onGround) {
return [false]
return
}

return Acars.ViolatedAfterDelay(
Expand All @@ -37,7 +37,9 @@ export default class LightsOver10K implements Rule {
data.landingLights &&
data.planeAltitude.Feet > this.meta.parameter! + 500

return [violated, this.meta.message + this.meta.parameter!]
if (violated) {
return [this.meta.message + this.meta.parameter!]
}
},
)
}
Expand Down
8 changes: 5 additions & 3 deletions src/rules/lights_under_10k.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export default class LightsUnder10K implements Rule {

violated(pirep: Pirep, data: Telemetry, previousData?: Telemetry): RuleValue {
if (!Acars.IsFeatureEnabled(AircraftFeature.LandingLights)) {
return [false]
return
}

if (data.onGround) {
return [false]
return
}

return Acars.ViolatedAfterDelay(
Expand All @@ -37,7 +37,9 @@ export default class LightsUnder10K implements Rule {
!data.landingLights &&
data.planeAltitude.Feet < this.meta.parameter! - 500

return [violated, this.meta.message + this.meta.parameter!]
if (violated) {
return [this.meta.message + this.meta.parameter!]
}
},
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/simrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ export default class SimRate implements Rule {
}

violated(pirep: Pirep, data: Telemetry, previousData?: Telemetry): RuleValue {
return [data.simRate > this.meta.parameter!]
return data.simRate > this.meta.parameter!
}
}
2 changes: 1 addition & 1 deletion src/rules/slew_activated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ export default class SlewActivated implements Rule {
}

violated(pirep: Pirep, data: Telemetry, previousData?: Telemetry): RuleValue {
return [data.slewActive]
return data.slewActive
}
}
Loading

0 comments on commit 6c66ae9

Please sign in to comment.