2019-11-17 14:47:34 +07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"math"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
|
|
|
var db *sql.DB
|
|
|
|
var lastMessage = make(map[string]time.Time)
|
|
|
|
var incrementMutex = sync.Mutex{}
|
2019-11-18 04:25:34 +07:00
|
|
|
var levelUpRequirementCache [30]int
|
2019-11-17 14:47:34 +07:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
var err error
|
|
|
|
db, err = sql.Open("sqlite3", "./bot.db")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
db.SetMaxOpenConns(1)
|
|
|
|
db.Exec("CREATE TABLE xp(id STRING NOT NULL UNIQUE, xp INTEGER NOT NULL)")
|
2019-11-18 04:25:34 +07:00
|
|
|
|
|
|
|
for i := 1; i <= 30; i++ {
|
|
|
|
//Constructed on Desmos:
|
|
|
|
//y=2^{\frac{x}{3}}+.1x^{2}+1.2x-1
|
|
|
|
fi := float64(i)
|
|
|
|
levelUpRequirementCache[i-1] = int(10000 * (math.Pow(2, fi/3) + 0.1*fi*fi + 1.2*fi - 1))
|
|
|
|
}
|
2019-11-17 14:47:34 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func incrementXP(dg *discordgo.Session, discordID string) {
|
|
|
|
amountToIncrement := calculateIncrement(discordID)
|
|
|
|
|
|
|
|
tx, err := db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
logError(dg, "incrementXP", "begin", discordID, err)
|
|
|
|
return
|
|
|
|
}
|
2019-11-17 14:52:44 +07:00
|
|
|
_, err = tx.Exec("INSERT INTO xp(id, xp) VALUES(?, ?) ON CONFLICT(id) DO UPDATE SET xp = xp+?;", discordID, amountToIncrement, amountToIncrement)
|
2019-11-17 14:47:34 +07:00
|
|
|
if err != nil {
|
|
|
|
logError(dg, "incrementXP", "insert", discordID, err)
|
|
|
|
tx.Rollback()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
logError(dg, "incrementXP", "commit", discordID, err)
|
|
|
|
return
|
|
|
|
}
|
2019-11-18 05:11:36 +07:00
|
|
|
addToRoleQueue(discordID)
|
2019-11-17 14:47:34 +07:00
|
|
|
}
|
|
|
|
|
2019-11-18 04:25:34 +07:00
|
|
|
func getLevelAndXP(dg *discordgo.Session, discordID string) (int, int) {
|
|
|
|
rows, err := db.Query("SELECT xp FROM xp WHERE id=?", discordID)
|
|
|
|
if err != nil {
|
|
|
|
logError(dg, "getLevelAndXP", "select", discordID, err)
|
|
|
|
return 1, 0
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
if rows.Next() {
|
|
|
|
var xp int
|
|
|
|
rows.Scan(&xp)
|
|
|
|
return convertXPIntoLevel(xp)
|
|
|
|
}
|
|
|
|
return 1, 0
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
//y=\frac{100}{\left(1+e^{-\left(\frac{x-15}{3}\right)}\right)}
|
|
|
|
return int(100 / (1 + math.Pow(math.E, -((delta.Seconds()-15)/3))))
|
|
|
|
}
|
2019-11-18 04:25:34 +07:00
|
|
|
|
|
|
|
func convertXPIntoLevel(xp int) (int, int) {
|
|
|
|
if xp < levelUpRequirementCache[0] {
|
|
|
|
return 1, xp
|
|
|
|
}
|
|
|
|
for i := 0; i < 30; i++ {
|
|
|
|
if levelUpRequirementCache[i] > xp {
|
|
|
|
return i + 1, xp - levelUpRequirementCache[i-1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 30, xp - levelUpRequirementCache[29]
|
|
|
|
}
|