Skip to content

Commit

Permalink
attempt to fix reassign target button
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler-Lentz committed Jun 24, 2024
1 parent 55c9ade commit f007c92
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 18 deletions.
60 changes: 49 additions & 11 deletions houston/src/pages/Report.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import L from 'leaflet';
import Button from '@mui/material-next/Button';
import { red, blue, green, yellow, purple, grey } from '@mui/material/colors';
import { post_targets, pull_targets } from "../utilities/pull_targets.ts";
import { MatchedTarget, IdentifiedTarget, Bottle, ODLCColor, ODLCShape, GPSCoord, oDLCShapeToJSON, oDLCColorToJSON, BottleDropIndex, bottleDropIndexToJSON } from '../protos/obc.pb';
import { MatchedTarget, IdentifiedTarget, Bottle, ODLCColor, ODLCShape, GPSCoord, oDLCShapeToJSON, oDLCColorToJSON, BottleDropIndex } from '../protos/obc.pb';

type UpdateItemFunction = () => void;

Expand Down Expand Up @@ -41,26 +41,64 @@ function Image({item, matchedItems, updateMatched}: ImageProps) {
if (value !== null) {
bottleIndex = value;
}

console.log('start bottleIndex', bottleIndex);

const tempMatched = matchedItems;

console.log('start tempMatched', tempMatched)

const removeItemIndex = matchedItems.findIndex((itemX) => itemX.Target?.id === item.id);

const updateTargetIndex = matchedItems.findIndex((itemX) => itemX.Bottle?.Index ? bottleDropIndexToJSON(itemX.Bottle.Index) === bottleIndex : null);
console.log('start removeItemIndex', removeItemIndex)

const updateTargetIndex = matchedItems.findIndex((itemX) => {
if (itemX.Bottle === undefined) {
return false;
}

console.log("itemX", itemX.Bottle.Index);

if (updateTargetIndex != removeItemIndex) {
console.log("updateTargetIndex", updateTargetIndex);
console.log("tempMatched2", tempMatched);
const temp = tempMatched[updateTargetIndex].Target;
tempMatched[updateTargetIndex].Target = item;
// hack because for some reason the indices are being sent as letters as they are in the enum
// instead of the 1-5 values

// console.log("tempMatched1", tempMatched[removeItemIndex].Target);
console.log("tempMatched", tempMatched[updateTargetIndex]);
if (removeItemIndex !== -1){
tempMatched[removeItemIndex].Target = temp;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return ((itemX.Bottle.Index as any) == bottleIndex);
});

console.log('start updateTargetIndex', updateTargetIndex);

// this mess is so bad but at every point it made sense to add a tiny little hack
// so it would just work
if (updateTargetIndex == -1) {
if (bottleIndex >= 'A' && bottleIndex <= 'F') {
const target = {
"Bottle": {
"Index": bottleIndex
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any; // love typescript
tempMatched.push(target as MatchedTarget);
} else {
alert("cannot find a bottle with that index");
return;
}
} else {
if (updateTargetIndex != removeItemIndex) {
console.log("updateTargetIndex", updateTargetIndex);
console.log("tempMatched2", tempMatched);
const temp = tempMatched[updateTargetIndex].Target;
tempMatched[updateTargetIndex].Target = item;

// console.log("tempMatched1", tempMatched[removeItemIndex].Target);
console.log("tempMatched", tempMatched[updateTargetIndex]);
if (removeItemIndex !== -1){
tempMatched[removeItemIndex].Target = temp;
}
}
}


const res = await post_targets(tempMatched);

if (res) {
Expand Down
26 changes: 19 additions & 7 deletions houston/src/utilities/pull_targets.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BottleDropIndex, IdentifiedTarget, ManualTargetMatch, MatchedTarget } from '../protos/obc.pb';
import { IdentifiedTarget, ManualTargetMatch, MatchedTarget } from '../protos/obc.pb';

/**
*
Expand Down Expand Up @@ -54,27 +54,39 @@ export async function post_targets(targets: MatchedTarget[]) {
};

targets.forEach((target) => {
switch (target.Bottle?.Index) {
case BottleDropIndex.A:
// eslint-disable-next-line @typescript-eslint/no-explicit-any
switch (target.Bottle?.Index as any) {
// hack because these are strings instead of num values for some reason
case "A":
matchings.bottleAId = target.Target?.id || -1;
break;
case BottleDropIndex.B:
case "B":
matchings.bottleBId = target.Target?.id || -1;
break;
case BottleDropIndex.C:
case "C":
matchings.bottleCId = target.Target?.id || -1;
break;
case BottleDropIndex.D:
case "D":
matchings.bottleDId = target.Target?.id || -1;
break;
case BottleDropIndex.E:
case "E":
matchings.bottleEId = target.Target?.id || -1;
break;
default:
break;
}
});

// -1 value gets set to 0 because 0 is the null value, but 0 is a real value we want to
// send, so we artificially increment by 1.
// that way when the OBC sees 1 it knows it is actually referring to target 0, and if it
// sees 0 then it knows that it was referring to a target you aren't rematching
matchings.bottleAId++;
matchings.bottleBId++;
matchings.bottleCId++;
matchings.bottleDId++;
matchings.bottleEId++;

const data = await fetch('/api/targets/matched', {
method: 'POST',
headers: {
Expand Down

0 comments on commit f007c92

Please sign in to comment.