Skip to content

Commit

Permalink
feat: add user ranking on profile and use their custom accent colour …
Browse files Browse the repository at this point in the history
…if set in discord
  • Loading branch information
wopian committed Jan 20, 2023
1 parent f04aaf2 commit 5c04e3b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
28 changes: 19 additions & 9 deletions src/commands/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import { Command } from '../command.js'
import { listRecords } from '../components/lists/listRecords.js'
import { database } from '../services/database.js'
import { getRecords } from '../services/records.js'
import { getUser } from '../services/users.js'
import { userSimilarity } from '../utils/index.js'
import { getUser, getUserRanking } from '../services/users.js'
import { formatOrdinal, userSimilarity } from '../utils/index.js'

const addDiscordAuthor = (
embed: EmbedBuilder,
Expand All @@ -27,6 +27,9 @@ const addDiscordAuthor = (
url: `https://zeepkist.wopian.me/user/${steamId}`
})
embed.setThumbnail(linkedAccount.avatarURL() ?? '')
if (linkedAccount.hexAccentColor) {
embed.setColor(linkedAccount.hexAccentColor)
}
}

export const user: Command = {
Expand Down Expand Up @@ -77,20 +80,20 @@ export const user: Command = {
}

try {
const user = await getUser({ steamId, id })
const user = await getUser({ SteamId: steamId, Id: id })
const userRanking = await getUserRanking({ SteamId: user.steamId })

/*
const allValidRecords = await getRecords({
UserSteamId: steamId,
UserId: id,
InvalidOnly: true,
Sort: '-id',
Limit: 5
})
*/
const allInvalidRecords = await getRecords({
UserSteamId: steamId,
UserId: id,
ValidOnly: false,
InvalidOnly: true,
Sort: '-id',
Limit: 5
})
Expand All @@ -109,8 +112,8 @@ export const user: Command = {
Limit: 5
})

const totalRuns = allInvalidRecords.totalAmount
// allValidRecords.totalAmount + allInvalidRecords.totalAmount
const totalRuns =
allValidRecords.totalAmount + allInvalidRecords.totalAmount

const embed = new EmbedBuilder()
.setColor(0xff_92_00)
Expand All @@ -119,14 +122,21 @@ export const user: Command = {
.addFields(
{
name: 'World Records',
value: String(worldRecords.totalAmount),
value: `${worldRecords.totalAmount} (${formatOrdinal(
userRanking.position
)})`,
inline: true
},
{
name: 'Best Times',
value: String(bestRecords.totalAmount),
inline: true
},
{
name: 'any% Times',
value: String(allValidRecords.totalAmount),
inline: true
},
{
name: 'Total Runs',
value: String(totalRuns),
Expand Down
5 changes: 5 additions & 0 deletions src/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export interface UserRankings {

export type UserResponse = User

export interface UserRankingResponse {
amountOfWorldRecords: number
position: number
}

export interface UserRankingsResponse {
totalAmount: number
rankings: UserRankings[]
Expand Down
27 changes: 20 additions & 7 deletions src/services/users.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
import type { UserRankingsResponse, UserResponse } from '../models/user.js'
import type {
UserRankingResponse,
UserRankingsResponse,
UserResponse
} from '../models/user.js'
import { api } from './api.js'

interface GetUserParameters {
id?: number
steamId?: string
Id?: number
SteamId?: string
}

export const getUser = async ({ id, steamId }: GetUserParameters) => {
const response = await (id
? api.get('users/id', { params: { id } })
: api.get('users/steamid', { params: { SteamId: steamId } }))
export const getUser = async ({ Id, SteamId }: GetUserParameters) => {
const response = await (Id
? api.get('users/id', { params: { Id } })
: api.get('users/steamid', { params: { SteamId } }))

if (response.status === 200) return response.data as UserResponse
else {
throw response.data.error
}
}

export const getUserRanking = async (query: GetUserParameters = {}) => {
const response = await api.get('users/ranking', { params: query })

if (response.status === 200) return response.data as UserRankingResponse
else {
throw response.data.error
}
}

interface GetUserRankingsParameters {
Limit?: number
Offset?: number
Expand Down
8 changes: 8 additions & 0 deletions src/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,11 @@ export const formatResultTime = (input: number, precision = 4) => {
precision
)}`)
}

export const formatOrdinal = (number: number) => {
const ordinals = ['th', 'st', 'nd', 'rd']
const modulo = number % 100
return (
number + (ordinals[(modulo - 20) % 10] || ordinals[modulo] || ordinals[0])
)
}

0 comments on commit 5c04e3b

Please sign in to comment.