Loading server/matchmaker.go +26 −26 Original line number Diff line number Diff line Loading @@ -132,25 +132,26 @@ type MatchmakerIndexGroup struct { } func groupIndexes(indexes []*MatchmakerIndex, required int) []*MatchmakerIndexGroup { if len(indexes) == 0 || required == 0 { if len(indexes) == 0 || required <= 0 { return nil } current, others := indexes[0], indexes[1:] if current.Count > required { // Current index is too large for the requirement, and cannot be used at all. return groupIndexes(others, required) } var results []*MatchmakerIndexGroup for i := 0; i < len(indexes); i++ { // Grab index combination not including the current index. current, before, after := indexes[i], indexes[:i], indexes[i+1:] others := make([]*MatchmakerIndex, len(before)+len(after)) copy(others, before) copy(others[len(before):], after) if current.Count == required { // 1. The current index by itself satisfies the requirement. // 1. The current index by itself satisfies the requirement. No need to combine with anything else. results = append(results, &MatchmakerIndexGroup{ indexes: []*MatchmakerIndex{current}, avgCreatedAt: current.CreatedAt, }) } else { } else if current.Count < required { // 2. The current index plus some combination(s) of the others. fillResults := groupIndexes(others, required-current.Count) for _, fillResult := range fillResults { Loading @@ -163,7 +164,6 @@ func groupIndexes(indexes []*MatchmakerIndex, required int) []*MatchmakerIndexGr // 3. Other combinations not including the current index. results = append(results, groupIndexes(others, required)...) } return results } Loading server/matchmaker_test.go +22 −0 Original line number Diff line number Diff line Loading @@ -17,6 +17,7 @@ package server import ( "context" "errors" "github.com/stretchr/testify/assert" "io/ioutil" "math" "os" Loading Loading @@ -1591,6 +1592,27 @@ func TestMatchmakerAddAndMatchAuthoritative(t *testing.T) { } } func TestGroupIndexes(t *testing.T) { a := &MatchmakerIndex{Ticket: "a", Count: 1, CreatedAt: 100} b := &MatchmakerIndex{Ticket: "b", Count: 2, CreatedAt: 110} c := &MatchmakerIndex{Ticket: "c", Count: 1, CreatedAt: 120} d := &MatchmakerIndex{Ticket: "d", Count: 1, CreatedAt: 130} e := &MatchmakerIndex{Ticket: "e", Count: 3, CreatedAt: 140} f := &MatchmakerIndex{Ticket: "f", Count: 2, CreatedAt: 150} indexes := []*MatchmakerIndex{a, b, c, d, e, f} required := 2 groups := groupIndexes(indexes, required) assert.EqualValues(t, []*MatchmakerIndexGroup{ {indexes: []*MatchmakerIndex{c, a}, avgCreatedAt: 110}, {indexes: []*MatchmakerIndex{d, a}, avgCreatedAt: 115}, {indexes: []*MatchmakerIndex{b}, avgCreatedAt: 110}, {indexes: []*MatchmakerIndex{d, c}, avgCreatedAt: 125}, {indexes: []*MatchmakerIndex{f}, avgCreatedAt: 150}, }, groups, "groups did not match") } // createTestMatchmaker creates a minimally configured LocalMatchmaker for testing purposes // // an optional messageCallback can be provided, in which case the callback will be Loading Loading
server/matchmaker.go +26 −26 Original line number Diff line number Diff line Loading @@ -132,25 +132,26 @@ type MatchmakerIndexGroup struct { } func groupIndexes(indexes []*MatchmakerIndex, required int) []*MatchmakerIndexGroup { if len(indexes) == 0 || required == 0 { if len(indexes) == 0 || required <= 0 { return nil } current, others := indexes[0], indexes[1:] if current.Count > required { // Current index is too large for the requirement, and cannot be used at all. return groupIndexes(others, required) } var results []*MatchmakerIndexGroup for i := 0; i < len(indexes); i++ { // Grab index combination not including the current index. current, before, after := indexes[i], indexes[:i], indexes[i+1:] others := make([]*MatchmakerIndex, len(before)+len(after)) copy(others, before) copy(others[len(before):], after) if current.Count == required { // 1. The current index by itself satisfies the requirement. // 1. The current index by itself satisfies the requirement. No need to combine with anything else. results = append(results, &MatchmakerIndexGroup{ indexes: []*MatchmakerIndex{current}, avgCreatedAt: current.CreatedAt, }) } else { } else if current.Count < required { // 2. The current index plus some combination(s) of the others. fillResults := groupIndexes(others, required-current.Count) for _, fillResult := range fillResults { Loading @@ -163,7 +164,6 @@ func groupIndexes(indexes []*MatchmakerIndex, required int) []*MatchmakerIndexGr // 3. Other combinations not including the current index. results = append(results, groupIndexes(others, required)...) } return results } Loading
server/matchmaker_test.go +22 −0 Original line number Diff line number Diff line Loading @@ -17,6 +17,7 @@ package server import ( "context" "errors" "github.com/stretchr/testify/assert" "io/ioutil" "math" "os" Loading Loading @@ -1591,6 +1592,27 @@ func TestMatchmakerAddAndMatchAuthoritative(t *testing.T) { } } func TestGroupIndexes(t *testing.T) { a := &MatchmakerIndex{Ticket: "a", Count: 1, CreatedAt: 100} b := &MatchmakerIndex{Ticket: "b", Count: 2, CreatedAt: 110} c := &MatchmakerIndex{Ticket: "c", Count: 1, CreatedAt: 120} d := &MatchmakerIndex{Ticket: "d", Count: 1, CreatedAt: 130} e := &MatchmakerIndex{Ticket: "e", Count: 3, CreatedAt: 140} f := &MatchmakerIndex{Ticket: "f", Count: 2, CreatedAt: 150} indexes := []*MatchmakerIndex{a, b, c, d, e, f} required := 2 groups := groupIndexes(indexes, required) assert.EqualValues(t, []*MatchmakerIndexGroup{ {indexes: []*MatchmakerIndex{c, a}, avgCreatedAt: 110}, {indexes: []*MatchmakerIndex{d, a}, avgCreatedAt: 115}, {indexes: []*MatchmakerIndex{b}, avgCreatedAt: 110}, {indexes: []*MatchmakerIndex{d, c}, avgCreatedAt: 125}, {indexes: []*MatchmakerIndex{f}, avgCreatedAt: 150}, }, groups, "groups did not match") } // createTestMatchmaker creates a minimally configured LocalMatchmaker for testing purposes // // an optional messageCallback can be provided, in which case the callback will be Loading