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

View File

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