From 0975c48883c76ff66f1716efd1feedb4ce28bb80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=A1vio=20Fernandes?= Date: Tue, 5 Dec 2023 00:13:58 +0000 Subject: [PATCH] Allow a chain of configuration personalizers (#12) --- base.go | 5 ++++- personalizer.go | 13 ++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/base.go b/base.go index d35a4a8..7380062 100644 --- a/base.go +++ b/base.go @@ -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 ( @@ -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) diff --git a/personalizer.go b/personalizer.go index fe21a3d..be1d34a 100644 --- a/personalizer.go +++ b/personalizer.go @@ -25,7 +25,7 @@ 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{} @@ -33,7 +33,7 @@ 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: @@ -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 }