Skip to content

Commit

Permalink
Edit getAvgVisitScore
Browse files Browse the repository at this point in the history
  • Loading branch information
magnetenstad committed Jan 29, 2024
1 parent ab85651 commit 584ebb8
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions src/games/x01.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ export const getX01Controller = (game: Game): GameController<GameState> => {
const visits = getVisitsOfUser(game, userId)
const avg = getAvgVisitScore(
game.legs.find((leg) => leg.userId == userId)?.visits ?? [],
game,
true
game
).toFixed(1)
return `${name}, ${visits?.length} visits, ${avg} average`
},
Expand Down Expand Up @@ -90,14 +89,13 @@ export const getX01LegScore = (
game: {
type: GameType
typeAttributes: string[]
},
includeUnfinished = true
}
) => {
let score = 0
const points = getGamePoints(game)
const finishType = getTypeAttribute(game, 'finish', 1)
visits?.forEach((v) => {
const visitScore = getX01VisitScore(v, includeUnfinished)
const visitScore = getX01VisitScore(v)
if (score + visitScore == points) {
if (
finishType == 1 ||
Expand All @@ -120,15 +118,12 @@ export const getAvgVisitScore = (
game: {
type: GameType
typeAttributes: string[]
},
includeUnfinished = false
}
) => {
if (!visits || visits.length == 0) return 0
const count = includeUnfinished
? visits.length
: visits.filter((visit) => !visit.includes(null)).length
if (!count) return 0
return getX01LegScore(visits, game, includeUnfinished) / count
const segmentCount = visits.flat().filter((visit) => visit != null).length
if (!segmentCount) return 0
return (getX01LegScore(visits, game) * 3) / segmentCount
}

export const getFirst9Avg = (
Expand All @@ -140,11 +135,10 @@ export const getFirst9Avg = (
) => {
if (!visits) return 0
const first9 = visits.slice(0, 3)
return getAvgVisitScore(first9, game, true)
return getAvgVisitScore(first9, game)
}

export const getX01VisitScore = (visit: Visit, includeUnfinished = true) => {
if (!includeUnfinished && visit.includes(null)) return 0
export const getX01VisitScore = (visit: Visit) => {
return visit.reduce((prev, current) => prev + getSegmentScore(current), 0)
}

Expand Down

0 comments on commit 584ebb8

Please sign in to comment.