Milen/db/level.go

41 lines
723 B
Go

package db
import (
"fmt"
"time"
"github.com/jinzhu/gorm"
)
type PlayerLevel struct {
UserID string `gorm:"primary_key"`
LastActive time.Time
XP int64
}
func IncrementXP(userID string, value int64) {
db.Where(&PlayerLevel{UserID: userID}).
UpdateColumn("xp", gorm.Expr("xp + ?", value)).
UpdateColumn("last_active", time.Now())
}
func GetLastActive(userID string) time.Time {
result := PlayerLevel{
UserID: userID,
}
db.Where(&result).First(&result)
return result.LastActive
}
func DeleteAllLevel() {
db.Delete(&PlayerLevel{})
}
func SetXP(userID string, lastActive time.Time, xp int64) {
db.Create(&PlayerLevel{
UserID: userID,
LastActive: lastActive,
XP: xp,
})
}