-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheconomy.go
114 lines (103 loc) · 2.83 KB
/
economy.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package economy
import (
"context"
"errors"
"github.com/df-mc/dragonfly/server/event"
"github.com/dgraph-io/ristretto"
"github.com/eko/gocache/lib/v4/cache"
ristretto2 "github.com/eko/gocache/store/ristretto/v4"
"github.com/google/uuid"
"github.com/provsalt/economy/handler"
"github.com/provsalt/economy/provider"
)
// Economy is a struct that contains the economy provider and the event handler.
type Economy struct {
p provider.Provider
h handler.EconomyHandler
c cache.Cache[uint64]
}
// ErrEventCancelled is an error that is returned when the event is cancelled.
var ErrEventCancelled = errors.New("event cancelled")
// New creates a new economy instance with a provider.
func New(p provider.Provider, h handler.EconomyHandler) *Economy {
ristrettoCache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1 << 16,
MaxCost: 1 << 8,
BufferItems: 64,
})
if err != nil {
panic(err)
}
ristrettoStore := ristretto2.NewRistretto(ristrettoCache)
cacheManager := cache.New[uint64](ristrettoStore)
return &Economy{
p,
h,
*cacheManager,
}
}
// Handle adds a new handler to handle economy changes.
func (e *Economy) Handle(h handler.EconomyHandler) {
e.h = h
}
// Balance returns the balance of a player.
func (e *Economy) Balance(UUID uuid.UUID) (uint64, error) {
ctx := context.Background()
d, err := e.c.Get(ctx, UUID)
if err != nil {
bal, err := e.p.Balance(UUID.String())
if err != nil {
return 0, err
}
err = e.c.Set(ctx, UUID, bal)
if err != nil {
return 0, err
}
return bal, nil
}
return d, nil
}
// Set sets the balance of a player to a given value.
// You should be using this function to initialize a player's balance or Balance function will throw an error.
func (e *Economy) Set(UUID uuid.UUID, amount uint64) error {
ctx := event.C()
e.h.HandleChange(ctx, UUID, handler.ChangeTypeSet, amount)
if ctx.Cancelled() {
return ErrEventCancelled
}
err := e.c.Set(context.Background(), UUID, amount)
if err != nil {
return err
}
return e.p.Set(UUID.String(), amount)
}
// Increase is a wrapper for Set that increases the balance of a player.
func (e *Economy) Increase(UUID uuid.UUID, amount uint64) error {
ctx := event.C()
e.h.HandleChange(ctx, UUID, handler.ChangeTypeIncrease, amount)
if ctx.Cancelled() {
return ErrEventCancelled
}
bal, err := e.Balance(UUID)
if err != nil {
return err
}
return e.Set(UUID, bal+amount)
}
// Decrease is a wrapper for Set that decreases the balance of a player.
func (e *Economy) Decrease(UUID uuid.UUID, amount uint64) error {
ctx := event.C()
e.h.HandleChange(ctx, UUID, handler.ChangeTypeDecrease, amount)
if ctx.Cancelled() {
return ErrEventCancelled
}
bal, err := e.Balance(UUID)
if err != nil {
return err
}
return e.Set(UUID, bal-amount)
}
// Close closes the economy provider.
func (e *Economy) Close() error {
return e.p.Close()
}