Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manual comp creation #184

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions components/stats/TeamPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,13 @@ function TeamCard(props: {

export default function TeamPage(props: { reports: Report[], pitReports: Pitreport[], subjectiveReports: SubjectiveReport[] }) {
const reports = props.reports;
const pitReports: { [key: number]: Pitreport } = {};
const [pitReports, setPitReports] = useState<{ [key: number]: Pitreport }>({});
const [teamReports, setTeamReports] = useState<{ [key: number]: Report[] }>(
{}
);
const [teamSubjectiveReports, setTeamSubjectiveReports] = useState<{ [key: number]: SubjectiveReport[] }>({});

const teamNumbers = Object.keys(teamReports);
const teamNumbers = Array.from(new Set([...Object.keys(teamReports), ...Object.keys(pitReports), ...Object.keys(teamSubjectiveReports)]));

const [selectedTeam, setSelectedTeam] = useState<number>();
const selectedReports = teamReports[selectedTeam ? selectedTeam : 0];
Expand All @@ -187,6 +187,24 @@ export default function TeamPage(props: { reports: Report[], pitReports: Pitrepo
}
});
setTeamReports(newTeamReports);

const newPitReports: typeof pitReports = {};
props.pitReports.forEach((pitReport) => {
newPitReports[pitReport.teamNumber] = pitReport;
});
setPitReports(newPitReports);

const subjectiveReports: typeof teamSubjectiveReports = {};
props.subjectiveReports.forEach((subjectiveReport) => {
for (const teamNumber of Object.keys(subjectiveReport.robotComments)) {
if (!Object.keys(subjectiveReports).includes(teamNumber)) {
subjectiveReports[Number(teamNumber)] = [subjectiveReport];
} else {
subjectiveReports[Number(teamNumber)].push(subjectiveReport);
}
}
});
setTeamSubjectiveReports(subjectiveReports);
};

useEffect(() => {
Expand All @@ -208,8 +226,9 @@ export default function TeamPage(props: { reports: Report[], pitReports: Pitrepo
const stDev = StandardDeviation(pointTotals);

useEffect(() => {
associateTeams();
}, [reports]);
console.log("Associating teams...");
associateTeams();
}, [reports, props.pitReports, props.subjectiveReports]);

// Associate pit reports
props.pitReports.forEach((pitReport) => {
Expand All @@ -219,24 +238,22 @@ export default function TeamPage(props: { reports: Report[], pitReports: Pitrepo
const teamRanking = Object.keys(teamReports).sort((a, b) => {
const a1 = AveragePoints(teamReports[Number(a)]);
const b1 = AveragePoints(teamReports[Number(b)]);
if (a1 < b1) {
return 1;
} else if (a1 > b1) {
return -1;
}
return 0;
return b1 - a1;
});

// Find teams not in team ranking
const missingTeams = teamNumbers.filter((team) => !teamRanking.includes(team));

return (
<div className="w-full h-min flex flex-row space-x-4">
<div className="w-1/5 h-[50rem] flex flex-col space-y-4 overflow-y-scroll">
{teamRanking.map((number, index) => (
{teamRanking.concat(missingTeams).map((number, index) => (
<TeamCard
key={number}
number={Number(number)}
selected={selectedTeam === Number(number)}
reports={teamReports[Number(number)]}
pitReport={pitReports[Number(number)]}
reports={teamReports[Number(number)] ?? []}
pitReport={pitReports[Number(number)] ?? []}
rank={index + 1}
onClick={() => setSelectedTeam(Number(number))}
compAvgPoints={avgPoints}
Expand Down
2 changes: 1 addition & 1 deletion components/stats/TeamStats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export default function TeamStats(props: {
}
}

const commentList = props.selectedReports.filter((report) => report.data.comments.length > 0);
const commentList = props.selectedReports?.filter((report) => report.data.comments.length > 0) ?? [];
if (commentList.length === 0) return setComments([]);

const promises = commentList.map((report) => api.findMatchById(report.match).then((match) => addComment(
Expand Down
40 changes: 34 additions & 6 deletions lib/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ export namespace API {
// time
// type
// }
var match = await db.addObject<Match>(
const match = await db.addObject<Match>(
Collections.Matches,
new Match(
data.number,
Expand All @@ -509,16 +509,20 @@ export namespace API {
data.blueAlliance
)
);
var comp = await db.findObjectById<Competition>(

const reportPromise = generateReportsForMatch(match);

const comp = await db.findObjectById<Competition>(
Collections.Competitions,
new ObjectId(data.compId)
);
comp.matches.push(match._id ? String(match._id) : "");
await db.updateObjectById(

await Promise.all([db.updateObjectById(
Collections.Competitions,
new ObjectId(comp._id),
comp
);
), reportPromise]);

return res.status(200).send(match);
},
Expand Down Expand Up @@ -800,7 +804,12 @@ export namespace API {
},

teamCompRanking: async (req, res, { tba, data }) => {
const { rankings } = await tba.req.getCompetitonRanking(data.tbaId);
const tbaResult = await tba.req.getCompetitonRanking(data.tbaId);
if (!tbaResult || !tbaResult.rankings) {
return res.status(200).send({ place: "?", max: "?" });
}

const { rankings } = tbaResult;

const rank = rankings.find((ranking) => ranking.team_key === `frc${data.team}`)?.rank;

Expand Down Expand Up @@ -978,7 +987,26 @@ export namespace API {
subjectiveScouter: userId,
});
return res.status(200).send({ result: "success" });
}
},

createPitReportForTeam: async (req, res, { db, data }) => {
const { teamNumber, compId } = data;

const compPromise = db.findObjectById<Competition>(Collections.Competitions, new ObjectId(compId));

const pitReport = new Pitreport(teamNumber);
const pitReportId = (await db.addObject<Pitreport>(Collections.Pitreports, pitReport))._id?.toString();

if (!pitReportId)
return res.status(500).send({ error: "Failed to create pit report" });

(await compPromise).pitReports.push(pitReportId);

await db.updateObjectById<Competition>(Collections.Competitions, new ObjectId(compId), {
pitReports: (await compPromise).pitReports,
});

return res.status(200).send({ result: "success" });
}
};
}
9 changes: 8 additions & 1 deletion lib/TheBlueAlliance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Alliance,
Pitreport,
} from "./Types";
import { NotLinkedToTba } from "./client/ClientUtils";

export namespace TheBlueAlliance {
export interface SimpleTeam {
Expand Down Expand Up @@ -255,6 +256,9 @@ export namespace TheBlueAlliance {
}

async getCompetitionMatches(tbaId: string): Promise<Match[]> {
if (tbaId === NotLinkedToTba)
return [];

let matches = (await this.req.getCompetitionMatches(tbaId)).map(
(data) =>
new Match(
Expand All @@ -266,11 +270,14 @@ export namespace TheBlueAlliance {
tbaIdsToTeamNumbers(data.alliances.blue.team_keys),
tbaIdsToTeamNumbers(data.alliances.red.team_keys)
)
);
) ?? [];
return matches;
}

async getCompetitionPitreports(tbaId: string): Promise<Pitreport[]> {
if (tbaId === NotLinkedToTba)
return [];

const competitionTeams = await this.req.getCompetitionTeams(tbaId);
return competitionTeams.map(
({ team_number }) => new Pitreport(team_number)
Expand Down
5 changes: 5 additions & 0 deletions lib/client/ClientAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export default class ClientAPI {
},
body: JSON.stringify(body),
});

return await rawResponse.json();
}

Expand Down Expand Up @@ -435,4 +436,8 @@ export default class ClientAPI {
return await this.request("/setSubjectiveScouterForMatch", { matchId, userId });
}

async createPitReportForTeam(teamNumber: number, compId: string) {
return await this.request("/createPitReportForTeam", { teamNumber, compId });
}

}
4 changes: 3 additions & 1 deletion lib/client/ClientUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ export function getIdsInProgressFromTimestamps(timestamps: { [id: string]: strin
const timestamp = timestamps[id];
return ((now - new Date(timestamp).getTime()) / 1000) < 10;
});
}
}

export const NotLinkedToTba = "not-linked";
Loading
Loading