Skip to content

Commit

Permalink
add alliance sync and fix AD spelling
Browse files Browse the repository at this point in the history
  • Loading branch information
Techno11 committed Sep 29, 2024
1 parent 24b075b commit 0e34239
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 4 deletions.
39 changes: 39 additions & 0 deletions back-end/api/src/controllers/Results.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
AllianceMember,
MatchKey,
reconcileMatchDetails,
reconcileMatchParticipants,
Expand Down Expand Up @@ -140,6 +141,22 @@ export const postMatchResults = async (
);
};

export const postAlliances = async (
alliances: AllianceMember[],
platform: SyncPlatform,
apiKey: string
) => {
return await request(
'/upload/alliances',
{
method: 'POST',
body: JSON.stringify(alliances)
},
platform,
apiKey
);
};

router.post(
'/sync/rankings/:eventKey/:tournamentKey',
async (req: Request, res: Response, next: NextFunction) => {
Expand Down Expand Up @@ -250,4 +267,26 @@ router.post(
}
);

router.post(
'/sync/aliances/:eventKey/:tournamentKey',
async (req: Request, res: Response, next: NextFunction) => {
logger.info(
environment.isProd()
? 'attempting to sync results'
: 'not syncing results'
);
if (!environment.isProd()) return res.send({ succuess: false });
const { eventKey, tournamentKey } = req.params;
const { platform, apiKey } = req.body;
const db = await getDB(eventKey);
const alliances = await db.selectAllWhere('alliance', `eventKey = "${eventKey}" AND tournamentKey = "${tournamentKey}"`);
const allianceReq = await postAlliances(
alliances,
platform,
apiKey
);
res.send({ succuess: allianceReq?.ok });
}
);

export default router;
15 changes: 15 additions & 0 deletions front-end/src/api/use-results-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ export const resultsSyncRankings = (
}
);

export const resultsSyncAlliances = (
eventKey: string,
tournamentKey: string,
platform: SyncPlatform,
apiKey: string
): Promise<SyncResponse> =>
clientFetcher<SyncResponse>(
`results/sync/alliances/${eventKey}/${tournamentKey}`,
'POST',
{
platform,
apiKey
}
);

export const resultsSyncTeams = (
eventKey: string,
platform: SyncPlatform,
Expand Down
9 changes: 9 additions & 0 deletions front-end/src/apps/admin/admin-app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
recalculatePlayoffsRankings
} from 'src/api/use-ranking-data';
import {
resultsSyncAlliances,
resultsSyncMatches,
resultsSyncRankings
} from 'src/api/use-results-sync';
Expand Down Expand Up @@ -50,6 +51,11 @@ export const AdminApp: FC = () => {
await resultsSyncRankings(eventKey, tournamentKey, platform, apiKey);
};

const syncAlliances = async (): Promise<void> => {
if (!tournamentKey) return;
await resultsSyncAlliances(eventKey, tournamentKey, platform, apiKey);
};

const handlePurge = async (): Promise<void> => {
try {
await purgeAll();
Expand Down Expand Up @@ -110,6 +116,9 @@ export const AdminApp: FC = () => {
<Button variant='contained' color='error' onClick={syncRankings}>
Sync Rankings
</Button>
<Button variant='contained' color='error' onClick={syncAlliances}>
Sync Alliances
</Button>
<Button
variant='contained'
color='error'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const Breakdown: ResultsBreakdown<MatchDetails>[] = [
<img src={Resevoir} />
</CustomIcon>
),
title: 'Resevoir Points',
title: 'Reservoir Points',
color: '#000000',
resultCalc: (match, alliance) => {
if (!match.details) return '0';
Expand Down
22 changes: 19 additions & 3 deletions front-end/src/apps/scorekeeper/hooks/use-post-results.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MatchState } from '@toa-lib/models';
import { MatchState, QUALIFICATION_LEVEL } from '@toa-lib/models';
import { useRecoilCallback, useRecoilValue } from 'recoil';
import { useMatchControl } from './use-match-control';
import { sendPostResults } from 'src/api/use-socket';
Expand All @@ -13,16 +13,19 @@ import { useSeasonFieldControl } from 'src/hooks/use-season-components';
import { useMatchesForTournament } from 'src/api/use-match-data';
import { useNextUnplayedMatch } from './use-next-unplayed-match';
import {
resultsSyncAlliances,
resultsSyncMatch,
resultsSyncRankings
} from 'src/api/use-results-sync';
import { useSyncConfig } from 'src/hooks/use-sync-config';
import { useCurrentTournament } from 'src/api/use-tournament-data';

export const usePostResultsCallback = () => {
const { canPostResults, setState } = useMatchControl();
const fieldControl = useSeasonFieldControl();
const eventKey = useRecoilValue(currentEventKeyAtom);
const tournamentKey = useRecoilValue(currentTournamentKeyAtom);
const tournament = useCurrentTournament();
const { data: matches, mutate: setMatches } = useMatchesForTournament(
eventKey,
tournamentKey
Expand Down Expand Up @@ -61,8 +64,21 @@ export const usePostResultsCallback = () => {
apiKey
);

let successAlliances = true;

if (tournament && tournament.tournamentLevel > QUALIFICATION_LEVEL) {
successAlliances = (
await resultsSyncAlliances(
match.eventKey,
match.tournamentKey,
platform,
apiKey
)
).success;
}

// Update local match array with posted = 1
if (successMatch && successRankings && matches) {
if (successMatch && successRankings && matches && successAlliances) {
const copy = [...matches];
const index = copy.findIndex(
(m) => m.id === match.id && m.tournamentKey === match.tournamentKey
Expand All @@ -84,6 +100,6 @@ export const usePostResultsCallback = () => {
sendPostResults();
setState(MatchState.RESULTS_POSTED);
},
[canPostResults, setState, matches, eventKey, tournamentKey]
[canPostResults, setState, matches, eventKey, tournamentKey, tournament]
);
};

0 comments on commit 0e34239

Please sign in to comment.