From e6f83eee2534641c572d5e665feef29faa42a09e Mon Sep 17 00:00:00 2001 From: Yufan Fei Date: Fri, 18 Oct 2019 16:48:27 -0700 Subject: [PATCH] Added a template for game frontend --- .../go.mod | 0 .../go.sum | 0 .../main.go | 0 om-agones-game-frontend-example/main.go | 74 +++++++++++++++++++ 4 files changed, 74 insertions(+) rename {agones-director-example => om-agones-director-example}/go.mod (100%) rename {agones-director-example => om-agones-director-example}/go.sum (100%) rename {agones-director-example => om-agones-director-example}/main.go (100%) create mode 100644 om-agones-game-frontend-example/main.go diff --git a/agones-director-example/go.mod b/om-agones-director-example/go.mod similarity index 100% rename from agones-director-example/go.mod rename to om-agones-director-example/go.mod diff --git a/agones-director-example/go.sum b/om-agones-director-example/go.sum similarity index 100% rename from agones-director-example/go.sum rename to om-agones-director-example/go.sum diff --git a/agones-director-example/main.go b/om-agones-director-example/main.go similarity index 100% rename from agones-director-example/main.go rename to om-agones-director-example/main.go diff --git a/om-agones-game-frontend-example/main.go b/om-agones-game-frontend-example/main.go new file mode 100644 index 0000000..8a74bfa --- /dev/null +++ b/om-agones-game-frontend-example/main.go @@ -0,0 +1,74 @@ +package gamefrontend + +import ( + "context" + "fmt" + "time" + + "google.golang.org/grpc" + "open-match.dev/open-match/pkg/pb" +) + +func createOMFrontendClient() (pb.FrontendClient, func() error) { + conn, err := grpc.Dial("om-frontend.open-match.svc.cluster.local:50504", grpc.WithInsecure()) + if err != nil { + panic(err) + } + return pb.NewFrontendClient(conn), conn.Close +} + +func createOMTicket() *pb.Ticket { + return &pb.Ticket{ + SearchFields: &pb.SearchFields{ + DoubleArgs: map[string]float64{"defense": 10, "attack": 20, "level": 50}, + StringArgs: map[string]string{"guildName": "open-match", "location": "alterac"}, + Tags: []string{"beta-gameplay", "demo-map"}, + }, + } +} + +// doGameQueue accepts a channel that represents status of a player in the queue. +// An openned channel means the player is actively seeking for a game match, while +// a closed channel means the player exited the game queue. +func doGameQueue(c chan bool) { + fc, closer := createOMFrontendClient() + defer closer() + + resp, err := fc.CreateTicket(context.Background(), &pb.CreateTicketRequest{Ticket: createOMTicket()}) + if err != nil { + panic(err.Error()) + } + + // Each channel represents a player waiting for a game, the channel will be closed if the player exit the queue + tid := resp.GetTicket().GetId() + for { + // TODO: once we find a game, have the game send something to this channel once the player starts a match making + _, ok := <-c + + ticket, err := fc.GetTicket(context.Background(), &pb.GetTicketRequest{TicketId: tid}) + if err != nil { + panic(err.Error()) + } + + tconn := ticket.GetAssignment().GetConnection() + // channel is ok. player is still in the queue and waiting for an assignment. + if ok && tconn == "" { + // wait for the next check + time.Sleep(1 * time.Second) + continue + } + + // otherwise player is not interested in joining a match anymore, delete the ticket + defer fc.DeleteTicket(context.Background(), &pb.DeleteTicketRequest{TicketId: tid}) + if ok { // player got an assignment, + fmt.Printf("a player got assigned to %v\n", tconn) + } else if tconn != "" { + // player left the queue after getting an assignment, need to recycle the staled game server. + // we assume this won't happen in a workshop scenario. + fmt.Printf("oops, we got a staled game server\n") + } else { // player left the queue without being assign. + fmt.Printf("oops, player rage quit after waiting so long\n") + } + return + } +}