Skip to content

Commit

Permalink
Factor flyer finish-early logic into a separate method
Browse files Browse the repository at this point in the history
  • Loading branch information
phrasmotica committed Apr 27, 2024
1 parent 8fca2e7 commit d385852
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
9 changes: 6 additions & 3 deletions src/components/play/FixtureScoreForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,11 @@ const updateScores = (finish: boolean) => {
isBye: false,
}))
flyerStore.updateScores(
const isFinishedEarly = flyerStore.updateScores(
currentPhase.value,
fixture.value.id,
newScores,
finish,
standings.value)
finish)
toast.add({
severity: 'success',
Expand All @@ -135,6 +134,10 @@ const updateScores = (finish: boolean) => {
contentStyleClass: isSmallScreen.value ? 'flyer-toast-sm' : '',
})
if (isFinishedEarly) {
flyerStore.finishEarly(currentPhase.value, standings.value)
}
hide()
}
Expand Down
26 changes: 20 additions & 6 deletions src/stores/flyer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type { Player } from "@/data/Player"
import type { PlayerRecord } from "@/data/PlayerRecord"
import type { Round } from "@/data/Round"
import { RoundRobinScheduler } from "@/data/RoundRobinScheduler"
import { createPlayOffSettings, Format } from "@/data/Specification"
import { Format, createPlayOffSettings } from "@/data/Specification"
import type { Table } from "@/data/Table"
import type { TieBreakerInfo } from "@/data/TieBreakerInfo"
import { WinnerStaysOnScheduler } from "@/data/WinnerStaysOnScheduler"
Expand Down Expand Up @@ -246,7 +246,6 @@ export const useFlyerStore = defineStore("flyer", () => {
fixtureId: string,
scores: Score[],
finishFixture: boolean,
currentRanking: PlayerRecord[],
addEvent = true,
) => {
for (const r of phase.rounds) {
Expand All @@ -262,9 +261,7 @@ export const useFlyerStore = defineStore("flyer", () => {
}

if (winsRequiredReached(phase)) {
cancelRemaining()
finishPhase(phase, currentRanking)
return
return true
}

const winnerId = getWinner(r.fixtures[idx])
Expand All @@ -280,6 +277,8 @@ export const useFlyerStore = defineStore("flyer", () => {
}
}
}

return false
}

const addTable = (name: string, costPerHour: number) => {
Expand Down Expand Up @@ -355,6 +354,15 @@ export const useFlyerStore = defineStore("flyer", () => {
return false
}

const finishEarly = (phase: Phase, ranking: PlayerRecord[]) => {
if (phase.settings.format !== Format.WinnerStaysOn) {
return
}

cancelRemaining()
finish(ranking)
}

const cancelRemaining = () => {
if (!flyer.value) {
return
Expand Down Expand Up @@ -453,9 +461,14 @@ export const useFlyerStore = defineStore("flyer", () => {
startFixture(phase, fixture.id, false)

updateComment(phase, fixture.id, "AUTO-COMPLETED")
updateScores(phase, fixture.id, newScores, true, currentRanking, false)

const isFinishedEarly = updateScores(phase, fixture.id, newScores, true, false)

addPhaseEvent(phase, `Fixture ${fixture.id} was auto-completed.`, PhaseEventLevel.Internal)

if (isFinishedEarly) {
finishEarly(phase, currentRanking)
}
}

const autoCompletePhase = (
Expand Down Expand Up @@ -518,6 +531,7 @@ export const useFlyerStore = defineStore("flyer", () => {
updateScores,
addTable,
generateRound,
finishEarly,
finishPhase,
cancelRemaining,
addPlayOff,
Expand Down

0 comments on commit d385852

Please sign in to comment.