Created
October 5, 2020 23:48
-
-
Save syntxerror/c634dccb96c686b50fee67f801d6e671 to your computer and use it in GitHub Desktop.
Skill Based Match Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func makeMatches(p *pb.MatchProfile, poolTickets map[string][]*pb.Ticket) ([]*pb.Match, error) { | |
// Create a colletion to hold match proposals | |
var matches []*pb.Match | |
count := 0 | |
for { | |
insufficientTickets := false | |
// Create a collection to hold tickets selected for a match | |
matchTickets := []*pb.Ticket{} | |
for pool, tickets := range poolTickets { | |
// Set flag if there are not enough tickets to create a match | |
if len(tickets) < ticketsPerPoolPerMatch { | |
insufficientTickets = true | |
break | |
} | |
// Sort the tickets based on skill | |
sort.Slice(tickets, func(i, j int) bool { | |
return tickets[i].SearchFields.DoubleArgs["mmr"] < tickets[j].SearchFields.DoubleArgs["mmr"] | |
}) | |
// Remove the Tickets from this pool and add to the match proposal. | |
matchTickets = append(matchTickets, tickets[0:ticketsPerPoolPerMatch]...) | |
poolTickets[pool] = tickets[ticketsPerPoolPerMatch:] | |
} | |
if insufficientTickets { | |
break | |
} | |
// Compute the match quality/score | |
matchQuality := computeQuality(matchTickets) | |
evaluationInput, err := ptypes.MarshalAny(&pb.DefaultEvaluationCriteria{ | |
Score: matchQuality, | |
}) | |
if err != nil { | |
log.Printf("Failed to marshal DefaultEvaluationCriteria, got %s.", err.Error()) | |
return nil, fmt.Errorf("Failed to marshal DefaultEvaluationCriteria, got %w", err) | |
} | |
// Output the match quality for our sanity | |
log.Printf("Quality for the generated match is %v", matchQuality) | |
// Create a match proposal | |
matches = append(matches, &pb.Match{ | |
MatchId: fmt.Sprintf("profile-%v-time-%v-%v", p.GetName(), time.Now().Format("2006-01-02T15:04:05.00"), count), | |
MatchProfile: p.GetName(), | |
MatchFunction: matchName, | |
Tickets: matchTickets, | |
Extensions: map[string]*any.Any{ | |
"evaluation_input": evaluationInput, | |
}, | |
}) | |
count++ | |
} | |
return matches, nil | |
} | |
// Compute the quality as a difference in the highest and lowest player skill levels. This can be used to determine if the match is outside a given skill differential | |
func computeQuality(tickets []*pb.Ticket) float64 { | |
quality := 0.0 | |
high := 0.0 | |
low := tickets[0].SearchFields.DoubleArgs["mmr"] | |
for _, ticket := range tickets { | |
if high < ticket.SearchFields.DoubleArgs["mmr"] { | |
high = ticket.SearchFields.DoubleArgs["mmr"] | |
} | |
if low > ticket.SearchFields.DoubleArgs["mmr"] { | |
low = ticket.SearchFields.DoubleArgs["mmr"] | |
} | |
} | |
quality = high - low | |
return quality | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment