diff --git a/CHANGELOG.md b/CHANGELOG.md index 13fd4975ecdf1699639e4f7d5d333e15bd7e696c..9eb29342b7b9db1b7fdf05aa6f50699a0a7db150 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ The format is based on [keep a changelog](http://keepachangelog.com) and this pr ## [Unreleased] ### Added - Add GroupUsersBan function to all runtimes. +- Add LeaderboardRecordsHaystack to all runtimes. - Add Groups page and associated endpoints to the developer console. - Log a warning when client IP address cannot be resolved. diff --git a/go.mod b/go.mod index 996447fe11cce08bba6fef3b1955a19dc1e03b49..1888d510e5ac2e9439680871a25a6b240523d1de 100644 --- a/go.mod +++ b/go.mod @@ -76,3 +76,5 @@ require ( gopkg.in/gorp.v1 v1.7.2 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) + +replace github.com/heroiclabe/nakama-common => ../nakama-common diff --git a/server/runtime_go_nakama.go b/server/runtime_go_nakama.go index b8a3dc5beed0ceb2c1623558f7c8bb1b1c9029a5..869aee39ba63b26c5c5944b7dabb9ac546f63f8b 100644 --- a/server/runtime_go_nakama.go +++ b/server/runtime_go_nakama.go @@ -2302,6 +2302,36 @@ func (n *RuntimeGoNakamaModule) LeaderboardRecordDelete(ctx context.Context, id, return LeaderboardRecordDelete(ctx, n.logger, n.db, n.leaderboardCache, n.leaderboardRankCache, uuid.Nil, id, ownerID) } +// @group leaderboards +// @summary Fetch the list of leaderboard records around the owner. +// @param ctx(type=context.Context) The context object represents information about the server and requester. +// @param id(type=string) The ID of the leaderboard to list records for. +// @param ownerId(type=string) The owner ID around which to show records. +// @param limit(type=int) Return only the required number of leaderboard records denoted by this limit value. Between 1-100. +// @param expiry(type=int64) Time since epoch in seconds. Must be greater than 0. +// @return leaderboardRecordsHaystack(*api.Leaderboard) A list of leaderboard records. +// @return error(error) An optional error value if an error occurred. +func (n *RuntimeGoNakamaModule) LeaderboardRecordsHaystack(ctx context.Context, id, ownerID string, limit int, expiry int64) ([]*api.LeaderboardRecord, error) { + if id == "" { + return nil, errors.New("expects a leaderboard ID string") + } + + owner, err := uuid.FromString(ownerID) + if err != nil { + return nil, errors.New("expects owner ID to be a valid identifier") + } + + if limit < 1 || limit > 100 { + return nil, errors.New("limit must be 1-100") + } + + if expiry < 0 { + return nil, errors.New("expiry should be time since epoch in seconds and has to be a positive integer") + } + + return LeaderboardRecordsHaystack(ctx, n.logger, n.db, n.leaderboardCache, n.leaderboardRankCache, id, owner, limit, expiry) +} + // @group leaderboards // @summary Fetch one or more leaderboards by ID. // @param ids(type=[]string) The table array of leaderboard ids. diff --git a/server/runtime_lua_nakama.go b/server/runtime_lua_nakama.go index f98acfbea3d3e30b1a1e34fb1f79160b115394fe..fa918551d42887cdb3164bc1a06a5f9a1e2fd3cd 100644 --- a/server/runtime_lua_nakama.go +++ b/server/runtime_lua_nakama.go @@ -234,6 +234,7 @@ func (n *RuntimeLuaNakamaModule) Loader(l *lua.LState) int { "leaderboard_list": n.leaderboardList, "leaderboard_records_list": n.leaderboardRecordsList, "leaderboard_record_write": n.leaderboardRecordWrite, + "leaderboard_records_haystack": n.leaderboardRecordsHaystack, "leaderboard_record_delete": n.leaderboardRecordDelete, "leaderboards_get_id": n.leaderboardsGetId, "purchase_validate_apple": n.purchaseValidateApple, @@ -6427,6 +6428,60 @@ func (n *RuntimeLuaNakamaModule) leaderboardRecordWrite(l *lua.LState) int { return 1 } +// @group leaderboards +// @summary Fetch the list of leaderboard records around the owner. +// @param id(type=string) The ID of the leaderboard to list records for. +// @param ownerId(type=string) The owner ID around which to show records. +// @param limit(type=OptNumber, optional=true, default=10) Return only the required number of leaderboard records denoted by this limit value. Between 1-100. +// @param expiry(type=OptNumber, optional=true, default=0) Time since epoch in seconds. Must be greater than 0. +// @return leaderboardRecordsHaystack(table) A list of leaderboard records. +// @return error(error) An optional error value if an error occurred. +func (n *RuntimeLuaNakamaModule) leaderboardRecordsHaystack(l *lua.LState) int { + id := l.CheckString(1) + if id == "" { + l.ArgError(1, "expects a leaderboard ID string") + return 0 + } + + userID, err := uuid.FromString(l.CheckString(2)) + if err != nil { + l.ArgError(2, "expects user ID to be a valid identifier") + return 0 + } + + limit := l.OptInt(3, 10) + if limit < 1 || limit > 100 { + l.ArgError(3, "limit must be 1-100") + return 0 + } + + expiry := l.OptInt(4, 0) + if expiry < 0 { + l.ArgError(4, "expiry should be time since epoch in seconds and has to be a positive integer") + return 0 + } + + records, err := LeaderboardRecordsHaystack(l.Context(), n.logger, n.db, n.leaderboardCache, n.rankCache, id, userID, limit, int64(expiry)) + if err != nil { + l.RaiseError("error listing leaderboard records haystack: %v", err.Error()) + return 0 + } + + recordsTable := l.CreateTable(len(records), 0) + for i, record := range records { + recordTable, err := recordToLuaTable(l, record) + if err != nil { + l.RaiseError(err.Error()) + return 0 + } + + recordsTable.RawSetInt(i+1, recordTable) + } + l.Push(recordsTable) + + return 1 +} + // @group leaderboards // @summary Remove an owner's record from a leaderboard, if one exists. // @param id(type=string) The unique identifier for the leaderboard to delete from. diff --git a/vendor/modules.txt b/vendor/modules.txt index 0ab3f456e4e608fff56f0393a9c127980c878911..1be27a443c9aebace636252864f9b257e5dbe59d 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -383,3 +383,4 @@ gopkg.in/yaml.v2 # gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b ## explicit gopkg.in/yaml.v3 +# github.com/heroiclabe/nakama-common => ../nakama-common