This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
PermissionGacha/modules/active/xp.go

59 lines
1.2 KiB
Go

package active
2019-11-17 14:47:34 +07:00
import (
"math"
"sync"
"time"
"github.com/bwmarrin/discordgo"
"PermissionGacha/db"
"PermissionGacha/modules/config"
"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{}
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)
2019-11-17 14:47:34 +07:00
if err != nil {
log.Error(s, err)
2019-11-17 14:47:34 +07:00
return
}
amountToIncrement *= multiplier
2019-11-17 14:47:34 +07:00
err = db.IncrementXP(m.Author.ID, amountToIncrement)
2019-11-18 04:25:34 +07:00
if err != nil {
log.Error(s, err)
return
2019-11-18 04:25:34 +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
}