Checkpoint: Tennis Training Hub v1.0 - 完整功能版本:用户名登录、AI训练计划生成、MediaPipe视频姿势识别、击球统计、挥拍速度分析、NTRP自动评分系统、训练进度追踪、视频库管理、AI矫正建议

这个提交包含在:
Manus
2026-03-14 07:41:43 -04:00
父节点 00d6319ffb
当前提交 36907d1110
修改 29 个文件,包含 4870 行新增228 行删除

查看文件

@@ -1,11 +1,18 @@
import { eq } from "drizzle-orm";
import { eq, desc, and, sql } from "drizzle-orm";
import { drizzle } from "drizzle-orm/mysql2";
import { InsertUser, users } from "../drizzle/schema";
import {
InsertUser, users,
usernameAccounts,
trainingPlans, InsertTrainingPlan,
trainingVideos, InsertTrainingVideo,
poseAnalyses, InsertPoseAnalysis,
trainingRecords, InsertTrainingRecord,
ratingHistory, InsertRatingHistory,
} from "../drizzle/schema";
import { ENV } from './_core/env';
let _db: ReturnType<typeof drizzle> | null = null;
// Lazily create the drizzle instance so local tooling can run without a DB.
export async function getDb() {
if (!_db && process.env.DATABASE_URL) {
try {
@@ -18,26 +25,19 @@ export async function getDb() {
return _db;
}
export async function upsertUser(user: InsertUser): Promise<void> {
if (!user.openId) {
throw new Error("User openId is required for upsert");
}
// ===== USER OPERATIONS =====
export async function upsertUser(user: InsertUser): Promise<void> {
if (!user.openId) throw new Error("User openId is required for upsert");
const db = await getDb();
if (!db) {
console.warn("[Database] Cannot upsert user: database not available");
return;
}
if (!db) { console.warn("[Database] Cannot upsert user: database not available"); return; }
try {
const values: InsertUser = {
openId: user.openId,
};
const values: InsertUser = { openId: user.openId };
const updateSet: Record<string, unknown> = {};
const textFields = ["name", "email", "loginMethod"] as const;
type TextField = (typeof textFields)[number];
const assignNullable = (field: TextField) => {
const value = user[field];
if (value === undefined) return;
@@ -45,48 +45,222 @@ export async function upsertUser(user: InsertUser): Promise<void> {
values[field] = normalized;
updateSet[field] = normalized;
};
textFields.forEach(assignNullable);
if (user.lastSignedIn !== undefined) {
values.lastSignedIn = user.lastSignedIn;
updateSet.lastSignedIn = user.lastSignedIn;
}
if (user.role !== undefined) {
values.role = user.role;
updateSet.role = user.role;
} else if (user.openId === ENV.ownerOpenId) {
values.role = 'admin';
updateSet.role = 'admin';
}
if (user.lastSignedIn !== undefined) { values.lastSignedIn = user.lastSignedIn; updateSet.lastSignedIn = user.lastSignedIn; }
if (user.role !== undefined) { values.role = user.role; updateSet.role = user.role; }
else if (user.openId === ENV.ownerOpenId) { values.role = 'admin'; updateSet.role = 'admin'; }
if (!values.lastSignedIn) values.lastSignedIn = new Date();
if (Object.keys(updateSet).length === 0) updateSet.lastSignedIn = new Date();
if (!values.lastSignedIn) {
values.lastSignedIn = new Date();
}
if (Object.keys(updateSet).length === 0) {
updateSet.lastSignedIn = new Date();
}
await db.insert(users).values(values).onDuplicateKeyUpdate({
set: updateSet,
});
} catch (error) {
console.error("[Database] Failed to upsert user:", error);
throw error;
}
await db.insert(users).values(values).onDuplicateKeyUpdate({ set: updateSet });
} catch (error) { console.error("[Database] Failed to upsert user:", error); throw error; }
}
export async function getUserByOpenId(openId: string) {
const db = await getDb();
if (!db) {
console.warn("[Database] Cannot get user: database not available");
return undefined;
}
if (!db) return undefined;
const result = await db.select().from(users).where(eq(users.openId, openId)).limit(1);
return result.length > 0 ? result[0] : undefined;
}
// TODO: add feature queries here as your schema grows.
export async function getUserByUsername(username: string) {
const db = await getDb();
if (!db) return undefined;
const result = await db.select().from(usernameAccounts).where(eq(usernameAccounts.username, username)).limit(1);
if (result.length === 0) return undefined;
const userResult = await db.select().from(users).where(eq(users.id, result[0].userId)).limit(1);
return userResult.length > 0 ? userResult[0] : undefined;
}
export async function createUsernameAccount(username: string): Promise<{ user: typeof users.$inferSelect; isNew: boolean }> {
const db = await getDb();
if (!db) throw new Error("Database not available");
// Check if username already exists
const existing = await db.select().from(usernameAccounts).where(eq(usernameAccounts.username, username)).limit(1);
if (existing.length > 0) {
const user = await db.select().from(users).where(eq(users.id, existing[0].userId)).limit(1);
if (user.length > 0) {
await db.update(users).set({ lastSignedIn: new Date() }).where(eq(users.id, user[0].id));
return { user: user[0], isNew: false };
}
}
// Create new user with username as openId
const openId = `username_${username}_${Date.now()}`;
await db.insert(users).values({
openId,
name: username,
loginMethod: "username",
lastSignedIn: new Date(),
ntrpRating: 1.5,
totalSessions: 0,
totalMinutes: 0,
});
const newUser = await db.select().from(users).where(eq(users.openId, openId)).limit(1);
if (newUser.length === 0) throw new Error("Failed to create user");
await db.insert(usernameAccounts).values({ username, userId: newUser[0].id });
return { user: newUser[0], isNew: true };
}
export async function updateUserProfile(userId: number, data: {
skillLevel?: "beginner" | "intermediate" | "advanced";
trainingGoals?: string;
ntrpRating?: number;
totalSessions?: number;
totalMinutes?: number;
}) {
const db = await getDb();
if (!db) return;
await db.update(users).set(data).where(eq(users.id, userId));
}
// ===== TRAINING PLAN OPERATIONS =====
export async function createTrainingPlan(plan: InsertTrainingPlan) {
const db = await getDb();
if (!db) throw new Error("Database not available");
// Deactivate existing active plans
await db.update(trainingPlans).set({ isActive: 0 }).where(and(eq(trainingPlans.userId, plan.userId), eq(trainingPlans.isActive, 1)));
const result = await db.insert(trainingPlans).values(plan);
return result[0].insertId;
}
export async function getUserTrainingPlans(userId: number) {
const db = await getDb();
if (!db) return [];
return db.select().from(trainingPlans).where(eq(trainingPlans.userId, userId)).orderBy(desc(trainingPlans.createdAt));
}
export async function getActivePlan(userId: number) {
const db = await getDb();
if (!db) return undefined;
const result = await db.select().from(trainingPlans).where(and(eq(trainingPlans.userId, userId), eq(trainingPlans.isActive, 1))).limit(1);
return result.length > 0 ? result[0] : undefined;
}
export async function updateTrainingPlan(planId: number, data: Partial<InsertTrainingPlan>) {
const db = await getDb();
if (!db) return;
await db.update(trainingPlans).set(data).where(eq(trainingPlans.id, planId));
}
// ===== VIDEO OPERATIONS =====
export async function createVideo(video: InsertTrainingVideo) {
const db = await getDb();
if (!db) throw new Error("Database not available");
const result = await db.insert(trainingVideos).values(video);
return result[0].insertId;
}
export async function getUserVideos(userId: number) {
const db = await getDb();
if (!db) return [];
return db.select().from(trainingVideos).where(eq(trainingVideos.userId, userId)).orderBy(desc(trainingVideos.createdAt));
}
export async function getVideoById(videoId: number) {
const db = await getDb();
if (!db) return undefined;
const result = await db.select().from(trainingVideos).where(eq(trainingVideos.id, videoId)).limit(1);
return result.length > 0 ? result[0] : undefined;
}
export async function updateVideoStatus(videoId: number, status: "pending" | "analyzing" | "completed" | "failed") {
const db = await getDb();
if (!db) return;
await db.update(trainingVideos).set({ analysisStatus: status }).where(eq(trainingVideos.id, videoId));
}
// ===== POSE ANALYSIS OPERATIONS =====
export async function createPoseAnalysis(analysis: InsertPoseAnalysis) {
const db = await getDb();
if (!db) throw new Error("Database not available");
const result = await db.insert(poseAnalyses).values(analysis);
return result[0].insertId;
}
export async function getAnalysisByVideoId(videoId: number) {
const db = await getDb();
if (!db) return undefined;
const result = await db.select().from(poseAnalyses).where(eq(poseAnalyses.videoId, videoId)).orderBy(desc(poseAnalyses.createdAt)).limit(1);
return result.length > 0 ? result[0] : undefined;
}
export async function getUserAnalyses(userId: number) {
const db = await getDb();
if (!db) return [];
return db.select().from(poseAnalyses).where(eq(poseAnalyses.userId, userId)).orderBy(desc(poseAnalyses.createdAt));
}
// ===== TRAINING RECORD OPERATIONS =====
export async function createTrainingRecord(record: InsertTrainingRecord) {
const db = await getDb();
if (!db) throw new Error("Database not available");
const result = await db.insert(trainingRecords).values(record);
return result[0].insertId;
}
export async function getUserTrainingRecords(userId: number, limit = 50) {
const db = await getDb();
if (!db) return [];
return db.select().from(trainingRecords).where(eq(trainingRecords.userId, userId)).orderBy(desc(trainingRecords.trainingDate)).limit(limit);
}
export async function markRecordCompleted(recordId: number, poseScore?: number) {
const db = await getDb();
if (!db) return;
await db.update(trainingRecords).set({ completed: 1, poseScore: poseScore ?? null }).where(eq(trainingRecords.id, recordId));
}
// ===== RATING HISTORY OPERATIONS =====
export async function createRatingEntry(entry: InsertRatingHistory) {
const db = await getDb();
if (!db) throw new Error("Database not available");
const result = await db.insert(ratingHistory).values(entry);
return result[0].insertId;
}
export async function getUserRatingHistory(userId: number, limit = 30) {
const db = await getDb();
if (!db) return [];
return db.select().from(ratingHistory).where(eq(ratingHistory.userId, userId)).orderBy(desc(ratingHistory.createdAt)).limit(limit);
}
// ===== STATS HELPERS =====
export async function getUserStats(userId: number) {
const db = await getDb();
if (!db) return null;
const [userRow] = await db.select().from(users).where(eq(users.id, userId)).limit(1);
if (!userRow) return null;
const analyses = await db.select().from(poseAnalyses).where(eq(poseAnalyses.userId, userId));
const records = await db.select().from(trainingRecords).where(eq(trainingRecords.userId, userId));
const videos = await db.select().from(trainingVideos).where(eq(trainingVideos.userId, userId));
const ratings = await db.select().from(ratingHistory).where(eq(ratingHistory.userId, userId)).orderBy(desc(ratingHistory.createdAt)).limit(30);
const completedRecords = records.filter(r => r.completed === 1);
const totalShots = analyses.reduce((sum, a) => sum + (a.shotCount || 0), 0);
const avgScore = analyses.length > 0 ? analyses.reduce((sum, a) => sum + (a.overallScore || 0), 0) / analyses.length : 0;
return {
ntrpRating: userRow.ntrpRating || 1.5,
totalSessions: completedRecords.length,
totalMinutes: records.reduce((sum, r) => sum + (r.durationMinutes || 0), 0),
totalVideos: videos.length,
analyzedVideos: videos.filter(v => v.analysisStatus === "completed").length,
totalShots,
averageScore: Math.round(avgScore * 10) / 10,
ratingHistory: ratings.reverse(),
recentAnalyses: analyses.slice(0, 10),
};
}

237
server/features.test.ts 普通文件
查看文件

@@ -0,0 +1,237 @@
import { describe, expect, it, vi, beforeEach } from "vitest";
import { appRouter } from "./routers";
import { COOKIE_NAME } from "../shared/const";
import type { TrpcContext } from "./_core/context";
type AuthenticatedUser = NonNullable<TrpcContext["user"]>;
function createTestUser(overrides?: Partial<AuthenticatedUser>): AuthenticatedUser {
return {
id: 1,
openId: "test-user-001",
email: "test@example.com",
name: "TestPlayer",
loginMethod: "username",
role: "user",
skillLevel: "beginner",
trainingGoals: null,
ntrpRating: 1.5,
totalSessions: 0,
totalMinutes: 0,
createdAt: new Date(),
updatedAt: new Date(),
lastSignedIn: new Date(),
...overrides,
};
}
function createMockContext(user: AuthenticatedUser | null = null): {
ctx: TrpcContext;
clearedCookies: { name: string; options: Record<string, unknown> }[];
setCookies: { name: string; value: string; options: Record<string, unknown> }[];
} {
const clearedCookies: { name: string; options: Record<string, unknown> }[] = [];
const setCookies: { name: string; value: string; options: Record<string, unknown> }[] = [];
return {
ctx: {
user,
req: {
protocol: "https",
headers: {},
} as TrpcContext["req"],
res: {
clearCookie: (name: string, options: Record<string, unknown>) => {
clearedCookies.push({ name, options });
},
cookie: (name: string, value: string, options: Record<string, unknown>) => {
setCookies.push({ name, value, options });
},
} as TrpcContext["res"],
},
clearedCookies,
setCookies,
};
}
describe("auth.me", () => {
it("returns null for unauthenticated users", async () => {
const { ctx } = createMockContext(null);
const caller = appRouter.createCaller(ctx);
const result = await caller.auth.me();
expect(result).toBeNull();
});
it("returns user data for authenticated users", async () => {
const user = createTestUser();
const { ctx } = createMockContext(user);
const caller = appRouter.createCaller(ctx);
const result = await caller.auth.me();
expect(result).toBeDefined();
expect(result?.name).toBe("TestPlayer");
expect(result?.openId).toBe("test-user-001");
});
});
describe("auth.logout", () => {
it("clears the session cookie and reports success", async () => {
const user = createTestUser();
const { ctx, clearedCookies } = createMockContext(user);
const caller = appRouter.createCaller(ctx);
const result = await caller.auth.logout();
expect(result).toEqual({ success: true });
expect(clearedCookies).toHaveLength(1);
expect(clearedCookies[0]?.name).toBe(COOKIE_NAME);
expect(clearedCookies[0]?.options).toMatchObject({
maxAge: -1,
secure: true,
sameSite: "none",
httpOnly: true,
path: "/",
});
});
});
describe("profile.stats", () => {
it("requires authentication", async () => {
const { ctx } = createMockContext(null);
const caller = appRouter.createCaller(ctx);
await expect(caller.profile.stats()).rejects.toThrow();
});
});
describe("plan.generate input validation", () => {
it("rejects invalid skill level", async () => {
const user = createTestUser();
const { ctx } = createMockContext(user);
const caller = appRouter.createCaller(ctx);
await expect(
caller.plan.generate({
skillLevel: "expert" as any,
durationDays: 7,
})
).rejects.toThrow();
});
it("rejects invalid duration", async () => {
const user = createTestUser();
const { ctx } = createMockContext(user);
const caller = appRouter.createCaller(ctx);
await expect(
caller.plan.generate({
skillLevel: "beginner",
durationDays: 0,
})
).rejects.toThrow();
});
it("rejects duration over 30", async () => {
const user = createTestUser();
const { ctx } = createMockContext(user);
const caller = appRouter.createCaller(ctx);
await expect(
caller.plan.generate({
skillLevel: "beginner",
durationDays: 31,
})
).rejects.toThrow();
});
});
describe("video.upload input validation", () => {
it("requires authentication", async () => {
const { ctx } = createMockContext(null);
const caller = appRouter.createCaller(ctx);
await expect(
caller.video.upload({
title: "test",
format: "mp4",
fileSize: 1000,
fileBase64: "dGVzdA==",
})
).rejects.toThrow();
});
});
describe("analysis.save input validation", () => {
it("requires authentication", async () => {
const { ctx } = createMockContext(null);
const caller = appRouter.createCaller(ctx);
await expect(
caller.analysis.save({
videoId: 1,
overallScore: 75,
})
).rejects.toThrow();
});
});
describe("analysis.getCorrections input validation", () => {
it("requires authentication", async () => {
const { ctx } = createMockContext(null);
const caller = appRouter.createCaller(ctx);
await expect(
caller.analysis.getCorrections({
poseMetrics: {},
exerciseType: "forehand",
detectedIssues: [],
})
).rejects.toThrow();
});
});
describe("record.create input validation", () => {
it("requires authentication", async () => {
const { ctx } = createMockContext(null);
const caller = appRouter.createCaller(ctx);
await expect(
caller.record.create({
exerciseName: "正手挥拍",
durationMinutes: 30,
})
).rejects.toThrow();
});
it("accepts valid exercise name", async () => {
const user = createTestUser();
const { ctx } = createMockContext(user);
const caller = appRouter.createCaller(ctx);
// This should not throw on input validation (may throw on DB)
// We just verify the input schema accepts a valid name
try {
await caller.record.create({
exerciseName: "正手挥拍",
durationMinutes: 30,
});
} catch (e: any) {
// DB errors are expected in test env, but input validation should pass
expect(e.message).not.toContain("invalid_type");
}
});
});
describe("rating.history", () => {
it("requires authentication", async () => {
const { ctx } = createMockContext(null);
const caller = appRouter.createCaller(ctx);
await expect(caller.rating.history()).rejects.toThrow();
});
});
describe("rating.current", () => {
it("requires authentication", async () => {
const { ctx } = createMockContext(null);
const caller = appRouter.createCaller(ctx);
await expect(caller.rating.current()).rejects.toThrow();
});
});

查看文件

@@ -1,28 +1,479 @@
import { COOKIE_NAME } from "@shared/const";
import { COOKIE_NAME, ONE_YEAR_MS } from "@shared/const";
import { getSessionCookieOptions } from "./_core/cookies";
import { systemRouter } from "./_core/systemRouter";
import { publicProcedure, router } from "./_core/trpc";
import { publicProcedure, protectedProcedure, router } from "./_core/trpc";
import { z } from "zod";
import { sdk } from "./_core/sdk";
import { invokeLLM } from "./_core/llm";
import { storagePut } from "./storage";
import * as db from "./db";
import { nanoid } from "nanoid";
export const appRouter = router({
// if you need to use socket.io, read and register route in server/_core/index.ts, all api should start with '/api/' so that the gateway can route correctly
system: systemRouter,
auth: router({
me: publicProcedure.query(opts => opts.ctx.user),
logout: publicProcedure.mutation(({ ctx }) => {
const cookieOptions = getSessionCookieOptions(ctx.req);
ctx.res.clearCookie(COOKIE_NAME, { ...cookieOptions, maxAge: -1 });
return {
success: true,
} as const;
return { success: true } as const;
}),
// Username-based login
loginWithUsername: publicProcedure
.input(z.object({ username: z.string().min(1).max(64) }))
.mutation(async ({ ctx, input }) => {
const { user, isNew } = await db.createUsernameAccount(input.username);
const sessionToken = await sdk.createSessionToken(user.openId, {
name: user.name || input.username,
expiresInMs: ONE_YEAR_MS,
});
const cookieOptions = getSessionCookieOptions(ctx.req);
ctx.res.cookie(COOKIE_NAME, sessionToken, { ...cookieOptions, maxAge: ONE_YEAR_MS });
return { user, isNew };
}),
}),
// User profile management
profile: router({
update: protectedProcedure
.input(z.object({
skillLevel: z.enum(["beginner", "intermediate", "advanced"]).optional(),
trainingGoals: z.string().optional(),
}))
.mutation(async ({ ctx, input }) => {
await db.updateUserProfile(ctx.user.id, input);
return { success: true };
}),
stats: protectedProcedure.query(async ({ ctx }) => {
return db.getUserStats(ctx.user.id);
}),
}),
// TODO: add feature routers here, e.g.
// todo: router({
// list: protectedProcedure.query(({ ctx }) =>
// db.getUserTodos(ctx.user.id)
// ),
// }),
// Training plan management
plan: router({
generate: protectedProcedure
.input(z.object({
skillLevel: z.enum(["beginner", "intermediate", "advanced"]),
durationDays: z.number().min(1).max(30).default(7),
focusAreas: z.array(z.string()).optional(),
}))
.mutation(async ({ ctx, input }) => {
const user = ctx.user;
// Get user's recent analyses for personalization
const analyses = await db.getUserAnalyses(user.id);
const recentScores = analyses.slice(0, 5).map(a => ({
score: a.overallScore,
issues: a.detectedIssues,
exerciseType: a.exerciseType,
shotCount: a.shotCount,
strokeConsistency: a.strokeConsistency,
footworkScore: a.footworkScore,
}));
const prompt = `你是一位专业网球教练。请为一位${
input.skillLevel === "beginner" ? "初级" : input.skillLevel === "intermediate" ? "中级" : "高级"
}水平的网球学员生成一个${input.durationDays}天的在家训练计划。
要求:
- 只需要球拍,不需要球场和球网
- 包含影子挥拍、墙壁练习、脚步移动、体能训练等
- 每天训练30-60分钟
${input.focusAreas?.length ? `- 重点关注: ${input.focusAreas.join(", ")}` : ""}
${recentScores.length > 0 ? `- 用户最近的分析数据: ${JSON.stringify(recentScores)}` : ""}
请返回JSON格式,包含每天的训练内容。`;
const response = await invokeLLM({
messages: [
{ role: "system", content: "你是专业网球教练AI助手。返回严格的JSON格式。" },
{ role: "user", content: prompt },
],
response_format: {
type: "json_schema",
json_schema: {
name: "training_plan",
strict: true,
schema: {
type: "object",
properties: {
title: { type: "string", description: "训练计划标题" },
exercises: {
type: "array",
items: {
type: "object",
properties: {
day: { type: "number" },
name: { type: "string" },
category: { type: "string" },
duration: { type: "number", description: "分钟" },
description: { type: "string" },
tips: { type: "string" },
sets: { type: "number" },
reps: { type: "number" },
},
required: ["day", "name", "category", "duration", "description", "tips", "sets", "reps"],
additionalProperties: false,
},
},
},
required: ["title", "exercises"],
additionalProperties: false,
},
},
},
});
const content = response.choices[0]?.message?.content;
const parsed = typeof content === "string" ? JSON.parse(content) : null;
if (!parsed) throw new Error("Failed to generate training plan");
const planId = await db.createTrainingPlan({
userId: user.id,
title: parsed.title,
skillLevel: input.skillLevel,
durationDays: input.durationDays,
exercises: parsed.exercises,
isActive: 1,
version: 1,
});
return { planId, plan: parsed };
}),
list: protectedProcedure.query(async ({ ctx }) => {
return db.getUserTrainingPlans(ctx.user.id);
}),
active: protectedProcedure.query(async ({ ctx }) => {
return db.getActivePlan(ctx.user.id);
}),
adjust: protectedProcedure
.input(z.object({ planId: z.number() }))
.mutation(async ({ ctx, input }) => {
const analyses = await db.getUserAnalyses(ctx.user.id);
const recentAnalyses = analyses.slice(0, 5);
const currentPlan = (await db.getUserTrainingPlans(ctx.user.id)).find(p => p.id === input.planId);
if (!currentPlan) throw new Error("Plan not found");
const prompt = `基于以下用户的姿势分析结果,调整训练计划:
当前计划: ${JSON.stringify(currentPlan.exercises)}
最近分析结果: ${JSON.stringify(recentAnalyses.map(a => ({
score: a.overallScore,
issues: a.detectedIssues,
corrections: a.corrections,
shotCount: a.shotCount,
strokeConsistency: a.strokeConsistency,
footworkScore: a.footworkScore,
fluidityScore: a.fluidityScore,
})))}
请根据分析结果调整训练计划,增加针对薄弱环节的训练,返回与原计划相同格式的JSON。`;
const response = await invokeLLM({
messages: [
{ role: "system", content: "你是专业网球教练AI助手。返回严格的JSON格式。" },
{ role: "user", content: prompt },
],
response_format: {
type: "json_schema",
json_schema: {
name: "adjusted_plan",
strict: true,
schema: {
type: "object",
properties: {
title: { type: "string" },
adjustmentNotes: { type: "string", description: "调整说明" },
exercises: {
type: "array",
items: {
type: "object",
properties: {
day: { type: "number" },
name: { type: "string" },
category: { type: "string" },
duration: { type: "number" },
description: { type: "string" },
tips: { type: "string" },
sets: { type: "number" },
reps: { type: "number" },
},
required: ["day", "name", "category", "duration", "description", "tips", "sets", "reps"],
additionalProperties: false,
},
},
},
required: ["title", "adjustmentNotes", "exercises"],
additionalProperties: false,
},
},
},
});
const content = response.choices[0]?.message?.content;
const parsed = typeof content === "string" ? JSON.parse(content) : null;
if (!parsed) throw new Error("Failed to adjust plan");
await db.updateTrainingPlan(input.planId, {
exercises: parsed.exercises,
adjustmentNotes: parsed.adjustmentNotes,
version: (currentPlan.version || 1) + 1,
});
return { success: true, adjustmentNotes: parsed.adjustmentNotes };
}),
}),
// Video management
video: router({
upload: protectedProcedure
.input(z.object({
title: z.string(),
format: z.string(),
fileSize: z.number(),
exerciseType: z.string().optional(),
fileBase64: z.string(),
}))
.mutation(async ({ ctx, input }) => {
const fileBuffer = Buffer.from(input.fileBase64, "base64");
const fileKey = `videos/${ctx.user.id}/${nanoid()}.${input.format}`;
const contentType = input.format === "webm" ? "video/webm" : "video/mp4";
const { url } = await storagePut(fileKey, fileBuffer, contentType);
const videoId = await db.createVideo({
userId: ctx.user.id,
title: input.title,
fileKey,
url,
format: input.format,
fileSize: input.fileSize,
exerciseType: input.exerciseType || null,
analysisStatus: "pending",
});
return { videoId, url };
}),
list: protectedProcedure.query(async ({ ctx }) => {
return db.getUserVideos(ctx.user.id);
}),
get: protectedProcedure
.input(z.object({ videoId: z.number() }))
.query(async ({ input }) => {
return db.getVideoById(input.videoId);
}),
updateStatus: protectedProcedure
.input(z.object({
videoId: z.number(),
status: z.enum(["pending", "analyzing", "completed", "failed"]),
}))
.mutation(async ({ input }) => {
await db.updateVideoStatus(input.videoId, input.status);
return { success: true };
}),
}),
// Pose analysis
analysis: router({
save: protectedProcedure
.input(z.object({
videoId: z.number(),
overallScore: z.number().optional(),
poseMetrics: z.any().optional(),
detectedIssues: z.any().optional(),
corrections: z.any().optional(),
exerciseType: z.string().optional(),
framesAnalyzed: z.number().optional(),
shotCount: z.number().optional(),
avgSwingSpeed: z.number().optional(),
maxSwingSpeed: z.number().optional(),
totalMovementDistance: z.number().optional(),
strokeConsistency: z.number().optional(),
footworkScore: z.number().optional(),
fluidityScore: z.number().optional(),
keyMoments: z.any().optional(),
movementTrajectory: z.any().optional(),
}))
.mutation(async ({ ctx, input }) => {
const analysisId = await db.createPoseAnalysis({
...input,
userId: ctx.user.id,
});
await db.updateVideoStatus(input.videoId, "completed");
// Auto-update NTRP rating after analysis
await recalculateNTRPRating(ctx.user.id, analysisId);
return { analysisId };
}),
getByVideo: protectedProcedure
.input(z.object({ videoId: z.number() }))
.query(async ({ input }) => {
return db.getAnalysisByVideoId(input.videoId);
}),
list: protectedProcedure.query(async ({ ctx }) => {
return db.getUserAnalyses(ctx.user.id);
}),
// Generate AI correction suggestions
getCorrections: protectedProcedure
.input(z.object({
poseMetrics: z.any(),
exerciseType: z.string(),
detectedIssues: z.any(),
}))
.mutation(async ({ input }) => {
const response = await invokeLLM({
messages: [
{
role: "system",
content: "你是一位专业网球教练。根据MediaPipe姿势分析数据,给出具体的姿势矫正建议。用中文回答。",
},
{
role: "user",
content: `分析以下网球动作数据并给出矫正建议:
动作类型: ${input.exerciseType}
姿势指标: ${JSON.stringify(input.poseMetrics)}
检测到的问题: ${JSON.stringify(input.detectedIssues)}
请给出:
1. 每个问题的具体矫正方法
2. 推荐的练习动作
3. 需要注意的关键点`,
},
],
});
return {
corrections: response.choices[0]?.message?.content || "暂无建议",
};
}),
}),
// Training records
record: router({
create: protectedProcedure
.input(z.object({
planId: z.number().optional(),
exerciseName: z.string(),
durationMinutes: z.number().optional(),
notes: z.string().optional(),
poseScore: z.number().optional(),
}))
.mutation(async ({ ctx, input }) => {
const recordId = await db.createTrainingRecord({
userId: ctx.user.id,
...input,
completed: 0,
});
return { recordId };
}),
complete: protectedProcedure
.input(z.object({
recordId: z.number(),
poseScore: z.number().optional(),
}))
.mutation(async ({ ctx, input }) => {
await db.markRecordCompleted(input.recordId, input.poseScore);
// Update user stats
const records = await db.getUserTrainingRecords(ctx.user.id, 1000);
const completed = records.filter(r => r.completed === 1);
const totalMinutes = records.reduce((sum, r) => sum + (r.durationMinutes || 0), 0);
await db.updateUserProfile(ctx.user.id, {
totalSessions: completed.length,
totalMinutes,
});
return { success: true };
}),
list: protectedProcedure
.input(z.object({ limit: z.number().default(50) }).optional())
.query(async ({ ctx, input }) => {
return db.getUserTrainingRecords(ctx.user.id, input?.limit || 50);
}),
}),
// Rating system
rating: router({
history: protectedProcedure.query(async ({ ctx }) => {
return db.getUserRatingHistory(ctx.user.id);
}),
current: protectedProcedure.query(async ({ ctx }) => {
const user = await db.getUserByOpenId(ctx.user.openId);
return { rating: user?.ntrpRating || 1.5 };
}),
}),
});
// NTRP Rating calculation function
async function recalculateNTRPRating(userId: number, latestAnalysisId: number) {
const analyses = await db.getUserAnalyses(userId);
if (analyses.length === 0) return;
// Weight recent analyses more heavily
const weightedScores = analyses.slice(0, 20).map((a, i) => {
const weight = Math.max(0.3, 1 - i * 0.05); // Recent = higher weight
return {
overallScore: (a.overallScore || 0) * weight,
strokeConsistency: (a.strokeConsistency || 0) * weight,
footworkScore: (a.footworkScore || 0) * weight,
fluidityScore: (a.fluidityScore || 0) * weight,
shotCount: (a.shotCount || 0) * weight,
avgSwingSpeed: (a.avgSwingSpeed || 0) * weight,
weight,
};
});
const totalWeight = weightedScores.reduce((sum, s) => sum + s.weight, 0);
const dimensions = {
poseAccuracy: weightedScores.reduce((sum, s) => sum + s.overallScore, 0) / totalWeight,
strokeConsistency: weightedScores.reduce((sum, s) => sum + s.strokeConsistency, 0) / totalWeight,
footwork: weightedScores.reduce((sum, s) => sum + s.footworkScore, 0) / totalWeight,
fluidity: weightedScores.reduce((sum, s) => sum + s.fluidityScore, 0) / totalWeight,
power: Math.min(100, weightedScores.reduce((sum, s) => sum + s.avgSwingSpeed, 0) / totalWeight * 5),
};
// Convert 0-100 scores to NTRP 1.0-5.0
// NTRP mapping: 0-20 → 1.0-1.5, 20-40 → 1.5-2.5, 40-60 → 2.5-3.5, 60-80 → 3.5-4.5, 80-100 → 4.5-5.0
const avgDimension = (
dimensions.poseAccuracy * 0.30 +
dimensions.strokeConsistency * 0.25 +
dimensions.footwork * 0.20 +
dimensions.fluidity * 0.15 +
dimensions.power * 0.10
);
let ntrpRating: number;
if (avgDimension <= 20) ntrpRating = 1.0 + (avgDimension / 20) * 0.5;
else if (avgDimension <= 40) ntrpRating = 1.5 + ((avgDimension - 20) / 20) * 1.0;
else if (avgDimension <= 60) ntrpRating = 2.5 + ((avgDimension - 40) / 20) * 1.0;
else if (avgDimension <= 80) ntrpRating = 3.5 + ((avgDimension - 60) / 20) * 1.0;
else ntrpRating = 4.5 + ((avgDimension - 80) / 20) * 0.5;
ntrpRating = Math.round(ntrpRating * 10) / 10;
ntrpRating = Math.max(1.0, Math.min(5.0, ntrpRating));
// Save rating history
await db.createRatingEntry({
userId,
rating: ntrpRating,
reason: `基于${analyses.length}次视频分析自动评分`,
dimensionScores: dimensions,
analysisId: latestAnalysisId,
});
// Update user's current rating
await db.updateUserProfile(userId, { ntrpRating });
}
export type AppRouter = typeof appRouter;