Skip to content

Commit

Permalink
feat: split team name from username field
Browse files Browse the repository at this point in the history
  • Loading branch information
wopian committed Feb 23, 2023
1 parent c0bda0c commit 71d9484
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface RawRecord {

interface User {
username: string
team?: string
steamId: string
}

Expand Down
3 changes: 3 additions & 0 deletions src/utils/handleEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ export const handleEvents = (
const seasonUser = seasonUsers.get(steamId)
if (seasonUser) {
seasonUser.username = user.username
seasonUser.team = user.team
} else {
seasonUsers.set(steamId, {
username: user.username,
team: user.team,
totalPoints: 0
})
}
Expand All @@ -41,6 +43,7 @@ export const handleEvents = (

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

Expand Down
5 changes: 4 additions & 1 deletion src/utils/handleLeaderboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { readFileSync } from 'node:fs'
import { parse } from 'csv-parse/sync'

import { LevelsMap, RawRecord, UsersMap } from '../types.js'
import { splitUserTeam } from './index.js'

interface Properties {
levels: LevelsMap
Expand All @@ -27,8 +28,10 @@ export const handleLeaderboard = ({
})

for (const record of records) {
const { username, team } = splitUserTeam(record.Username)
users.set(record.SteamId, {
username: record.Username,
username,
team,
totalPoints: 0
})

Expand Down
5 changes: 4 additions & 1 deletion src/utils/handleSeasons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ export const handleSeasons = (
seasonMetadata.set(season, metadata)
}

writeFileSync(`${output}/metadata.json`, JSON.stringify([...seasonMetadata]))
writeFileSync(
`${output}/metadata.json`,
JSON.stringify([...seasonMetadata], undefined, 2)
)

console.log(`Generated ${seasons.length} seasons`)
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './handleEvents.js'
export * from './handleLeaderboard.js'
export * from './handleSeasons.js'
export * from './pointsDistribution.js'
export * from './splitUserTeam.js'
48 changes: 48 additions & 0 deletions src/utils/splitUserTeam.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import test from 'ava'

import { splitUserTeam } from './index.js'

test('splitUserTeam returns username without team name', t => {
t.deepEqual(splitUserTeam('username'), {
username: 'username'
})

t.deepEqual(splitUserTeam('[username'), {
username: '[username'
})

t.deepEqual(splitUserTeam('[username]'), {
username: '[username]'
})
})

test('splitUserTeam returns username with team name', t => {
t.deepEqual(splitUserTeam('[team]username'), {
username: 'username',
team: 'team'
})

t.deepEqual(splitUserTeam('[team] username'), {
username: 'username',
team: 'team'
})

t.deepEqual(splitUserTeam('[team] [username]'), {
username: '[username]',
team: 'team'
})
})

test('splitUserTeam returns username with team name and removes bad taste teams', t => {
t.deepEqual(splitUserTeam('[CTR SUCKS]username'), {
username: 'username'
})

t.deepEqual(splitUserTeam('[CTR SUCKS] username'), {
username: 'username'
})

t.deepEqual(splitUserTeam('[CTR SUCKS] [username]'), {
username: '[username]'
})
})
31 changes: 31 additions & 0 deletions src/utils/splitUserTeam.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const removeBadTasteTeams = (input: string) =>
['CTR SUCKS'].includes(input) ? undefined : input

export const splitUserTeam = (input: string) => {
if (input.startsWith('[')) {
const [team, ...username] = input.split(']')
const formattedUsername = username.join(']').trim()
const formattedTeam = removeBadTasteTeams(team.slice(1))

if (!formattedUsername) {
return {
username: input
}
}

if (!formattedTeam) {
return {
username: formattedUsername
}
}

return {
team: formattedTeam,
username: formattedUsername
}
} else {
return {
username: input
}
}
}

0 comments on commit 71d9484

Please sign in to comment.