Skip to content

Commit

Permalink
Add start to delayed actions
Browse files Browse the repository at this point in the history
I love ctrl c ctrl v
  • Loading branch information
jfabellera committed Sep 16, 2024
1 parent d50f4dd commit de20de1
Show file tree
Hide file tree
Showing 3 changed files with 260 additions and 6 deletions.
17 changes: 14 additions & 3 deletions back-end/realtime/src/rooms/FCS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Server, Socket } from 'socket.io';
import {
defaultFieldOptions,
FcsPackets,
FeedingTheFuture,
FieldControlUpdatePacket,
FieldOptions,
getFcsPackets,
Expand All @@ -19,6 +20,8 @@ import {
buildWledSetColorPacket
} from '../util/WLEDHelper.js';
import logger from '../util/Logger.js';
import { matchUpdateCallback } from '../util/FeedingTheFutureCallbacks.js';
import { defaultMatchDetails } from '@toa-lib/models/build/seasons/FeedingTheFuture.js';

export default class FCS extends Room {
private readonly latestFcsStatus: FieldControlUpdatePacket = {
Expand All @@ -28,6 +31,8 @@ export default class FCS extends Room {
private fcsPackets: FcsPackets = getFcsPackets(defaultFieldOptions);
private wledSockets: Record<string, WebSocket> = {};
public readonly matchRoom: Match;
private previousMatchDetails: FeedingTheFuture.MatchDetails =
defaultMatchDetails;

public constructor(server: Server, matchRoom: Match) {
super(server, 'fcs');
Expand Down Expand Up @@ -58,7 +63,13 @@ export default class FCS extends Room {
matchRoom.localEmitter.on(
MatchSocketEvent.UPDATE,
(match: MatchObj<any>) => {
this.broadcastFcsUpdate(processMatchData(match.details));
if (!match.details) return;
matchUpdateCallback(
this.previousMatchDetails,
match.details,
this.broadcastFcsUpdate
);
this.previousMatchDetails = { ...match.details };
}
);
}
Expand Down Expand Up @@ -102,7 +113,7 @@ export default class FCS extends Room {
socket.emit('fcs:update', this.latestFcsStatus);
}

private broadcastFcsUpdate(update: FieldControlUpdatePacket): void {
private broadcastFcsUpdate = (update: FieldControlUpdatePacket): void => {
this.broadcast().emit('fcs:update', update);

// Handle wleds
Expand Down Expand Up @@ -165,7 +176,7 @@ export default class FCS extends Room {
}
}
}
}
};

// TODO(jan): Handle disconnects? Regular websockets suck

Expand Down
243 changes: 243 additions & 0 deletions back-end/realtime/src/util/FeedingTheFutureCallbacks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
import {
applyPatternToStrips,
applyStateToGoal,
FeedingTheFuture,
FieldControlUpdatePacket,
FieldOptions,
LedStrip,
Match
} from '@toa-lib/models';
import { NexusGoalState } from '@toa-lib/models/build/seasons/FeedingTheFuture.js';
import { Socket } from 'socket.io';

const timers = new Map<string, NodeJS.Timeout>();

export const matchUpdateCallback = (
previousDetails: FeedingTheFuture.MatchDetails,
currentDetails: FeedingTheFuture.MatchDetails,
broadcast: (update: FieldControlUpdatePacket) => void
) => {
handleGoalStateChange(
previousDetails.redNexusState.CW1,
currentDetails.redNexusState.CW1,
LedStrip.RED_SIDE_GOALS[0],
'red.CW1',
broadcast
);
handleGoalStateChange(
previousDetails.redNexusState.CW2,
currentDetails.redNexusState.CW2,
LedStrip.RED_SIDE_GOALS[1],
'red.CW2',
broadcast
);
handleGoalStateChange(
previousDetails.redNexusState.CW3,
currentDetails.redNexusState.CW3,
LedStrip.RED_SIDE_GOALS[2],
'red.CW3',
broadcast
);
handleGoalStateChange(
previousDetails.redNexusState.CW4,
currentDetails.redNexusState.CW4,
LedStrip.RED_SIDE_GOALS[3],
'red.CW4',
broadcast
);
handleGoalStateChange(
previousDetails.redNexusState.CW5,
currentDetails.redNexusState.CW5,
LedStrip.RED_SIDE_GOALS[4],
'red.CW5',
broadcast
);
handleGoalStateChange(
previousDetails.redNexusState.CW6,
currentDetails.redNexusState.CW6,
LedStrip.RED_SIDE_GOALS[5],
'red.CW6',
broadcast
);
handleGoalStateChange(
previousDetails.redNexusState.EC1,
currentDetails.redNexusState.EC1,
LedStrip.RED_CENTER_GOALS[0],
'red.EC1',
broadcast
);
handleGoalStateChange(
previousDetails.redNexusState.EC2,
currentDetails.redNexusState.EC2,
LedStrip.RED_CENTER_GOALS[1],
'red.EC2',
broadcast
);
handleGoalStateChange(
previousDetails.redNexusState.EC3,
currentDetails.redNexusState.EC3,
LedStrip.RED_CENTER_GOALS[2],
'red.EC3',
broadcast
);
handleGoalStateChange(
previousDetails.redNexusState.EC4,
currentDetails.redNexusState.EC4,
LedStrip.RED_CENTER_GOALS[3],
'red.EC4',
broadcast
);
handleGoalStateChange(
previousDetails.redNexusState.EC5,
currentDetails.redNexusState.EC5,
LedStrip.RED_CENTER_GOALS[4],
'red.EC5',
broadcast
);
handleGoalStateChange(
previousDetails.redNexusState.EC6,
currentDetails.redNexusState.EC6,
LedStrip.RED_CENTER_GOALS[5],
'red.EC6',
broadcast
);

handleGoalStateChange(
previousDetails.blueNexusState.CW1,
currentDetails.blueNexusState.CW1,
LedStrip.BLUE_SIDE_GOALS[0],
'blue.CW1',
broadcast
);
handleGoalStateChange(
previousDetails.blueNexusState.CW2,
currentDetails.blueNexusState.CW2,
LedStrip.BLUE_SIDE_GOALS[1],
'blue.CW2',
broadcast
);
handleGoalStateChange(
previousDetails.blueNexusState.CW3,
currentDetails.blueNexusState.CW3,
LedStrip.BLUE_SIDE_GOALS[2],
'blue.CW3',
broadcast
);
handleGoalStateChange(
previousDetails.blueNexusState.CW4,
currentDetails.blueNexusState.CW4,
LedStrip.BLUE_SIDE_GOALS[3],
'blue.CW4',
broadcast
);
handleGoalStateChange(
previousDetails.blueNexusState.CW5,
currentDetails.blueNexusState.CW5,
LedStrip.BLUE_SIDE_GOALS[4],
'blue.CW5',
broadcast
);
handleGoalStateChange(
previousDetails.blueNexusState.CW6,
currentDetails.blueNexusState.CW6,
LedStrip.BLUE_SIDE_GOALS[5],
'blue.CW6',
broadcast
);
handleGoalStateChange(
previousDetails.blueNexusState.EC1,
currentDetails.blueNexusState.EC1,
LedStrip.BLUE_CENTER_GOALS[0],
'blue.EC1',
broadcast
);
handleGoalStateChange(
previousDetails.blueNexusState.EC2,
currentDetails.blueNexusState.EC2,
LedStrip.BLUE_CENTER_GOALS[1],
'blue.EC2',
broadcast
);
handleGoalStateChange(
previousDetails.blueNexusState.EC3,
currentDetails.blueNexusState.EC3,
LedStrip.BLUE_CENTER_GOALS[2],
'blue.EC3',
broadcast
);
handleGoalStateChange(
previousDetails.blueNexusState.EC4,
currentDetails.blueNexusState.EC4,
LedStrip.BLUE_CENTER_GOALS[3],
'blue.EC4',
broadcast
);
handleGoalStateChange(
previousDetails.blueNexusState.EC5,
currentDetails.blueNexusState.EC5,
LedStrip.BLUE_CENTER_GOALS[4],
'blue.EC5',
broadcast
);
handleGoalStateChange(
previousDetails.blueNexusState.EC6,
currentDetails.blueNexusState.EC6,
LedStrip.BLUE_CENTER_GOALS[5],
'blue.EC6',
broadcast
);
};

const handleGoalStateChange = (
previousState: NexusGoalState,
currentState: NexusGoalState,
strip: LedStrip,
goal: string,
broadcast: (update: FieldControlUpdatePacket) => void
) => {
const result: FieldControlUpdatePacket = { hubs: {}, wleds: {} };

// Only set color on state change
if (previousState !== currentState) {
switch (currentState) {
case NexusGoalState.Full:
applyPatternToStrips('ffa500', [strip], result);
break;
case NexusGoalState.BlueOnly:
applyPatternToStrips('0000ff', [strip], result);
break;
case NexusGoalState.GreenOnly:
applyPatternToStrips('00ff00', [strip], result);
break;
default:
applyPatternToStrips('000000', [strip], result);
}
}

if (
currentState === NexusGoalState.Full &&
previousState !== NexusGoalState.Full
) {
// Start timer with callback
timers.set(
goal,
setTimeout(() => {
// Set pattern
const result: FieldControlUpdatePacket = { hubs: {}, wleds: {} };
applyPatternToStrips('ffffff', [strip], result);
broadcast(result);

// TODO(jan): Update match to mark food as dispensed
}, 5000) // TODO(jan): Make this time configurable
);
} else if (
currentState !== NexusGoalState.Full &&
previousState === NexusGoalState.Full
) {
// Cancel timer if there is one
clearTimeout(timers.get(goal));
}

// Broadcast update
broadcast(result);
};
6 changes: 3 additions & 3 deletions lib/models/src/fcs/Packets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function createNexusGoalSegments(
return segments;
}

function applyPatternToStrips(
export function applyPatternToStrips(
color: string,
strips: LedStrip[],
packet: FieldControlUpdatePacket
Expand Down Expand Up @@ -83,7 +83,7 @@ function applySetpointToMotors(

type WledController = 'center' | 'red' | 'blue';

class LedStrip {
export class LedStrip {
public static readonly RED_SIDE_GOALS = [
new LedStrip('red', 0),
new LedStrip('red', 1),
Expand Down Expand Up @@ -350,7 +350,7 @@ export function getFcsPackets(fieldOptions: FieldOptions): FcsPackets {
};
}

function applyStateToGoal(
export function applyStateToGoal(
state: NexusGoalState,
strip: LedStrip,
result: FieldControlUpdatePacket
Expand Down

0 comments on commit de20de1

Please sign in to comment.