aboutsummaryrefslogtreecommitdiff
path: root/xp/member.go
blob: ad9e6c5e433654ea39df347bbd8a067a96dbbd55 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package xp

import (
	"context"
	"errors"
	"fmt"
	"github.com/anhgelus/gokord"
	"github.com/anhgelus/gokord/utils"
	"github.com/bwmarrin/discordgo"
	"github.com/redis/go-redis/v9"
	"gorm.io/gorm"
	"math"
	"strconv"
	"time"
)

type Copaing struct {
	gorm.Model
	DiscordID string `gorm:"not null"`
	XP        uint   `gorm:"default:0"`
	GuildID   string `gorm:"not null"`
}

type leftCopaing struct {
	ID         uint
	StopDelete chan<- interface{}
}

var (
	redisClient *redis.Client

	leftCopaingsMap = map[string]*leftCopaing{}
)

const (
	LastEvent      = "last_event"
	AlreadyRemoved = "already_removed"
)

func GetCopaing(discordID string, guildID string) *Copaing {
	c := Copaing{DiscordID: discordID, GuildID: guildID}
	if err := c.Load(); err != nil {
		utils.SendAlert(
			"xp/member.go - Loading copaing",
			err.Error(),
			"discord_id",
			discordID,
			"guild_id",
			guildID,
		)
		return nil
	}
	return &c
}

func (c *Copaing) Load() error {
	// check if user left in the past 48 hours
	k := c.GuildID + ":" + c.DiscordID
	l, ok := leftCopaingsMap[k]
	if !ok || l == nil {
		// if not, common first or create
		return gokord.DB.Where("discord_id = ? and guild_id = ?", c.DiscordID, c.GuildID).FirstOrCreate(c).Error
	}
	// else, getting last data
	tmp := Copaing{
		Model: gorm.Model{
			ID: c.ID,
		},
		DiscordID: c.DiscordID,
		GuildID:   c.GuildID,
	}
	if err := gokord.DB.Unscoped().Find(&tmp).Error; err != nil {
		// if error, avoid getting old data and use new one
		utils.SendAlert(
			"xp/member.go - Getting copaing in soft delete", err.Error(),
			"discord_id", c.DiscordID,
			"guild_id", c.DiscordID,
			"last_id", l.ID,
		)
		return gokord.DB.Where("discord_id = ? and guild_id = ?", c.DiscordID, c.GuildID).FirstOrCreate(c).Error
	}
	// resetting internal data
	tmp.Model = gorm.Model{}
	l.StopDelete <- true
	leftCopaingsMap[k] = nil
	// creating new data
	err := gokord.DB.Create(&tmp).Error
	if err != nil {
		return err
	}
	// delete old data
	if err = gokord.DB.Unscoped().Delete(&tmp).Error; err != nil {
		utils.SendAlert(
			"xp/member.go - Deleting copaing in soft delete", err.Error(),
			"discord_id", c.DiscordID,
			"guild_id", c.DiscordID,
			"last_id", l.ID,
		)
	}
	return nil
}

func (c *Copaing) Save() error {
	return gokord.DB.Save(c).Error
}

func (c *Copaing) GenKey(key string) string {
	return fmt.Sprintf("%s:%s:%s", c.GuildID, c.DiscordID, key)
}

func (c *Copaing) AddXP(s *discordgo.Session, m *discordgo.Member, xp uint, fn func(uint, uint)) {
	pastLevel := Level(c.XP)
	old := c.XP
	c.XP += xp
	if err := c.Save(); err != nil {
		utils.SendAlert(
			"xp/level.go - Saving copaing",
			err.Error(),
			"xp",
			c.XP,
			"discord_id",
			c.DiscordID,
			"guild_id",
			c.GuildID,
		)
		c.XP = old
		return
	}
	newLevel := Level(c.XP)
	if newLevel > pastLevel {
		fn(c.XP, newLevel)
		onNewLevel(s, m, newLevel)
	}
}

func (c *Copaing) SetLastEvent() {
	client, err := getRedisClient()
	if err != nil {
		utils.SendAlert("xp/member.go - Getting redis client (set)", err.Error())
		return
	}
	t := time.Now().Unix()
	err = client.Set(context.Background(), c.GenKey(LastEvent), strconv.FormatInt(t, 10), 0).Err()
	if err != nil {
		utils.SendAlert("xp/member.go - Setting last event", err.Error(), "time", t, "base_key", c.GenKey(""))
		return
	}
	err = client.Set(context.Background(), c.GenKey(AlreadyRemoved), "0", 0).Err()
	if err != nil {
		utils.SendAlert(
			"xp/member.go - Setting already removed to 0",
			err.Error(),
			"time",
			t,
			"base_key",
			c.GenKey(""),
		)
		return
	}
}

func (c *Copaing) HourSinceLastEvent() uint {
	client, err := getRedisClient()
	if err != nil {
		utils.SendAlert("xp/member.go - Getting redis client (get)", err.Error())
		return 0
	}
	res := client.Get(context.Background(), c.GenKey(LastEvent))
	if errors.Is(res.Err(), redis.Nil) {
		return 0
	} else if res.Err() != nil {
		utils.SendAlert("xp/member.go - Getting last event", res.Err().Error(), "base_key", c.GenKey(""))
		return 0
	}
	t := time.Now().Unix()
	last, err := strconv.Atoi(res.Val())
	if err != nil {
		utils.SendAlert(
			"xp/member.go - Converting time fetched into int (last event)",
			err.Error(),
			"base_key",
			c.GenKey(""),
			"val",
			res.Val(),
		)
		return 0
	}
	if gokord.Debug {
		return uint(math.Floor(float64(t-int64(last)) / 60)) // not hours of unix, is minutes of unix
	}
	return utils.HoursOfUnix(t - int64(last))
}

func (c *Copaing) AddXPAlreadyRemoved(xp uint) uint {
	client, err := getRedisClient()
	if err != nil {
		utils.SendAlert("xp/member.go - Getting redis client (set)", err.Error())
		return 0
	}
	exp := xp + c.XPAlreadyRemoved()
	err = client.Set(context.Background(), c.GenKey(AlreadyRemoved), exp, 0).Err()
	if err != nil {
		utils.SendAlert(
			"xp/member.go - Setting already removed",
			err.Error(),
			"xp already removed",
			exp,
			"base_key",
			c.GenKey(""),
		)
		return 0
	}
	return exp
}

func (c *Copaing) XPAlreadyRemoved() uint {
	client, err := getRedisClient()
	if err != nil {
		utils.SendAlert("xp/member.go - Getting redis client (xp)", err.Error())
		return 0
	}
	res := client.Get(context.Background(), fmt.Sprintf("%s:%s", c.GenKey(""), AlreadyRemoved))
	if errors.Is(res.Err(), redis.Nil) {
		return 0
	} else if res.Err() != nil {
		utils.SendAlert("xp/member.go - Getting already removed", res.Err().Error(), "base_key", c.GenKey(""))
		return 0
	}
	xp, err := strconv.Atoi(res.Val())
	if err != nil {
		utils.SendAlert(
			"xp/member.go - Converting time fetched into int (already removed)",
			err.Error(),
			"base_key",
			c.GenKey(""),
			"val",
			res.Val(),
		)
		return 0
	}
	if xp < 0 {
		utils.SendAlert(
			"xp/member.go - Assertion xp >= 0",
			"xp is negative",
			"base_key",
			c.GenKey(""),
			"xp",
			xp,
		)
		return 0
	}
	return uint(xp)
}

func (c *Copaing) Reset() {
	gokord.DB.Where("guild_id = ? AND discord_id = ?", c.GuildID, c.DiscordID).Delete(c)
}

func (c *Copaing) AfterDelete(db *gorm.DB) error {
	id := c.ID
	dID := c.DiscordID
	gID := c.GuildID
	k := c.GuildID + ":" + c.DiscordID
	ch := utils.NewTimer(48*time.Hour, func(stop chan<- interface{}) {
		if err := db.Unscoped().Where("id = ?", id).Delete(c).Error; err != nil {
			utils.SendAlert(
				"xp/member.go - Removing copaing from database", err.Error(),
				"discord_id", dID,
				"guild_id", gID,
			)
		}
		stop <- true
		leftCopaingsMap[k] = nil
	})
	leftCopaingsMap[k] = &leftCopaing{id, ch}
	return nil
}

func getRedisClient() (*redis.Client, error) {
	if redisClient == nil {
		var err error
		redisClient, err = gokord.BaseCfg.GetRedisCredentials().Connect()
		return redisClient, err
	}
	return redisClient, nil
}

func CloseRedisClient() {
	if redisClient == nil {
		return
	}
	err := redisClient.Close()
	if err != nil {
		utils.SendAlert("xp/member.go - Closing redis client", err.Error())
	}
}