-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
1093 lines (996 loc) · 27.5 KB
/
database.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"math/big"
"reflect"
"runtime"
"strconv"
"strings"
"time"
"github.com/go-redis/redis"
"github.com/iopred/discordgo"
"github.com/mitchellh/mapstructure"
log "github.com/sirupsen/logrus"
)
var redisClient *redis.Client
const locDensityExpiration time.Duration = 3 * time.Hour
func init() {
GetConfigs()
// client, err := elastic.NewClient(elastic.SetURL("http://10.0.0.2:9200"))
// if err != nil {
// log.Panic(err)
// }
// hook, err := elogrus.NewElasticHook(client, "localhost", log.DebugLevel, "fishy-dev")
// if err != nil {
// log.Panic(err)
// }
// log.AddHook(hook)
log.SetLevel(log.DebugLevel)
// log.AddHook(discordrus.NewHook(
// Config.Webhook,
// log.ErrorLevel,
// &discordrus.Opts{
// Username: "fishy-api1",
// Author: "",
// DisableTimestamp: false,
// TimestampFormat: "Jan 2 15:04:05.00000",
// EnableCustomColors: true,
// CustomLevelColors: &discordrus.LevelColors{
// Debug: 10170623,
// Info: 3581519,
// Warn: 14327864,
// Error: 13631488,
// Panic: 13631488,
// Fatal: 13631488,
// },
// DisableInlineFields: true,
// },
// ))
redisClient = redis.NewClient(&redis.Options{
Addr: Config.Redis.URL,
Password: Config.Redis.Password,
DB: Config.Redis.DB,
})
if err := redisClient.Ping().Err(); err != nil {
log.Fatal(err)
}
}
func logError(ctx string, err error) {
pc, _, _, _ := runtime.Caller(1)
log.WithFields(log.Fields{
"Error": err.Error(),
"Function": runtime.FuncForPC(pc).Name(),
}).Error(ctx)
}
func logInfo(ctx string, err error) {
pc, _, _, _ := runtime.Caller(1)
log.WithFields(log.Fields{
"Error": err.Error(),
"Function": runtime.FuncForPC(pc).Name(),
}).Info(ctx)
}
// logError("", err)
// DBGetLocDensity will get current location density or set default if it doesn't exist in the database
func DBGetLocDensity(userID string) (UserLocDensity, error) {
var LocDensity UserLocDensity
key := LocDensityKey(userID)
// check to see if key exists in db (true == exists, false == doesn't exist)
if keyExists(key) {
// get key
cmd := redisClient.Get(key).Val()
// map key's stored JSON to the UserLocDensity struct
err := json.Unmarshal([]byte(cmd), &LocDensity)
if err != nil {
return UserLocDensity{}, err
}
} else {
// set to default density
LocDensity = UserLocDensity{100, 100, 100}
// turn struct into a JSON byte array and set in redis
err := marshalAndSet(LocDensity, key, locDensityExpiration)
if err != nil {
return UserLocDensity{}, err
}
}
return LocDensity, nil
}
// DBSetLocDensity randomly assigns density to a new location after fishing
func DBSetLocDensity(location string, userID string) (UserLocDensity, error) {
var LocDensity UserLocDensity
key := LocDensityKey(userID)
cmd := redisClient.Get(key).Val()
err := json.Unmarshal([]byte(cmd), &LocDensity)
if err != nil {
return UserLocDensity{}, err
}
r1, err := rand.Int(rand.Reader, big.NewInt(2))
if err != nil {
logError("error generating random number", err)
return UserLocDensity{}, err
}
r2, err := rand.Int(rand.Reader, big.NewInt(99))
if err != nil {
logError("error generating random number", err)
return UserLocDensity{}, err
}
randDensity := int(r1.Int64()) + 1
randLocation := int(r2.Int64())
loc := ""
switch location {
case "lake":
LocDensity.Lake -= randDensity
if randLocation < 50 {
LocDensity.River += randDensity
loc = "river"
} else {
LocDensity.Ocean += randDensity
loc = "ocean"
}
case "river":
LocDensity.River -= randDensity
if randLocation < 50 {
LocDensity.Lake += randDensity
loc = "lake"
} else {
LocDensity.Ocean += randDensity
loc = "ocean"
}
case "ocean":
LocDensity.Ocean -= randDensity
if randLocation < 50 {
LocDensity.Lake += randDensity
loc = "lake"
} else {
LocDensity.River += randDensity
loc = "river"
}
default:
return UserLocDensity{}, errors.New("Invalid Location")
}
go log.WithFields(log.Fields{
"user": userID,
"rand-density": randDensity,
"rand-location": loc,
"current-location": location,
}).Debug("loc-density-change")
go marshalAndSet(LocDensity, key, locDensityExpiration)
return LocDensity, nil
}
// DBGetSetLocDensity returns the location density then sets a new one
func DBGetSetLocDensity(location string, userID string) (UserLocDensity, error) {
var LocDensity UserLocDensity
var err error
LocDensity, err = DBGetLocDensity(userID)
if err != nil {
return UserLocDensity{}, err
}
_, err = DBSetLocDensity(location, userID)
if err != nil {
return UserLocDensity{}, err
}
return LocDensity, nil
}
// DBCheckRateLimit checks the ratelimit of a given command
func DBCheckRateLimit(cmd string, userID string) (bool, time.Duration) {
key := RateLimitKey(cmd, userID)
timeRemaining, _ := redisClient.TTL(key).Result()
if time.Duration(0)*time.Second >= timeRemaining {
return false, time.Duration(0)
}
return true, timeRemaining
}
// DBSetRateLimit sets a new ratelimit for a given command
func DBSetRateLimit(cmd string, userID string, ttl time.Duration) error {
key := RateLimitKey(cmd, userID)
err := redisClient.Set(key, "", ttl).Err()
if err != nil {
return err
}
return nil
}
// DBGetLocation returns a users current location
func DBGetLocation(userID string) string {
key := LocationKey(userID)
cmd, err := redisClient.Get(key).Result()
if err != nil {
if err = redisClient.Set(key, "lake", 0).Err(); err != nil { // set default location if no key exists
logError("Error setting location key", err)
return ""
}
return "lake"
}
return cmd
}
// DBSetLocation sets a users location
func DBSetLocation(userID string, loc string) error {
return redisClient.Set(LocationKey(userID), loc, 0).Err()
}
// DBGetBiteRate returns the biterate for a given user
func DBGetBiteRate(userID string, locDen UserLocDensity, loc string) int64 {
switch loc {
case "lake":
return calcBiteRate(int64(locDen.Lake))
case "river":
return calcBiteRate(int64(locDen.River))
case "ocean":
return calcBiteRate(int64(locDen.Ocean))
}
log.WithFields(log.Fields{
"User": userID,
"Location": loc,
}).Error("User does not have a known location")
return 0
}
//
func DBGetCatchRate(userID string) (int64, error) {
inv := DBGetInventory(userID)
switch inv.Rod.Current {
case 200:
return 50, nil
case 201:
return 55, nil
case 202:
return 60, nil
case 203:
return 70, nil
case 204:
return 80, nil
}
return 50, nil
}
//
func DBGetFishRate(userID string) (int64, error) {
inv := DBGetInventory(userID)
switch inv.Hook.Current {
case 300:
return 50, nil
case 301:
return 60, nil
case 302:
return 70, nil
case 303:
return 80, nil
case 304:
return 90, nil
}
return 50, nil
}
// DBGetInventory returns a users inventory tiers
func DBGetInventory(userID string) UserItems {
var items UserItems
conv := map[string]map[string]interface{}{}
key := InventoryKey(userID)
if DBInventoryCheckExists(userID) {
keys, err := redisClient.HGetAll(key).Result()
if err != nil {
logError("Unable to retrieve inventory", err)
return UserItems{}
}
for i, e := range keys {
c, err := strconv.Atoi(e)
if err != nil {
logInfo("Unable to convert inventory tier to int", err)
redisClient.HDel(key, i)
continue
}
conv[i] = map[string]interface{}{"current": c, "owned": DBGetOwnedItems(userID, i)}
}
err = mapstructure.Decode(conv, &items)
if err != nil {
logError("Unable to decode inventory map", err)
return UserItems{}
}
return items
}
return defaultUserItems
}
var defaultUserItems = UserItems{
UserItem{0, []int{}},
UserItem{0, []int{}},
UserItem{0, []int{}},
UserItem{0, []int{}},
UserItem{0, []int{}},
}
// DBInventoryCheckExists makes sure a user has an inventory key before modifying it
func DBInventoryCheckExists(userID string) bool {
key := InventoryKey(userID)
if keyExists(key) {
return true
}
redisClient.HMSet(key, map[string]interface{}{"bait": 0, "rod": 0, "hook": 0, "vehicle": 0, "baitbox": 0})
return false
}
// DBGetGlobalScore gets a users global xp (score) for a specific user
func DBGetGlobalScore(userID string) float64 {
exp, err := redisClient.ZScore(ScoreGlobalKey, userID).Result()
if err != nil {
z := redis.Z{Score: 0, Member: userID}
redisClient.ZAdd(ScoreGlobalKey, z)
return float64(0)
}
return exp
}
// DBGiveGlobalScore increments a users global exp
func DBGiveGlobalScore(userID string, amt float64) error {
err := redisClient.ZIncrBy(ScoreGlobalKey, amt, userID).Err()
if err != nil {
logError("Unable to increment global exp", err)
return err
}
return nil
}
// DBGetGlobalScorePage gets a specific page of global scores
func DBGetGlobalScorePage(p int) ([]redis.Z, error) {
if p == 1 {
return redisClient.ZRevRangeWithScores(ScoreGlobalKey, 0, 9).Result()
}
return redisClient.ZRevRangeWithScores(ScoreGlobalKey, int64(p-1)*10, int64(p*10)-1).Result()
}
// DBGetGlobalScoreRank returns a users global score ranking
func DBGetGlobalScoreRank(u string) (int64, float64) {
return redisClient.ZRevRank(ScoreGlobalKey, u).Val(), redisClient.ZScore(ScoreGlobalKey, u).Val()
}
// DBGetGuildScore gets a users global xp for a specific user
func DBGetGuildScore(userID string, guildID string) float64 {
exp, err := redisClient.ZScore(ScoreGuildKey(guildID), userID).Result()
if err != nil {
z := redis.Z{Score: 0, Member: userID}
redisClient.ZAdd(ScoreGuildKey(guildID), z)
return float64(0)
}
return exp
}
// DBGiveGuildScore increments a users global exp
func DBGiveGuildScore(userID string, amt float64, guildID string) error {
err := redisClient.ZIncrBy(ScoreGuildKey(guildID), amt, userID).Err()
if err != nil {
logError("Unable to increment guild exp", err)
return err
}
return nil
}
// DBGetGuildScorePage gets a specific page of a guilds scores
func DBGetGuildScorePage(g string, p int) ([]redis.Z, error) {
if p == 1 {
return redisClient.ZRevRangeWithScores(ScoreGuildKey(g), 1, 10).Result()
}
return redisClient.ZRevRangeWithScores(ScoreGuildKey(g), int64(p*10)+1, int64(p+1)*10).Result()
}
// DBGetGuildScoreRank returns a users guild score ranking
func DBGetGuildScoreRank(u string, g string) (int64, float64) {
return redisClient.ZRevRank(ScoreGuildKey(g), u).Val(), redisClient.ZScore(ScoreGuildKey(g), u).Val()
}
// DBGetItemTier gets a users specific item tier
func DBGetItemTier(userID string, item string) int {
DBInventoryCheckExists(userID)
tier, err := strconv.Atoi(redisClient.HGet(InventoryKey(userID), item).Val())
if err != nil {
logError("Unable to convert item tier to int", err)
return 0
}
return tier
}
// DBEditItemTier changes a users item tier unsafely (without checking for tier progression)
func DBEditItemTier(userID string, item string, tier string) error {
DBInventoryCheckExists(userID)
if allowedItems[item] {
return redisClient.HSet(InventoryKey(userID), item, tier).Err()
}
return fmt.Errorf("Item %s not allowed", item)
}
var allowedItems = map[string]bool{
"rod": true,
"hook": true,
"vehicle": true,
"baitbox": true,
"bait": true,
}
// DBEditItemTiersSafe changes a users item tiers and checks for progression
func DBEditItemTiersSafe(userID string, tiers map[string]string) error {
DBInventoryCheckExists(userID)
var err error
v := reflect.ValueOf(DBGetInventory(userID))
typ := v.Type()
for i := 0; i < v.NumField(); i++ {
for item, tier := range tiers {
fi := typ.Field(i)
if tagv := fi.Tag.Get("json"); tagv == item {
currentTier, _ := strconv.Atoi(v.Field(i).Interface().(string))
newTier, _ := strconv.Atoi(tier)
if currentTier != newTier-1 {
return errors.New("User does not own prior tier of " + item)
}
err = DBEditItemTier(userID, item, tier)
if err != nil {
return err
}
}
}
}
return errors.New("Item not found")
}
// DBEditItemTiersUnsafe changes a users item tiers and does not check for progression
func DBEditItemTiersUnsafe(userID string, tiers map[string]string) error {
DBInventoryCheckExists(userID)
var err error
v := reflect.ValueOf(DBGetInventory(userID))
typ := v.Type()
for i := 0; i < v.NumField(); i++ {
for item, tier := range tiers {
fi := typ.Field(i)
if tagv := fi.Tag.Get("json"); tagv == item {
err = DBEditItemTier(userID, item, tier)
if err != nil {
return err
}
}
}
}
return errors.New("Item not found")
}
// DBCheckMissingInventory returns a list of items a user does not own that you can't fish without
func DBCheckMissingInventory(userID string) []string {
DBInventoryCheckExists(userID)
var items []string
inv := redisClient.HGetAll(InventoryKey(userID)).Val()
for k, v := range inv {
if v == "0" {
if k == "rod" || k == "hook" {
items = append(items, k)
}
}
}
return items
}
// DBBlackListUser sfsafd
func DBBlackListUser(userID string) {
redisClient.Set(BlackListKey(userID), "", 0)
}
// DBUnblackListUser sfsafd
func DBUnblackListUser(userID string) {
redisClient.Del(BlackListKey(userID), "")
}
// DBCheckBlacklist checks if a user is blacklisted
func DBCheckBlacklist(userID string) bool {
return keyExists(BlackListKey(userID))
}
// DBStartGatherBait starts the bait gathering timeout
func DBStartGatherBait(userID string) error {
return redisClient.Set(GatherBaitKey(userID), "", GatherBaitTimeout).Err()
}
// DBCheckGatherBait checks to see whether or not a user is currently gathering bait
func DBCheckGatherBait(userID string) (bool, time.Duration) {
key := GatherBaitKey(userID)
timeRemaining := redisClient.TTL(key).Val()
if time.Duration(0)*time.Second >= timeRemaining {
return false, time.Duration(0)
}
return true, timeRemaining
}
// DBTrackUser tracks a name, discriminator and avatar associated with a given user id
func DBTrackUser(user *discordgo.User) {
redisClient.HMSet(UserTrackKey(user.ID), map[string]interface{}{"name": user.Username, "discriminator": user.Discriminator, "avatar": discordgo.EndpointUserAvatar(user.ID, user.Avatar)})
}
// DBGetTrackedUser returns the username and discriminator of a user
func DBGetTrackedUser(userID string) string {
user, err := redisClient.HMGet(UserTrackKey(userID), "name", "discriminator").Result()
if err != nil {
logError("Unable to retrieve tracked user", err)
return ""
}
return fmt.Sprintf("%v#%v", user[0], user[1])
}
// DBGetTrackedUserAvatar returns the URL for the avatar of a tracked user
func DBGetTrackedUserAvatar(userID string) string {
avatar, err := redisClient.HGet(UserTrackKey(userID), "avatar").Result()
if err != nil {
logError("Unable to retrieve tracked user avatar", err)
return ""
}
return avatar
}
// DBIncInvEE [REDACTED]
func DBIncInvEE(userID string) {
redisClient.Incr(NoInvEEKey(userID))
}
// DBGetInvEE [REDACTED]
func DBGetInvEE(userID string) int {
e, _ := strconv.Atoi(redisClient.Get(NoInvEEKey(userID)).Val())
return e
}
// DBGetGlobalStats gets a users global stats
func DBGetGlobalStats(userID string) UserStats {
var stats UserStats
var conv = map[string]interface{}{}
key := GlobalStatsKey(userID)
if keyExists(key) {
data := redisClient.HGetAll(key).Val()
for i, e := range data {
switch strings.ToLower(i) {
case "garbage", "fish", "casts":
c, err := strconv.Atoi(e)
if err != nil {
logError("error converting stat value to int", err)
redisClient.HSet(key, i, 0)
conv[i] = 0
continue
}
conv[i] = c
case "avglength":
c, err := strconv.ParseFloat(e, 64)
if err != nil {
logError("error converting stat value to int", err)
redisClient.HSet(key, i, 0)
conv[i] = float64(0)
continue
}
conv[i] = c
}
}
err := mapstructure.Decode(conv, &stats)
if err != nil {
logError("Unable to decode guild stats map", err)
return UserStats{}
}
return stats
}
redisClient.HMSet(key, map[string]interface{}{"garbage": 0, "fish": 0, "avgLength": 0, "casts": 0})
return UserStats{0, 0, 0, 0}
}
// DBGetGuildStats gets a users guild stats
func DBGetGuildStats(userID, guildID string) UserStats {
var stats UserStats
var conv = map[string]interface{}{}
key := GuildStatsKey(userID, guildID)
if keyExists(key) {
data := redisClient.HGetAll(key).Val()
for i, e := range data {
switch strings.ToLower(i) {
case "garbage", "fish", "casts":
c, err := strconv.Atoi(e)
if err != nil {
logError("error converting stat value to int", err)
redisClient.HSet(key, i, 0)
conv[i] = 0
continue
}
conv[i] = c
case "avglength":
c, err := strconv.ParseFloat(e, 64)
if err != nil {
logError("error converting stat value to int", err)
redisClient.HSet(key, i, 0)
conv[i] = float64(0)
continue
}
conv[i] = c
}
}
err := mapstructure.Decode(conv, &stats)
if err != nil {
logError("Unable to decode guild stats map", err)
return UserStats{}
}
return stats
}
redisClient.HMSet(key, map[string]interface{}{"garbage": 0, "fish": 0, "avgLength": 0, "casts": 0})
return UserStats{0, 0, 0, 0}
}
// DBAddGlobalCast adds one to a users global cast stats
func DBAddGlobalCast(userID string) {
err := redisClient.HIncrBy(GlobalStatsKey(userID), "casts", 1).Err()
if err != nil {
logError("Unable to increment global casts stat", err)
return
}
}
// DBAddGuildCast adds one to a users guild cast stats
func DBAddGuildCast(userID, guildID string) {
err := redisClient.HIncrBy(GuildStatsKey(userID, guildID), "casts", 1).Err()
if err != nil {
logError("Unable to increment guild casts stat", err)
return
}
}
// DBAddCast adds both guild and global cast stats
func DBAddCast(userID, guildID string) {
go DBAddGlobalCast(userID)
go DBAddGuildCast(userID, guildID)
}
// DBAddGlobalGarbage adds one to a users global garbage stats
func DBAddGlobalGarbage(userID string) {
err := redisClient.HIncrBy(GlobalStatsKey(userID), "garbage", 1).Err()
if err != nil {
logError("Unable to increment global garbage stat", err)
return
}
}
// DBAddGuildGarbage adds one to a users guild garbage stats
func DBAddGuildGarbage(userID, guildID string) {
err := redisClient.HIncrBy(GuildStatsKey(userID, guildID), "garbage", 1).Err()
if err != nil {
logError("Unable to increment guild garbage stat", err)
return
}
}
// DBAddGarbage adds both guild and global garbage stats
func DBAddGarbage(userID, guildID string) {
go DBAddGlobalGarbage(userID)
go DBAddGuildGarbage(userID, guildID)
}
//
func DBIncrGlobalAvgFishStats(userID string, len float64) {
key := GlobalStatsKey(userID)
stats := DBGetGlobalStats(userID)
totL := float64(stats.Fish) * float64(stats.AvgLength)
stats.Fish++
totL += len
stats.AvgLength = totL / float64(stats.Fish)
err := redisClient.HSet(key, "fish", stats.Fish).Err()
if err != nil {
logError("Unable to set new fish stat", err)
return
}
err = redisClient.HSet(key, "avgLength", stats.AvgLength).Err()
if err != nil {
logError("Unable to set new avgLength stat", err)
return
}
}
//
func DBIncrGuildAvgFishStats(userID, guildID string, len float64) {
key := GuildStatsKey(userID, guildID)
stats := DBGetGuildStats(userID, guildID)
totL := float64(stats.Fish) * float64(stats.AvgLength)
stats.Fish++
totL += len
stats.AvgLength = totL / float64(stats.Fish)
err := redisClient.HSet(key, "fish", stats.Fish).Err()
if err != nil {
logError("Unable to set new fish total fish", err)
return
}
err = redisClient.HSet(key, "avgLength", stats.AvgLength).Err()
if err != nil {
logError("Unable to set new avgLength stat", err)
return
}
}
// DBIncrAvgFishStats increments guild and global fish stats
func DBIncrAvgFishStats(userID, guildID string, len float64) {
go DBIncrGlobalAvgFishStats(userID, len)
go DBIncrGuildAvgFishStats(userID, guildID, len)
}
// DBGetFishInv returns an array of catches, representing a user's fish inventory
func DBGetFishInv(userID string) FishInv {
key := FishInvKey(userID)
if !keyExists(key) {
redisClient.HMSet(key, map[string]interface{}{"fish": 0, "garbage": 0, "legendaries": 0, "worth": 0})
return FishInv{0, 0, 0, 0}
}
conv := map[string]int{}
inv := FishInv{}
keys := redisClient.HGetAll(key).Val()
for i, e := range keys {
c, err := strconv.Atoi(e)
if err != nil {
logError("Unable to convert fish stats to int", err)
return FishInv{0, 0, 0, 0}
}
conv[i] = c
}
mapstructure.Decode(conv, &inv)
return inv
}
//
func DBAddFishToInv(userID, catchType string, worth float64) error {
key := FishInvKey(userID)
if DBGetInvSize(userID) >= DBGetInvCapacity(userID) {
return errors.New("Inventory full")
}
switch catchType {
case "fish":
redisClient.HIncrBy(key, "fish", 1)
case "garbage":
redisClient.HIncrBy(key, "garbage", 1)
case "legendary":
redisClient.HIncrBy(key, "legendary", 1)
}
redisClient.HIncrBy(key, "worth", int64(worth))
return nil
}
//
func DBGetInvSize(userID string) int {
key := FishInvKey(userID)
fish, _ := strconv.Atoi(redisClient.HGet(key, "fish").Val())
legendary, _ := strconv.Atoi(redisClient.HGet(key, "legendary").Val())
//fmt.Println(fish, legendary)
return fish + legendary
}
//
func DBSellFish(userID string) map[string]string {
key := FishInvKey(userID)
fish := redisClient.HGetAll(key).Val()
redisClient.HMSet(key, map[string]interface{}{"fish": 0, "garbage": 0, "legendaries": 0, "worth": 0})
return fish
}
//
func DBGetInvCapacity(userID string) int {
inv := DBGetInventory(userID)
switch inv.Vehicle.Current {
case 401:
return 50
case 402:
return 100
case 403:
return 250
case 404:
return 500
}
return 25
}
//
func DBGetBaitCapacity(userID string) int {
inv := DBGetInventory(userID)
switch inv.BaitBox.Current {
case 501:
return 50
case 502:
return 75
case 503:
return 100
case 504:
return 150
}
return 25
}
//
func DBGetBaitInv(userID string) BaitInv {
key := BaitInvKey(userID)
conv := map[string]int{}
var bait BaitInv
if keyExists(key) {
inv, err := redisClient.HGetAll(key).Result()
if err != nil {
logError("Unable to get bait inventory", err)
return BaitInv{}
}
for i, e := range inv {
b, err := strconv.Atoi(e)
if err != nil {
logInfo("Fixing broken bait type amt", errors.New("unable to convert bait tier amount to int"))
redisClient.HSet(key, i, 0)
conv["t"+i] = 0
continue
}
conv["t"+i] = b
}
err = mapstructure.Decode(conv, &bait)
if err != nil {
logError("Unable to decode bait inventory map to struct", err)
return BaitInv{}
}
return bait
}
err := redisClient.HMSet(key, map[string]interface{}{"1": 0, "2": 0, "3": 0, "4": 0, "5": 0}).Err()
if err != nil {
logError("Unable to set default bait inventory", err)
return BaitInv{}
}
return BaitInv{0, 0, 0, 0, 0}
}
//
func DBGetBaitUsage(userID string) int {
conv := map[string]int{}
var bait BaitInv
key := BaitInvKey(userID)
inv, err := redisClient.HGetAll(key).Result()
if err != nil {
return 0
}
for i, e := range inv {
b, err := strconv.Atoi(e)
if err != nil {
logError("Unable to convert bait to int", err)
return 0
}
conv["t"+i] = b
}
err = mapstructure.Decode(conv, &bait)
if err != nil {
logError("Unable to decode bait inventory map to struct", err)
return 0
}
return bait.T1 + bait.T2 + bait.T3 + bait.T4 + bait.T5
}
//
func DBAddBait(userID string, tier, amt int) (int, int64, error) {
cur, err := DBGetBaitTierAmount(userID, tier)
if err != nil {
logError("Unable to get current bait tier amount", err)
return -1, -1, err
}
cap := DBGetBaitCapacity(userID)
if cur+amt > cap && amt != -1 {
return -1, -1, fmt.Errorf("%v exceeds the bait limit of %v", cur+amt, cap)
}
tot, err := redisClient.HIncrBy(BaitInvKey(userID), strconv.Itoa(tier), int64(amt)).Result()
return cur, tot, err
}
//
func DBGetBaitTierAmount(userID string, tier int) (int, error) {
key := BaitInvKey(userID)
if keyExists(key) {
if a := redisClient.HGet(BaitInvKey(userID), strconv.Itoa(tier)).Val(); a != "" {
return strconv.Atoi(a)
}
}
DBSetBaitDefault(userID)
return 0, nil
}
//
func DBSetBaitDefault(userID string) BaitInv {
d := map[string]interface{}{
"1": 0,
"2": 0,
"3": 0,
"4": 0,
"5": 0,
}
redisClient.HMSet(BaitInvKey(userID), d)
return BaitInv{0, 0, 0, 0, 0}
}
//
func DBGetCurrentBaitTier(userID string) int {
key := BaitTierKey(userID)
if keyExists(key) {
tier, err := strconv.Atoi(redisClient.Get(key).Val())
if err != nil {
logError("Unable to parse current bait tier", err)
return 0
}
return tier
}
err := redisClient.Set(key, 1, 0).Err()
if err != nil {
logError("Unable to set current bait tier", err)
return 0
}
return 1
}
//
func DBSetCurrentBaitTier(userID string, tier float64) error {
return redisClient.Set(BaitTierKey(userID), tier, 0).Err()
}
//
func DBGetCurrentBaitAmt(userID string) (int, error) {
tier := DBGetCurrentBaitTier(userID)
key := BaitInvKey(userID)
n, err := strconv.Atoi(redisClient.HGet(key, fmt.Sprintf("%v", tier)).Val())
if err != nil {
redisClient.HSet(key, fmt.Sprintf("%v", tier), 0)
return DBGetCurrentBaitAmt(userID)
}
return n, nil
}
//
func DBLoseBait(userID string) (int, error) {
_, rem, err := DBAddBait(userID, DBGetCurrentBaitTier(userID), -1)
if err != nil {
logError("Error subtracting bait after successful catch", err)
return -1, errors.New("Error subtracting bait")
}
return int(rem), nil
}
//
func DBGetOwnedItems(userID, item string) []int {
key := OwnedItemKey(userID, item)
if keyExists(key) {
conv := []int{}
owned, err := redisClient.SMembers(key).Result()
if err != nil {
logError("error retrieving owned items", err)
return []int{}
}
for _, e := range owned {
if e != "" {
c, err := strconv.Atoi(e)
if err != nil {
logError("unable to convert owned item to int", err)
continue
}
conv = append(conv, c)
}
}
return conv
}
return []int{}
}