59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package active
|
|
|
|
import (
|
|
"math"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
"PermissionGacha/db"
|
|
"PermissionGacha/modules/config"
|
|
"PermissionGacha/modules/level"
|
|
"PermissionGacha/modules/log"
|
|
)
|
|
|
|
var lastMessage = make(map[string]time.Time)
|
|
var incrementMutex = sync.Mutex{}
|
|
|
|
func Reward(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|
if m.Author.Bot {
|
|
return
|
|
}
|
|
|
|
amountToIncrement := calculateIncrement(m.Author.ID)
|
|
if m.ChannelID != config.GeneralChannelID {
|
|
amountToIncrement /= config.NotInGeneralNerf
|
|
}
|
|
multiplier, err := db.GetPrestigeMultiplier(m.Author.ID)
|
|
if err != nil {
|
|
log.Error(s, err)
|
|
return
|
|
}
|
|
amountToIncrement *= multiplier
|
|
|
|
err = db.IncrementXP(m.Author.ID, amountToIncrement)
|
|
if err != nil {
|
|
log.Error(s, err)
|
|
return
|
|
}
|
|
level.QueueCheck(m.Author.ID)
|
|
}
|
|
|
|
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:
|
|
//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))))
|
|
}
|