Skip to content

Commit

Permalink
Allow a chain of configuration personalizers (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviofernandes004 authored Dec 5, 2023
1 parent 08ac408 commit 0975c48
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
5 changes: 4 additions & 1 deletion base.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ import (
"context"
"database/sql"
"errors"
"plugin"

"github.com/heroiclabs/nakama-common/api"
"github.com/heroiclabs/nakama-common/runtime"
"google.golang.org/protobuf/encoding/protojson"
"plugin"
)

var (
Expand Down Expand Up @@ -68,7 +69,9 @@ type AfterAuthenticateFn func(ctx context.Context, logger runtime.Logger, db *sq

// Hiro provides a type which combines all gameplay systems.
type Hiro interface {
// Deprecated in favor of AddPersonalizer function to compose a chain of configuration personalization.
SetPersonalizer(Personalizer)
AddPersonalizer(personalizer Personalizer)

SetAfterAuthenticate(fn AfterAuthenticateFn)

Expand Down
13 changes: 6 additions & 7 deletions personalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ import (
// definitions defined for the gameplay systems.
type Personalizer interface {
// GetValue returns a config which has been modified for a gameplay system.
GetValue(ctx context.Context, logger runtime.Logger, nk runtime.NakamaModule, system System, identity string) (any, error)
GetValue(ctx context.Context, logger runtime.Logger, nk runtime.NakamaModule, system System, identity string, inConfig any) (any, error)
}

var _ Personalizer = &SatoriPersonalizer{}

type SatoriPersonalizer struct {
}

func (p *SatoriPersonalizer) GetValue(ctx context.Context, logger runtime.Logger, nk runtime.NakamaModule, system System, userID string) (any, error) {
func (p *SatoriPersonalizer) GetValue(ctx context.Context, logger runtime.Logger, nk runtime.NakamaModule, system System, userID string, inCgf any) (any, error) {
var flagName string
switch system.GetType() {
case SystemTypeAchievements:
Expand Down Expand Up @@ -72,16 +72,15 @@ func (p *SatoriPersonalizer) GetValue(ctx context.Context, logger runtime.Logger
return nil, err
}

// If this caller doesn't have the given flag, return no override.
// If this caller doesn't have the given flag, return the current configuration state.
if len(flagList.Flags) < 1 {
return nil, nil
return inCgf, nil
}

config := system.GetConfig()
if err := json.Unmarshal([]byte(flagList.Flags[0].Value), config); err != nil {
if err := json.Unmarshal([]byte(flagList.Flags[0].Value), inCgf); err != nil {
logger.WithField("userID", userID).WithField("error", err.Error()).Error("error merging Satori flag value")
return nil, err
}

return config, nil
return inCgf, nil
}

0 comments on commit 0975c48

Please sign in to comment.