Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions sdks/go/alpha.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,69 +17,70 @@ package sdk
import (
"context"

"github.com/pkg/errors"
"google.golang.org/grpc"

"agones.dev/agones/pkg/sdk/alpha"
"agones.dev/agones/pkg/util/errors"
)

// Alpha is the struct for Alpha SDK functionality.
type Alpha struct {
client alpha.SDKClient
errs *errors.Errors
}

// newAlpha creates a new Alpha SDK with the passed in connection.
func newAlpha(conn *grpc.ClientConn) *Alpha {
return &Alpha{
client: alpha.NewSDKClient(conn),
}
a := &Alpha{client: alpha.NewSDKClient(conn)}
a.errs = errors.FromStruct(a)
return a
}

// GetPlayerCapacity gets the last player capacity that was set through the SDK.
// If the player capacity is set from outside the SDK, use SDK.GameServer() instead.
func (a *Alpha) GetPlayerCapacity() (int64, error) {
c, err := a.client.GetPlayerCapacity(context.Background(), &alpha.Empty{})
return c.GetCount(), errors.Wrap(err, "could not get player capacity")
return c.GetCount(), a.errs.Wrap(err, "could not get player capacity")
}

// SetPlayerCapacity changes the player capacity to a new value.
func (a *Alpha) SetPlayerCapacity(capacity int64) error {
_, err := a.client.SetPlayerCapacity(context.Background(), &alpha.Count{Count: capacity})
return errors.Wrap(err, "could not set player capacity")
return a.errs.Wrap(err, "could not set player capacity")
}

// PlayerConnect increases the SDK’s stored player count by one, and appends this playerID to status.players.id.
// Will return true and add the playerID to the list of playerIDs if the playerIDs was not already in the
// list of connected playerIDs.
func (a *Alpha) PlayerConnect(id string) (bool, error) {
ok, err := a.client.PlayerConnect(context.Background(), &alpha.PlayerID{PlayerID: id})
return ok.GetBool(), errors.Wrap(err, "could not register connected player")
return ok.GetBool(), a.errs.Wrap(err, "could not register connected player")
}

// PlayerDisconnect Decreases the SDK’s stored player count by one, and removes the playerID from status.players.id.
// Will return true and remove the supplied playerID from the list of connected playerIDs if the
// playerID value exists within the list.
func (a *Alpha) PlayerDisconnect(id string) (bool, error) {
ok, err := a.client.PlayerDisconnect(context.Background(), &alpha.PlayerID{PlayerID: id})
return ok.GetBool(), errors.Wrap(err, "could not register disconnected player")
return ok.GetBool(), a.errs.Wrap(err, "could not register disconnected player")
}

// GetPlayerCount returns the current player count.
func (a *Alpha) GetPlayerCount() (int64, error) {
count, err := a.client.GetPlayerCount(context.Background(), &alpha.Empty{})
return count.GetCount(), errors.Wrap(err, "could not get player count")
return count.GetCount(), a.errs.Wrap(err, "could not get player count")
}

// IsPlayerConnected returns if the playerID is currently connected to the GameServer.
// This is always accurate, even if the value hasn’t been updated to the GameServer status yet.
func (a *Alpha) IsPlayerConnected(id string) (bool, error) {
ok, err := a.client.IsPlayerConnected(context.Background(), &alpha.PlayerID{PlayerID: id})
return ok.GetBool(), errors.Wrap(err, "could not get if player is connected")
return ok.GetBool(), a.errs.Wrap(err, "could not get if player is connected")
}

// GetConnectedPlayers returns the list of the currently connected player ids.
// This is always accurate, even if the value hasn’t been updated to the GameServer status yet.
func (a *Alpha) GetConnectedPlayers() ([]string, error) {
list, err := a.client.GetConnectedPlayers(context.Background(), &alpha.Empty{})
return list.GetList(), errors.Wrap(err, "could not list connected players")
return list.GetList(), a.errs.Wrap(err, "could not list connected players")
}
39 changes: 20 additions & 19 deletions sdks/go/beta.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,33 @@ package sdk
import (
"context"

"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/fieldmaskpb"
"google.golang.org/protobuf/types/known/wrapperspb"

"agones.dev/agones/pkg/sdk/beta"
"agones.dev/agones/pkg/util/errors"
)

// Beta is the struct for Beta SDK functionality.
type Beta struct {
client beta.SDKClient
errs *errors.Errors
}

// newBeta creates a new Beta SDK with the passed in connection.
func newBeta(conn *grpc.ClientConn) *Beta {
return &Beta{
client: beta.NewSDKClient(conn),
}
b := &Beta{client: beta.NewSDKClient(conn)}
b.errs = errors.FromStruct(b)
return b
}

// GetCounterCount returns the Count for a Counter, given the Counter's key (name).
// Will error if the key was not predefined in the GameServer resource on creation.
func (b *Beta) GetCounterCount(key string) (int64, error) {
counter, err := b.client.GetCounter(context.Background(), &beta.GetCounterRequest{Name: key})
if err != nil {
return -1, errors.Wrapf(err, "could not get Counter %s count", key)
return -1, b.errs.Wrapf(err, "could not get Counter %s count", key)
}
return counter.Count, nil
}
Expand All @@ -58,15 +59,15 @@ func (b *Beta) GetCounterCount(key string) (int64, error) {
// value is batched asynchronous any value incremented past the capacity will be silently truncated.
func (b *Beta) IncrementCounter(key string, amount int64) error {
if amount < 0 {
return errors.Errorf("amount must be a positive int64, found %d", amount)
return b.errs.Errorf("amount must be a positive int64, found %d", amount)
}
_, err := b.client.UpdateCounter(context.Background(), &beta.UpdateCounterRequest{
CounterUpdateRequest: &beta.CounterUpdateRequest{
Name: key,
CountDiff: amount,
}})
if err != nil {
return errors.Wrapf(err, "could not increment Counter %s by amount %d", key, amount)
return b.errs.Wrapf(err, "could not increment Counter %s by amount %d", key, amount)
}
return nil
}
Expand All @@ -76,15 +77,15 @@ func (b *Beta) IncrementCounter(key string, amount int64) error {
// Will error if the count is at 0 (to the latest knowledge of the SDK), and no decrement will occur.
func (b *Beta) DecrementCounter(key string, amount int64) error {
if amount < 0 {
return errors.Errorf("amount must be a positive int64, found %d", amount)
return b.errs.Errorf("amount must be a positive int64, found %d", amount)
}
_, err := b.client.UpdateCounter(context.Background(), &beta.UpdateCounterRequest{
CounterUpdateRequest: &beta.CounterUpdateRequest{
Name: key,
CountDiff: amount * -1,
}})
if err != nil {
return errors.Wrapf(err, "could not decrement Counter %s by amount %d", key, amount)
return b.errs.Wrapf(err, "could not decrement Counter %s by amount %d", key, amount)
}
return nil
}
Expand All @@ -98,7 +99,7 @@ func (b *Beta) SetCounterCount(key string, amount int64) error {
Count: wrapperspb.Int64(amount),
}})
if err != nil {
return errors.Wrapf(err, "could not set Counter %s count to amount %d", key, amount)
return b.errs.Wrapf(err, "could not set Counter %s count to amount %d", key, amount)
}
return nil
}
Expand All @@ -108,7 +109,7 @@ func (b *Beta) SetCounterCount(key string, amount int64) error {
func (b *Beta) GetCounterCapacity(key string) (int64, error) {
counter, err := b.client.GetCounter(context.Background(), &beta.GetCounterRequest{Name: key})
if err != nil {
return -1, errors.Wrapf(err, "could not get Counter %s capacity", key)
return -1, b.errs.Wrapf(err, "could not get Counter %s capacity", key)
}
return counter.Capacity, nil
}
Expand All @@ -121,7 +122,7 @@ func (b *Beta) SetCounterCapacity(key string, amount int64) error {
Capacity: wrapperspb.Int64(amount),
}})
if err != nil {
return errors.Wrapf(err, "could not set Counter %s capacity to amount %d", key, amount)
return b.errs.Wrapf(err, "could not set Counter %s capacity to amount %d", key, amount)
}
return nil
}
Expand All @@ -131,7 +132,7 @@ func (b *Beta) SetCounterCapacity(key string, amount int64) error {
func (b *Beta) GetListCapacity(key string) (int64, error) {
list, err := b.client.GetList(context.Background(), &beta.GetListRequest{Name: key})
if err != nil {
return -1, errors.Wrapf(err, "could not get List %s", key)
return -1, b.errs.Wrapf(err, "could not get List %s", key)
}
return list.Capacity, nil
}
Expand All @@ -147,7 +148,7 @@ func (b *Beta) SetListCapacity(key string, amount int64) error {
UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"capacity"}},
})
if err != nil {
return errors.Wrapf(err, "could not set List %s capacity to amount %d", key, amount)
return b.errs.Wrapf(err, "could not set List %s capacity to amount %d", key, amount)
}
return nil
}
Expand All @@ -158,7 +159,7 @@ func (b *Beta) SetListCapacity(key string, amount int64) error {
func (b *Beta) ListContains(key, value string) (bool, error) {
list, err := b.client.GetList(context.Background(), &beta.GetListRequest{Name: key})
if err != nil {
return false, errors.Wrapf(err, "could not get List %s", key)
return false, b.errs.Wrapf(err, "could not get List %s", key)
}
for _, val := range list.Values {
if val == value {
Expand All @@ -173,7 +174,7 @@ func (b *Beta) ListContains(key, value string) (bool, error) {
func (b *Beta) GetListLength(key string) (int, error) {
list, err := b.client.GetList(context.Background(), &beta.GetListRequest{Name: key})
if err != nil {
return -1, errors.Wrapf(err, "could not get List %s", key)
return -1, b.errs.Wrapf(err, "could not get List %s", key)
}
return len(list.Values), nil
}
Expand All @@ -183,7 +184,7 @@ func (b *Beta) GetListLength(key string) (int, error) {
func (b *Beta) GetListValues(key string) ([]string, error) {
list, err := b.client.GetList(context.Background(), &beta.GetListRequest{Name: key})
if err != nil {
return nil, errors.Wrapf(err, "could not get List %s", key)
return nil, b.errs.Wrapf(err, "could not get List %s", key)
}
return list.Values, nil
}
Expand All @@ -194,7 +195,7 @@ func (b *Beta) GetListValues(key string) ([]string, error) {
func (b *Beta) AppendListValue(key, value string) error {
_, err := b.client.AddListValue(context.Background(), &beta.AddListValueRequest{Name: key, Value: value})
if err != nil {
return errors.Wrapf(err, "could not get List %s", key)
return b.errs.Wrapf(err, "could not get List %s", key)
}
return nil
}
Expand All @@ -205,7 +206,7 @@ func (b *Beta) AppendListValue(key, value string) error {
func (b *Beta) DeleteListValue(key, value string) error {
_, err := b.client.RemoveListValue(context.Background(), &beta.RemoveListValueRequest{Name: key, Value: value})
if err != nil {
return errors.Wrapf(err, "could not get List %s", key)
return b.errs.Wrapf(err, "could not get List %s", key)
}
return nil
}
39 changes: 21 additions & 18 deletions sdks/go/beta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ package sdk

import (
"context"
"fmt"
"testing"

"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"

"agones.dev/agones/pkg/sdk/beta"
"agones.dev/agones/pkg/util/errors"
)

func TestBetaGetAndUpdateCounter(t *testing.T) {
Expand Down Expand Up @@ -65,6 +66,7 @@ func TestBetaGetAndUpdateCounter(t *testing.T) {
b := Beta{
client: mock,
}
b.errs = errors.FromStruct(&b)

t.Parallel()

Expand Down Expand Up @@ -206,6 +208,7 @@ func TestBetaGetAndUpdateList(t *testing.T) {
b := Beta{
client: mock,
}
b.errs = errors.FromStruct(&b)

t.Parallel()

Expand Down Expand Up @@ -286,7 +289,7 @@ func (b *betaMock) GetCounter(_ context.Context, in *beta.GetCounterRequest, _ .
if counter, ok := b.counters[in.Name]; ok {
return counter, nil
}
return nil, errors.Errorf("counter not found: %s", in.Name)
return nil, fmt.Errorf("counter not found: %s", in.Name)
}

func (b *betaMock) UpdateCounter(ctx context.Context, in *beta.UpdateCounterRequest, _ ...grpc.CallOption) (*beta.Counter, error) {
Expand All @@ -299,23 +302,23 @@ func (b *betaMock) UpdateCounter(ctx context.Context, in *beta.UpdateCounterRequ
case in.CounterUpdateRequest.CountDiff != 0:
count := counter.Count + in.CounterUpdateRequest.CountDiff
if count < 0 || count > counter.Capacity {
return nil, errors.Errorf("out of range. Count must be within range [0,Capacity]. Found Count: %d, Capacity: %d", count, counter.Capacity)
return nil, fmt.Errorf("out of range. Count must be within range [0,Capacity]. Found Count: %d, Capacity: %d", count, counter.Capacity)
}
counter.Count = count
case in.CounterUpdateRequest.Count != nil:
countSet := in.CounterUpdateRequest.Count.GetValue()
if countSet < 0 || countSet > counter.Capacity {
return nil, errors.Errorf("out of range. Count must be within range [0,Capacity]. Found Count: %d, Capacity: %d", countSet, counter.Capacity)
return nil, fmt.Errorf("out of range. Count must be within range [0,Capacity]. Found Count: %d, Capacity: %d", countSet, counter.Capacity)
}
counter.Count = countSet
case in.CounterUpdateRequest.Capacity != nil:
capacity := in.CounterUpdateRequest.Capacity.GetValue()
if capacity < 0 {
return nil, errors.Errorf("out of range. Capacity must be greater than or equal to 0. Found Capacity: %d", capacity)
return nil, fmt.Errorf("out of range. Capacity must be greater than or equal to 0. Found Capacity: %d", capacity)
}
counter.Capacity = capacity
default:
return nil, errors.Errorf("invalid argument. Malformed CounterUpdateRequest: %v",
return nil, fmt.Errorf("invalid argument. Malformed CounterUpdateRequest: %v",
in.CounterUpdateRequest)
}

Expand All @@ -327,26 +330,26 @@ func (b *betaMock) UpdateCounter(ctx context.Context, in *beta.UpdateCounterRequ
// a list with any pending batched changes applied.
func (b *betaMock) GetList(_ context.Context, in *beta.GetListRequest, _ ...grpc.CallOption) (*beta.List, error) {
if in == nil {
return nil, errors.Errorf("GetListRequest cannot be nil")
return nil, fmt.Errorf("GetListRequest cannot be nil")
}
if list, ok := b.lists[in.Name]; ok {
return list, nil
}
return nil, errors.Errorf("list not found: %s", in.Name)
return nil, fmt.Errorf("list not found: %s", in.Name)
}

// Note: unlike the SDK Server, UpdateList does not batch changes and instead updates the list
// directly.
func (b *betaMock) UpdateList(_ context.Context, in *beta.UpdateListRequest, _ ...grpc.CallOption) (*beta.List, error) {
if in == nil {
return nil, errors.Errorf("UpdateListRequest cannot be nil")
return nil, fmt.Errorf("UpdateListRequest cannot be nil")
}
list, ok := b.lists[in.List.Name]
if !ok {
return nil, errors.Errorf("list not found: %s", in.List.Name)
return nil, fmt.Errorf("list not found: %s", in.List.Name)
}
if in.List.Capacity < 0 || in.List.Capacity > 1000 {
return nil, errors.Errorf("out of range. Capacity must be within range [0,1000]. Found Capacity: %d", in.List.Capacity)
return nil, fmt.Errorf("out of range. Capacity must be within range [0,1000]. Found Capacity: %d", in.List.Capacity)
}
list.Capacity = in.List.Capacity
if len(list.Values) > int(list.Capacity) {
Expand All @@ -360,18 +363,18 @@ func (b *betaMock) UpdateList(_ context.Context, in *beta.UpdateListRequest, _ .
// directly.
func (b *betaMock) AddListValue(_ context.Context, in *beta.AddListValueRequest, _ ...grpc.CallOption) (*beta.List, error) {
if in == nil {
return nil, errors.Errorf("AddListValueRequest cannot be nil")
return nil, fmt.Errorf("AddListValueRequest cannot be nil")
}
list, ok := b.lists[in.Name]
if !ok {
return nil, errors.Errorf("list not found: %s", in.Name)
return nil, fmt.Errorf("list not found: %s", in.Name)
}
if int(list.Capacity) <= len(list.Values) {
return nil, errors.Errorf("out of range. No available capacity. Current Capacity: %d, List Size: %d", list.Capacity, len(list.Values))
return nil, fmt.Errorf("out of range. No available capacity. Current Capacity: %d, List Size: %d", list.Capacity, len(list.Values))
}
for _, val := range list.Values {
if in.Value == val {
return nil, errors.Errorf("already exists. Value: %s already in List: %s", in.Value, in.Name)
return nil, fmt.Errorf("already exists. Value: %s already in List: %s", in.Value, in.Name)
}
}
list.Values = append(list.Values, in.Value)
Expand All @@ -383,11 +386,11 @@ func (b *betaMock) AddListValue(_ context.Context, in *beta.AddListValueRequest,
// directly.
func (b *betaMock) RemoveListValue(_ context.Context, in *beta.RemoveListValueRequest, _ ...grpc.CallOption) (*beta.List, error) {
if in == nil {
return nil, errors.Errorf("RemoveListValueRequest cannot be nil")
return nil, fmt.Errorf("RemoveListValueRequest cannot be nil")
}
list, ok := b.lists[in.Name]
if !ok {
return nil, errors.Errorf("list not found: %s", in.Name)
return nil, fmt.Errorf("list not found: %s", in.Name)
}
for i, val := range list.Values {
if in.Value != val {
Expand All @@ -397,5 +400,5 @@ func (b *betaMock) RemoveListValue(_ context.Context, in *beta.RemoveListValueRe
b.lists[in.Name] = list
return &beta.List{}, nil
}
return nil, errors.Errorf("not found. Value: %s not found in List: %s", in.Value, in.Name)
return nil, fmt.Errorf("not found. Value: %s not found in List: %s", in.Value, in.Name)
}
Loading