Skip to content

Commit

Permalink
fix(handicap): Use correct order of games
Browse files Browse the repository at this point in the history
  • Loading branch information
PrettyCoffee committed Oct 28, 2024
1 parent f8d0293 commit 19682ae
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/app/routes/settings/RulesetSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export const HandicapSlider = () => {
className="mb-2"
htmlFor={"handicap"}
hint={
"Defines the severity of the handicap a player will receive after winning a game. " +
"Defines the severity of the handicap a player will receive after winning multiple times in a row. " +
"A value of 0 will turn the handicap off. "
}
>
Expand Down
32 changes: 17 additions & 15 deletions src/data/games.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
createSlice,
localStorage,
useAtomValue,
createEffect,
} from "~/lib/yaasl"
import { createId } from "~/utils/createId"
import { dateIsValid, timeBetween, timeSince, today } from "~/utils/date"
Expand All @@ -31,33 +32,34 @@ export interface Game extends Omit<RawGame, "playerId"> {
player?: Player
}

const sortByDate = (games: RawGame[]) =>
games.sort((a, b) => a.date.localeCompare(b.date))
const sortByDate = createEffect<undefined, RawGame[]>({
set: ({ set }) => {
set(games => games.sort((a, b) => a.date.localeCompare(b.date)))
},
})

export const gamesSlice = createSlice({
defaultValue: [] as RawGame[],
name: "games",
effects: [localStorage()],
effects: [sortByDate(), localStorage()],

reducers: {
add: (state, game: RawGame) => sortByDate([...state, game]),
add: (state, game: RawGame) => [...state, game],
edit: (state, id: string, data: Partial<RawGame>) =>
state.map(game => (game.id === id ? { ...game, ...data } : game)),
remove: (state, id: string) => state.filter(game => game.id !== id),

merge: (state, games: RawGame[]) =>
sortByDate(
games.reduce(
(state, game) => {
const gameExists = state.some(({ name }) => name === game.name)
if (gameExists || !dateIsValid(game.date)) {
return state
}
state.push(game)
games.reduce(
(state, game) => {
const gameExists = state.some(({ name }) => name === game.name)
if (gameExists || !dateIsValid(game.date)) {
return state
},
[...state]
)
}
state.push(game)
return state
},
[...state]
),
},
})
Expand Down
16 changes: 10 additions & 6 deletions src/data/handicap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { rulesetAtom } from "./ruleset"

export const calcHandicap = (wins: number, max: number, severity = 0.75) => {
if (severity <= 0) return 0
const result = Math.pow((1 / max) * wins, 1 / severity)
const successive = wins - 1
if (successive < 1) return 0

const result = Math.pow((1 / max) * successive, 1 / severity)
return Math.round(result * 100) / 100
}

Expand All @@ -18,12 +21,12 @@ export interface Handicap {
const handicapAtom = createSelector(
[gamesSlice, rulesetAtom],
(games, ruleset): Handicap => {
const reversedGames = games.reverse()
const reversed = games.reverse()

let wins = 0
let wins = 0 // ignore the first win
let latestPlayer: string | undefined = undefined
while (!latestPlayer || reversedGames[wins]?.playerId === latestPlayer) {
const current = reversedGames[wins]
while (!latestPlayer || reversed[wins]?.playerId === latestPlayer) {
const current = reversed[wins]
wins++
if (!current) break
if (!latestPlayer) {
Expand All @@ -32,7 +35,8 @@ const handicapAtom = createSelector(
}

return {
wins,
// Ignore one win
wins: wins - 1,
amount: calcHandicap(wins, ruleset.gamesPerPerson, ruleset.handicap),
playerId: latestPlayer,
}
Expand Down

0 comments on commit 19682ae

Please sign in to comment.