Skip to content
New issue

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

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

Already on GitHub? # to your account

ranked logging #6

Merged
merged 2 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion server/api/challs/submit.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
req.log.info({
chall: challengeid,
flag: submittedFlag,
type: challenge.type
type: challenge.type,
challenge: challenge
}, 'flag submission attempt')

if (!challenge) {
Expand All @@ -77,7 +78,7 @@
const bufCorrectFlag = Buffer.from(challenge.flag)

const challengeType = challenge.type
let submittedHash, submittedScore = null

Check failure on line 81 in server/api/challs/submit.js

View workflow job for this annotation

GitHub Actions / lint (12)

Split initialized 'let' declarations into multiple statements

if (challengeType === 'ranked') {
const parts = submittedFlag.split('.')
Expand All @@ -86,7 +87,7 @@
}
[submittedHash, submittedScore] = parts
// The user will receive SHA256(FLAG || answerLength) || '.' || answerLength

Check failure on line 90 in server/api/challs/submit.js

View workflow job for this annotation

GitHub Actions / lint (12)

Trailing spaces not allowed
const correctHash = crypto.createHash('sha256').update(bufCorrectFlag).update(submittedScore).digest('hex')
if (submittedHash != correctHash) {
return responses.badFlagRanked
Expand Down
1 change: 1 addition & 0 deletions server/cache/leaderboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export const setLeaderboard = async ({ challengeValues, solveAmount, leaderboard
challengeValues.forEach((value, key) => {
challengeInfo.push(key, `${value},${solveAmount.get(key)}`)
})
console.dir({ challengeValues, solveAmount, leaderboard, leaderboardUpdate }, { depth: null })
await redisEvalsha(
await setLeaderboardScript,
keys.length,
Expand Down
1 change: 1 addition & 0 deletions server/challenges/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import config from '../config/server'
import path from 'path'
import { Challenge, CleanedChallenge } from './types'

Check warning on line 3 in server/challenges/index.ts

View workflow job for this annotation

GitHub Actions / lint (12)

'Challenge' is defined but never used

Check warning on line 3 in server/challenges/index.ts

View workflow job for this annotation

GitHub Actions / lint (12)

'CleanedChallenge' is defined but never used
import { Provider, ProviderConstructor } from './Provider'

Check warning on line 4 in server/challenges/index.ts

View workflow job for this annotation

GitHub Actions / lint (12)

'Provider' is defined but never used

Check warning on line 4 in server/challenges/index.ts

View workflow job for this annotation

GitHub Actions / lint (12)

'ProviderConstructor' is defined but never used
import { challUpdateEmitter, publishChallUpdate } from '../cache/challs'
import { EventEmitter } from 'events'

Expand Down Expand Up @@ -35,6 +35,7 @@
}

const onUpdate = (newChallenges: Challenge[]): void => {
console.dir({ newChallenges }, { depth: null })
challenges = newChallenges
challengesMap = new Map(newChallenges.map(c => [c.id, c]))
cleanedChallenges = challenges.map(cleanChallenge)
Expand Down
19 changes: 18 additions & 1 deletion server/leaderboard/calculate.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const {
}
} = workerData

console.dir({ allChallenges }, { depth: null })
const solveAmount = new Map()
const challengeTiebreakEligibles = new Map()
for (let i = 0; i < allChallenges.length; i++) {
Expand Down Expand Up @@ -81,6 +82,7 @@ const calculateScores = (sample) => {
))
}

const rankedSolvesForLogging = []
for (let i = 0; i < users.length; i++) {
const user = users[i]
let currScore = 0
Expand All @@ -100,7 +102,17 @@ const calculateScores = (sample) => {
rankedMetadata.minScore,
rankedMetadata.maxScore,
solveScore
)
)
rankedSolvesForLogging.push({
userId: user.id,
challengeId: solvedChallId,
score: solveScore,
value,
min: rankedMetadata.min,
max: rankedMetadata.max,
minScore: rankedMetadata.minScore,
maxScore: rankedMetadata.maxScore
})
} else {
// Add the score for the specific solve loaded from the challengeValues array using ids

Expand All @@ -121,6 +133,9 @@ const calculateScores = (sample) => {
])
}

console.dir({ rankedSolvesForLogging }, { depth: null })
console.dir({ challengeRankedMetadata }, { depth: null })

return {
challengeValues,
userScores
Expand Down Expand Up @@ -159,6 +174,8 @@ samples.forEach((sample) => {
const { userScores, challengeValues } = calculateScores(leaderboardUpdate)
const sortedUsers = userScores.sort(userCompare).map((user) => user.slice(0, 4))

console.log({ sortedUsers })

parentPort.postMessage({
leaderboard: sortedUsers,
graphLeaderboards,
Expand Down
5 changes: 4 additions & 1 deletion server/leaderboard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ const fetchData = async () => {
database.users.getAllUsers(),
cache.leaderboard.getGraphUpdate()
])
return {
const workerData = {
solves,
users,
graphUpdate,
allChallenges: getAllChallenges()
}
console.dir({ workerData }, { depth: null })
return workerData
}

let updating = false
Expand All @@ -32,6 +34,7 @@ const runUpdate = async () => {
}
})
worker.once('message', async (data) => {
console.dir({ leaderboardData: data }, { depth: null })
await cache.leaderboard.setLeaderboard(data)
await cache.leaderboard.setGraph({ leaderboards: data.graphLeaderboards })
updating = false
Expand Down
2 changes: 2 additions & 0 deletions server/providers/challenges/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class DatabaseProvider extends EventEmitter implements Provider {
}
})

console.dir({ databaseUpdateChallenges: this.challenges }, { depth: null })

this.emit('update', this.challenges)
} catch (e) {
// TODO: wrap error?
Expand Down
Loading