Skip to content

Commit

Permalink
PlayerConnect/Disconnect & related Go SDK functions
Browse files Browse the repository at this point in the history
Includes implementations and unit tests for PlayerConnect,
PlayerDisconnect, GetPlayerCount, IsPlayerConnected and
GetConnectedPlayers.

Conformance tests will come in the next PR.

Work on #1507
  • Loading branch information
markmandel committed May 6, 2020
1 parent b5a2272 commit 11bd744
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 9 deletions.
37 changes: 37 additions & 0 deletions sdks/go/alpha.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
58 changes: 49 additions & 9 deletions sdks/go/alpha_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}

0 comments on commit 11bd744

Please sign in to comment.