feat(成就): 下架三个拿不到的成就、理顺稀有度和图标;接口多给本班几人有和「本班」卡片
- 0032:下架六边形战士(判题机只剩 3 种语言)、坚持三天/一周(按天算基本拿不到); 不撞南墙改白银、屡败屡战改黄金;百发百中换 💯、屡败屡战换 💪,不再和别的撞图标; 代码行数只算做对的、空行不算,并按新口径重算所有人;已经拿到的一个不收回 - 奖杯收藏家只数上架的成就(判题结算、补发重算、0032 存量一起改) - GET /achievements:每个成就带全站几人有、本班几人有;看自己时带「本班」(最近拿到、成就最多前五、自己第几); 隐藏成就同一串拿到一档就整串亮出来;连续几周给「现在连着几周」而不是历史最长 - 删掉只有旧成就页在用的 /achievements/summary Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
689d40b116
commit
85108df9d8
9 files changed
+4220
-213
No files matched your search
@@ -1,16 +1,18 @@
|
||||
import {
|
||||
markAchievementsReadSchema,
|
||||
STUDENT_ROLES,
|
||||
type Achievement,
|
||||
type AchievementClassBoard,
|
||||
type AchievementList,
|
||||
type AchievementSummary,
|
||||
type PendingAchievement,
|
||||
} from "@oj2/contract"
|
||||
import { and, asc, count, desc, eq, inArray } from "drizzle-orm"
|
||||
import { and, asc, count, desc, eq, inArray, isNull, max } from "drizzle-orm"
|
||||
import { Hono } from "hono"
|
||||
|
||||
import { requireAuth, type AppEnv } from "../auth/middleware"
|
||||
import { db, schema } from "../db"
|
||||
import { failure, parseBody, success } from "../http"
|
||||
import { currentWeekStreak } from "../services/problem-progress"
|
||||
import { asRecord } from "./helpers"
|
||||
|
||||
export const achievementRoutes = new Hono<AppEnv>()
|
||||
@@ -18,14 +20,22 @@ export const achievementRoutes = new Hono<AppEnv>()
|
||||
async function resolveUser(requested: string | undefined, currentId: number) {
|
||||
if (!requested) {
|
||||
const [current] = await db
|
||||
.select({ id: schema.user.id, username: schema.user.username })
|
||||
.select({
|
||||
id: schema.user.id,
|
||||
username: schema.user.username,
|
||||
className: schema.user.className,
|
||||
})
|
||||
.from(schema.user)
|
||||
.where(eq(schema.user.id, currentId))
|
||||
.limit(1)
|
||||
return current ?? null
|
||||
}
|
||||
const [target] = await db
|
||||
.select({ id: schema.user.id, username: schema.user.username })
|
||||
.select({
|
||||
id: schema.user.id,
|
||||
username: schema.user.username,
|
||||
className: schema.user.className,
|
||||
})
|
||||
.from(schema.user)
|
||||
.where(and(eq(schema.user.username, requested), eq(schema.user.isDisabled, false)))
|
||||
.limit(1)
|
||||
@@ -43,9 +53,10 @@ function pendingData(row: { achievement: typeof schema.achievement.$inferSelect
|
||||
}
|
||||
|
||||
achievementRoutes.get("/achievements", requireAuth, async (c) => {
|
||||
const target = await resolveUser(c.req.query("username"), c.get("user")!.id)
|
||||
const me = c.get("user")!
|
||||
const target = await resolveUser(c.req.query("username"), me.id)
|
||||
if (!target) return failure(c, 404, "user-not-found", "用户不存在")
|
||||
const [achievements, unlockedRows, statRows, activeRows] = await Promise.all([
|
||||
const [achievements, unlockedRows, statRows] = await Promise.all([
|
||||
db
|
||||
.select()
|
||||
.from(schema.achievement)
|
||||
@@ -57,15 +68,31 @@ achievementRoutes.get("/achievements", requireAuth, async (c) => {
|
||||
.from(schema.userStat)
|
||||
.where(eq(schema.userStat.userId, target.id))
|
||||
.limit(1),
|
||||
db.select({ value: count() }).from(schema.user).where(eq(schema.user.isDisabled, false)),
|
||||
])
|
||||
const unlocked = new Map(unlockedRows.map((row) => [row.achievementId, row]))
|
||||
const metrics = asRecord(statRows[0]?.metrics)
|
||||
const active = activeRows[0]?.value ?? 0
|
||||
const weekStreak = currentWeekStreak(metrics)
|
||||
// 隐藏的成就:同一个指标上拿到过一档,这一串就整串亮出来(拿到「一发入魂」就看得到「神射手」)
|
||||
const revealedMetrics = new Set(
|
||||
achievements.filter((item) => unlocked.has(item.id)).map((item) => item.metric),
|
||||
)
|
||||
const classHolders = target.className
|
||||
? new Map(
|
||||
(
|
||||
await db
|
||||
.select({ id: schema.userAchievement.achievementId, n: count() })
|
||||
.from(schema.userAchievement)
|
||||
.innerJoin(schema.user, eq(schema.user.id, schema.userAchievement.userId))
|
||||
.where(classMembers(target.className))
|
||||
.groupBy(schema.userAchievement.achievementId)
|
||||
).map((row) => [row.id, row.n]),
|
||||
)
|
||||
: null
|
||||
const result = achievements.map((achievement) => {
|
||||
const record = unlocked.get(achievement.id)
|
||||
const masked = achievement.hidden && !record
|
||||
const progress = metrics[achievement.metric]
|
||||
const masked = achievement.hidden && !record && !revealedMetrics.has(achievement.metric)
|
||||
const progress =
|
||||
achievement.metric === "max_ac_week_streak" ? weekStreak : metrics[achievement.metric]
|
||||
return {
|
||||
id: achievement.id,
|
||||
name: masked ? "???" : achievement.name,
|
||||
@@ -80,61 +107,100 @@ achievementRoutes.get("/achievements", requireAuth, async (c) => {
|
||||
unlockTime: record?.unlockTime ?? null,
|
||||
backfilled: record?.backfilled ?? false,
|
||||
progress: masked ? null : typeof progress === "number" ? progress : 0,
|
||||
unlockRate: active > 0 ? Math.round((achievement.unlockCount / active) * 1000) / 10 : 0,
|
||||
holders: achievement.unlockCount,
|
||||
classHolders: classHolders ? (classHolders.get(achievement.id) ?? 0) : null,
|
||||
} satisfies Achievement
|
||||
})
|
||||
const classBoard = target.id === me.id && target.className ? await buildClassBoard(target) : null
|
||||
return success(c, {
|
||||
username: target.username,
|
||||
achievements: result,
|
||||
classBoard,
|
||||
} satisfies AchievementList)
|
||||
})
|
||||
|
||||
achievementRoutes.get("/achievements/summary", requireAuth, async (c) => {
|
||||
const target = await resolveUser(c.req.query("username"), c.get("user")!.id)
|
||||
if (!target) return failure(c, 404, "user-not-found", "用户不存在")
|
||||
const [achievements, unlockedRows] = await Promise.all([
|
||||
db
|
||||
.select({ id: schema.achievement.id, rarity: schema.achievement.rarity })
|
||||
.from(schema.achievement)
|
||||
.where(eq(schema.achievement.visible, true)),
|
||||
/** 本班 = 同一个班级的学生(含学生管理员),停用的不算 */
|
||||
function classMembers(className: string) {
|
||||
return and(
|
||||
eq(schema.user.className, className),
|
||||
inArray(schema.user.adminType, [...STUDENT_ROLES]),
|
||||
eq(schema.user.isDisabled, false),
|
||||
)
|
||||
}
|
||||
|
||||
const CLASS_RECENT = 5
|
||||
const CLASS_MOST = 5
|
||||
|
||||
async function buildClassBoard(target: {
|
||||
id: number
|
||||
className: string | null
|
||||
}): Promise<AchievementClassBoard> {
|
||||
const className = target.className!
|
||||
const [recent, counts] = await Promise.all([
|
||||
db
|
||||
.select({
|
||||
record: schema.userAchievement,
|
||||
achievement: schema.achievement,
|
||||
username: schema.user.username,
|
||||
achievementId: schema.userAchievement.achievementId,
|
||||
unlockTime: schema.userAchievement.unlockTime,
|
||||
})
|
||||
.from(schema.userAchievement)
|
||||
.innerJoin(schema.user, eq(schema.user.id, schema.userAchievement.userId))
|
||||
.innerJoin(
|
||||
schema.achievement,
|
||||
eq(schema.userAchievement.achievementId, schema.achievement.id),
|
||||
eq(schema.achievement.id, schema.userAchievement.achievementId),
|
||||
)
|
||||
.where(
|
||||
and(eq(schema.userAchievement.userId, target.id), eq(schema.achievement.visible, true)),
|
||||
and(
|
||||
classMembers(className),
|
||||
eq(schema.achievement.visible, true),
|
||||
eq(schema.userAchievement.backfilled, false),
|
||||
),
|
||||
)
|
||||
.orderBy(desc(schema.userAchievement.unlockTime)),
|
||||
.orderBy(desc(schema.userAchievement.unlockTime))
|
||||
.limit(CLASS_RECENT),
|
||||
// 不计入排名的人(老师在排名里关掉的)也不进「成就最多」
|
||||
db
|
||||
.select({
|
||||
id: schema.user.id,
|
||||
username: schema.user.username,
|
||||
count: count(schema.achievement.id),
|
||||
last: max(schema.userAchievement.unlockTime),
|
||||
})
|
||||
.from(schema.user)
|
||||
.leftJoin(schema.userAchievement, eq(schema.userAchievement.userId, schema.user.id))
|
||||
.leftJoin(
|
||||
schema.achievement,
|
||||
and(
|
||||
eq(schema.achievement.id, schema.userAchievement.achievementId),
|
||||
eq(schema.achievement.visible, true),
|
||||
),
|
||||
)
|
||||
.where(and(classMembers(className), isNull(schema.user.rankHiddenAt)))
|
||||
.groupBy(schema.user.id, schema.user.username),
|
||||
])
|
||||
const labels = {
|
||||
bronze: "青铜",
|
||||
silver: "白银",
|
||||
gold: "黄金",
|
||||
platinum: "白金",
|
||||
// 一样多的名次相同;同名次里先拿到的(最后一个成就拿得早的)排前面
|
||||
counts.sort(
|
||||
(a, b) =>
|
||||
b.count - a.count ||
|
||||
(a.last ?? "").localeCompare(b.last ?? "") ||
|
||||
a.username.localeCompare(b.username),
|
||||
)
|
||||
const ranked = counts.map((row) => ({
|
||||
...row,
|
||||
rank: counts.findIndex((other) => other.count === row.count) + 1,
|
||||
}))
|
||||
const top = ranked.slice(0, CLASS_MOST)
|
||||
const mine = ranked.find((row) => row.id === target.id)
|
||||
return {
|
||||
className,
|
||||
recent,
|
||||
most: top.map((row) => ({ username: row.username, count: row.count, rank: row.rank })),
|
||||
me:
|
||||
mine && !top.some((row) => row.id === target.id)
|
||||
? { rank: mine.rank, count: mine.count }
|
||||
: null,
|
||||
}
|
||||
const rarities = ["bronze", "silver", "gold", "platinum"] as const
|
||||
const total = achievements.length
|
||||
const unlocked = unlockedRows.length
|
||||
return success(c, {
|
||||
username: target.username,
|
||||
total,
|
||||
unlocked,
|
||||
percent: total > 0 ? Math.round((unlocked / total) * 1000) / 10 : 0,
|
||||
rarity: rarities.map((rarity) => ({
|
||||
rarity,
|
||||
label: labels[rarity],
|
||||
total: achievements.filter((item) => item.rarity === rarity).length,
|
||||
unlocked: unlockedRows.filter((item) => item.achievement.rarity === rarity).length,
|
||||
})),
|
||||
recent: unlockedRows.slice(0, 10).map(pendingData),
|
||||
} satisfies AchievementSummary)
|
||||
})
|
||||
}
|
||||
|
||||
achievementRoutes.get("/achievements/pending", requireAuth, async (c) => {
|
||||
const rows = await db
|
||||
|
||||
Reference in new issue
Block a user