aboutsummaryrefslogtreecommitdiff
path: root/user/state.go
blob: 540d496c9b613928fd6b1fbce9e4703eb8ed8d0d (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package user

import (
	"context"
	"errors"
	"sync"
	"time"

	"github.com/nyttikord/gokord/state"
)

var ErrSyncingUnsavedData = errors.New("trying to sync unsaved data")

type CopaingCached struct {
	ID        uint
	DiscordID string
	GuildID   string
	XPs       uint
	XPToAdd   uint
	lastSync  time.Time // time.Time of the lastSync
}

// copaing turns a CopaingCached into a Copaing.
// This operation get the copaing synced with the database.
// It doesn't:
// - save the copaing in the database, use CopaingCached.SaveInDB for that;
// - sync the copaing cached, use CopaingCached.Sync for that.
// TL;DR: don't use this method, unless you know what are you doing.
func (cc *CopaingCached) copaing() *Copaing {
	c := &Copaing{DiscordID: cc.DiscordID, GuildID: cc.GuildID}
	if err := c.load(); err != nil {
		panic(err)
	}
	return c
}

func (cc *CopaingCached) Sync(ctx context.Context) error {
	if cc.mustSave() {
		return ErrSyncingUnsavedData
	}
	synced := FromCopaing(cc.copaing())
	synced.XPs += cc.XPToAdd
	synced.XPToAdd = cc.XPToAdd
	err := synced.Save(ctx)
	if err != nil {
		return err
	}
	*cc = *synced
	return nil
}

func (cc *CopaingCached) Save(ctx context.Context) error {
	state := GetState(ctx)

	state.mu.Lock()
	defer state.mu.Unlock()

	return state.storage.Write(KeyCopaingCachedRaw(cc.GuildID, cc.DiscordID), *cc)
}

func (cc *CopaingCached) SaveInDB(ctx context.Context) error {
	c := cc.copaing()
	c.CopaingXPs = append(c.CopaingXPs, CopaingXP{CopaingID: c.ID, XP: cc.XPToAdd, GuildID: c.GuildID})
	err := c.Save()
	if err != nil {
		return err
	}
	cc.XPToAdd = 0
	return cc.Save(ctx)
}

func (cc *CopaingCached) Delete(ctx context.Context) error {
	c := cc.copaing()
	err := c.Delete()
	if err != nil {
		return err
	}
	state := GetState(ctx)

	state.mu.Lock()
	defer state.mu.Unlock()

	return state.storage.Delete(KeyCopaingCached(c))
}

func (cc *CopaingCached) mustSave() bool {
	return cc.XPToAdd > 0
}

func saveStateInDB(ctx context.Context) error {
	for _, v := range GetState(ctx).storage {
		if v.mustSave() {
			err := v.SaveInDB(ctx)
			if err != nil {
				return err
			}
		}
	}
	return nil
}

func FromCopaing(c *Copaing) *CopaingCached {
	return &CopaingCached{
		ID:        c.ID,
		DiscordID: c.DiscordID,
		GuildID:   c.GuildID,
		XPs:       calcXP(c),
		XPToAdd:   0,
	}
}

const KeyCopaingCachedPrefix = "cc:"

func KeyCopaingCached(c *Copaing) state.Key {
	return KeyCopaingCachedRaw(c.GuildID, c.DiscordID)
}

func KeyCopaingCachedRaw(guildID, copaingID string) state.Key {
	return KeyCopaingCachedPrefix + state.Key(guildID+":"+copaingID)
}

type State struct {
	mu      sync.RWMutex
	storage state.MapStorage[CopaingCached]
}

func NewState() *State {
	return &State{
		storage: state.MapStorage[CopaingCached]{},
	}
}

const ContextKeyState = "state"

func GetState(ctx context.Context) *State {
	return ctx.Value(ContextKeyState).(*State)
}

func SetState(ctx context.Context, state *State) context.Context {
	return context.WithValue(ctx, ContextKeyState, state)
}

func (s *State) Copaing(guildID, copaingID string) (*CopaingCached, error) {
	s.mu.RLock()
	defer s.mu.RUnlock()

	c, err := s.storage.Get(KeyCopaingCachedRaw(guildID, copaingID))
	if err != nil {
		return nil, err
	}
	mC := c.(CopaingCached)
	return &mC, nil
}

func (s *State) Copaings(guild string) []CopaingCached {
	s.mu.RLock()
	defer s.mu.RUnlock()

	var ccs []CopaingCached
	for _, cc := range s.storage {
		if cc.GuildID == guild {
			ccs = append(ccs, cc)
		}
	}
	return ccs
}

func calcXP(c *Copaing) uint {
	var sum uint
	for _, entry := range c.CopaingXPs {
		sum += entry.XP
	}
	return sum
}