Commit 3dc3aa62 authored by Fernando Takagi's avatar Fernando Takagi
Browse files

Adjust combinations for parties and additional count checks.

parent 0647653d
Loading
Loading
Loading
Loading
+20 −7
Original line number Diff line number Diff line
@@ -489,14 +489,14 @@ func (m *LocalMatchmaker) processCustom() [][]*MatchmakerEntry {
			hitIndexes = append(hitIndexes, hitIndex)
		}

		for hitIndexes := range combinationsMinMax(hitIndexes, index.MinCount-index.Count, index.MaxCount-index.Count) {
		for hitIndexes := range combineIndexes(hitIndexes, index.MinCount-index.Count, index.MaxCount-index.Count) {
			// Check the min and max counts are met across the hit.
			var hitCount int
			for _, hitIndex := range hitIndexes {
				hitCount += hitIndex.Count
			}
			hitCount += index.Count
			if hitCount > index.MaxCount {
			if hitCount > index.MaxCount || hitCount < index.MinCount {
				continue
			}
			if hitCount%index.CountMultiple != 0 {
@@ -504,6 +504,11 @@ func (m *LocalMatchmaker) processCustom() [][]*MatchmakerEntry {
			}
			var reject bool
			for _, hitIndex := range hitIndexes {
				// Check hit max count.
				if hitCount > hitIndex.MaxCount || hitCount < hitIndex.MinCount {
					reject = true
					break
				}
				// Check if count multiple is satisfied for this hit.
				if hitCount%hitIndex.CountMultiple != 0 {
					reject = true
@@ -652,8 +657,8 @@ func (m *LocalMatchmaker) processCustom() [][]*MatchmakerEntry {
	return finalMatchedEntries
}

func combinationsMinMax[T any](from []T, min, max int) <-chan []T {
	c := make(chan []T)
func combineIndexes(from []*MatchmakerIndex, min, max int) <-chan []*MatchmakerIndex {
	c := make(chan []*MatchmakerIndex)

	go func() {
		defer close(c)
@@ -661,21 +666,29 @@ func combinationsMinMax[T any](from []T, min, max int) <-chan []T {

		// Go through all possible combinations of from 1 (only first element in subset) to 2^length (all objects in subset)
		// and return those that contain between min and max elements.
	combination:
		for combinationBits := 1; combinationBits < (1 << length); combinationBits++ {
			count := bits.OnesCount(uint(combinationBits))
			if count < min || count > max {
			if count > max {
				continue
			}

			combination := make([]T, 0, count)
			combination := make([]*MatchmakerIndex, 0, count)
			entryCount := 0
			for element := uint(0); element < length; element++ {
				// Check if element should be contained in combination by checking if bit 'element' is set in combinationBits.
				if (combinationBits>>element)&1 == 1 {
					entryCount = entryCount + from[element].Count
					if entryCount > max {
						continue combination
					}
					combination = append(combination, from[element])
				}
			}
			if entryCount >= min {
				c <- combination
			}
		}
	}()
	return c
}