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

Adds List SDK methods to simple-game-server #3500

Merged
merged 2 commits into from
Nov 13, 2023
Merged
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
4 changes: 2 additions & 2 deletions examples/simple-game-server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ WITH_ARM64 ?= 1
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
project_path := $(dir $(mkfile_path))
ifeq ($(REPOSITORY),)
server_tag := simple-game-server:0.21
server_tag := simple-game-server:0.22
else
server_tag := $(REPOSITORY)/simple-game-server:0.21
server_tag := $(REPOSITORY)/simple-game-server:0.22
endif

ifeq ($(WITH_WINDOWS), 1)
Expand Down
50 changes: 31 additions & 19 deletions examples/simple-game-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,37 @@ for UDP as an echo or "ACK TCP:<text content>" for TCP.

There are some text commands you can send the server to affect its behavior:

| Command | Behavior |
|---------------------|------------------------------------------------------------------------------------------|
| "EXIT" | Causes the game server to exit cleanly calling `os.Exit(0)` |
| "UNHEALTHY" | Stopping sending health checks |
| "GAMESERVER" | Sends back the game server name |
| "READY" | Marks the server as Ready |
| "ALLOCATE" | Allocates the game server |
| "RESERVE" | Reserves the game server after the specified duration |
| "WATCH" | Instructs the game server to log changes to the resource |
| "LABEL" | Sets the specified label on the game server resource |
| "CRASH" | Causes the game server to exit / crash immediately |
| "ANNOTATION" | Sets the specified annotation on the game server resource |
| "PLAYER_CAPACITY" | With one argument, gets the player capacity; with two arguments sets the player capacity |
| "PLAYER_CONNECT" | Connects the specified player to the game server |
| "PLAYER_DISCONNECT" | Disconnects the specified player from the game server |
| "PLAYER_CONNECTED" | Returns true/false depending on whether the specified player is connected |
| "GET_PLAYERS" | Returns a list of the connected players |
| "PLAYER_COUNT" | Returns a count of the connected players |

| Command | Behavior |
|------------------------|------------------------------------------------------------------------------------------|
| "EXIT" | Causes the game server to exit cleanly calling `os.Exit(0)` |
| "UNHEALTHY" | Stopping sending health checks |
| "GAMESERVER" | Sends back the game server name |
| "READY" | Marks the server as Ready |
| "ALLOCATE" | Allocates the game server |
| "RESERVE" | Reserves the game server after the specified duration |
| "WATCH" | Instructs the game server to log changes to the resource |
| "LABEL" | Sets the specified label on the game server resource |
| "CRASH" | Causes the game server to exit / crash immediately |
| "ANNOTATION" | Sets the specified annotation on the game server resource |
| "PLAYER_CAPACITY" | With one argument, gets the player capacity; with two arguments sets the player capacity |
| "PLAYER_CONNECT" | Connects the specified player to the game server |
| "PLAYER_DISCONNECT" | Disconnects the specified player from the game server |
| "PLAYER_CONNECTED" | Returns true/false depending on whether the specified player is connected |
| "GET_PLAYERS" | Returns a list of the connected players |
| "PLAYER_COUNT" | Returns a count of the connected players |
| "GET_COUNTER_COUNT" | Returns a count of a given Counter |
igooch marked this conversation as resolved.
Show resolved Hide resolved
| "INCREMENT_COUNTER" | Increases the count of the given Counter by the given nonnegative integer amount |
| "DECREMENT_COUNTER" | Decreases the count of the given Counter by the given nonnegative integer amount |
| "SET_COUNTER_COUNT" | Sets a count of the given Counter to the given amount |
| "GET_COUNTER_CAPACITY" | Returns the Capacity of the given Counter |
| "SET_COUNTER_CAPACITY" | Sets the Capacity of the given Counter to the given amount |
| "GET_LIST_CAPACITY" | Returns the Capacity of the given List |
| "SET_LIST_CAPACITY | Returns if the List was set to a new Capacity successfully (true) or not (false) |
| "LIST_CONTAINS | Returns true if the given value is in the given List, false otherwise |
| "GET_LIST_LENGTH | Returns the length (number of values) of the given List as a string |
| "GET_LIST_VALUES | Return the values in the given List as a comma delineated string |
| "APPEND_LIST_VALUE | Returns if the given value was successfuly added to the List (true) or not (false) |
| "DELETE_LIST_VALUE | Rreturns if the given value was successfuly deleted from the List (true) or not (false) |

## Configuration

Expand Down
2 changes: 1 addition & 1 deletion examples/simple-game-server/dev-gameserver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ spec:
spec:
containers:
- name: simple-game-server
image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.21
image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.22
2 changes: 1 addition & 1 deletion examples/simple-game-server/fleet-distributed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ spec:
spec:
containers:
- name: simple-game-server
image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.21
image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.22
resources:
requests:
memory: 64Mi
Expand Down
2 changes: 1 addition & 1 deletion examples/simple-game-server/fleet-tcp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ spec:
spec:
containers:
- name: simple-game-server
image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.21
image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.22
env:
# Disables the UDP listener (Enabled by default)
- name: UDP
Expand Down
2 changes: 1 addition & 1 deletion examples/simple-game-server/fleet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ spec:
spec:
containers:
- name: simple-game-server
image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.21
image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.22
resources:
requests:
memory: 64Mi
Expand Down
2 changes: 1 addition & 1 deletion examples/simple-game-server/gameserver-passthrough.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ spec:
spec:
containers:
- name: simple-game-server
image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.21
image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.22
env:
- name: PASSTHROUGH
value: 'TRUE'
Expand Down
2 changes: 1 addition & 1 deletion examples/simple-game-server/gameserver-windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ spec:
spec:
containers:
- name: simple-game-server
image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.21
image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.22
resources:
requests:
memory: 64Mi
Expand Down
2 changes: 1 addition & 1 deletion examples/simple-game-server/gameserver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ spec:
spec:
containers:
- name: simple-game-server
image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.21
image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.22
resources:
requests:
memory: 64Mi
Expand Down
2 changes: 1 addition & 1 deletion examples/simple-game-server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module agones.dev/agones/examples/simple-game-server

go 1.20

require agones.dev/agones v1.35.0
require agones.dev/agones v1.36.0

require (
github.com/golang/protobuf v1.5.3 // indirect
Expand Down
137 changes: 137 additions & 0 deletions examples/simple-game-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,69 @@ func handleResponse(txt string, s *sdk.SDK, cancel context.CancelFunc) (response
}
response, responseError = setCounterCapacity(s, parts[1], parts[2])
addACK = false

case "GET_LIST_CAPACITY":
igooch marked this conversation as resolved.
Show resolved Hide resolved
if len(parts) < 2 {
response = "Invalid GET_LIST_CAPACITY, should have 1 arguments"
responseError = fmt.Errorf("Invalid GET_LIST_CAPACITY, should have 1 arguments")
return
}
response, responseError = getListCapacity(s, parts[1])
addACK = false

case "SET_LIST_CAPACITY":
if len(parts) < 3 {
response = "Invalid SET_LIST_CAPACITY, should have 2 arguments"
responseError = fmt.Errorf("Invalid SET_LIST_CAPACITY, should have 2 arguments")
return
}
response, responseError = setListCapacity(s, parts[1], parts[2])
addACK = false

case "LIST_CONTAINS":
if len(parts) < 3 {
response = "Invalid LIST_CONTAINS, should have 2 arguments"
responseError = fmt.Errorf("Invalid LIST_CONTAINS, should have 2 arguments")
return
}
response, responseError = listContains(s, parts[1], parts[2])
addACK = false

case "GET_LIST_LENGTH":
if len(parts) < 2 {
response = "Invalid GET_LIST_LENGTH, should have 1 arguments"
responseError = fmt.Errorf("Invalid GET_LIST_LENGTH, should have 1 arguments")
return
}
response, responseError = getListLength(s, parts[1])
addACK = false

case "GET_LIST_VALUES":
if len(parts) < 2 {
response = "Invalid GET_LIST_VALUES, should have 1 arguments"
responseError = fmt.Errorf("Invalid GET_LIST_VALUES, should have 1 arguments")
return
}
response, responseError = getListValues(s, parts[1])
addACK = false

case "APPEND_LIST_VALUE":
if len(parts) < 3 {
response = "Invalid APPEND_LIST_VALUE, should have 2 arguments"
responseError = fmt.Errorf("Invalid APPEND_LIST_VALUE, should have 2 arguments")
return
}
response, responseError = appendListValue(s, parts[1], parts[2])
addACK = false

case "DELETE_LIST_VALUE":
if len(parts) < 3 {
response = "Invalid DELETE_LIST_VALUE, should have 2 arguments"
responseError = fmt.Errorf("Invalid DELETE_LIST_VALUE, should have 2 arguments")
return
}
response, responseError = deleteListValue(s, parts[1], parts[2])
addACK = false
}
return
}
Expand Down Expand Up @@ -694,6 +757,80 @@ func setCounterCapacity(s *sdk.SDK, counterName string, amount string) (string,
return strconv.FormatBool(ok), err
}

// getListCapacity returns the Capacity of the given List as a string
func getListCapacity(s *sdk.SDK, listName string) (string, error) {
log.Printf("Retrieving List %s Capacity", listName)
capacity, err := s.Alpha().GetListCapacity(listName)
if err != nil {
log.Printf("Error getting List %s Capacity: %s", listName, err)
}
return strconv.FormatInt(capacity, 10), err
}

// setListCapacity returns if the List was set to a new Capacity successfully (true) or not (false)
func setListCapacity(s *sdk.SDK, listName string, amount string) (string, error) {
amountInt, err := strconv.ParseInt(amount, 10, 64)
if err != nil {
return "false", fmt.Errorf("could not set List %s to unparseable amount %s: %s", listName, amount, err)
}
log.Printf("Setting List %s Capacity to amount %d", listName, amountInt)
ok, err := s.Alpha().SetListCapacity(listName, amountInt)
if err != nil {
log.Printf("Error setting List %s Capacity to amount %d: %s", listName, amountInt, err)
}
return strconv.FormatBool(ok), err
}

// listContains returns true if the given value is in the given List, false otherwise
func listContains(s *sdk.SDK, listName string, value string) (string, error) {
log.Printf("Getting List %s contains value %s", listName, value)
ok, err := s.Alpha().ListContains(listName, value)
if err != nil {
log.Printf("Error getting List %s contains value %s: %s", listName, value, err)
}
return strconv.FormatBool(ok), err
}

// getListLength returns the length (number of values) of the given List as a string
func getListLength(s *sdk.SDK, listName string) (string, error) {
log.Printf("Getting List %s length", listName)
length, err := s.Alpha().GetListLength(listName)
if err != nil {
log.Printf("Error getting List %s length: %s", listName, err)
}
return strconv.Itoa(length), err
}

// getListValues return the values in the given List as a comma delineated string
func getListValues(s *sdk.SDK, listName string) (string, error) {
log.Printf("Getting List %s values", listName)
values, err := s.Alpha().GetListValues(listName)
if err != nil {
log.Printf("Error getting List %s values: %s", listName, err)
}
return strings.Join(values, ",") + "\n", err
}

// appendListValue returns if the given value was successfuly added to the List (true) or not (false)
func appendListValue(s *sdk.SDK, listName string, value string) (string, error) {
log.Printf("Appending Value %s to List %s", value, listName)
ok, err := s.Alpha().AppendListValue(listName, value)
if err != nil {
log.Printf("Error appending Value %s to List %s: %s", value, listName, err)
}
return strconv.FormatBool(ok), err
}

// deleteListValue returns if the given value was successfuly deleted from the List (true) or not (false)
func deleteListValue(s *sdk.SDK, listName string, value string) (string, error) {
log.Printf("Deleting Value %s from List %s", value, listName)
ok, err := s.Alpha().DeleteListValue(listName, value)
if err != nil {
log.Printf("Error deleting Value %s to List %s: %s", value, listName, err)
}
return strconv.FormatBool(ok), err
}

// doHealth sends the regular Health Pings
func doHealth(sdk *sdk.SDK, ctx context.Context) {
tick := time.Tick(2 * time.Second)
Expand Down
2 changes: 1 addition & 1 deletion sdks/go/alpha.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (a *Alpha) GetCounterCapacity(key string) (int64, error) {
return counter.Capacity, nil
}

// SetCounterCapacity sets the capacity for a given count. A capacity of 0 is no capacity.
// SetCounterCapacity sets the capacity for the given Counter. A capacity of 0 is no capacity.
func (a *Alpha) SetCounterCapacity(key string, amount int64) (bool, error) {
_, err := a.client.UpdateCounter(context.Background(), &alpha.UpdateCounterRequest{
CounterUpdateRequest: &alpha.CounterUpdateRequest{
Expand Down