-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatchfunction.go
More file actions
65 lines (52 loc) · 1.62 KB
/
matchfunction.go
File metadata and controls
65 lines (52 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package openmatch_mmf_testing
import (
"fmt"
"time"
"open-match.dev/open-match/pkg/matchfunction"
"open-match.dev/open-match/pkg/pb"
)
type MatchFunctionService struct {
qsc pb.QueryServiceClient
}
func NewMatchFunctionService(qsc pb.QueryServiceClient) *MatchFunctionService {
return &MatchFunctionService{qsc: qsc}
}
func (s *MatchFunctionService) Run(request *pb.RunRequest, stream pb.MatchFunction_RunServer) error {
poolTickets, err := matchfunction.QueryPools(stream.Context(), s.qsc, request.Profile.Pools)
if err != nil {
return fmt.Errorf("failed to query pools: %+v", err)
}
matches, err := makeMatches(poolTickets, request.Profile)
for _, match := range matches {
if err := stream.Send(&pb.RunResponse{Proposal: match}); err != nil {
return fmt.Errorf("failed to send proposal: %+v", err)
}
}
return nil
}
func makeMatches(poolTickets map[string][]*pb.Ticket, profile *pb.MatchProfile) ([]*pb.Match, error) {
tickets := map[string]*pb.Ticket{}
for _, pool := range poolTickets {
for _, ticket := range pool {
tickets[ticket.GetId()] = ticket
}
}
var matches []*pb.Match
t := time.Now().Format("2006-01-02T15:04:05.00")
thisMatch := make([]*pb.Ticket, 0, 2)
matchNum := 0
for _, ticket := range tickets {
thisMatch = append(thisMatch, ticket)
if len(thisMatch) >= 2 {
matches = append(matches, &pb.Match{
MatchId: fmt.Sprintf("profile-%s-time-%s-num-%d", profile.Name, t, matchNum),
MatchProfile: profile.Name,
MatchFunction: "demo-match-function",
Tickets: thisMatch,
})
thisMatch = make([]*pb.Ticket, 0, 2)
matchNum++
}
}
return matches, nil
}