refactor: 拆分核心玩法模块并优化代码质量
将 WechatGame 单体模块拆分为独立的 User、Level、GameConfig 模块, 新增体力值系统、关卡闯关流程,并修复多项代码质量问题: - 体力不足错误码从 401 修正为 400 - enterLevel 改用 findById 替代全表扫描 - consumeStamina 增加原子更新防止并发竞态 - 并行化独立数据库查询 (Promise.all) - 移除 WechatGameService/Controller 死代码
This commit is contained in:
@@ -26,4 +26,21 @@ export class UserRepository implements IUserRepository {
|
||||
async save(user: User): Promise<User> {
|
||||
return this.repository.save(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* 原子更新体力值,使用 WHERE 条件防止并发竞态。
|
||||
* 只有当 stamina 仍等于 expectedOldStamina 时才更新。
|
||||
*/
|
||||
async updateStaminaAtomic(
|
||||
userId: string,
|
||||
expectedOldStamina: number,
|
||||
newStamina: number,
|
||||
staminaUpdatedAt: Date,
|
||||
): Promise<{ affected: number }> {
|
||||
const result = await this.repository.update(
|
||||
{ id: userId, stamina: expectedOldStamina },
|
||||
{ stamina: newStamina, staminaUpdatedAt },
|
||||
);
|
||||
return { affected: result.affected ?? 0 };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user