-
Notifications
You must be signed in to change notification settings - Fork 10
/
base.go
395 lines (337 loc) · 14.6 KB
/
base.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// Copyright 2023 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"
"database/sql"
"errors"
"plugin"
"github.com/heroiclabs/nakama-common/api"
"github.com/heroiclabs/nakama-common/runtime"
"google.golang.org/protobuf/encoding/protojson"
)
var (
ErrInternal = runtime.NewError("internal error occurred", 13) // INTERNAL
ErrBadInput = runtime.NewError("bad input", 3) // INVALID_ARGUMENT
ErrFileNotFound = runtime.NewError("file not found", 3)
ErrNoSessionUser = runtime.NewError("no user ID in session", 3) // INVALID_ARGUMENT
ErrNoSessionID = runtime.NewError("no session ID in session", 3) // INVALID_ARGUMENT
ErrNoSessionUsername = runtime.NewError("no username in session", 3) // INVALID_ARGUMENT
ErrPayloadDecode = runtime.NewError("cannot decode json", 13) // INTERNAL
ErrPayloadEmpty = runtime.NewError("payload should not be empty", 3) // INVALID_ARGUMENT
ErrPayloadEncode = runtime.NewError("cannot encode json", 13) // INTERNAL
ErrPayloadInvalid = runtime.NewError("payload is invalid", 3) // INVALID_ARGUMENT
ErrSessionUser = runtime.NewError("user ID in session", 3) // INVALID_ARGUMENT
ErrSystemNotAvailable = runtime.NewError("system not available", 13) // INTERNAL
ErrSystemNotFound = runtime.NewError("system not found", 13) // INTERNAL
)
// The BaseSystem provides various small features which aren't large enough to be in their own gameplay systems.
type BaseSystem interface {
System
// RateApp uses the SMTP configuration to receive feedback from players via email.
RateApp(ctx context.Context, logger runtime.Logger, nk runtime.NakamaModule, userID, username string, score uint32, message string) (err error)
// SetDevicePrefs sets push notification tokens on a user's account so push messages can be received.
SetDevicePrefs(ctx context.Context, logger runtime.Logger, db *sql.DB, userID, deviceID, pushTokenAndroid, pushTokenIos string, preferences map[string]bool) (err error)
// Sync processes an operation to update the server with offline state changes.
Sync(ctx context.Context, logger runtime.Logger, nk runtime.NakamaModule, userID string, req *SyncRequest) (resp *SyncResponse, err error)
}
// BaseSystemConfig is the data definition for the BaseSystem type.
type BaseSystemConfig struct {
RateAppSmtpAddr string `json:"rate_app_smtp_addr,omitempty"` // "smtp.gmail.com"
RateAppSmtpUsername string `json:"rate_app_smtp_username,omitempty"` // "email@domain"
RateAppSmtpPassword string `json:"rate_app_smtp_password,omitempty"` // "password"
RateAppSmtpEmailFrom string `json:"rate_app_smtp_email_from,omitempty"` // "[email protected]"
RateAppSmtpEmailFromName string `json:"rate_app_smtp_email_from_name,omitempty"` // My Game Company
RateAppSmtpEmailSubject string `json:"rate_app_smtp_email_subject,omitempty"` // "RateApp Feedback"
RateAppSmtpEmailTo string `json:"rate_app_smtp_email_to,omitempty"` // "[email protected]"
RateAppSmtpPort int `json:"rate_app_smtp_port,omitempty"` // 587
RateAppTemplate string `json:"rate_app_template"` // HTML email template
}
type AfterAuthenticateFn func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, session *api.Session) error
type CollectionResolverFn func(ctx context.Context, systemType SystemType, collection string) (string, error)
// Hiro provides a type which combines all gameplay systems.
type Hiro interface {
// SetPersonalizer is deprecated in favor of AddPersonalizer function to compose a chain of configuration personalization.
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.
SetCollectionResolver(fn CollectionResolverFn)
GetAchievementsSystem() AchievementsSystem
GetBaseSystem() BaseSystem
GetEconomySystem() EconomySystem
GetEnergySystem() EnergySystem
GetInventorySystem() InventorySystem
GetLeaderboardsSystem() LeaderboardsSystem
GetStatsSystem() StatsSystem
GetTeamsSystem() TeamsSystem
GetTutorialsSystem() TutorialsSystem
GetUnlockablesSystem() UnlockablesSystem
GetEventLeaderboardsSystem() EventLeaderboardsSystem
GetProgressionSystem() ProgressionSystem
GetIncentivesSystem() IncentivesSystem
GetAuctionsSystem() AuctionsSystem
GetStreaksSystem() StreaksSystem
}
// The SystemType identifies each of the gameplay systems.
type SystemType uint
const (
SystemTypeUnknown SystemType = iota
SystemTypeBase
SystemTypeEnergy
SystemTypeUnlockables
SystemTypeTutorials
SystemTypeLeaderboards
SystemTypeStats
SystemTypeTeams
SystemTypeInventory
SystemTypeAchievements
SystemTypeEconomy
SystemTypeEventLeaderboards
SystemTypeProgression
SystemTypeIncentives
SystemTypeAuctions
SystemTypeStreaks
)
// Init initializes a Hiro type with the configurations provided.
func Init(ctx context.Context, logger runtime.Logger, nk runtime.NakamaModule, initializer runtime.Initializer, binPath string, licenseKey string, configs ...SystemConfig) (Hiro, error) {
// Open the plugin.
binFile, err := nk.ReadFile(binPath)
if err != nil {
return nil, err
}
//goland:noinspection GoUnhandledErrorResult
defer binFile.Close()
p, err := plugin.Open(binFile.Name())
if err != nil {
return nil, err
}
// Look up the required initialisation function.
f, err := p.Lookup("Init")
if err != nil {
return nil, err
}
// Ensure the function has the correct types.
fn, ok := f.(func(context.Context, runtime.Logger, runtime.NakamaModule, runtime.Initializer, *protojson.MarshalOptions, *protojson.UnmarshalOptions, string, ...SystemConfig) (Hiro, error))
if !ok {
return nil, errors.New("error reading hiro-gdk.Init function in Go module")
}
marshaler := &protojson.MarshalOptions{
UseEnumNumbers: true,
UseProtoNames: true,
EmitUnpopulated: false,
}
unmarshaler := &protojson.UnmarshalOptions{DiscardUnknown: false}
return fn(ctx, logger, nk, initializer, marshaler, unmarshaler, licenseKey, configs...)
}
// The SystemConfig describes the configuration that each gameplay system must use to configure itself.
type SystemConfig interface {
// GetType returns the runtime type of the gameplay system.
GetType() SystemType
// GetConfigFile returns the configuration file used for the data definitions in the gameplay system.
GetConfigFile() string
// GetRegister returns true if the gameplay system's RPCs should be registered with the game server.
GetRegister() bool
// GetExtra returns the extra parameter used to configure the gameplay system.
GetExtra() any
}
var _ SystemConfig = &systemConfig{}
type systemConfig struct {
systemType SystemType
configFile string
register bool
extra any
}
func (sc *systemConfig) GetType() SystemType {
return sc.systemType
}
func (sc *systemConfig) GetConfigFile() string {
return sc.configFile
}
func (sc *systemConfig) GetRegister() bool {
return sc.register
}
func (sc *systemConfig) GetExtra() any {
return sc.extra
}
// OnReward is a function which can be used by each gameplay system to provide an override reward.
type OnReward[T any] func(ctx context.Context, logger runtime.Logger, nk runtime.NakamaModule, userID, sourceID string, source T, rewardConfig *EconomyConfigReward, reward *Reward) (*Reward, error)
// A System is a base type for a gameplay system.
type System interface {
// GetType provides the runtime type of the gameplay system.
GetType() SystemType
// GetConfig returns the configuration type of the gameplay system.
GetConfig() any
}
// UsernameOverrideFn can be used to provide a different username generation strategy from the default in Nakama server.
type UsernameOverrideFn func() string
// WithAchievementsSystem configures an AchievementsSystem type and optionally registers its RPCs with the game server.
func WithAchievementsSystem(configFile string, register bool) SystemConfig {
return &systemConfig{
systemType: SystemTypeAchievements,
configFile: configFile,
register: register,
}
}
// WithBaseSystem configures a BaseSystem type and optionally registers its RPCs with the game server.
func WithBaseSystem(configFile string, register bool, usernameOverride ...UsernameOverrideFn) SystemConfig {
return &systemConfig{
systemType: SystemTypeBase,
configFile: configFile,
register: register,
extra: usernameOverride,
}
}
// WithEconomySystem configures an EconomySystem type and optionally registers its RPCs with the game server.
func WithEconomySystem(configFile string, register bool, ironSrcPrivKey ...string) SystemConfig {
return &systemConfig{
systemType: SystemTypeEconomy,
configFile: configFile,
register: register,
extra: ironSrcPrivKey,
}
}
// WithEnergySystem configures an EnergySystem type and optionally registers its RPCs with the game server.
func WithEnergySystem(configFile string, register bool) SystemConfig {
return &systemConfig{
systemType: SystemTypeEnergy,
configFile: configFile,
register: register,
}
}
// WithInventorySystem configures an InventorySystem type and optionally registers its RPCs with the game server.
func WithInventorySystem(configFile string, register bool) SystemConfig {
return &systemConfig{
systemType: SystemTypeInventory,
configFile: configFile,
register: register,
}
}
// WithLeaderboardsSystem configures a LeaderboardsSystem type.
func WithLeaderboardsSystem(configFile string, register bool, validateWriteScore ...ValidateWriteScoreFn) SystemConfig {
return &systemConfig{
systemType: SystemTypeLeaderboards,
configFile: configFile,
register: register,
extra: validateWriteScore,
}
}
// WithStatsSystem configures a StatsSystem type and optionally registers its RPCs with the game server.
func WithStatsSystem(configFile string, register bool) SystemConfig {
return &systemConfig{
systemType: SystemTypeStats,
configFile: configFile,
register: register,
}
}
// WithTeamsSystem configures a TeamsSystem type and optionally registers its RPCs with the game server.
func WithTeamsSystem(configFile string, register bool, validateCreateTeam ...ValidateCreateTeamFn) SystemConfig {
return &systemConfig{
systemType: SystemTypeTeams,
configFile: configFile,
register: register,
extra: validateCreateTeam,
}
}
// WithTutorialsSystem configures a TutorialsSystem type and optionally registers its RPCs with the game server.
func WithTutorialsSystem(configFile string, register bool) SystemConfig {
return &systemConfig{
systemType: SystemTypeTutorials,
configFile: configFile,
register: register,
}
}
// WithUnlockablesSystem configures an UnlockablesSystem type and optionally registers its RPCs with the game server.
func WithUnlockablesSystem(configFile string, register bool) SystemConfig {
return &systemConfig{
systemType: SystemTypeUnlockables,
configFile: configFile,
register: register,
}
}
// WithEventLeaderboardsSystem configures an EventLeaderboardsSystem type and optionally registers its RPCs with the game server.
func WithEventLeaderboardsSystem(configFile string, register bool) SystemConfig {
return &systemConfig{
systemType: SystemTypeEventLeaderboards,
configFile: configFile,
register: register,
}
}
// WithProgressionSystem configures a ProgressionSystem type and optionally registers its RPCs with the game server.
func WithProgressionSystem(configFile string, register bool) SystemConfig {
return &systemConfig{
systemType: SystemTypeProgression,
configFile: configFile,
register: register,
}
}
// WithIncentivesSystem configures a IncentivesSystem type and optionally registers its RPCs with the game server.
func WithIncentivesSystem(configFile string, register bool) SystemConfig {
return &systemConfig{
systemType: SystemTypeIncentives,
configFile: configFile,
register: register,
}
}
// WithAuctionsSystem configures a AuctionsSystem type and optionally registers its RPCs with the game server.
func WithAuctionsSystem(configFile string, register bool) SystemConfig {
return &systemConfig{
systemType: SystemTypeAuctions,
configFile: configFile,
register: register,
}
}
// WithStreaksSystem configures a StreaksSystem type and optionally registers its RPCs with the game server.
func WithStreaksSystem(configFile string, register bool) SystemConfig {
return &systemConfig{
systemType: SystemTypeStreaks,
configFile: configFile,
register: register,
}
}
// UnregisterRpc clears the implementation of one or more RPCs registered in Nakama by Hiro gameplay systems with a
// no-op version (http response 404). This is useful to remove individual RPCs which you do not want to be callable by
// game clients:
//
// hiro.UnregisterRpc(initializer, hiro.RpcId_RPC_ID_ECONOMY_GRANT, hiro.RpcId_RPC_ID_INVENTORY_GRANT)
//
// The behaviour of `initializer.RegisterRpc` in Nakama is last registration wins. It's recommended to use UnregisterRpc
// only after `hiro.Init` has been executed.
func UnregisterRpc(initializer runtime.Initializer, ids ...RpcId) error {
noopFn := func(context.Context, runtime.Logger, *sql.DB, runtime.NakamaModule, string) (string, error) {
return "", runtime.NewError("not found", 12) // GRPC - UNIMPLEMENTED
}
for _, id := range ids {
if err := initializer.RegisterRpc(id.String(), noopFn); err != nil {
return err
}
}
return nil
}
// UnregisterDebugRpc clears the implementation of ALL debug RPCs registered in Nakama by Hiro gameplay systems with
// a no-op version (http response 404). This is useful to remove debug RPCs if you do not want them to be callable
// by game clients:
//
// hiro.UnregisterDebugRpc(initializer)
//
// The behaviour of `initializer.RegisterRpc` in Nakama is last registration wins. It's recommended to use
// UnregisterDebugRpc only after `hiro.Init` has been executed.
func UnregisterDebugRpc(initializer runtime.Initializer) error {
ids := []RpcId{
RpcId_RPC_ID_EVENT_LEADERBOARD_DEBUG_FILL,
RpcId_RPC_ID_EVENT_LEADERBOARD_DEBUG_RANDOM_SCORES,
}
return UnregisterRpc(initializer, ids...)
}