Skip to content
Merged
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
2 changes: 2 additions & 0 deletions convex/_generated/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import type * as _model_matchResultLikes_queries_getMatchResultLike from "../_mo
import type * as _model_matchResultLikes_queries_getMatchResultLikesByMatchResult from "../_model/matchResultLikes/queries/getMatchResultLikesByMatchResult.js";
import type * as _model_matchResultLikes_queries_getMatchResultLikesByUser from "../_model/matchResultLikes/queries/getMatchResultLikesByUser.js";
import type * as _model_matchResults__helpers_checkMatchResultAuth from "../_model/matchResults/_helpers/checkMatchResultAuth.js";
import type * as _model_matchResults__helpers_checkMatchResultBattlePlanVisibility from "../_model/matchResults/_helpers/checkMatchResultBattlePlanVisibility.js";
import type * as _model_matchResults__helpers_deepenMatchResult from "../_model/matchResults/_helpers/deepenMatchResult.js";
import type * as _model_matchResults__helpers_getShallowMatchResult from "../_model/matchResults/_helpers/getShallowMatchResult.js";
import type * as _model_matchResults_fields from "../_model/matchResults/fields.js";
Expand Down Expand Up @@ -240,6 +241,7 @@ declare const fullApi: ApiFromModules<{
"_model/matchResultLikes/queries/getMatchResultLikesByMatchResult": typeof _model_matchResultLikes_queries_getMatchResultLikesByMatchResult;
"_model/matchResultLikes/queries/getMatchResultLikesByUser": typeof _model_matchResultLikes_queries_getMatchResultLikesByUser;
"_model/matchResults/_helpers/checkMatchResultAuth": typeof _model_matchResults__helpers_checkMatchResultAuth;
"_model/matchResults/_helpers/checkMatchResultBattlePlanVisibility": typeof _model_matchResults__helpers_checkMatchResultBattlePlanVisibility;
"_model/matchResults/_helpers/deepenMatchResult": typeof _model_matchResults__helpers_deepenMatchResult;
"_model/matchResults/_helpers/getShallowMatchResult": typeof _model_matchResults__helpers_getShallowMatchResult;
"_model/matchResults/fields": typeof _model_matchResults_fields;
Expand Down
3 changes: 2 additions & 1 deletion convex/_model/fowV4/calculateFowV4MatchResultScore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Doc } from '../../_generated/dataModel';
import { DeepMatchResult } from '../matchResults';

/**
* Calculate the Victory Points (i.e. score) for a given match result.
Expand All @@ -9,7 +10,7 @@ import { Doc } from '../../_generated/dataModel';
* @param matchResult - The match result to score
* @returns - A tuple with the scores for player 0 and 1 respectively
*/
export const calculateFowV4MatchResultScore = (matchResult: Doc<'matchResults'>): [number, number] => {
export const calculateFowV4MatchResultScore = (matchResult: Doc<'matchResults'> | DeepMatchResult): [number, number] => {

// TODO: Add some guards in case matchResult is not FowV4

Expand Down
3 changes: 2 additions & 1 deletion convex/_model/fowV4/extractFowV4MatchResultBaseStats.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Doc } from '../../_generated/dataModel';
import { DeepMatchResult } from '../matchResults';
import { calculateFowV4MatchResultScore } from './calculateFowV4MatchResultScore';
import { FowV4BaseStats } from './types';

Expand All @@ -9,7 +10,7 @@ import { FowV4BaseStats } from './types';
* @returns
*/

export const extractFowV4MatchResultBaseStats = (matchResult: Doc<'matchResults'>): [FowV4BaseStats, FowV4BaseStats] => {
export const extractFowV4MatchResultBaseStats = (matchResult: Doc<'matchResults'> | DeepMatchResult): [FowV4BaseStats, FowV4BaseStats] => {
const score = calculateFowV4MatchResultScore(matchResult);
return [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { getAuthUserId } from '@convex-dev/auth/server';

import { Doc } from '../../../_generated/dataModel';
import { QueryCtx } from '../../../_generated/server';
import { getTournamentShallow } from '../../../_model/tournaments';
import { deepenTournamentPairing } from '../../tournamentPairings';

/**
* Checks if a match result's battle plans should be visible or not.
*
* @param ctx - Convex query context
* @param matchResult - Raw match result document
* @returns True if the battle plans should be visible, false if not
*/
export const checkMatchResultBattlePlanVisibility = async (
ctx: QueryCtx,
matchResult: Doc<'matchResults'>,
): Promise<boolean> => {
const userId = await getAuthUserId(ctx);

// If the match result doesn't belong to a tournament pairing, battle plans should be visible:
if (!matchResult?.tournamentPairingId) {
return true;
}

const tournamentPairing = await ctx.db.get(matchResult.tournamentPairingId);

// If the match result's pairing has gone missing, treat it the same as a single match:
if (!tournamentPairing) {
return true;
}
const deepTournamentPairing = await deepenTournamentPairing(ctx, tournamentPairing);
const tournament = await getTournamentShallow(ctx, deepTournamentPairing.tournamentId);

// If the match result is not from an on-going tournament, battle plans should be visible:
if (tournament?.status !== 'active') {
return true;
}

if (userId) {

// If the requesting user is an organizer, battle plans should be visible:
if (tournament.organizerUserIds.includes(userId)) {
return true;
}

// If the requesting user is a player within that pairing, battle plans should be visible:
if (deepTournamentPairing.playerUserIds.includes(userId)) {
return true;
}
}

// Hide battle plans in all other cases:
return false;
};
11 changes: 10 additions & 1 deletion convex/_model/matchResults/_helpers/deepenMatchResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Doc } from '../../../_generated/dataModel';
import { QueryCtx } from '../../../_generated/server';
import { getMission } from '../../fowV4/getMission';
import { getUser } from '../../users/queries/getUser';
import { checkMatchResultBattlePlanVisibility } from './checkMatchResultBattlePlanVisibility';

/* eslint-disable @typescript-eslint/explicit-function-return-type */
/**
Expand All @@ -24,19 +25,27 @@ export const deepenMatchResult = async (
const player1User = matchResult?.player1UserId ? await getUser(ctx, {
id: matchResult.player1UserId,
}) : null;
const mission = getMission(matchResult.details.missionId);

// Social
const comments = await ctx.db.query('matchResultComments')
.withIndex('by_match_result_id',((q) => q.eq('matchResultId', matchResult._id)))
.collect();
const likes = await ctx.db.query('matchResultLikes')
.withIndex('by_match_result_id',((q) => q.eq('matchResultId', matchResult._id)))
.collect();

// Details
const mission = getMission(matchResult.details.missionId);
const battlePlansVisible = await checkMatchResultBattlePlanVisibility(ctx, matchResult);

return {
...matchResult,
...(player0User ? { player0User } : {}),
...(player1User ? { player1User } : {}),
details: {
...matchResult.details,
player0BattlePlan: battlePlansVisible ? matchResult.details.player0BattlePlan : undefined,
player1BattlePlan: battlePlansVisible ? matchResult.details.player1BattlePlan : undefined,
missionName: mission?.displayName,
},
likedByUserIds: likes.map((like) => like.userId),
Expand Down
2 changes: 1 addition & 1 deletion convex/_model/matchResults/queries/getMatchResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { deepenMatchResult, DeepMatchResult } from '../_helpers/deepenMatchResul
export const getMatchResults = async (
ctx: QueryCtx,
): Promise<DeepMatchResult[]> => {
const matchResults = await ctx.db.query('matchResults').collect();
const matchResults = await ctx.db.query('matchResults').order('desc').collect();
return await Promise.all(matchResults.map(
async (item) => await deepenMatchResult(ctx, item),
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const getMatchResultsByTournament = async (
): Promise<DeepMatchResult[]> => {
const matchResults = await ctx.db.query('matchResults')
.withIndex('by_tournament_id', (q) => q.eq('tournamentId', args.tournamentId))
.order('desc')
.collect();
return await Promise.all(matchResults.map(
async (item) => await deepenMatchResult(ctx, item),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const getMatchResultsByTournamentPairing = async (
): Promise<DeepMatchResult[]> => {
const matchResults = await ctx.db.query('matchResults')
.withIndex('by_tournament_pairing_id', (q) => q.eq('tournamentPairingId', args.tournamentPairingId))
.order('desc')
.collect();
return await Promise.all(matchResults.map(
async (item) => await deepenMatchResult(ctx, item),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const getMatchResultsByTournamentRound = async (
.collect();
const matchResults = await ctx.db.query('matchResults')
.withIndex('by_tournament_id', (q) => q.eq('tournamentId', args.tournamentId))
.order('desc')
.collect();
const filteredMatchResults = matchResults.filter((result) => (
!!tournamentPairings.find((item) => item._id === result.tournamentPairingId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ export const formatOutcome = (
}
if (details.winner !== -1 && details.outcomeType === 'force_broken') {
if (details.winner === 0) {
return `${playerNames[0]} broke ${playerNames[1]}\u{2019}s formation(s).`;
return `${playerNames[0]} broke ${playerNames[1]}\u{2019}s formation.`;
}
if (details.winner === 1) {
return `${playerNames[1]} broke ${playerNames[0]}\u{2019}s formation(s).`;
return `${playerNames[1]} broke ${playerNames[0]}\u{2019}s formation.`;
}
}
return 'Draw / Time Out';
Expand Down

This file was deleted.

69 changes: 0 additions & 69 deletions src/pages/MatchResultDetailPage/components/MatchResultDetails.tsx

This file was deleted.

This file was deleted.