Commit 4cc0dac8 authored by Andrei Mihu's avatar Andrei Mihu
Browse files

Better handling of leaderboard and tournament score submissions that result in no changes.

parent c6f055dc
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ The format is based on [keep a changelog](http://keepachangelog.com) and this pr
- Better handling for Lua runtime wallet update operation errors.
- Fix handling of leaderboard record writes that do not need to update the database.
- Fix parsing edge case in TypeScript/JavaScript runtime storage delete operations.
- Better handling of leaderboard and tournament score submissions that result in no changes.

### Changed
- Set JS runtime custom error message as the returned payload message in RPC requests.
+3 −1
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import (
	"encoding/base64"
	"encoding/gob"
	"errors"
	"github.com/jackc/pgconn"
	"strconv"
	"strings"
	"time"
@@ -478,7 +479,8 @@ func LeaderboardRecordWrite(ctx context.Context, logger *zap.Logger, db *sql.DB,
	var dbUpdateTime pgtype.Timestamptz

	if err := db.QueryRowContext(ctx, query, params...).Scan(&dbUsername, &dbScore, &dbSubscore, &dbNumScore, &dbMaxNumScore, &dbMetadata, &dbCreateTime, &dbUpdateTime); err != nil {
		if err != sql.ErrNoRows {
		var pgErr *pgconn.PgError
		if err != sql.ErrNoRows && !(errors.As(err, &pgErr) && pgErr.Code == dbErrorUniqueViolation && strings.Contains(pgErr.Message, "leaderboard_record_pkey")) {
			logger.Error("Error writing leaderboard record", zap.Error(err))
			return nil, err
		}
+10 −1
Original line number Diff line number Diff line
@@ -20,8 +20,10 @@ import (
	"database/sql"
	"encoding/base64"
	"encoding/gob"
	"errors"
	"fmt"
	"github.com/heroiclabs/nakama-common/runtime"
	"github.com/jackc/pgconn"
	"strconv"
	"strings"
	"time"
@@ -35,6 +37,9 @@ import (
	"google.golang.org/protobuf/types/known/wrapperspb"
)

// Internal error used to signal out of transactional wrappers.
var errTournamentWriteNoop = errors.New("tournament write noop")

type TournamentListCursor struct {
	Id string
}
@@ -498,6 +503,10 @@ func TournamentRecordWrite(ctx context.Context, logger *zap.Logger, db *sql.DB,
		if err := ExecuteInTx(ctx, tx, func() error {
			recordQueryResult, err := tx.ExecContext(ctx, query, params...)
			if err != nil {
				var pgErr *pgconn.PgError
				if errors.As(err, &pgErr) && pgErr.Code == dbErrorUniqueViolation && strings.Contains(pgErr.Message, "leaderboard_record_pkey") {
					return errTournamentWriteNoop
				}
				return err
			}

@@ -532,7 +541,7 @@ func TournamentRecordWrite(ctx context.Context, logger *zap.Logger, db *sql.DB,
			}

			return nil
		}); err != nil {
		}); err != nil && err != errTournamentWriteNoop {
			if err == runtime.ErrTournamentWriteMaxNumScoreReached || err == runtime.ErrTournamentMaxSizeReached {
				logger.Info("Aborted writing tournament record", zap.String("reason", err.Error()), zap.String("tournament_id", tournamentId), zap.String("owner_id", ownerId.String()))
			} else {