Skip to content

Commit

Permalink
Update event publishing interface, allow customizable publishers.
Browse files Browse the repository at this point in the history
  • Loading branch information
zyro committed Oct 25, 2024
1 parent 1c093aa commit 827a478
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 19 deletions.
2 changes: 2 additions & 0 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ type Hiro interface {
SetPersonalizer(Personalizer)
AddPersonalizer(personalizer Personalizer)

AddPublisher(publisher Publisher)

SetAfterAuthenticate(fn AfterAuthenticateFn)

// SetCollectionResolver sets a function that may change the storage collection target for Hiro systems. Not typically used.
Expand Down
118 changes: 99 additions & 19 deletions personalizer_satori.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package hiro
import (
"context"
"encoding/json"
"errors"
"strings"
"sync"
"sync/atomic"
Expand All @@ -25,25 +26,7 @@ import (
"github.com/heroiclabs/nakama-common/runtime"
)

type SatoriPublisher interface {
IsPublishAuthenticateRequest() bool
IsPublishAchievementsEvents() bool
IsPublishBaseEvents() bool
IsPublishEconomyEvents() bool
IsPublishEnergyEvents() bool
IsPublishEventLeaderboardsEvents() bool
IsPublishIncentivesEvents() bool
IsPublishInventoryEvents() bool
IsPublishLeaderboardsEvents() bool
IsPublishProgressionEvents() bool
IsPublishStatsEvents() bool
IsPublishTeamsEvents() bool
IsPublishTutorialsEvents() bool
IsPublishUnlockablesEvents() bool
IsPublishAuctionsEvents() bool
}

var _ SatoriPublisher = (*SatoriPersonalizer)(nil)
var _ Publisher = (*SatoriPersonalizer)(nil)

var _ Personalizer = (*SatoriPersonalizer)(nil)

Expand Down Expand Up @@ -235,6 +218,103 @@ type SatoriPersonalizer struct {
cache map[context.Context]*SatoriPersonalizerCache
}

func (p *SatoriPersonalizer) Authenticate(ctx context.Context, logger runtime.Logger, nk runtime.NakamaModule, userID string, created bool) {
if !p.IsPublishAuthenticateRequest() {
return
}
if err := nk.GetSatori().Authenticate(ctx, userID); err != nil && !errors.Is(err, runtime.ErrSatoriConfigurationInvalid) {
logger.WithField("error", err.Error()).Error("failed to authenticate with Satori")
}
}

func (p *SatoriPersonalizer) Send(ctx context.Context, logger runtime.Logger, nk runtime.NakamaModule, userID string, events []*PublisherEvent) {
if len(events) == 0 {
return
}

satoriEvents := make([]*runtime.Event, 0, len(events))
for _, event := range events {
switch event.System.GetType() {
case SystemTypeAchievements:
if !p.IsPublishAchievementsEvents() {
continue
}
case SystemTypeBase:
if !p.IsPublishBaseEvents() {
continue
}
case SystemTypeEconomy:
if !p.IsPublishEconomyEvents() {
continue
}
case SystemTypeEnergy:
if !p.IsPublishEnergyEvents() {
continue
}
case SystemTypeInventory:
if !p.IsPublishInventoryEvents() {
continue
}
case SystemTypeLeaderboards:
if !p.IsPublishLeaderboardsEvents() {
continue
}
case SystemTypeTeams:
if !p.IsPublishTeamsEvents() {
continue
}
case SystemTypeTutorials:
if !p.IsPublishTutorialsEvents() {
continue
}
case SystemTypeUnlockables:
if !p.IsPublishUnlockablesEvents() {
continue
}
case SystemTypeStats:
if !p.IsPublishStatsEvents() {
continue
}
case SystemTypeEventLeaderboards:
if !p.IsPublishEventLeaderboardsEvents() {
continue
}
case SystemTypeProgression:
if !p.IsPublishProgressionEvents() {
continue
}
case SystemTypeIncentives:
if !p.IsPublishIncentivesEvents() {
continue
}
case SystemTypeAuctions:
if !p.IsPublishAuctionsEvents() {
continue
}
case SystemTypeStreaks:
if !p.IsPublishStreaksEvents() {
continue
}
default:
}

satoriEvent := &runtime.Event{
Name: event.Name,
Id: event.Id,
Metadata: event.Metadata,
Value: event.Value,
Timestamp: event.Timestamp,
}
satoriEvents = append(satoriEvents, satoriEvent)
}
if len(satoriEvents) == 0 {
return
}
if err := nk.GetSatori().EventsPublish(ctx, userID, satoriEvents); err != nil {
logger.WithField("error", err.Error()).Error("failed to publish Satori events")
}
}

func NewSatoriPersonalizer(ctx context.Context, opts ...SatoriPersonalizerOption) *SatoriPersonalizer {
s := &SatoriPersonalizer{
cacheMutex: sync.RWMutex{},
Expand Down
55 changes: 55 additions & 0 deletions publisher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2024 Heroic Labs & Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package hiro

import (
"context"

"github.com/heroiclabs/nakama-common/runtime"
)

type PublisherEvent struct {
Name string `json:"name,omitempty"`
Id string `json:"id,omitempty"`
Timestamp int64 `json:"timestamp,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
Value string `json:"value,omitempty"`

// The Hiro system that generated this event.
System System `json:"-"`
// Source ID represents the identifier of the event source, such as an achievement ID.
SourceId string `json:"-"`
// Source represents the configuration of the event source, such as an achievement config.
Source any `json:"-"`
}

// The Publisher describes a service or similar target implementation that wishes to receive and process
// analytics-style events generated server-side by the various available Hiro systems.
//
// Each Publisher may choose to process or ignore each event as it sees fit. It may also choose to buffer
// events for batch processing at its discretion, but must take care to.
//
// Publisher implementations must safely handle concurrent calls.
//
// Implementations must handle any errors or retries internally, callers will not repeat calls in case
// of errors.
type Publisher interface {
// Authenticate is called every time a user authenticates with Hiro. The 'created' flag is true if this
// is a newly created user account, and each implementation may choose to handle this as it chooses.
Authenticate(ctx context.Context, logger runtime.Logger, nk runtime.NakamaModule, userID string, created bool)

// Send is called when there are one or more events generated.
Send(ctx context.Context, logger runtime.Logger, nk runtime.NakamaModule, userID string, events []*PublisherEvent)
}

0 comments on commit 827a478

Please sign in to comment.