Skip to content

Commit

Permalink
Plumb players_per_match from terraform.tfvars down to matchfunction (
Browse files Browse the repository at this point in the history
…#155)

* Plumb `players_per_match` from terraform.tfvars.sample down to matchfunction

* Adds open-match-matchfunction.players_per_match terraform variable, default 4
* Adds configmap generated from variable
* Plumbs configmap into match function

* Move to `envFrom`
  • Loading branch information
zmerlynn authored Mar 14, 2023
1 parent 0234865 commit b8764da
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2023 Google LLC All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: v1
kind: ConfigMap
metadata:
name: open-match-matchfunction
namespace: open-match
data:
PLAYERS_PER_MATCH: ${players_per_match}
8 changes: 8 additions & 0 deletions infrastructure/services-gke.tf
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ resource "local_file" "services-frontend-config-map" {
filename = "${path.module}/${var.services_directory}/frontend/config.yaml"
}

resource "local_file" "open-match-matchfunction-config-map" {
content = templatefile(
"${path.module}/files/services/open-match-matchfunction-config.yaml.tpl", {
players_per_match = format("%q", var.open-match-matchfunction.players_per_match)
})
filename = "${path.module}/${var.services_directory}/open-match/matchfunction/config.yaml"
}

resource "google_gke_hub_membership" "services-gke-membership" {
provider = google-beta
project = var.project
Expand Down
5 changes: 5 additions & 0 deletions infrastructure/terraform.tfvars.sample
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ frontend-service = {
jwt_key = "r@nd0m$"
}

# Open Match Match Function Config Values
open-match-matchfunction = {
players_per_match = 4
}

# Artifact Registry variables
### NOTE: If you change the Artifact registry location, please make sure to change `cloudbuild.yaml` in
### `services/clouddeploy.yaml;` as it is not dynamically created.
Expand Down
9 changes: 9 additions & 0 deletions infrastructure/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ variable "services_directory" {
description = "Services Directory for output to Cloud Deploy related files"
}

### Open Match Match Function Variables ###

variable "open-match-matchfunction" {
type = object({
players_per_match = number
})
description = "Configuration for the Open Match Match Function"
}

### Dedicated Game Server Variables

variable "github_pat" {
Expand Down
15 changes: 15 additions & 0 deletions services/open-match/matchfunction/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2023 Google LLC All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

config.yaml
20 changes: 18 additions & 2 deletions services/open-match/matchfunction/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@
// matchmaking logic in this function based on your game's requirements.
package main

import "github.com/googleforgames/global-multiplayer-demo/services/open-match/matchfunction/mmf"
import (
"log"
"os"
"strconv"

"github.com/googleforgames/global-multiplayer-demo/services/open-match/matchfunction/mmf"
)

// This tutorial implenents a basic Match Function that is hosted in the below
// configured port. You can also configure the Open Match QueryService endpoint
Expand All @@ -31,5 +37,15 @@ const (
)

func main() {
mmf.Start(queryServiceAddress, serverPort)
mmf.Start(queryServiceAddress, serverPort, playersPerMatch())
}

func playersPerMatch() int {
ppms := os.Getenv("PLAYERS_PER_MATCH")
ppm, err := strconv.Atoi(ppms)
if err != nil {
log.Fatalf("PLAYERS_PER_MATCH not a valid int: %q", ppms)
}
log.Printf("PLAYERS_PER_MATCH=%d", ppm)
return ppm
}
3 changes: 3 additions & 0 deletions services/open-match/matchfunction/matchfunction.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ spec:
ports:
- name: grpc
containerPort: 50502
envFrom:
- configMapRef:
name: open-match-matchfunction
---
kind: Service
apiVersion: v1
Expand Down
14 changes: 6 additions & 8 deletions services/open-match/matchfunction/mmf/matchfunction.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import (

const (
matchName = "match-skill-and-latency"

ticketsPerMatch = 4
)

// Run is this match function's implementation of the gRPC call defined in api/matchfunction.proto.
Expand All @@ -45,7 +43,7 @@ func (s *MatchFunctionService) Run(req *pb.RunRequest, stream pb.MatchFunction_R

// Generate proposals.
idPrefix := fmt.Sprintf("profile-%v-time-%v", p.GetName(), time.Now().Format("2006-01-02T15:04:05.00"))
proposals, err := makeMatches(p.GetName(), idPrefix, tickets)
proposals, err := s.makeMatches(p.GetName(), idPrefix, tickets)
if err != nil {
log.Printf("Failed to generate matches: %v", err)
return err
Expand All @@ -64,8 +62,8 @@ func (s *MatchFunctionService) Run(req *pb.RunRequest, stream pb.MatchFunction_R
}

// Find all matches for the given profile.
func makeMatches(profileName, idPrefix string, tickets []*pb.Ticket) ([]*pb.Match, error) {
if len(tickets) < ticketsPerMatch {
func (s *MatchFunctionService) makeMatches(profileName, idPrefix string, tickets []*pb.Ticket) ([]*pb.Match, error) {
if len(tickets) < s.playersPerMatch {
return nil, nil
}

Expand All @@ -80,9 +78,9 @@ func makeMatches(profileName, idPrefix string, tickets []*pb.Ticket) ([]*pb.Matc

var matches []*pb.Match
count := 0
for len(tickets) >= ticketsPerMatch {
matchTickets := tickets[:ticketsPerMatch]
tickets = tickets[ticketsPerMatch:]
for len(tickets) >= s.playersPerMatch {
matchTickets := tickets[:s.playersPerMatch]
tickets = tickets[s.playersPerMatch:]

var matchScore float64
for _, ticket := range matchTickets {
Expand Down
4 changes: 3 additions & 1 deletion services/open-match/matchfunction/mmf/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ type MatchFunctionService struct {
grpc *grpc.Server
queryServiceClient pb.QueryServiceClient
port int
playersPerMatch int
}

// Start creates and starts the Match Function server and also connects to Open
// Match's queryService service. This connection is used at runtime to fetch tickets
// for pools specified in MatchProfile.
func Start(queryServiceAddr string, serverPort int) {
func Start(queryServiceAddr string, serverPort int, playersPerMatch int) {
// Connect to QueryService.
conn, err := grpc.Dial(queryServiceAddr, grpc.WithInsecure())
if err != nil {
Expand All @@ -44,6 +45,7 @@ func Start(queryServiceAddr string, serverPort int) {

mmfService := MatchFunctionService{
queryServiceClient: pb.NewQueryServiceClient(conn),
playersPerMatch: playersPerMatch,
}

// Create and host a new gRPC service on the configured port.
Expand Down
1 change: 1 addition & 0 deletions services/skaffold.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ manifests:
- frontend/deployment.yaml
- open-match/director/director.yaml
- open-match/matchfunction/matchfunction.yaml
- open-match/matchfunction/config.yaml
- open-match/fake-frontend/fake-frontend.yaml
deploy:
kubectl:
Expand Down

0 comments on commit b8764da

Please sign in to comment.