Skip to content
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
14 changes: 12 additions & 2 deletions src/services/instance/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
{ params: [hash.toString()] }
)
if (!metaData) {
throw new Error("Metadata not found")

Check warning on line 190 in src/services/instance/instance.ts

View workflow job for this annotation

GitHub Actions / test

190 line is not covered with tests
}
return metaData
}
Expand All @@ -210,9 +210,8 @@
rank: number
}>(
`SELECT rank::int
FROM team_pantheon_custom_race_leaderboard
FROM pantheon_custom_race_snapshot
WHERE instance_id = $1::bigint
ORDER BY rank ASC
LIMIT 1;`,
{ params: [instanceId] }
)
Expand All @@ -224,17 +223,28 @@
"code" in error &&
error.code === "42P01"
)
) {
throw error

Check warning on line 227 in src/services/instance/instance.ts

View workflow job for this annotation

GitHub Actions / test

226-227 lines are not covered with tests
}

customRaceEntry = await pgReader.queryRow<{
rank: number
}>(
`SELECT rank::int
FROM team_pantheon_custom_race_leaderboard
WHERE instance_id = $1::bigint
ORDER BY rank ASC
LIMIT 1;`,
{ params: [instanceId] }
)
}

if (!versionEntry) {
return customRaceEntry
}
if (!customRaceEntry) {
return versionEntry
}

Check warning on line 247 in src/services/instance/instance.ts

View workflow job for this annotation

GitHub Actions / test

244-247 lines are not covered with tests

return versionEntry.rank <= customRaceEntry.rank ? versionEntry : customRaceEntry
}
Expand Down
109 changes: 77 additions & 32 deletions src/services/leaderboard/team/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,16 @@
/** Insurrection Prime Revolutionary (version 134) — final pantheon community raid race. */
export const PANTHEON_COMMUNITY_RACE_VERSION_ID = 134

export const getPantheonCustomRaceTeamLeaderboard = async ({
skip,
take
}: {
skip: number
take: number
}) => {
return await pgReader.queryRows<TeamLeaderboardEntry>(
`SELECT
position::int,
rank::int,
value,
instance_id as "instanceId",
"lateral".players
FROM team_pantheon_custom_race_leaderboard
const PANTHEON_RACE_SNAPSHOT = "pantheon_custom_race_snapshot"
const PANTHEON_RACE_LEGACY_MV = "team_pantheon_custom_race_leaderboard"

const isMissingRelation = (error: unknown) =>
typeof error === "object" &&
error !== null &&
"code" in error &&
(error as { code: string }).code === "42P01"

const pantheonRacePlayerLateral = (source: string) => `
LEFT JOIN LATERAL (
SELECT
COALESCE(
Expand All @@ -40,20 +35,50 @@
) as "players"
FROM instance_player
INNER JOIN player USING (membership_id)
WHERE instance_player.instance_id = team_pantheon_custom_race_leaderboard.instance_id
WHERE instance_player.instance_id = ${source}.instance_id
AND instance_player.completed
) as "lateral" ON true
) as "lateral" ON true`

const withPantheonRaceSource = async <T>(run: (source: string) => Promise<T>): Promise<T> => {
try {
return await run(PANTHEON_RACE_SNAPSHOT)
} catch (error) {
if (isMissingRelation(error)) {
return await run(PANTHEON_RACE_LEGACY_MV)
}

Check warning on line 48 in src/services/leaderboard/team/custom.ts

View workflow job for this annotation

GitHub Actions / test

48 line is not covered with tests
throw error
}
}

export const getPantheonCustomRaceTeamLeaderboard = async ({
skip,
take
}: {
skip: number
take: number
}) => {
return await withPantheonRaceSource(source =>
pgReader.queryRows<TeamLeaderboardEntry>(
`SELECT
position::int,
rank::int,
value,
instance_id as "instanceId",
"lateral".players
FROM ${source}
${pantheonRacePlayerLateral(source)}
WHERE position > $1 AND position <= ($1 + $2)
ORDER BY position ASC`,
{
params: [skip, take],
transformers: {
players: {
membershipId: convertStringToBigInt,
lastSeen: convertStringToDate
{
params: [skip, take],
transformers: {
players: {
membershipId: convertStringToBigInt,
lastSeen: convertStringToDate
}
}
}
}
)
)
}

Expand All @@ -64,22 +89,42 @@
membershipId: bigint | string
take: number
}) => {
const result = await pgReader.queryRow<{ position: number }>(
`SELECT position::int
FROM team_pantheon_custom_race_leaderboard
WHERE membership_ids @> $1::jsonb
ORDER BY position ASC
LIMIT 1`,
{ params: [`${[membershipId]}`] }
)
const result = await withPantheonRaceSource(async source => {
if (source === PANTHEON_RACE_SNAPSHOT) {
return await pgReader.queryRow<{ position: number }>(
`SELECT s.position::int
FROM pantheon_custom_race_snapshot s
WHERE EXISTS (
SELECT 1
FROM instance_player ip
WHERE ip.instance_id = s.instance_id
AND ip.membership_id = $1::bigint
AND ip.completed
)
ORDER BY s.position ASC
LIMIT 1`,
{ params: [membershipId] }
)
}

return await pgReader.queryRow<{ position: number }>(
`SELECT position::int
FROM team_pantheon_custom_race_leaderboard
WHERE membership_ids @> $1::jsonb
ORDER BY position ASC
LIMIT 1`,
{ params: [`${[membershipId]}`] }
)
})

if (!result) return null

const page = Math.ceil(result.position / take)
return {
page,
entries: await getPantheonCustomRaceTeamLeaderboard({
skip: (page - 1) * take,
take
})

Check warning on line 128 in src/services/leaderboard/team/custom.ts

View workflow job for this annotation

GitHub Actions / test

122-128 lines are not covered with tests
}
}
Loading