Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit players when registering a server #2

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion scripts/servers/register/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func createRegisterServerInput(port uint16, name string) types.RegisterServerInp
bonusFrequency := new(uint16)
country := new(string)
info := new(string)
numBots := new(uint16)
numBots := new(uint8)
private := new(bool)
realistic := new(bool)
respawn := new(uint32)
Expand Down
16 changes: 13 additions & 3 deletions servers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestFieldsWithDefaultValuesOnRegisteringNewServer(t *testing.T) {
bonusFrequency := new(uint16)
country := new(string)
info := new(string)
numBots := new(uint16)
numBots := new(uint8)
private := new(bool)
realistic := new(bool)
respawn := new(uint32)
Expand Down Expand Up @@ -153,7 +153,7 @@ func TestMissingFieldsOnRegisteringNewServer(t *testing.T) {
antiCheatOn := new(bool)
bonusFrequency := new(uint16)
info := new(string)
numBots := new(uint16)
numBots := new(uint8)
realistic := new(bool)
respawn := new(uint32)
survival := new(bool)
Expand Down Expand Up @@ -259,6 +259,16 @@ func TestTooLongOSOnRegisteringNewServer(t *testing.T) {
assert.Equal(t, http.StatusBadRequest, returnedCode)
}

func TestMorePlayersThanMaxPlayersOnRegisteringNewServer(t *testing.T) {
router := setupRouter()
registerServerInput := createRegisterServerInput(23073, "Test Name")
registerServerInput.MaxPlayers = 2
registerServerInput.Players = []string{"1", "2", "3"}
invalidInputJson, _ := json.Marshal(registerServerInput)
returnedCode, _ := sendJsonToPostEndpoint(router, "/servers", invalidInputJson)
assert.Equal(t, http.StatusBadRequest, returnedCode)
}

func TestTooLongPlayerNameOnRegisteringNewServer(t *testing.T) {
router := setupRouter()
registerServerInput := createRegisterServerInput(23073, "Test Name")
Expand Down Expand Up @@ -312,7 +322,7 @@ func createRegisterServerInput(port uint16, name string) types.RegisterServerInp
bonusFrequency := new(uint16)
country := new(string)
info := new(string)
numBots := new(uint16)
numBots := new(uint8)
private := new(bool)
realistic := new(bool)
respawn := new(uint32)
Expand Down
8 changes: 6 additions & 2 deletions types/registerServerInput.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ type RegisterServerInput struct {
CurrentMap string `json:"current_map" binding:"required"`
GameStyle string `json:"game_style" binding:"required"`
Info *string `json:"info" binding:"required"`
MaxPlayers uint16 `json:"max_players" binding:"required"`
MaxPlayers uint8 `json:"max_players" binding:"required"`
Name string `json:"name" binding:"required"`
NumBots *uint16 `json:"num_bots" binding:"required"`
NumBots *uint8 `json:"num_bots" binding:"required"`
OS string `json:"os" binding:"required"`
Players []string `json:"players" binding:"required"`
Port uint16 `json:"port" binding:"required"`
Expand Down Expand Up @@ -80,6 +80,10 @@ func ValidateRegisterServerInput(registerServerInput RegisterServerInput) bool {
return false
}

if len(registerServerInput.Players) > int(registerServerInput.MaxPlayers) {
return false
}

for _, playerName := range registerServerInput.Players {
if len(playerName) > MaxPlayerNameSize {
return false
Expand Down
4 changes: 2 additions & 2 deletions types/serverList.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ type Server struct {
GameStyle string `json:"game_style"`
IP string `json:"ip"`
Info string `json:"info"`
MaxPlayers uint16 `json:"max_players"`
MaxPlayers uint8 `json:"max_players"`
Name string `json:"name"`
NumBots uint16 `json:"num_bots"`
NumBots uint8 `json:"num_bots"`
OS string `json:"os"`
Players []string `json:"players"`
Port uint16 `json:"port"`
Expand Down