Skip to content

Commit

Permalink
Local SDK: Counters and Lists (#3660)
Browse files Browse the repository at this point in the history
* local SDK: counter and lists

* Review changes

* Review changes
  • Loading branch information
Kalaiselvi84 authored Mar 1, 2024
1 parent 929a3e7 commit 42b1a92
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
34 changes: 22 additions & 12 deletions pkg/sdkserver/localsdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,19 @@ import (
"sync"
"time"

agonesv1 "agones.dev/agones/pkg/apis/agones/v1"
"agones.dev/agones/pkg/sdk"
"agones.dev/agones/pkg/sdk/alpha"
"agones.dev/agones/pkg/sdk/beta"
"agones.dev/agones/pkg/util/apiserver"
"agones.dev/agones/pkg/util/runtime"
"github.com/fsnotify/fsnotify"
"github.com/mennanov/fmutils"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/proto"
"k8s.io/apimachinery/pkg/util/yaml"

agonesv1 "agones.dev/agones/pkg/apis/agones/v1"
"agones.dev/agones/pkg/sdk"
"agones.dev/agones/pkg/sdk/alpha"
"agones.dev/agones/pkg/sdk/beta"
"agones.dev/agones/pkg/util/apiserver"
"agones.dev/agones/pkg/util/runtime"
)

var (
Expand All @@ -46,7 +47,7 @@ var (
)

func defaultGs() *sdk.GameServer {
return &sdk.GameServer{
gs := &sdk.GameServer{
ObjectMeta: &sdk.GameServer_ObjectMeta{
Name: "local",
Namespace: "default",
Expand All @@ -71,6 +72,17 @@ func defaultGs() *sdk.GameServer {
Ports: []*sdk.GameServer_Status_Port{{Name: "default", Port: 7777}},
},
}

if runtime.FeatureEnabled(runtime.FeatureCountsAndLists) {
gs.Status.Counters = map[string]*sdk.GameServer_Status_CounterStatus{
"rooms": {Count: 1, Capacity: 10},
}
gs.Status.Lists = map[string]*sdk.GameServer_Status_ListStatus{
"players": {Values: []string{"test0", "test1", "test2"}, Capacity: 100},
}
}

return gs
}

// LocalSDKServer type is the SDKServer implementation for when the sidecar
Expand Down Expand Up @@ -144,15 +156,13 @@ func NewLocalSDKServer(filePath string, testSdkName string) (*LocalSDKServer, er
if runtime.FeatureEnabled(runtime.FeaturePlayerTracking) && l.gs.Status.Players == nil {
l.gs.Status.Players = &sdk.GameServer_Status_PlayerStatus{}
}

if runtime.FeatureEnabled(runtime.FeatureCountsAndLists) {
// Adding test Counter and List for the conformance tests (not nil for LocalSDKServer tests)
if l.gs.Status.Counters == nil {
l.gs.Status.Counters = map[string]*sdk.GameServer_Status_CounterStatus{
"rooms": {Count: 1, Capacity: 10}}
l.gs.Status.Counters = make(map[string]*sdk.GameServer_Status_CounterStatus)
}
if l.gs.Status.Lists == nil {
l.gs.Status.Lists = map[string]*sdk.GameServer_Status_ListStatus{
"players": {Values: []string{"test0", "test1", "test2"}, Capacity: 100}}
l.gs.Status.Lists = make(map[string]*sdk.GameServer_Status_ListStatus)
}
}

Expand Down
2 changes: 2 additions & 0 deletions site/content/en/docs/Guides/Client SDKs/local.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ this will be picked up by the local server, and will change the current active c
events for `WatchGameServer()`. This is a useful way of testing functionality, such as changes of state from `Ready` to
`Allocated` in your game server code.

It's important to note that during local development, only specific parts of the GameServer configuration can be modified through SDK calls. For instance, `counters` and `lists` should be placed within the `gameserver.status` section of the configuration file. By making this change, the relevant parts of the configuration are properly exposed and can be accessed through the SDK calls.

{{< alert title="Note" color="info">}}
File modification events can fire more than one for each save (for a variety of reasons),
but it's best practice to ensure handlers that implement `WatchGameServer()` be idempotent regardless, as repeats can
Expand Down

0 comments on commit 42b1a92

Please sign in to comment.