Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for best x rounds in season #267

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: add support for best x rounds in season
wopian committed Oct 22, 2024
commit ecbae3b66e0f4213d296c462ffbf98531265450a
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ interface User {

export interface UserStanding extends User {
totalPoints: number
pointsPerRound: number[]
}

export type UserStandingMap = Omit<UserStanding, 'steamId'>
@@ -49,6 +50,7 @@ export interface Metadata {
}
points?: number[]
finishPoints?: number
bestOf?: number
}

export type LevelsMap = Map<string, UserLevelStandingMap[]>
20 changes: 16 additions & 4 deletions src/utils/handleEvents.ts
Original file line number Diff line number Diff line change
@@ -29,28 +29,40 @@ export const handleEvents = (
seasonUsers.set(steamId, {
username: user.username,
team: user.team,
totalPoints: 0
totalPoints: 0,
pointsPerRound: []
})
}
}

for (const [, data] of levels) {
for (const item of data) {
const seasonUser = seasonUsers.get(item.steamId)
const user = users.get(item.steamId)

if (user) {
user.totalPoints += item.points ?? 0
user.pointsPerRound.push(item.points ?? 0)
}

const seasonUser = seasonUsers.get(item.steamId)
item.username = seasonUser?.username ?? user?.username ?? ''
item.team = seasonUser?.team ?? user?.team ?? undefined
}
}

for (const [steamId, user] of users) {
const seasonUser = seasonUsers.get(steamId)

if (seasonUser) {
seasonUser.totalPoints += user.totalPoints / levels.size
const bestOf = metadata.bestOf ?? Number.MAX_SAFE_INTEGER

// add points for each round separately
seasonUser.pointsPerRound.push(Math.round((user.totalPoints / levels.size) * 10))

const sortedPoints = [...seasonUser.pointsPerRound].sort((a, b) => b - a);

// calculate total points for the user (optionally based on X best point-scoring rounds if bestOf is set in metadata.json)
seasonUser.totalPoints = sortedPoints.slice(0, bestOf).reduce((acc, point) => acc + point, 0);
}
}

@@ -87,7 +99,7 @@ export const handleEvents = (
[...seasonUsers]
.sort((a, b) => b[1].totalPoints - a[1].totalPoints)
.map(([steamId, user]) => {
const totalPoints = Number((user.totalPoints * 100).toFixed(0))
const totalPoints = Number((user.totalPoints).toFixed(0))

return {
...user,
3 changes: 2 additions & 1 deletion src/utils/handleLeaderboard.ts
Original file line number Diff line number Diff line change
@@ -33,7 +33,8 @@ export const handleLeaderboard = ({
users.set(record.SteamId, {
username,
team,
totalPoints: 0
totalPoints: 0,
pointsPerRound: [],
})

const level = levels.get(uid)