open-match.dev/open-match@v1.8.1/examples/demo/components/director/director.go (about) 1 // Copyright 2019 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package director 16 17 import ( 18 "context" 19 "fmt" 20 "io" 21 "math/rand" 22 "time" 23 24 "google.golang.org/grpc" 25 26 "open-match.dev/open-match/examples/demo/components" 27 "open-match.dev/open-match/pkg/pb" 28 ) 29 30 func Run(ds *components.DemoShared) { 31 for !isContextDone(ds.Ctx) { 32 run(ds) 33 } 34 } 35 36 func isContextDone(ctx context.Context) bool { 37 select { 38 case <-ctx.Done(): 39 return true 40 default: 41 return false 42 } 43 } 44 45 type status struct { 46 Status string 47 LatestMatches []*pb.Match 48 } 49 50 func run(ds *components.DemoShared) { 51 defer func() { 52 r := recover() 53 if r != nil { 54 err, ok := r.(error) 55 if !ok { 56 err = fmt.Errorf("pkg: %v", r) 57 } 58 59 ds.Update(status{Status: fmt.Sprintf("Encountered error: %s", err.Error())}) 60 time.Sleep(time.Second * 10) 61 } 62 }() 63 64 s := status{} 65 66 ////////////////////////////////////////////////////////////////////////////// 67 s.Status = "Connecting to backend" 68 ds.Update(s) 69 70 // See https://open-match.dev/site/docs/guides/api/ 71 conn, err := grpc.Dial("open-match-backend.open-match.svc.cluster.local:50505", grpc.WithInsecure()) 72 if err != nil { 73 panic(err) 74 } 75 defer conn.Close() 76 be := pb.NewBackendServiceClient(conn) 77 78 ////////////////////////////////////////////////////////////////////////////// 79 s.Status = "Match Match: Sending Request" 80 ds.Update(s) 81 82 var matches []*pb.Match 83 { 84 req := &pb.FetchMatchesRequest{ 85 Config: &pb.FunctionConfig{ 86 Host: "om-function.open-match-demo.svc.cluster.local", 87 Port: 50502, 88 Type: pb.FunctionConfig_GRPC, 89 }, 90 Profile: &pb.MatchProfile{ 91 Name: "1v1", 92 Pools: []*pb.Pool{ 93 { 94 Name: "Everyone", 95 }, 96 }, 97 }, 98 } 99 100 stream, err := be.FetchMatches(ds.Ctx, req) 101 if err != nil { 102 panic(err) 103 } 104 105 for { 106 resp, err := stream.Recv() 107 if err == io.EOF { 108 break 109 } 110 if err != nil { 111 panic(err) 112 } 113 matches = append(matches, resp.GetMatch()) 114 } 115 } 116 117 ////////////////////////////////////////////////////////////////////////////// 118 s.Status = "Matches Found" 119 s.LatestMatches = matches 120 ds.Update(s) 121 122 ////////////////////////////////////////////////////////////////////////////// 123 s.Status = "Assigning Players" 124 ds.Update(s) 125 126 for _, match := range matches { 127 ids := []string{} 128 129 for _, t := range match.Tickets { 130 ids = append(ids, t.Id) 131 } 132 133 req := &pb.AssignTicketsRequest{ 134 Assignments: []*pb.AssignmentGroup{ 135 { 136 TicketIds: ids, 137 Assignment: &pb.Assignment{ 138 Connection: fmt.Sprintf("%d.%d.%d.%d:2222", rand.Intn(256), rand.Intn(256), rand.Intn(256), rand.Intn(256)), 139 }, 140 }, 141 }, 142 } 143 144 resp, err := be.AssignTickets(ds.Ctx, req) 145 if err != nil { 146 panic(err) 147 } 148 149 _ = resp 150 } 151 152 ////////////////////////////////////////////////////////////////////////////// 153 s.Status = "Sleeping" 154 ds.Update(s) 155 156 time.Sleep(time.Second * 5) 157 }