parent
48b6994dc9
commit
4887d55707
@ -0,0 +1,60 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
prestigeCache = make(map[string]int)
|
||||
prestigeMutex sync.Mutex
|
||||
)
|
||||
|
||||
func SetPrestigeMultiplier(discordID string, newMultiplier int) error {
|
||||
prestigeMutex.Lock()
|
||||
defer prestigeMutex.Unlock()
|
||||
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
return fmt.Errorf("db: SetPrestige: underlying SQL error on 'begin': %w", err)
|
||||
}
|
||||
_, err = tx.Exec("REPLACE INTO prestige(id, multiplier) VALUES(?,?)", discordID, newMultiplier)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return fmt.Errorf("db: SetPrestige: underlying SQL error on 'replace/prestige': %w", err)
|
||||
}
|
||||
_, err = tx.Exec("REPLACE INTO xp(id, xp) VALUES(?,0)", discordID)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return fmt.Errorf("db: SetPrestige: underlying SQL error on 'replace/xp': %w", err)
|
||||
}
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return fmt.Errorf("db: SetPrestige: underlying SQL error on 'commit': %w", err)
|
||||
}
|
||||
prestigeCache[discordID] = newMultiplier
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetPrestigeMultiplier(discordID string) (int, error) {
|
||||
prestigeMutex.Lock()
|
||||
defer prestigeMutex.Unlock()
|
||||
|
||||
if prestigeCache[discordID] > 0 {
|
||||
return prestigeCache[discordID], nil
|
||||
}
|
||||
|
||||
rows, err := db.Query("SELECT multiplier FROM prestige WHERE id=?", discordID)
|
||||
if err != nil {
|
||||
return 1, fmt.Errorf("db: GetPrestigeMultiplier: underlying SQL error on 'select' (%s): %w", discordID, err)
|
||||
}
|
||||
defer rows.Close()
|
||||
if rows.Next() {
|
||||
var multiplier int
|
||||
rows.Scan(&multiplier)
|
||||
prestigeCache[discordID] = multiplier
|
||||
return multiplier, nil
|
||||
}
|
||||
prestigeCache[discordID] = 1
|
||||
return 1, nil
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package db
|
||||
|
||||
import "fmt"
|
||||
|
||||
func RewardShard(discordID string, count int) error {
|
||||
_, err := db.Exec("INSERT INTO shard(id, shard) VALUES(?, 1) ON CONFLICT(id) DO UPDATE SET shard = shard+1", discordID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("db: RewardShard: underlying SQL error on 'insert' (%s, %d): %w", discordID, count, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetShard(discordID string) (int, error) {
|
||||
rows, err := db.Query("SELECT shard FROM shard WHERE id=?", discordID)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("db: GetShard: underlying SQL error on 'select': %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
if rows.Next() {
|
||||
var shard int
|
||||
rows.Scan(&shard)
|
||||
return shard, nil
|
||||
}
|
||||
return 0, nil
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package db
|
||||
|
||||
import "fmt"
|
||||
|
||||
func HasStarredBefore(discordID string) (bool, error) {
|
||||
rows, err := db.Query("SELECT id FROM starred WHERE id=?", discordID)
|
||||
if err != nil {
|
||||
return true, fmt.Errorf("db: HasStarredBefore: underlying SQL error on 'select' (%s): %w", discordID, err)
|
||||
}
|
||||
defer rows.Close()
|
||||
return rows.Next(), nil
|
||||
}
|
||||
|
||||
func SetStarred(discordID string) error {
|
||||
_, err := db.Exec("INSERT INTO starred(id) VALUES(?)", discordID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("db: SetStarred: underlying SQL error on 'insert' (%s): %w", discordID, err)
|
||||
}
|
||||
return nil
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package db
|
||||
|
||||
import "fmt"
|
||||
|
||||
func IncrementXP(discordID string, amount int) error {
|
||||
_, err := db.Exec(
|
||||
"INSERT INTO xp(id, xp) VALUES(?, ?) ON CONFLICT(id) DO UPDATE SET xp = xp+?",
|
||||
discordID, amount, amount,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("db: IncrementXP: underlying SQL error on 'insert' (%s, %d): %w", discordID, amount, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetXP(discordID string) (int, error) {
|
||||
rows, err := db.Query("SELECT xp FROM xp WHERE id=?", discordID)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("db: GetXP: underlying SQL error on 'select' (%s): %w", discordID, err)
|
||||
}
|
||||
defer rows.Close()
|
||||
if rows.Next() {
|
||||
var xp int
|
||||
rows.Scan(&xp)
|
||||
return xp, nil
|
||||
}
|
||||
return 0, nil
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
func rewardShard(dg *discordgo.Session, discordID string) {
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
logError(dg, "rewardShard", "begin", discordID, err)
|
||||
return
|
||||
}
|
||||
_, err = tx.Exec("INSERT INTO shard(id, shard) VALUES(?, 1) ON CONFLICT(id) DO UPDATE SET shard = shard+1;", discordID)
|
||||
if err != nil {
|
||||
logError(dg, "rewardShard", "insert", discordID, err)
|
||||
tx.Rollback()
|
||||
return
|
||||
}
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
logError(dg, "rewardShard", "commit", discordID, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func getShard(dg *discordgo.Session, discordID string) int {
|
||||
rows, err := db.Query("SELECT shard FROM shard WHERE id=?", discordID)
|
||||
if err != nil {
|
||||
logError(dg, "getShard", "select", discordID, err)
|
||||
return 0
|
||||
}
|
||||
defer rows.Close()
|
||||
if rows.Next() {
|
||||
var shard int
|
||||
rows.Scan(&shard)
|
||||
return shard
|
||||
}
|
||||
return 0
|
||||
}
|
Reference in New Issue