Skip to content

Commit

Permalink
feat: Add SetCurrentPlayers
Browse files Browse the repository at this point in the history
Add a new method, SetCurrentPlayers, which sets the player count to the number specified. This can be used as an alternative to PlayerJoined and PlayerLeft.
  • Loading branch information
nweedon-u committed Oct 11, 2023
1 parent 8c46d57 commit 07845b5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
13 changes: 13 additions & 0 deletions game-server-hosting/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,19 @@ func (s *Server) PlayerLeft() int32 {
return s.state.CurrentPlayers
}

// SetCurrentPlayers sets the number of players currently in the game. Can be used as an alternative to PlayerJoined
// and PlayerLeft.
func (s *Server) SetCurrentPlayers(players int32) {
s.stateLock.Lock()
defer s.stateLock.Unlock()

if players < 0 {
players = 0
}

s.state.CurrentPlayers = players
}

// SetMaxPlayers sets the maximum players this server will host. It does not enforce this number,
// it only serves for query / metrics.
func (s *Server) SetMaxPlayers(max int32) {
Expand Down
13 changes: 13 additions & 0 deletions game-server-hosting/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,19 @@ func Test_PlayerJoinedAndLeft(t *testing.T) {
require.Equal(t, int32(0), s.state.CurrentPlayers)
}

func Test_SetCurrentPlayers(t *testing.T) {
t.Parallel()

s, err := New(TypeAllocation)
require.NoError(t, err)

s.SetCurrentPlayers(10)
require.Equal(t, int32(10), s.state.CurrentPlayers)

s.SetCurrentPlayers(-1)
require.Equal(t, int32(0), s.state.CurrentPlayers)
}

func Test_DataSettings(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 07845b5

Please sign in to comment.