fixed a bug

This commit is contained in:
2026-05-19 16:25:00 +08:00
parent 257cee6fb2
commit dec29186e0
2 changed files with 13 additions and 5 deletions

View File

@@ -1,13 +1,14 @@
import mongoose, { Document, Schema } from 'mongoose'; import mongoose, { Document, Schema } from 'mongoose';
export type SudokuDifficulty = 'easy' | 'medium' | 'hard'; export type SudokuDifficulty = 'easy' | 'medium' | 'hard';
export type SudokuGameMode = 'standard' | 'irregular';
export interface ISudokuRecord extends Document { export interface ISudokuRecord extends Document {
userId: mongoose.Types.ObjectId; userId: mongoose.Types.ObjectId;
username: string; username: string;
fullname: string; fullname: string;
difficulty: SudokuDifficulty; difficulty: SudokuDifficulty;
gameMode: 'standard' | 'irregular'; gameMode: SudokuGameMode;
timeSeconds: number; timeSeconds: number;
won: boolean; won: boolean;
createdAt: Date; createdAt: Date;
@@ -18,7 +19,7 @@ export interface ISudokuLeaderboardRecord {
userId: mongoose.Types.ObjectId; userId: mongoose.Types.ObjectId;
username: string; username: string;
fullname: string; fullname: string;
gameMode: 'standard' | 'irregular'; gameMode: SudokuGameMode;
bestTime: number; bestTime: number;
totalGames: number; totalGames: number;
wonGames: number; wonGames: number;
@@ -69,7 +70,7 @@ sudokuRecordSchema.index({ difficulty: 1, won: 1, gameMode: 1, timeSeconds: 1 })
sudokuRecordSchema.statics.getLeaderboard = function( sudokuRecordSchema.statics.getLeaderboard = function(
difficulty: SudokuDifficulty, difficulty: SudokuDifficulty,
gameMode: 'standard' | 'irregular' | 'all', gameMode: SudokuGameMode | 'all',
skipCount: number, skipCount: number,
pageSize: number pageSize: number
): mongoose.Aggregate<ISudokuLeaderboardRecord[]> { ): mongoose.Aggregate<ISudokuLeaderboardRecord[]> {
@@ -134,6 +135,7 @@ sudokuRecordSchema.statics.getLeaderboard = function(
interface SudokuRecordModel extends mongoose.Model<ISudokuRecord> { interface SudokuRecordModel extends mongoose.Model<ISudokuRecord> {
getLeaderboard( getLeaderboard(
difficulty: SudokuDifficulty, difficulty: SudokuDifficulty,
gameMode: SudokuGameMode | 'all',
skipCount: number, skipCount: number,
pageSize: number pageSize: number
): mongoose.Aggregate<ISudokuLeaderboardRecord[]>; ): mongoose.Aggregate<ISudokuLeaderboardRecord[]>;

View File

@@ -1,5 +1,5 @@
import express, { Request, Response } from 'express'; import express, { Request, Response } from 'express';
import { SudokuRecord, SudokuDifficulty } from '../models/SudokuRecord'; import { SudokuRecord, SudokuDifficulty, SudokuGameMode } from '../models/SudokuRecord';
import { auth } from '../middleware/auth'; import { auth } from '../middleware/auth';
const router = express.Router(); const router = express.Router();
@@ -7,7 +7,7 @@ const router = express.Router();
// 提交数独记录(需要登录) // 提交数独记录(需要登录)
router.post('/record', auth, async (req: Request, res: Response) => { router.post('/record', auth, async (req: Request, res: Response) => {
try { try {
const { difficulty, timeSeconds, won } = req.body; const { difficulty, timeSeconds, won, gameMode = 'standard' } = req.body;
if (!req.user?._id) { if (!req.user?._id) {
return res.status(401).json({ error: '未登录' }); return res.status(401).json({ error: '未登录' });
@@ -26,11 +26,17 @@ router.post('/record', auth, async (req: Request, res: Response) => {
return res.status(400).json({ error: '无效的游戏结果' }); return res.status(400).json({ error: '无效的游戏结果' });
} }
const validGameModes: SudokuGameMode[] = ['standard', 'irregular'];
if (!validGameModes.includes(gameMode)) {
return res.status(400).json({ error: '无效的游戏模式' });
}
const record = new SudokuRecord({ const record = new SudokuRecord({
userId: req.user._id, userId: req.user._id,
username: req.user.username, username: req.user.username,
fullname: req.user.fullname || req.user.username, fullname: req.user.fullname || req.user.username,
difficulty, difficulty,
gameMode,
timeSeconds, timeSeconds,
won won
}); });