Unverified Commit 8b6c2821 authored by Simon Esposito's avatar Simon Esposito Committed by GitHub
Browse files

Add LeaderboardRecordsHaystack to runtimes (#777)

parent b49aae86
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -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.

+2 −0
Original line number Diff line number Diff line
@@ -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
+30 −0
Original line number Diff line number Diff line
@@ -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.
+55 −0
Original line number Diff line number Diff line
@@ -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.
+1 −0
Original line number Diff line number Diff line
@@ -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