Added !poker and auth entry point

This commit is contained in:
bryce
2025-07-15 20:24:16 +12:00
parent 049051a7e1
commit b0be1f99c8
6 changed files with 271 additions and 5 deletions

84
internal/points/points.go Normal file
View File

@@ -0,0 +1,84 @@
package points
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
)
const (
dataDir = "internal/storage"
pointsFile = "points.json"
)
var (
mu sync.Mutex
pts map[string]int
)
const DefaultPoints = 100000
// init or load points from disk
func LoadPoints() error {
mu.Lock()
defer mu.Unlock()
pts = make(map[string]int)
path := filepath.Join(dataDir, pointsFile)
data, err := ioutil.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
return json.Unmarshal(data, &pts)
}
// save balances to disk
func SavePoints() error {
mu.Lock()
data, err := json.MarshalIndent(pts, "", " ")
mu.Unlock()
if err != nil {
return err
}
if err := os.MkdirAll(dataDir, 0755); err != nil {
return err
}
return ioutil.WriteFile(filepath.Join(dataDir, pointsFile), data, 0644)
}
// return point balance for user, init if needed
func GetPoints(user string) int {
mu.Lock()
defer mu.Unlock()
key := strings.ToLower(user)
bal, ok := pts[key]
if !ok {
pts[key] = DefaultPoints
bal = DefaultPoints
}
return bal
}
// add points to user's bal, save & return new bal
func AddPoints(user string, delta int) (int error) {
mu.Lock()
key := strings.ToLower(user)
bal, ok := pts[key]
if !ok {
bal = DefaultPoints
}
bal += delta
pts[key] = bal
mu.Unlock()
if err := SavePoints(); err != nil {
return bal, err
}
return bal, nil
}