2019-11-27 09:07:48 +07:00
|
|
|
package active
|
2019-11-17 14:47:34 +07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/bwmarrin/discordgo"
|
2019-11-20 12:59:48 +07:00
|
|
|
|
|
|
|
"PermissionGacha/db"
|
2019-11-27 12:06:42 +07:00
|
|
|
"PermissionGacha/modules/config"
|
2019-11-27 09:07:48 +07:00
|
|
|
"PermissionGacha/modules/level"
|
|
|
|
"PermissionGacha/modules/log"
|
2019-11-17 14:47:34 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
var lastMessage = make(map[string]time.Time)
|
|
|
|
var incrementMutex = sync.Mutex{}
|
|
|
|
|
2019-11-27 09:07:48 +07:00
|
|
|
func Reward(s *discordgo.Session, m *discordgo.MessageCreate) {
|
2019-11-27 10:40:31 +07:00
|
|
|
if m.Author.Bot {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-20 12:59:48 +07:00
|
|
|
amountToIncrement := calculateIncrement(m.Author.ID)
|
2019-11-27 12:06:42 +07:00
|
|
|
if m.ChannelID != config.GeneralChannelID {
|
|
|
|
amountToIncrement /= config.NotInGeneralNerf
|
2019-11-18 15:23:40 +07:00
|
|
|
}
|
2019-11-20 12:59:48 +07:00
|
|
|
multiplier, err := db.GetPrestigeMultiplier(m.Author.ID)
|
2019-11-17 14:47:34 +07:00
|
|
|
if err != nil {
|
2019-11-27 09:07:48 +07:00
|
|
|
log.Error(s, err)
|
2019-11-17 14:47:34 +07:00
|
|
|
return
|
|
|
|
}
|
2019-11-20 12:59:48 +07:00
|
|
|
amountToIncrement *= multiplier
|
2019-11-17 14:47:34 +07:00
|
|
|
|
2019-11-20 12:59:48 +07:00
|
|
|
err = db.IncrementXP(m.Author.ID, amountToIncrement)
|
2019-11-18 04:25:34 +07:00
|
|
|
if err != nil {
|
2019-11-27 09:07:48 +07:00
|
|
|
log.Error(s, err)
|
2019-11-20 12:59:48 +07:00
|
|
|
return
|
2019-11-18 04:25:34 +07:00
|
|
|
}
|
2019-11-27 09:07:48 +07:00
|
|
|
level.QueueCheck(m.Author.ID)
|
2019-11-18 04:25:34 +07:00
|
|
|
}
|
|
|
|
|
2019-11-17 14:47:34 +07:00
|
|
|
func calculateIncrement(discordID string) int {
|
|
|
|
incrementMutex.Lock()
|
|
|
|
defer incrementMutex.Unlock()
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
delta := now.Sub(lastMessage[discordID])
|
|
|
|
lastMessage[discordID] = now
|
|
|
|
|
|
|
|
if delta.Seconds() < 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
//Constructed on Desmos:
|
2019-11-19 07:14:26 +07:00
|
|
|
//y=\frac{100}{\left(1+e^{-\left(\frac{x}{2}-5\right)}\right)}
|
|
|
|
return int(100 / (1 + math.Pow(math.E, -((delta.Seconds()/2)-5))))
|
2019-11-17 14:47:34 +07:00
|
|
|
}
|