Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 0 additions & 68 deletions backend/controllers/profile_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"log"
"math"
"net/http"
"net/url"
"strings"
Expand All @@ -19,18 +18,6 @@ import (
"go.mongodb.org/mongo-driver/mongo/options"
)

// Calculate new Elo ratings using float64
func calculateEloRating(ratingA, ratingB float64, scoreA float64) (newRatingA, newRatingB float64) {
const K = 32.0
expectedA := 1.0 / (1.0 + math.Pow(10, (ratingB-ratingA)/400.0))
expectedB := 1.0 - expectedA
scoreB := 1.0 - scoreA

newRatingA = ratingA + K*(scoreA-expectedA)
newRatingB = ratingB + K*(scoreB-expectedB)
return newRatingA, newRatingB
}

func extractNameFromEmail(email string) string {
for i, char := range email {
if char == '@' {
Expand Down Expand Up @@ -348,58 +335,3 @@ func UpdateProfile(ctx *gin.Context) {
}
ctx.JSON(http.StatusOK, gin.H{"message": "Profile updated successfully"})
}

func UpdateEloAfterDebate(ctx *gin.Context) {
var req struct {
WinnerID string `json:"winnerId"`
LoserID string `json:"loserId"`
Topic string `json:"topic"`
}
if err := ctx.ShouldBindJSON(&req); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
return
}

dbCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

winnerID, _ := primitive.ObjectIDFromHex(req.WinnerID)
loserID, _ := primitive.ObjectIDFromHex(req.LoserID)

var winner, loser models.User
_ = db.MongoDatabase.Collection("users").FindOne(dbCtx, bson.M{"_id": winnerID}).Decode(&winner)
_ = db.MongoDatabase.Collection("users").FindOne(dbCtx, bson.M{"_id": loserID}).Decode(&loser)

newWinnerElo, newLoserElo := calculateEloRating(winner.Rating, loser.Rating, 1.0)

winnerChange := newWinnerElo - winner.Rating
loserChange := newLoserElo - loser.Rating

// Update users
db.MongoDatabase.Collection("users").UpdateOne(dbCtx, bson.M{"_id": winnerID}, bson.M{"$set": bson.M{"rating": newWinnerElo}})
db.MongoDatabase.Collection("users").UpdateOne(dbCtx, bson.M{"_id": loserID}, bson.M{"$set": bson.M{"rating": newLoserElo}})

// Log debates
now := time.Now()
db.MongoDatabase.Collection("debates").InsertOne(dbCtx, bson.M{
"email": winner.Email,
"topic": req.Topic,
"result": "win",
"eloChange": winnerChange,
"rating": newWinnerElo,
"date": now,
})
db.MongoDatabase.Collection("debates").InsertOne(dbCtx, bson.M{
"email": loser.Email,
"topic": req.Topic,
"result": "loss",
"eloChange": loserChange,
"rating": newLoserElo,
"date": now,
})

ctx.JSON(http.StatusOK, gin.H{
"winnerNewElo": int(newWinnerElo),
"loserNewElo": int(newLoserElo),
})
}
4 changes: 0 additions & 4 deletions backend/routes/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,3 @@ func GetProfileRouteHandler(ctx *gin.Context) {
func UpdateProfileRouteHandler(ctx *gin.Context) {
controllers.UpdateProfile(ctx)
}

func UpdateEloAfterDebateRouteHandler(ctx *gin.Context) {
controllers.UpdateEloAfterDebate(ctx)
}