forked from heroiclabs/nakama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore_multi.go
72 lines (60 loc) · 2.19 KB
/
core_multi.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
// Copyright 2020 The Nakama Authors
//
// 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 server
import (
"context"
"database/sql"
"github.com/heroiclabs/nakama-common/api"
"github.com/heroiclabs/nakama-common/runtime"
"go.uber.org/zap"
)
func MultiUpdate(ctx context.Context, logger *zap.Logger, db *sql.DB, accountUpdates []*accountUpdate, storageWrites StorageOpWrites, walletUpdates []*walletUpdate, updateLedger bool) ([]*api.StorageObjectAck, []*runtime.WalletUpdateResult, error) {
if len(accountUpdates) == 0 && len(storageWrites) == 0 && len(walletUpdates) == 0 {
return nil, nil, nil
}
var storageWriteAcks []*api.StorageObjectAck
var walletUpdateResults []*runtime.WalletUpdateResult
tx, err := db.BeginTx(ctx, nil)
if err != nil {
logger.Error("Could not begin database transaction.", zap.Error(err))
return nil, nil, err
}
if err = ExecuteInTx(ctx, tx, func() error {
storageWriteAcks = nil
walletUpdateResults = nil
// Execute any account updates.
updateErr := updateAccounts(ctx, logger, tx, accountUpdates)
if updateErr != nil {
return updateErr
}
// Execute any storage updates.
storageWriteAcks, updateErr = storageWriteObjects(ctx, logger, tx, true, storageWrites)
if updateErr != nil {
return updateErr
}
// Execute any wallet updates.
walletUpdateResults, updateErr = updateWallets(ctx, logger, tx, walletUpdates, updateLedger)
if updateErr != nil {
return updateErr
}
return nil
}); err != nil {
if e, ok := err.(*statusError); ok {
return nil, walletUpdateResults, e.Cause()
}
logger.Error("Error running multi update.", zap.Error(err))
return nil, walletUpdateResults, err
}
return storageWriteAcks, walletUpdateResults, nil
}