Skip to content

Commit

Permalink
Add user to many tournaments in a tournament (#2001)
Browse files Browse the repository at this point in the history
* Initial

* Load as objects

* fix types

* Progress

* Summarizer

* Show alert about readding player

* Tests
  • Loading branch information
Sendouc authored Dec 28, 2024
1 parent ca972ec commit 9678112
Show file tree
Hide file tree
Showing 14 changed files with 456 additions and 249 deletions.
28 changes: 21 additions & 7 deletions app/features/tournament-bracket/components/MatchActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,26 @@ export function MatchActions({
>(() => {
if (result) {
return [
result.participantIds.filter((id) =>
teams[0].members.some((member) => member.userId === id),
),
result.participantIds.filter((id) =>
teams[1].members.some((member) => member.userId === id),
),
result.participants
.filter((participant) =>
teams[0].members.some(
(member) =>
member.userId === participant.userId &&
(!participant.tournamentTeamId ||
teams[0].id === participant.tournamentTeamId),
),
)
.map((p) => p.userId),
result.participants
.filter((participant) =>
teams[1].members.some(
(member) =>
member.userId === participant.userId &&
(!participant.tournamentTeamId ||
teams[1].id === participant.tournamentTeamId),
),
)
.map((p) => p.userId),
];
}

Expand Down Expand Up @@ -340,7 +354,7 @@ function EditScoreForm({
return (
<fetcher.Form
method="post"
className="stack horizontal md justify-center"
className="stack horizontal md justify-center mt-6"
>
<input type="hidden" name="resultId" value={resultId} />
<input
Expand Down
4 changes: 3 additions & 1 deletion app/features/tournament-bracket/components/StartedMatch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,9 @@ function StartedMatchTabs({
teams[1],
tournament.minMembersPerTeam,
),
result?.participantIds,
result?.participants
.map((p) => `${p.userId}-${p.tournamentTeamId}`)
.join(","),
result?.opponentOnePoints,
result?.opponentTwoPoints,
].join("-");
Expand Down
16 changes: 11 additions & 5 deletions app/features/tournament-bracket/components/TeamRosterInputs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,14 @@ function _TeamRoster({
);

const checkedInputPlayerIds = () => {
if (result?.participantIds && !revising) {
return result.participantIds;
if (result?.participants && !revising) {
return result.participants
.filter(
(participant) =>
!participant.tournamentTeamId ||
participant.tournamentTeamId === team.id,
)
.map((participant) => participant.userId);
}
if (editingRoster) return checkedPlayers.split(",").map(Number);

Expand Down Expand Up @@ -216,12 +222,12 @@ function _TeamRoster({
teamId={team.id}
checkedPlayers={checkedInputPlayerIds()}
presentational={!revising && (presentational || !editingRoster)}
handlePlayerClick={(playerId: number) => {
handlePlayerClick={(playerId) => {
if (!setCheckedPlayers) return;

setCheckedPlayers((oldPlayers) => {
const newPlayers = clone(oldPlayers);
if (oldPlayers.flat().includes(playerId)) {
if (oldPlayers[idx].includes(playerId)) {
newPlayers[idx] = newPlayers[idx].filter((id) => id !== playerId);
} else {
newPlayers[idx].push(playerId);
Expand Down Expand Up @@ -452,7 +458,7 @@ function TeamRosterInputsCheckboxes({
name="playerName"
disabled={mode() === "DISABLED" || mode() === "PRESENTATIONAL"}
value={member.id}
checked={checkedPlayers.flat().includes(member.id)}
checked={checkedPlayers.includes(member.id)}
onChange={() => handlePlayerClick(member.id)}
data-testid={`player-checkbox-${i}`}
/>{" "}
Expand Down
15 changes: 14 additions & 1 deletion app/features/tournament-bracket/core/Tournament.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1041,9 +1041,22 @@ export class Tournament {
teamMemberOfByUser(user: OptionalIdObject) {
if (!user) return null;

return this.ctx.teams.find((team) =>
const teams = this.ctx.teams.filter((team) =>
team.members.some((member) => member.userId === user.id),
);

let result: (typeof teams)[number] | null = null;
let latestCreatedAt = 0;
for (const team of teams) {
const member = team.members.find((member) => member.userId === user.id)!;

if (member.createdAt > latestCreatedAt) {
result = team;
latestCreatedAt = member.createdAt;
}
}

return result;
}

teamMemberOfProgressStatus(user: OptionalIdObject) {
Expand Down
Loading

0 comments on commit 9678112

Please sign in to comment.