Skip to content

Commit

Permalink
Remove clan scores when user leaves/kicked
Browse files Browse the repository at this point in the history
  • Loading branch information
Swan committed Aug 3, 2024
1 parent 5586c14 commit ff4a8f0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions db/scores.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,13 @@ func GetClanPlayerScoresOnMap(md5 string, clanId int) ([]*Score, error) {
return scores, nil
}

// RemoveUserClanScores Removes all user scores from a clan
func RemoveUserClanScores(clanId int, userId int) error {
return SQL.Model(&Score{}).
Where("user_id = ? AND clan_id = ?", userId, clanId).
Update("clan_id", nil).Error
}

// CalculateOverallRating Calculates overall rating from a list of scores
func CalculateOverallRating(scores []*Score) float64 {
if len(scores) == 0 {
Expand Down
8 changes: 8 additions & 0 deletions handlers/clan_members.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ func LeaveClan(c *gin.Context) *APIError {
return APIErrorServerError("Error inserting clan activity", err)
}

if err := db.RemoveUserClanScores(clan.Id, user.Id); err != nil {
return APIErrorServerError("Error removing user clan scores", err)
}

if err := db.PerformFullClanRecalculation(clan.Id); err != nil {
return APIErrorServerError("Error performing full clan recalc", err)
}
Expand Down Expand Up @@ -140,6 +144,10 @@ func KickClanMember(c *gin.Context) *APIError {
return APIErrorServerError("Error inserting clan kicked notification", err)
}

if err := db.RemoveUserClanScores(target.Clan.Id, target.TargetUser.Id); err != nil {
return APIErrorServerError("Error removing user clan scores", err)
}

if err := db.PerformFullClanRecalculation(target.Clan.Id); err != nil {
return APIErrorServerError("Error performing full clan recalc", err)
}
Expand Down

0 comments on commit ff4a8f0

Please sign in to comment.