Skip to content

Commit

Permalink
feat(user): add ranking score to user command (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
wopian authored May 8, 2023
1 parent 5335cf5 commit 9659e30
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"dependencies": {
"@twurple/api": "6.2.0",
"@twurple/auth": "6.2.0",
"@zeepkist/gtr-api": "3.2.0",
"@zeepkist/gtr-api": "3.2.1",
"date-fns": "2.30.0",
"discord.js": "14.11.0",
"dotenv": "16.0.3",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 15 additions & 10 deletions src/commands/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
getRecords,
getUser,
getUserBySteamId,
getUserRanking
getUserRanking,
UserRanking
} from '@zeepkist/gtr-api'
import {
ActionRowBuilder,
Expand All @@ -23,11 +24,7 @@ import { listRecords } from '../components/lists/listRecords.js'
import { STEAM_URL, ZEEPKIST_URL } from '../constants.js'
import { database } from '../services/database.js'
import { getPlayerSummaries } from '../services/steam.js'
import {
formatOrdinal,
log,
userSimilarity
} from '../utils/index.js'
import { formatOrdinal, log, userSimilarity } from '../utils/index.js'

const addDiscordAuthor = (
interaction: CommandInteraction,
Expand Down Expand Up @@ -123,21 +120,24 @@ export const user: Command = {
interaction
)

let userRanking
let userRanking: UserRanking
try {
userRanking = await getUserRanking({ SteamId: user.steamId })
userRanking = await getUserRanking(user.id)
log.info(`Found user ranking: ${userRanking.position}`, interaction)
} catch (error) {
if (error instanceof HTTPError && error.response.status === 404) {
userRanking = {
position: 0,
totalAmount: 0
score: 0,
amountOfWorldRecords: 0
}
} else {
throw error
}
}

const userRankingScore = Math.floor(userRanking.score)

const userRankingPosition = userRanking.position
? `(${formatOrdinal(userRanking.position)})`
: ''
Expand Down Expand Up @@ -192,9 +192,14 @@ export const user: Command = {
.setTitle(`${user.steamName}'s Stats`)
.setURL(`${ZEEPKIST_URL}/user/${user.steamId}`)
.addFields(
{
name: 'Points',
value: `${userRankingScore} ${userRankingPosition}`.trim(),
inline: true
},
{
name: 'World Records',
value: `${worldRecords.totalAmount} ${userRankingPosition}`.trim(),
value: `${worldRecords.totalAmount}`,
inline: true
},
{
Expand Down
7 changes: 1 addition & 6 deletions src/utils/format.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ import test from 'ava'

import { Level } from '../models/level.js'
import { User } from '../models/user.js'
import {
formatLevel,
formatOrdinal,
formatRank,
formatUser
} from './index.js'
import { formatLevel, formatOrdinal, formatRank, formatUser } from './index.js'

const macro = test.macro((t, input: string, expected: string) =>
t.is(input, expected)
Expand Down

0 comments on commit 9659e30

Please sign in to comment.