Skip to content

Commit

Permalink
Add additional match states to handle behavior during prestart and abort
Browse files Browse the repository at this point in the history
  • Loading branch information
jfabellera committed Sep 25, 2024
1 parent 6d3ec30 commit eab5071
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions lib/models/src/fcs/FeedingTheFutureFCS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ export class PacketManager {
private broadcastCallback: (update: FieldControlUpdatePacket) => void;
private matchEmitter: EventEmitter;
private actionQueue = new Map<string, Action>();
private matchInProgress: boolean = false;
private matchState: 'prestart' | 'in progress' | 'ended' | 'aborted' =
'prestart';

private previousBalanced = true;

Expand Down Expand Up @@ -249,7 +250,7 @@ export class PacketManager {
};

public handleAbort = (): void => {
this.matchInProgress = false;
this.matchState = 'aborted';

const result: FieldControlUpdatePacket = { hubs: {}, wleds: {} };
applyPatternToStrips(
Expand Down Expand Up @@ -289,7 +290,7 @@ export class PacketManager {
};

public handleMatchStart = (): void => {
this.matchInProgress = true;
this.matchState = 'in progress';

const result: FieldControlUpdatePacket = { hubs: {}, wleds: {} };
applyPatternToStrips(
Expand All @@ -311,7 +312,7 @@ export class PacketManager {
};

public handleMatchEnd = (): void => {
this.matchInProgress = false;
this.matchState = 'ended';

const result: FieldControlUpdatePacket = { hubs: {}, wleds: {} };
applyPatternToStrips(
Expand Down Expand Up @@ -349,7 +350,7 @@ export class PacketManager {
currentDetails: FeedingTheFuture.MatchDetails,
broadcast: (update: FieldControlUpdatePacket) => void
) => {
if (!this.matchInProgress) return;
if (this.matchState !== 'in progress') return;

this.handleGoalStateChange(
previousDetails.redNexusState.CW1,
Expand Down Expand Up @@ -631,22 +632,24 @@ export class PacketManager {
this.actionQueue.set('ramp', {
timestamp: Date.now() + hysteresisWindowMs,
callback: () => {
if (this.matchInProgress) {
if (this.matchState === 'in progress') {
this.matchEmitter.emit(MatchSocketEvent.MATCH_UPDATE_DETAILS_ITEM, {
key: 'fieldBalanced',
value: balanced
} satisfies ItemUpdate);
}

const result: FieldControlUpdatePacket = { hubs: {}, wleds: {} };
applyPatternToStrips(
balanced
? this.fieldOptions.rampBalancedColor
: this.fieldOptions.rampUnbalancedColor,
[LedStripA.RAMP],
result
);
this.broadcastCallback(result);
if (this.matchState !== 'prestart' && this.matchState !== 'aborted') {
const result: FieldControlUpdatePacket = { hubs: {}, wleds: {} };
applyPatternToStrips(
balanced
? this.fieldOptions.rampBalancedColor
: this.fieldOptions.rampUnbalancedColor,
[LedStripA.RAMP],
result
);
this.broadcastCallback(result);
}
}
});
};
Expand All @@ -660,7 +663,7 @@ export class PacketManager {
const steps = 10;

const recurse = (count: number) => {
if (!this.matchInProgress) return;
if (this.matchState !== 'in progress') return;

// Base state. Food production is over, dispense food and update lights
if (count === 0) {
Expand Down

0 comments on commit eab5071

Please sign in to comment.