Skip to content

Commit

Permalink
added a button to fill missing qualifier scores with 0s
Browse files Browse the repository at this point in the history
  • Loading branch information
Chupalika committed Aug 29, 2024
1 parent 796ace3 commit 6639568
Showing 1 changed file with 57 additions and 3 deletions.
60 changes: 57 additions & 3 deletions client/src/components/pages/Stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default function Stats({ tourney, user }) {
refetchDataInProgress: false,
refetchScoresInProgress: false,
assignSeedsInProgress: false,
fillMissingScoresInProgress: false,
freemodFilter: "none",
});

Expand Down Expand Up @@ -64,15 +65,15 @@ export default function Stats({ tourney, user }) {
const processedTeamScores = [];

let currentRank = 1;
let currentScore;
let currentScore = undefined;
let currentFilteredRank = 0;
let ties = 0;
let currentFilteredScore;
for (let i = 0; i < sortedPlayerScores.length; i++) {
const playerScore = sortedPlayerScores[i];
const filterActive = (mod === "FM" || mod === "TB") && state.freemodFilter !== "none";
const matchesFreemodFilter = state.freemodFilter === playerScore.mod;
if (!currentScore || playerScore.score < currentScore) {
if (currentScore === undefined || playerScore.score < currentScore) {
currentRank = i + 1;
currentScore = playerScore.score;
}
Expand Down Expand Up @@ -117,7 +118,7 @@ export default function Stats({ tourney, user }) {
currentScore = undefined;
for (let i = 0; i < sortedTeamScores.length; i++) {
const teamScore = sortedTeamScores[i];
if (!currentScore || teamScore.score < currentScore) {
if (currentScore === undefined || teamScore.score < currentScore) {
currentRank = i + 1;
currentScore = teamScore.score;
}
Expand Down Expand Up @@ -638,6 +639,53 @@ export default function Stats({ tourney, user }) {
}
};

const fillMissingScores = async () => {
const stageStatsCopy = JSON.parse(JSON.stringify(state.stageStats));
for (let stageMap of state.stageMaps) {
let mapStats = stageStatsCopy.maps.find((mapStats) => mapStats.mapId === stageMap.mapId);
if (mapStats === undefined) {
mapStats = {
mapId: stageMap.mapId,
playerScores: [],
teamScores: [],
};
stageStatsCopy.maps.push(mapStats);
}

const isTeamsTourney = !!state.tourneyModel?.teams;
if (isTeamsTourney) {
const existingTeamNames = mapStats.teamScores.map(teamScore => teamScore.teamName);
for (let teamName of state.teams.keys()) {
if (!existingTeamNames.includes(teamName)) {
mapStats.teamScores.push({ teamName, score: 0 });
}
}
} else {
const existingPlayerIds = mapStats.playerScores.map(playerScore => playerScore.userId);
for (let playerId of state.players.keys()) {
if (!existingPlayerIds.includes(Number(playerId))) {
mapStats.playerScores.push({ userId: Number(playerId), score: 0 });
}
}
}
}
const updatedStats = await post("/api/stage-stats", { tourney, stats: stageStatsCopy });
setState({
...state,
stageStats: updatedStats,
recalculateStats: true,
});
}

const confirmFillMissingScores = async () => {
const isTeamsTourney = !!state.tourneyModel?.teams;
if (isTeamsTourney) {
if (confirm("Fill missing scores from all registered teams on all maps in this stage with 0s?")) fillMissingScores();
} else {
if (confirm("Fill missing scores from all registered players on all maps in this stage with 0s?")) fillMissingScores();
}
};

const handleFreemodFilterChange = (event) => {
const value = event.target.value;
setState({
Expand Down Expand Up @@ -693,6 +741,12 @@ export default function Stats({ tourney, user }) {
</Button>
)}
{state.assignSeedsInProgress && <Spin />}
{!state.fillMissingScoresInProgress && state.isQualifiers && (
<Button className="settings-button" type="primary" onClick={confirmFillMissingScores}>
Fill Missing Scores
</Button>
)}
{state.fillMissingScoresInProgress && <Spin />}
</Form>
)}
{state.inEditMode && (
Expand Down

0 comments on commit 6639568

Please sign in to comment.