@@ -8,6 +8,8 @@ import { XpLevelService } from '../../users/providers/xp-level.service';
88import { User } from '../../users/user.entity' ;
99import { DailyQuest } from '../../quests/entities/daily-quest.entity' ;
1010import { getPointsByDifficulty } from '../../puzzles/enums/puzzle-difficulty.enum' ;
11+ import { ScoreService } from '../../score/providers/score.service' ;
12+
1113
1214export interface AnswerValidationResult {
1315 isCorrect : boolean ;
@@ -32,6 +34,7 @@ export class ProgressCalculationProvider {
3234 private readonly userRepository : Repository < User > ,
3335 @InjectRepository ( DailyQuest )
3436 private readonly dailyQuestRepository : Repository < DailyQuest > ,
37+ private readonly scoreService : ScoreService ,
3538 ) { }
3639
3740 /**
@@ -84,20 +87,7 @@ export class ProgressCalculationProvider {
8487 ) ;
8588 }
8689
87- /**
88- * Calculates level based on total XP
89- */
90- calculateLevel ( totalXP : number ) : number {
91- if ( totalXP < 1000 ) return 1 ;
92- if ( totalXP < 2500 ) return 2 ;
93- if ( totalXP < 5000 ) return 3 ;
94- if ( totalXP < 10000 ) return 4 ;
95-
96- // Level 5+: Exponential scaling: 10000 + (level - 4) * some_growth
97- // Simplified: level 5 starts at 10000, each level after adds 5000+
98- return Math . floor ( ( totalXP - 10000 ) / 5000 ) + 5 ;
99- }
100-
90+
10191 /**
10292 * Processes answer submission and creates user progress record
10393 */
@@ -135,11 +125,18 @@ export class ProgressCalculationProvider {
135125 ) ;
136126
137127 // Calculate points
138- let pointsEarned = this . calculatePoints (
139- puzzle ,
140- submitAnswerDto . timeSpent ,
141- validation . isCorrect ,
142- ) ;
128+ const basePoints = this . calculatePoints (
129+ puzzle ,
130+ submitAnswerDto . timeSpent ,
131+ validation . isCorrect ,
132+ ) ;
133+
134+ const scoreResult = this . scoreService . calculateScore ( {
135+ correct : validation . isCorrect ,
136+ basePoints,
137+ } ) ;
138+
139+ let pointsEarned = scoreResult . score ;
143140
144141 // Fetch user and apply streak bonus
145142 const user = await this . userRepository . findOne ( {
@@ -148,20 +145,20 @@ export class ProgressCalculationProvider {
148145 } ) ;
149146
150147 if ( user && validation . isCorrect ) {
151- const streakCount = user . streak ?. currentStreak || 0 ;
152- let streakMultiplier = 0 ;
153- if ( streakCount >= 7 ) {
154- streakMultiplier = 0.25 ;
155- } else if ( streakCount >= 3 ) {
156- streakMultiplier = 0.1 ;
157- }
158- pointsEarned = Math . round ( pointsEarned * ( 1 + streakMultiplier ) ) ;
148+ const streakCount = user . streak ?. currentStreak || 0 ;
159149
160- // Update User XP and Level
161- user . xp += pointsEarned ;
162- user . level = this . calculateLevel ( user . xp ) ;
163- await this . userRepository . save ( user ) ;
164- }
150+ let streakMultiplier = 0 ;
151+
152+ if ( streakCount >= 7 ) {
153+ streakMultiplier = 0.25 ;
154+ } else if ( streakCount >= 3 ) {
155+ streakMultiplier = 0.1 ;
156+ }
157+
158+ pointsEarned = Math . round (
159+ pointsEarned * ( 1 + streakMultiplier ) ,
160+ ) ;
161+ }
165162
166163 validation . pointsEarned = pointsEarned ;
167164
@@ -195,12 +192,10 @@ export class ProgressCalculationProvider {
195192 dailyQuest . completedAt = new Date ( ) ;
196193 // Award bonus XP for daily quest completion (e.g., 50 XP as hinted in "completion screen")
197194 if ( user ) {
198- user . xp += 50 ;
199- user . level = this . calculateLevel ( user . xp ) ;
200- await this . userRepository . save ( user ) ;
195+ await this . xpLevelService . addXp ( user . id , 50 ) ;
196+ }
201197 }
202- }
203- await this . dailyQuestRepository . save ( dailyQuest ) ;
198+ await this . dailyQuestRepository . save ( dailyQuest ) ;
204199 }
205200 }
206201 }
0 commit comments