Commit 8d472233 authored by Mo Firouz's avatar Mo Firouz
Browse files

Add option to skip updating wallet ledger if not needed. (#259)

parent 76fa8151
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -316,8 +316,8 @@ type NakamaModule interface {
	NotificationSend(ctx context.Context, userID, subject string, content map[string]interface{}, code int, sender string, persistent bool) error
	NotificationsSend(ctx context.Context, notifications []*NotificationSend) error

	WalletUpdate(ctx context.Context, userID string, changeset, metadata map[string]interface{}) error
	WalletsUpdate(ctx context.Context, updates []*WalletUpdate) error
	WalletUpdate(ctx context.Context, userID string, changeset, metadata map[string]interface{}, updateLedger bool) error
	WalletsUpdate(ctx context.Context, updates []*WalletUpdate, updateLedger bool) error
	WalletLedgerUpdate(ctx context.Context, itemID string, metadata map[string]interface{}) (WalletLedgerItem, error)
	WalletLedgerList(ctx context.Context, userID string) ([]WalletLedgerItem, error)

+14 −11
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ import (
	"database/sql"
	"encoding/json"
	"fmt"

	"github.com/cockroachdb/cockroach-go/crdb"
	"github.com/gofrs/uuid"
	"github.com/lib/pq"
@@ -68,7 +69,7 @@ func (w *walletLedger) GetMetadata() map[string]interface{} {
	return w.Metadata
}

func UpdateWallets(ctx context.Context, logger *zap.Logger, db *sql.DB, updates []*walletUpdate) error {
func UpdateWallets(ctx context.Context, logger *zap.Logger, db *sql.DB, updates []*walletUpdate, updateLedger bool) error {
	if len(updates) == 0 {
		return nil
	}
@@ -115,6 +116,7 @@ func UpdateWallets(ctx context.Context, logger *zap.Logger, db *sql.DB, updates
				return err
			}

			if updateLedger {
				changesetData, err := json.Marshal(update.Changeset)
				if err != nil {
					logger.Debug("Error converting new user wallet changeset.", zap.String("user_id", update.UserID.String()), zap.Error(err))
@@ -128,6 +130,7 @@ func UpdateWallets(ctx context.Context, logger *zap.Logger, db *sql.DB, updates
					return err
				}
			}
		}
		return nil
	}); err != nil {
		logger.Error("Error updating wallets.", zap.Error(err))
+4 −4
Original line number Diff line number Diff line
@@ -775,7 +775,7 @@ func (n *RuntimeGoNakamaModule) NotificationsSend(ctx context.Context, notificat
	return NotificationSend(ctx, n.logger, n.db, n.router, ns)
}

func (n *RuntimeGoNakamaModule) WalletUpdate(ctx context.Context, userID string, changeset, metadata map[string]interface{}) error {
func (n *RuntimeGoNakamaModule) WalletUpdate(ctx context.Context, userID string, changeset, metadata map[string]interface{}, updateLedger bool) error {
	uid, err := uuid.FromString(userID)
	if err != nil {
		return errors.New("expects a valid user id")
@@ -793,10 +793,10 @@ func (n *RuntimeGoNakamaModule) WalletUpdate(ctx context.Context, userID string,
		UserID:    uid,
		Changeset: changeset,
		Metadata:  string(metadataBytes),
	}})
	}}, updateLedger)
}

func (n *RuntimeGoNakamaModule) WalletsUpdate(ctx context.Context, updates []*runtime.WalletUpdate) error {
func (n *RuntimeGoNakamaModule) WalletsUpdate(ctx context.Context, updates []*runtime.WalletUpdate, updateLedger bool) error {
	size := len(updates)
	if size == 0 {
		return nil
@@ -825,7 +825,7 @@ func (n *RuntimeGoNakamaModule) WalletsUpdate(ctx context.Context, updates []*ru
		}
	}

	return UpdateWallets(ctx, n.logger, n.db, walletUpdates)
	return UpdateWallets(ctx, n.logger, n.db, walletUpdates, updateLedger)
}

func (n *RuntimeGoNakamaModule) WalletLedgerUpdate(ctx context.Context, itemID string, metadata map[string]interface{}) (runtime.WalletLedgerItem, error) {
+6 −2
Original line number Diff line number Diff line
@@ -2813,11 +2813,13 @@ func (n *RuntimeLuaNakamaModule) walletUpdate(l *lua.LState) int {
		}
	}

	updateLedger := l.OptBool(4, true)

	if err = UpdateWallets(l.Context(), n.logger, n.db, []*walletUpdate{&walletUpdate{
		UserID:    userID,
		Changeset: changesetMap,
		Metadata:  string(metadataBytes),
	}}); err != nil {
	}}, updateLedger); err != nil {
		l.RaiseError(fmt.Sprintf("failed to update user wallet: %s", err.Error()))
	}
	return 0
@@ -2913,7 +2915,9 @@ func (n *RuntimeLuaNakamaModule) walletsUpdate(l *lua.LState) int {
		return 0
	}

	if err := UpdateWallets(l.Context(), n.logger, n.db, updates); err != nil {
	updateLedger := l.OptBool(2, false)

	if err := UpdateWallets(l.Context(), n.logger, n.db, updates, updateLedger); err != nil {
		l.RaiseError(fmt.Sprintf("failed to update user wallet: %s", err.Error()))
	}
	return 0