open-match.dev/open-match@v1.8.1/examples/demo/components/clients/clients.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 clients
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"math/rand"
    21  	"time"
    22  
    23  	"google.golang.org/grpc"
    24  
    25  	"open-match.dev/open-match/examples/demo/components"
    26  	"open-match.dev/open-match/examples/demo/updater"
    27  	"open-match.dev/open-match/pkg/pb"
    28  )
    29  
    30  func Run(ds *components.DemoShared) {
    31  	u := updater.NewNested(ds.Ctx, ds.Update)
    32  
    33  	for i := 0; i < 5; i++ {
    34  		name := fmt.Sprintf("fakeplayer_%d", i)
    35  		go func() {
    36  			for !isContextDone(ds.Ctx) {
    37  				runScenario(ds.Ctx, name, u.ForField(name))
    38  			}
    39  		}()
    40  	}
    41  }
    42  
    43  func isContextDone(ctx context.Context) bool {
    44  	select {
    45  	case <-ctx.Done():
    46  		return true
    47  	default:
    48  		return false
    49  	}
    50  }
    51  
    52  type status struct {
    53  	Status     string
    54  	Assignment *pb.Assignment
    55  }
    56  
    57  func runScenario(ctx context.Context, name string, update updater.SetFunc) {
    58  	defer func() {
    59  		r := recover()
    60  		if r != nil {
    61  			err, ok := r.(error)
    62  			if !ok {
    63  				err = fmt.Errorf("pkg: %v", r)
    64  			}
    65  
    66  			update(status{Status: fmt.Sprintf("Encountered error: %s", err.Error())})
    67  			time.Sleep(time.Second * 10)
    68  		}
    69  	}()
    70  
    71  	s := status{}
    72  
    73  	//////////////////////////////////////////////////////////////////////////////
    74  	s.Status = "Main Menu"
    75  	update(s)
    76  
    77  	time.Sleep(time.Duration(rand.Int63()) % (time.Second * 15))
    78  
    79  	//////////////////////////////////////////////////////////////////////////////
    80  	s.Status = "Connecting to Open Match frontend"
    81  	update(s)
    82  
    83  	// See https://open-match.dev/site/docs/guides/api/
    84  	conn, err := grpc.Dial("open-match-frontend.open-match.svc.cluster.local:50504", grpc.WithInsecure())
    85  	if err != nil {
    86  		panic(err)
    87  	}
    88  	defer conn.Close()
    89  	fe := pb.NewFrontendServiceClient(conn)
    90  
    91  	//////////////////////////////////////////////////////////////////////////////
    92  	s.Status = "Creating Open Match Ticket"
    93  	update(s)
    94  
    95  	var ticketId string
    96  	{
    97  		req := &pb.CreateTicketRequest{
    98  			Ticket: &pb.Ticket{},
    99  		}
   100  
   101  		resp, err := fe.CreateTicket(ctx, req)
   102  		if err != nil {
   103  			panic(err)
   104  		}
   105  		ticketId = resp.Id
   106  	}
   107  
   108  	//////////////////////////////////////////////////////////////////////////////
   109  	s.Status = fmt.Sprintf("Waiting match with ticket Id %s", ticketId)
   110  	update(s)
   111  
   112  	var assignment *pb.Assignment
   113  	{
   114  		req := &pb.WatchAssignmentsRequest{
   115  			TicketId: ticketId,
   116  		}
   117  
   118  		stream, err := fe.WatchAssignments(ctx, req)
   119  		for assignment.GetConnection() == "" {
   120  			resp, err := stream.Recv()
   121  			if err != nil {
   122  				// For now we don't expect to get EOF, so that's still an error worthy of panic.
   123  				panic(err)
   124  			}
   125  
   126  			assignment = resp.Assignment
   127  		}
   128  
   129  		err = stream.CloseSend()
   130  		if err != nil {
   131  			panic(err)
   132  		}
   133  	}
   134  
   135  	//////////////////////////////////////////////////////////////////////////////
   136  	s.Status = "Sleeping (pretend this is playing a match...)"
   137  	s.Assignment = assignment
   138  	update(s)
   139  
   140  	time.Sleep(time.Second * 10)
   141  }