agones.dev/agones@v1.54.0/sdks/go/alpha.go (about)

     1  // Copyright 2020 Google LLC All Rights Reserved.
     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 sdk
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/pkg/errors"
    21  	"google.golang.org/grpc"
    22  
    23  	"agones.dev/agones/pkg/sdk/alpha"
    24  )
    25  
    26  // Alpha is the struct for Alpha SDK functionality.
    27  type Alpha struct {
    28  	client alpha.SDKClient
    29  }
    30  
    31  // newAlpha creates a new Alpha SDK with the passed in connection.
    32  func newAlpha(conn *grpc.ClientConn) *Alpha {
    33  	return &Alpha{
    34  		client: alpha.NewSDKClient(conn),
    35  	}
    36  }
    37  
    38  // GetPlayerCapacity gets the last player capacity that was set through the SDK.
    39  // If the player capacity is set from outside the SDK, use SDK.GameServer() instead.
    40  func (a *Alpha) GetPlayerCapacity() (int64, error) {
    41  	c, err := a.client.GetPlayerCapacity(context.Background(), &alpha.Empty{})
    42  	return c.GetCount(), errors.Wrap(err, "could not get player capacity")
    43  }
    44  
    45  // SetPlayerCapacity changes the player capacity to a new value.
    46  func (a *Alpha) SetPlayerCapacity(capacity int64) error {
    47  	_, err := a.client.SetPlayerCapacity(context.Background(), &alpha.Count{Count: capacity})
    48  	return errors.Wrap(err, "could not set player capacity")
    49  }
    50  
    51  // PlayerConnect increases the SDK’s stored player count by one, and appends this playerID to status.players.id.
    52  // Will return true and add the playerID to the list of playerIDs if the playerIDs was not already in the
    53  // list of connected playerIDs.
    54  func (a *Alpha) PlayerConnect(id string) (bool, error) {
    55  	ok, err := a.client.PlayerConnect(context.Background(), &alpha.PlayerID{PlayerID: id})
    56  	return ok.GetBool(), errors.Wrap(err, "could not register connected player")
    57  }
    58  
    59  // PlayerDisconnect Decreases the SDK’s stored player count by one, and removes the playerID from status.players.id.
    60  // Will return true and remove the supplied playerID from the list of connected playerIDs if the
    61  // playerID value exists within the list.
    62  func (a *Alpha) PlayerDisconnect(id string) (bool, error) {
    63  	ok, err := a.client.PlayerDisconnect(context.Background(), &alpha.PlayerID{PlayerID: id})
    64  	return ok.GetBool(), errors.Wrap(err, "could not register disconnected player")
    65  }
    66  
    67  // GetPlayerCount returns the current player count.
    68  func (a *Alpha) GetPlayerCount() (int64, error) {
    69  	count, err := a.client.GetPlayerCount(context.Background(), &alpha.Empty{})
    70  	return count.GetCount(), errors.Wrap(err, "could not get player count")
    71  }
    72  
    73  // IsPlayerConnected returns if the playerID is currently connected to the GameServer.
    74  // This is always accurate, even if the value hasn’t been updated to the GameServer status yet.
    75  func (a *Alpha) IsPlayerConnected(id string) (bool, error) {
    76  	ok, err := a.client.IsPlayerConnected(context.Background(), &alpha.PlayerID{PlayerID: id})
    77  	return ok.GetBool(), errors.Wrap(err, "could not get if player is connected")
    78  }
    79  
    80  // GetConnectedPlayers returns the list of the currently connected player ids.
    81  // This is always accurate, even if the value hasn’t been updated to the GameServer status yet.
    82  func (a *Alpha) GetConnectedPlayers() ([]string, error) {
    83  	list, err := a.client.GetConnectedPlayers(context.Background(), &alpha.Empty{})
    84  	return list.GetList(), errors.Wrap(err, "could not list connected players")
    85  }