-
Notifications
You must be signed in to change notification settings - Fork 1
/
points.go
49 lines (39 loc) · 1.03 KB
/
points.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
package main
import (
"encoding/binary"
"fmt"
"go.etcd.io/bbolt"
)
func setPoints(id string, amount int) error {
if err := POINTS_DB.Update(func(tx *bbolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte("points"))
if err != nil {
return fmt.Errorf("error creating bucket: %w", err)
}
n := binary.BigEndian.AppendUint64([]byte{}, uint64(amount))
if err := b.Put([]byte(id), n); err != nil {
return fmt.Errorf("error setting points: %w", err)
}
return nil
}); err != nil {
return fmt.Errorf("error in transaction: %w", err)
}
return nil
}
func getPoints(id string) (int, error) {
var points int
if err := POINTS_DB.Update(func(tx *bbolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte("points"))
if err != nil {
return fmt.Errorf("error creating bucket: %w", err)
}
n := b.Get([]byte(id))
if n != nil {
points = int(binary.BigEndian.Uint64(b.Get([]byte(id))))
}
return nil
}); err != nil {
return points, fmt.Errorf("error in transaction: %w", err)
}
return points, nil
}