-
Notifications
You must be signed in to change notification settings - Fork 2
/
simple1vs1.go
61 lines (51 loc) · 1.51 KB
/
simple1vs1.go
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
package main
import (
"context"
"log"
"time"
"github.com/bojand/hri"
"github.com/castaneai/minimatch"
pb "github.com/castaneai/minimatch/gen/openmatch"
)
var matchProfile = &pb.MatchProfile{
Name: "simple-1vs1",
Pools: []*pb.Pool{
{Name: "test-pool"},
},
}
func main() {
// Create minimatch instance with miniredis
mm, err := minimatch.NewMiniMatchWithRedis()
if err != nil {
log.Fatalf("failed to start minimatch: %+v", err)
}
// Add backend (Match Profile, Match Function and Assigner)
mm.AddMatchFunction(matchProfile, minimatch.MatchFunctionSimple1vs1)
// Start minimatch backend service with Director's interval
go func() { mm.StartBackend(context.Background(), minimatch.AssignerFunc(dummyAssign), 1*time.Second) }()
// Start minimatch frontend service
mm.StartFrontend(":50504")
}
// Assigner assigns a GameServer to a match.
// For example, it could call Agones' Allocate service.
// For the sake of simplicity, a dummy GameServer name is assigned here.
func dummyAssign(ctx context.Context, matches []*pb.Match) ([]*pb.AssignmentGroup, error) {
var asgs []*pb.AssignmentGroup
for _, match := range matches {
tids := ticketIDs(match)
conn := hri.Random()
log.Printf("assign '%s' to tickets: %v", conn, tids)
asgs = append(asgs, &pb.AssignmentGroup{
TicketIds: tids,
Assignment: &pb.Assignment{Connection: conn},
})
}
return asgs, nil
}
func ticketIDs(match *pb.Match) []string {
var ids []string
for _, ticket := range match.Tickets {
ids = append(ids, ticket.Id)
}
return ids
}