@@ -52,25 +52,44 @@ func (s *ScoreService) ProcessScoreData(scoreData *model.ScoreData) error {
5252 // Continue processing even if this check fails
5353 }
5454
55- // If all rounds are done, update match status to completed (status 3)
56- if allDone {
57- err = s .repo .UpdateMatchStatus (scoreData .MatchID , 3 )
58- if err != nil {
59- logger .Log .Error ("Failed to update match status" , zap .Error (err ))
60- return fmt .Errorf ("error updating match status: %w" , err )
55+ shouldComplete := allDone
56+ completionReason := "all rounds completed"
57+ if ! shouldComplete {
58+ reachedLimit , checkErr := s .repo .HasTeamReachedRoundWinLimit (scoreData .MatchID )
59+ if checkErr != nil {
60+ logger .Log .Error ("Failed to determine if round win limit was reached" , zap .Error (checkErr ))
61+ } else if reachedLimit {
62+ shouldComplete = true
63+ completionReason = "round win limit reached"
6164 }
65+ }
6266
63- // Update scores again after marking match as complete
64- if err := s . updateScores ( scoreData .MatchID ); err != nil {
65- logger . Log . Error ( "Failed to update scores via external API after completion" , zap . Error ( err ))
66- return fmt . Errorf ( "error updating scores: %w" , err )
67+ if shouldComplete {
68+ logger . Log . Info ( "Marking match as completed" , zap . Int ( "matchID" , scoreData .MatchID ), zap . String ( "reason" , completionReason ))
69+ if err := s . completeMatch ( scoreData . MatchID ); err != nil {
70+ return err
6771 }
6872 }
6973
7074 logger .Log .Info ("Successfully processed score data" , zap .Int ("matchID" , scoreData .MatchID ), zap .Int ("roundID" , scoreData .RoundID ))
7175 return nil
7276}
7377
78+ // completeMatch finalizes a match by setting its status and notifying the league site
79+ func (s * ScoreService ) completeMatch (matchID int ) error {
80+ if err := s .repo .UpdateMatchStatus (matchID , 3 ); err != nil {
81+ logger .Log .Error ("Failed to update match status" , zap .Error (err ), zap .Int ("matchID" , matchID ))
82+ return fmt .Errorf ("error updating match status: %w" , err )
83+ }
84+
85+ if err := s .updateScores (matchID ); err != nil {
86+ logger .Log .Error ("Failed to update scores via external API after completion" , zap .Error (err ), zap .Int ("matchID" , matchID ))
87+ return fmt .Errorf ("error updating scores: %w" , err )
88+ }
89+
90+ return nil
91+ }
92+
7493// updateScores calls the external API to update scores
7594func (s * ScoreService ) updateScores (matchID int ) error {
7695 updateScoresURL := fmt .Sprintf ("https://udl.tf/leagues/matches/%d/update_scores" , matchID )
0 commit comments