feat(share): 更新分享关卡进度逻辑,优化时间限制检查
This commit is contained in:
@@ -10,6 +10,6 @@ NODE_ENV=production
|
|||||||
PORT=3000
|
PORT=3000
|
||||||
|
|
||||||
|
|
||||||
WX_APPID=wx0f9c909d20d19396
|
WX_APPID=wxaf07f8bb60098991
|
||||||
WX_SECRET=c5635680747cccf351f5f323c01178e6
|
WX_SECRET=b390c318fe83600e60fd4ad5d88b603f
|
||||||
JWT_SECRET=mp-xieyingen
|
JWT_SECRET=mp-xieyingen
|
||||||
@@ -10,7 +10,7 @@ import { validateEnvironment } from './env.validation';
|
|||||||
isGlobal: true,
|
isGlobal: true,
|
||||||
load: [databaseConfig],
|
load: [databaseConfig],
|
||||||
validate: validateEnvironment,
|
validate: validateEnvironment,
|
||||||
envFilePath: ['.env.local', '.env.production', '.env'],
|
envFilePath: ['.env', '.env.local', '.env.production'],
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
exports: [ConfigModule],
|
exports: [ConfigModule],
|
||||||
|
|||||||
@@ -122,7 +122,13 @@ export class ShareService {
|
|||||||
throw new NotFoundException('分享不存在或已过期');
|
throw new NotFoundException('分享不存在或已过期');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. 查找或创建 ShareParticipant
|
// 2. 查找关卡获取时间限制(提前查找,后面会用到)
|
||||||
|
const level = await this.levelRepository.findById(dto.levelId);
|
||||||
|
if (!level) {
|
||||||
|
throw new NotFoundException('关卡不存在');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 查找或创建 ShareParticipant
|
||||||
let participant =
|
let participant =
|
||||||
await this.shareParticipantRepository.findByShareConfigAndParticipant(
|
await this.shareParticipantRepository.findByShareConfigAndParticipant(
|
||||||
config.id,
|
config.id,
|
||||||
@@ -136,54 +142,34 @@ export class ShareService {
|
|||||||
participant = await this.shareParticipantRepository.save(participant);
|
participant = await this.shareParticipantRepository.save(participant);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. 如果 passed=true,检查是否已有通关记录
|
// 4. 查找现有进度
|
||||||
if (dto.passed) {
|
|
||||||
const existing =
|
|
||||||
await this.shareLevelProgressRepository.findByParticipantAndLevel(
|
|
||||||
participant.id,
|
|
||||||
dto.levelId,
|
|
||||||
);
|
|
||||||
if (existing?.passed) {
|
|
||||||
const existingLevel = await this.levelRepository.findById(dto.levelId);
|
|
||||||
if (!existingLevel) {
|
|
||||||
throw new NotFoundException('关卡不存在');
|
|
||||||
}
|
|
||||||
const wasWithinTimeLimit =
|
|
||||||
existingLevel.timeLimit === null ||
|
|
||||||
existing.timeSpent <= existingLevel.timeLimit;
|
|
||||||
return {
|
|
||||||
passed: true,
|
|
||||||
timeLimit: existingLevel.timeLimit,
|
|
||||||
withinTimeLimit: wasWithinTimeLimit,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 4. 查找关卡获取时间限制
|
|
||||||
const level = await this.levelRepository.findById(dto.levelId);
|
|
||||||
if (!level) {
|
|
||||||
throw new NotFoundException('关卡不存在');
|
|
||||||
}
|
|
||||||
|
|
||||||
// 5. 判断是否在时间限制内通过
|
|
||||||
const withinTimeLimit = dto.passed
|
|
||||||
? level.timeLimit === null || dto.timeSpent <= level.timeLimit
|
|
||||||
: false;
|
|
||||||
|
|
||||||
// 6. 创建或更新进度
|
|
||||||
let progress =
|
let progress =
|
||||||
await this.shareLevelProgressRepository.findByParticipantAndLevel(
|
await this.shareLevelProgressRepository.findByParticipantAndLevel(
|
||||||
participant.id,
|
participant.id,
|
||||||
dto.levelId,
|
dto.levelId,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// 5. 如果 passed=true 且已有通关记录,直接返回(不覆盖)
|
||||||
|
if (dto.passed && progress?.passed) {
|
||||||
|
const wasWithinTimeLimit =
|
||||||
|
level.timeLimit === null || progress.timeSpent <= level.timeLimit;
|
||||||
|
return {
|
||||||
|
passed: true,
|
||||||
|
timeLimit: level.timeLimit,
|
||||||
|
withinTimeLimit: wasWithinTimeLimit,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// 6. 判断是否在时间限制内通过
|
||||||
|
const withinTimeLimit = dto.passed
|
||||||
|
? level.timeLimit === null || dto.timeSpent <= level.timeLimit
|
||||||
|
: false;
|
||||||
|
|
||||||
|
// 7. 创建或更新进度
|
||||||
if (progress) {
|
if (progress) {
|
||||||
progress = this.shareLevelProgressRepository.create({
|
progress.passed = dto.passed;
|
||||||
...progress,
|
progress.timeSpent = dto.timeSpent;
|
||||||
passed: dto.passed,
|
progress.completedAt = dto.passed ? new Date() : progress.completedAt;
|
||||||
timeSpent: dto.timeSpent,
|
|
||||||
completedAt: dto.passed ? new Date() : progress.completedAt,
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
progress = this.shareLevelProgressRepository.create({
|
progress = this.shareLevelProgressRepository.create({
|
||||||
participantId: participant.id,
|
participantId: participant.id,
|
||||||
|
|||||||
Reference in New Issue
Block a user