Skip to content

Commit

Permalink
Implement fairer and more stable sorting for tied teams
Browse files Browse the repository at this point in the history
  • Loading branch information
J12934 committed Nov 24, 2024
1 parent ef4ff9c commit 9569558
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 14 deletions.
22 changes: 20 additions & 2 deletions balancer/pkg/scoring/scoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ type TeamScore struct {

// PersistedChallengeProgress is stored as a json array on the JuiceShop deployments, saving which challenges have been solved and when
type ChallengeProgress struct {
Key string `json:"key"`
SolvedAt string `json:"solvedAt"`
Key string `json:"key"`
SolvedAt time.Time `json:"solvedAt"`
}

var cachedChallengesMap map[string](bundle.JuiceShopChallenge)
Expand Down Expand Up @@ -152,6 +152,16 @@ func calculateScore(bundle *bundle.Bundle, teamDeployment *appsv1.Deployment, ch
}
}

func getLatestChallengeSolve(challenges []ChallengeProgress) time.Time {
var maxTime time.Time
for _, challenge := range challenges {
if challenge.SolvedAt.After(maxTime) {
maxTime = challenge.SolvedAt
}
}
return maxTime
}

func sortTeamsByScoreAndCalculatePositions(teamScores map[string]*TeamScore) []*TeamScore {
sortedTeamScores := make([]*TeamScore, len(teamScores))

Expand All @@ -162,6 +172,14 @@ func sortTeamsByScoreAndCalculatePositions(teamScores map[string]*TeamScore) []*
}

sort.Slice(sortedTeamScores, func(i, j int) bool {
if sortedTeamScores[i].Score == sortedTeamScores[j].Score {
iTime := getLatestChallengeSolve(sortedTeamScores[i].Challenges)
jTime := getLatestChallengeSolve(sortedTeamScores[j].Challenges)
if iTime == jTime {
return sortedTeamScores[i].Name < sortedTeamScores[j].Name
}
return iTime.Before(jTime)
}
return sortedTeamScores[i].Score > sortedTeamScores[j].Score
})

Expand Down
87 changes: 79 additions & 8 deletions balancer/pkg/scoring/scoring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"testing"
"time"

"github.com/juice-shop/multi-juicer/balancer/pkg/testutil"
"github.com/stretchr/testify/assert"
Expand All @@ -12,7 +13,7 @@ import (
"k8s.io/client-go/kubernetes/fake"
)

func TestScoreBoardHandler(t *testing.T) {
func TestScoreingService(t *testing.T) {
createTeam := func(team string, challenges string, solvedChallenges string) *appsv1.Deployment {
return &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -33,6 +34,7 @@ func TestScoreBoardHandler(t *testing.T) {
},
}
}
novemberFirst := time.Date(2024, 11, 1, 19, 55, 48, 211000000, time.UTC)
t.Run("correctly calculates team scores", func(t *testing.T) {
clientset := fake.NewSimpleClientset(
createTeam("foobar", `[{"key":"scoreBoardChallenge","solvedAt":"2024-11-01T19:55:48.211Z"},{"key":"nullByteChallenge","solvedAt":"2024-11-01T19:55:48.211Z"}]`, "2"),
Expand All @@ -55,11 +57,11 @@ func TestScoreBoardHandler(t *testing.T) {
Challenges: []ChallengeProgress{
{
Key: "scoreBoardChallenge",
SolvedAt: "2024-11-01T19:55:48.211Z",
SolvedAt: novemberFirst,
},
{
Key: "nullByteChallenge",
SolvedAt: "2024-11-01T19:55:48.211Z",
SolvedAt: novemberFirst,
},
},
},
Expand All @@ -73,6 +75,7 @@ func TestScoreBoardHandler(t *testing.T) {
})

t.Run("teams with the same score get the same position assigned", func(t *testing.T) {

clientset := fake.NewSimpleClientset(
createTeam("foobar", `[{"key":"scoreBoardChallenge","solvedAt":"2024-11-01T19:55:48.211Z"},{"key":"nullByteChallenge","solvedAt":"2024-11-01T19:55:48.211Z"}]`, "2"),
createTeam("barfoo-1", `[{"key":"scoreBoardChallenge","solvedAt":"2024-11-01T19:55:48.211Z"}]`, "1"),
Expand All @@ -96,11 +99,11 @@ func TestScoreBoardHandler(t *testing.T) {
Challenges: []ChallengeProgress{
{
Key: "scoreBoardChallenge",
SolvedAt: "2024-11-01T19:55:48.211Z",
SolvedAt: novemberFirst,
},
{
Key: "nullByteChallenge",
SolvedAt: "2024-11-01T19:55:48.211Z",
SolvedAt: novemberFirst,
},
},
},
Expand All @@ -111,7 +114,7 @@ func TestScoreBoardHandler(t *testing.T) {
Challenges: []ChallengeProgress{
{
Key: "scoreBoardChallenge",
SolvedAt: "2024-11-01T19:55:48.211Z",
SolvedAt: novemberFirst,
},
},
},
Expand All @@ -122,7 +125,7 @@ func TestScoreBoardHandler(t *testing.T) {
Challenges: []ChallengeProgress{
{
Key: "scoreBoardChallenge",
SolvedAt: "2024-11-01T19:55:48.211Z",
SolvedAt: novemberFirst,
},
},
},
Expand Down Expand Up @@ -157,7 +160,7 @@ func TestScoreBoardHandler(t *testing.T) {
Challenges: []ChallengeProgress{
{
Key: "nullByteChallenge",
SolvedAt: "2024-11-01T19:55:48.211Z",
SolvedAt: novemberFirst,
},
},
},
Expand All @@ -170,3 +173,71 @@ func TestScoreBoardHandler(t *testing.T) {
}, scores)
})
}

func TestScoreingSorting(t *testing.T) {
createTeamScore := func(team string, score int, challenges ...ChallengeProgress) *TeamScore {
return &TeamScore{
Name: team,
Score: score,
Challenges: challenges,
}
}

now := time.Now()

t.Run("sorts score in this order: score -> 'time to reach score' -> team name", func(t *testing.T) {
scores := map[string]*TeamScore{
"0-last-place": createTeamScore("0-last-place", 0),
// last place is shared by two teams with the same score and same time to reach the score, they should be sorted by team name for consistency.
"1-actual-last-place": createTeamScore("1-second-last-place", 0),
"0-winning-team": createTeamScore("0-winning-team", 100,
ChallengeProgress{Key: "scoreBoardChallenge", SolvedAt: now},
ChallengeProgress{Key: "nullByteChallenge", SolvedAt: now},
ChallengeProgress{Key: "anotherChallenge", SolvedAt: now},
),
"1-second-place": createTeamScore("1-second-place", 50,
ChallengeProgress{Key: "scoreBoardChallenge", SolvedAt: now.Add(-10 * time.Second)},
ChallengeProgress{Key: "nullByteChallenge", SolvedAt: now.Add(-30 * time.Second)},
),
// same score as 1-second-place but it solved the challenges later, so it should be placed after 1-second-place
"0-second-place": createTeamScore("0-second-place", 50,
ChallengeProgress{Key: "scoreBoardChallenge", SolvedAt: now},
ChallengeProgress{Key: "nullByteChallenge", SolvedAt: now},
),
// forth place is shared by two teams with the same score and same time to reach the score, they should be sorted by team name for consistency.
// the likelyhood of two teams having the same score and solving the challenges at the same time is nearly zero so we ignore the unfairness of sorting by team name
// there is no 3rd place because 1-second-place and 0-second-place share the same position
"1-forth-place": createTeamScore("1-forth-place", 40,
ChallengeProgress{Key: "nullByteChallenge", SolvedAt: now},
),
"0-forth-place": createTeamScore("0-forth-place", 40,
ChallengeProgress{Key: "nullByteChallenge", SolvedAt: now},
),
}

sortedTeams := sortTeamsByScoreAndCalculatePositions(scores)

type TeamNameWithPosition struct {
Name string
Position int
}

sortedTeamWithPositions := make([]TeamNameWithPosition, len(sortedTeams))
for i, team := range sortedTeams {
sortedTeamWithPositions[i] = TeamNameWithPosition{
Name: team.Name,
Position: team.Position,
}
}

assert.Equal(t, []TeamNameWithPosition{
{Name: "0-winning-team", Position: 1},
{Name: "1-second-place", Position: 2},
{Name: "0-second-place", Position: 2},
{Name: "0-forth-place", Position: 4},
{Name: "1-forth-place", Position: 4},
{Name: "0-last-place", Position: 6},
{Name: "1-second-last-place", Position: 6},
}, sortedTeamWithPositions)
})
}
3 changes: 2 additions & 1 deletion balancer/routes/individualScore.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package routes
import (
"encoding/json"
"net/http"
"time"

b "github.com/juice-shop/multi-juicer/balancer/pkg/bundle"
"github.com/juice-shop/multi-juicer/balancer/pkg/scoring"
Expand Down Expand Up @@ -53,7 +54,7 @@ func handleIndividualScore(bundle *b.Bundle, scoringService *scoring.ScoringServ
Key: challenge.Key,
Name: challengesByKeys[challenge.Key].Name,
Difficulty: challengesByKeys[challenge.Key].Difficulty,
SolvedAt: challenge.SolvedAt,
SolvedAt: challenge.SolvedAt.Format(time.RFC3339),
}
}

Expand Down
2 changes: 1 addition & 1 deletion balancer/routes/individualScore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestIndividualScoreHandler(t *testing.T) {
server.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)
assert.JSONEq(t, `{"name":"foobar","score":10,"position":1,"solvedChallenges":[{"key":"scoreBoardChallenge","name":"Score Board","difficulty":1,"solvedAt":"2024-11-01T19:55:48.211Z"}],"totalTeams":1}`, rr.Body.String())
assert.JSONEq(t, `{"name":"foobar","score":10,"position":1,"solvedChallenges":[{"key":"scoreBoardChallenge","name":"Score Board","difficulty":1,"solvedAt":"2024-11-01T19:55:48Z"}],"totalTeams":1}`, rr.Body.String())
})

t.Run("returns a 404 if the scores haven't been calculated yet", func(t *testing.T) {
Expand Down
6 changes: 4 additions & 2 deletions balancer/routes/score-board_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/juice-shop/multi-juicer/balancer/pkg/scoring"
"github.com/juice-shop/multi-juicer/balancer/pkg/testutil"
Expand Down Expand Up @@ -38,6 +39,7 @@ func TestScoreBoardHandler(t *testing.T) {
},
}
}
novemberFirst := time.Date(2024, 11, 1, 19, 55, 48, 211000000, time.UTC)
t.Run("lists teams and calculates the score", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/balancer/api/score-board/top", nil)
rr := httptest.NewRecorder()
Expand Down Expand Up @@ -66,8 +68,8 @@ func TestScoreBoardHandler(t *testing.T) {
Score: 50,
Position: 1,
Challenges: []scoring.ChallengeProgress{
{Key: "scoreBoardChallenge", SolvedAt: "2024-11-01T19:55:48.211Z"},
{Key: "nullByteChallenge", SolvedAt: "2024-11-01T19:55:48.211Z"},
{Key: "scoreBoardChallenge", SolvedAt: novemberFirst},
{Key: "nullByteChallenge", SolvedAt: novemberFirst},
},
},
{
Expand Down

0 comments on commit 9569558

Please sign in to comment.