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

Implement Win Loss #808

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Implement Win Loss #808

wants to merge 4 commits into from

Conversation

lowtorola
Copy link
Contributor

Implements the querying of a team's win-loss-tie record and adds the tile to the home screen, account page, and scrimmaging page! Resolves #781 🚀

Tile:
Screenshot 2024-05-12 at 11 57 58 PM

Screenshot 2024-05-12 at 11 58 09 PM

@lowtorola lowtorola requested a review from acrantel May 13, 2024 03:58
@acrantel
Copy link
Member

we should release after this pr is merged

working win loss tie
Comment on lines +28 to +44
const scrimmagingRecordAll = useScrimmagingRecord({
episodeId,
teamId: team.id,
scrimmageType: CompeteMatchScrimmagingRecordRetrieveScrimmageTypeEnum.All,
});
const scrimmagingRecordUnranked = useScrimmagingRecord({
episodeId,
teamId: team.id,
scrimmageType:
CompeteMatchScrimmagingRecordRetrieveScrimmageTypeEnum.Unranked,
});
const scrimmagingRecordRanked = useScrimmagingRecord({
episodeId,
teamId: team.id,
scrimmageType:
CompeteMatchScrimmagingRecordRetrieveScrimmageTypeEnum.Ranked,
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are bits of repeated code that could be pushed into the WinLossTie component by having that component take the scrimmage type.

Comment on lines +67 to +95
{!hideAllScrimmages && (
<WinLossTie
scrimmageType={
CompeteMatchScrimmagingRecordRetrieveScrimmageTypeEnum.All
}
loading={scrimmagingRecordAll.isFetching}
wins={scrimmagingRecordAll.data?.wins ?? 0}
losses={scrimmagingRecordAll.data?.losses ?? 0}
ties={scrimmagingRecordAll.data?.ties ?? 0}
/>
)}
{!hideUnranked && (
<WinLossTie
scrimmageType={
CompeteMatchScrimmagingRecordRetrieveScrimmageTypeEnum.Unranked
}
loading={scrimmagingRecordUnranked.isFetching}
wins={scrimmagingRecordUnranked.data?.wins ?? 0}
losses={scrimmagingRecordUnranked.data?.losses ?? 0}
ties={scrimmagingRecordUnranked.data?.ties ?? 0}
/>
)}
{!hideRanked && (
<WinLossTie
scrimmageType={
CompeteMatchScrimmagingRecordRetrieveScrimmageTypeEnum.Ranked
}
loading={scrimmagingRecordRanked.isFetching}
wins={scrimmagingRecordRanked.data?.wins ?? 0}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can reduce the repetitiveness of this code by moving the useScrimmagingRecord call into the WinLossTie component

Comment on lines +31 to +61
const userTeamRatingData: Record<string, ChartData[]> | undefined =
useMemo(() => {
if (!userTeamRatingHistory.isSuccess) return undefined;
const ratingRecord: Record<string, ChartData[]> = {};
return userTeamRatingHistory.data.reduce((record, teamData) => {
if (teamData.team_rating !== undefined) {
record[teamData.team_rating.team.name] =
teamData.team_rating.rating_history.map((match) => [
match.timestamp.getTime(),
match.rating,
]);
}
return record;
}, ratingRecord);
}, [userTeamRatingHistory]);

const topTeamRatingData: Record<string, ChartData[]> | undefined =
useMemo(() => {
if (!topRatingHistory.isSuccess) return undefined;
const ratingRecord: Record<string, ChartData[]> = {};
return topRatingHistory.data.reduce((record, teamData) => {
if (teamData.team_rating !== undefined) {
record[teamData.team_rating.team.name] =
teamData.team_rating.rating_history.map((match) => [
match.timestamp.getTime(),
match.rating,
]);
}
return record;
}, ratingRecord);
}, [topRatingHistory]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets see if we can improve the repetitiveness of this code. We can probably drop the stuff inside the useMemo into a function that takes a rating history

Comment on lines +32 to +58
queryKey: buildKey(scrimmagingRecordFactory.queryKey, {
episodeId,
teamId: teamDataCached.id,
scrimmageType:
CompeteMatchScrimmagingRecordRetrieveScrimmageTypeEnum.Ranked,
}),
});

void queryClient.ensureQueryData({
queryKey: buildKey(scrimmagingRecordFactory.queryKey, {
episodeId,
teamId: teamDataCached.id,
scrimmageType:
CompeteMatchScrimmagingRecordRetrieveScrimmageTypeEnum.Unranked,
}),
});

void queryClient.ensureQueryData({
queryKey: buildKey(scrimmagingRecordFactory.queryKey, {
episodeId,
teamId: teamDataCached.id,
scrimmageType:
CompeteMatchScrimmagingRecordRetrieveScrimmageTypeEnum.All,
}),
});
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can reduce the repetitiveness here too? the main thing that's different is the scrimmage type

@@ -1,3 +1,3 @@
REACT_APP_THIS_URL=http://localhost:3000
REACT_APP_BACKEND_URL=http://api.staging.battlecode.org/
REACT_APP_BACKEND_URL=http://localhost:8000
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i accidentally changed this to staging in a recent pr, it's good that we're changing it back to localhost here

Comment on lines +489 to +516
queryset = self.get_queryset().filter(tournament_round__isnull=True)

scrimmage_type = self.request.query_params.get("scrimmage_type")
if scrimmage_type is not None:
if scrimmage_type == "ranked":
queryset = queryset.filter(is_ranked=True)
elif scrimmage_type == "unranked":
queryset = queryset.filter(is_ranked=False)

team_id = parse_int(self.request.query_params.get("team_id"))
if team_id is None and request.user.pk is not None:
team_id = (
Team.objects.filter(members__pk=request.user.pk)
.filter(episode_id=episode_id)
.first()
.id
)
if team_id is None:
return Response(status=status.HTTP_400_BAD_REQUEST)
if team_id is not None:
queryset = queryset.filter(participants__team=team_id)
else:
return Response(status=status.HTTP_400_BAD_REQUEST)
has_invisible = self.get_queryset().filter(
participants__team__status=TeamStatus.INVISIBLE
)
queryset = queryset.exclude(pk__in=Subquery(has_invisible.values("pk")))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see if we can improve this whole function's performance using the code here
#558

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Win/Loss Record Home Tile
2 participants