Skip to content

Commit

Permalink
Compute a phase's tie breakers just once, when when phase finishes, a…
Browse files Browse the repository at this point in the history
…nd store them in the phase object
  • Loading branch information
phrasmotica committed Apr 27, 2024
1 parent 8dcd2ad commit 9cc6828
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
15 changes: 1 addition & 14 deletions src/composables/useStandings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useArrayFilter, useSorted } from "@vueuse/core"
import { v4 as uuidv4 } from "uuid"
import { computed } from "vue"

import { useFixtureList } from "./useFixtureList"
import { usePhase } from "./usePhase"
import { usePlayers } from "./usePlayers"
import { useRankings } from "./useRankings"
Expand All @@ -23,27 +22,15 @@ export const useStandings = (p: Phase | null) => {

const {
isKnockout,
isRoundRobin,
} = usePhaseSpecification(phase.value)

const {
isStarted,
} = useFixtureList(phase.value)

const {
computeStandings,
computeTieBreakers,
} = useRankings()

const standings = computed(() => computeStandings(phase.value, true))

const tieBreakers = computed(() => {
if (!isStarted.value) {
return []
}

return computeTieBreakers(phase.value)
})
const tieBreakers = computed(() => phase.value?.tieBreakers || [])

const orderedTieBreakers = useSorted(tieBreakers, (a, b) => b.forRank - a.forRank)

Expand Down
3 changes: 2 additions & 1 deletion src/stores/flyer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,11 @@ export const useFlyerStore = defineStore("flyer", () => {
}
}

const finishPhase = (phase: Phase, ranking: PlayerRecord[]) => {
const finishPhase = (phase: Phase, ranking: PlayerRecord[], tieBreakers: TieBreakerInfo[]) => {
if (!phase.finishTime) {
phase.finishTime = Date.now()
phase.ranking = ranking
phase.tieBreakers = tieBreakers

addPhaseEvent(phase, `${phase.settings.name} was finished.`)
}
Expand Down
17 changes: 15 additions & 2 deletions src/views/PlayView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import PlaySections from "@/components/play/PlaySections.vue"
import { useFixtureSwaps } from "@/composables/useFixtureSwaps"
import { useFlyer } from "@/composables/useFlyer"
import { usePhaseEvents } from "@/composables/usePhaseEvents"
import { usePhaseTiming } from "@/composables/usePhaseTiming"
import { useQueryParams } from "@/composables/useQueryParams"
import { useRankings } from "@/composables/useRankings"
import { useRound } from "@/composables/useRound"
import { useRounds } from "@/composables/useRounds"
import { useRouting } from "@/composables/useRouting"
Expand All @@ -28,7 +30,6 @@ import { useStandings } from "@/composables/useStandings"
import type { Fixture } from "@/data/Fixture"
import { PlayViewSection } from "@/data/UiSettings"
import { usePhaseEvents } from "@/composables/usePhaseEvents"
import { useFlyerStore } from "@/stores/flyer"
import { useUiStore } from "@/stores/ui"
Expand Down Expand Up @@ -65,6 +66,10 @@ const {
nextRoundToGenerate,
} = useRounds(currentPhase.value)
const {
computeTieBreakers,
} = useRankings()
const {
standings: currentStandings,
} = useStandings(currentPhase.value)
Expand Down Expand Up @@ -125,6 +130,7 @@ const sections = computed(() => {
]
if (isKnockout.value) {
// BUG: should always show standings if the main phase was round-robin or winner stays on
relevantSections.splice(1, 1)
}
Expand Down Expand Up @@ -164,12 +170,19 @@ const finishPhase = () => {
throw "No phase to finish!"
}
const success = flyerStore.finishPhase(currentPhase.value, currentStandings.value)
// BUG: this doesn't seem to be setting tie-breakers for a phase after
// the main phase
const success = flyerStore.finishPhase(
currentPhase.value,
currentStandings.value,
computeTieBreakers(currentPhase.value))
if (!success) {
throw "Failed to finish phase!"
}
if (isKnockout.value) {
// BUG: this should only happen if it's also the main phase
flyerStore.finish(currentStandings.value)
}
Expand Down

0 comments on commit 9cc6828

Please sign in to comment.