From 11bd7448e8565f0d79064f53990210bd0a6286ec Mon Sep 17 00:00:00 2001 From: Mark Mandel Date: Wed, 6 May 2020 14:00:19 -0700 Subject: [PATCH] PlayerConnect/Disconnect & related Go SDK functions Includes implementations and unit tests for PlayerConnect, PlayerDisconnect, GetPlayerCount, IsPlayerConnected and GetConnectedPlayers. Conformance tests will come in the next PR. Work on #1507 --- sdks/go/alpha.go | 37 +++++++++++++++++++++++++++ sdks/go/alpha_test.go | 58 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 86 insertions(+), 9 deletions(-) diff --git a/sdks/go/alpha.go b/sdks/go/alpha.go index fdba84edf0..ed67659382 100644 --- a/sdks/go/alpha.go +++ b/sdks/go/alpha.go @@ -46,3 +46,40 @@ 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") } + +// PlayerConnect increases the SDK’s stored player count by one, and appends this playerID to status.players.id. +// Returns true and adds 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.Bool, errors.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.Bool, errors.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.Count, errors.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.Bool, errors.Wrap(err, "could not get if played is connection") +} + +// 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.List, errors.Wrap(err, "could not list connected players") +} diff --git a/sdks/go/alpha_test.go b/sdks/go/alpha_test.go index 3b5a5dc877..4f6fd94a9b 100644 --- a/sdks/go/alpha_test.go +++ b/sdks/go/alpha_test.go @@ -36,26 +36,66 @@ func TestAlphaGetAndSetPlayerCapacity(t *testing.T) { capacity, err := a.GetPlayerCapacity() assert.NoError(t, err) assert.Equal(t, int64(15), capacity) + + playerID := "one" + ok, err := a.PlayerConnect(playerID) + assert.NoError(t, err) + assert.True(t, ok) + assert.Equal(t, playerID, mock.playerConnected) + + count, err := a.GetPlayerCount() + assert.NoError(t, err) + assert.Equal(t, int64(1), count) + + ok, err = a.PlayerDisconnect(playerID) + assert.NoError(t, err) + assert.True(t, ok) + assert.Equal(t, playerID, mock.playerDisconnected) + + // put the player back in + ok, err = a.PlayerConnect(playerID) + assert.NoError(t, err) + assert.True(t, ok) + assert.Equal(t, int64(1), count) + + ok, err = a.IsPlayerConnected(playerID) + assert.NoError(t, err) + assert.True(t, ok, "Player should be connected") + + ok, err = a.IsPlayerConnected("false") + assert.NoError(t, err) + assert.False(t, ok, "Player should not be connected") + + list, err := a.GetConnectedPlayers() + assert.NoError(t, err) + assert.Equal(t, []string{playerID}, list) } type alphaMock struct { - capacity int64 + capacity int64 + playerCount int64 + playerConnected string + playerDisconnected string } -func (a *alphaMock) PlayerConnect(ctx context.Context, in *alpha.PlayerID, opts ...grpc.CallOption) (*alpha.Bool, error) { - panic("implement me") +func (a *alphaMock) PlayerConnect(ctx context.Context, id *alpha.PlayerID, opts ...grpc.CallOption) (*alpha.Bool, error) { + a.playerConnected = id.PlayerID + a.playerCount++ + return &alpha.Bool{Bool: true}, nil } -func (a *alphaMock) PlayerDisconnect(ctx context.Context, in *alpha.PlayerID, opts ...grpc.CallOption) (*alpha.Bool, error) { - panic("implement me") +func (a *alphaMock) PlayerDisconnect(ctx context.Context, id *alpha.PlayerID, opts ...grpc.CallOption) (*alpha.Bool, error) { + a.playerDisconnected = id.PlayerID + a.playerCount-- + return &alpha.Bool{Bool: true}, nil } func (a *alphaMock) IsPlayerConnected(ctx context.Context, id *alpha.PlayerID, opts ...grpc.CallOption) (*alpha.Bool, error) { - panic("implement me") + return &alpha.Bool{Bool: id.PlayerID == a.playerConnected}, nil } -func (a *alphaMock) GetConnectedPlayers(ctx context.Context, id *alpha.Empty, opts ...grpc.CallOption) (*alpha.PlayerIDList, error) { - panic("implement me") +func (a *alphaMock) GetConnectedPlayers(ctx context.Context, in *alpha.Empty, opts ...grpc.CallOption) (*alpha.PlayerIDList, error) { + return &alpha.PlayerIDList{List: []string{a.playerConnected}}, nil } func (a *alphaMock) SetPlayerCapacity(ctx context.Context, in *alpha.Count, opts ...grpc.CallOption) (*alpha.Empty, error) { @@ -68,5 +108,5 @@ func (a *alphaMock) GetPlayerCapacity(ctx context.Context, in *alpha.Empty, opts } func (a *alphaMock) GetPlayerCount(ctx context.Context, in *alpha.Empty, opts ...grpc.CallOption) (*alpha.Count, error) { - panic("implement me") + return &alpha.Count{Count: a.playerCount}, nil }