Bugfixes. Optimizations/refactor. Add redis for player-cache. Add docker files. Replace sqlite dep. Single-Calc for existing players. Game-Metrics in JSON.
This commit is contained in:
55
internal/cache/cache.go
vendored
Normal file
55
internal/cache/cache.go
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"InfantrySkillCalculator/models"
|
||||
"InfantrySkillCalculator/utils"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
var ctx = context.Background()
|
||||
|
||||
func GetValue(key string) (string, error) {
|
||||
val, err := models.Cache.Get(ctx, key).Result()
|
||||
if err != nil {
|
||||
return "", err // cache miss or error
|
||||
} else {
|
||||
return val, nil // cache hit
|
||||
}
|
||||
}
|
||||
|
||||
func SetValue(key string, value interface{}) error {
|
||||
return models.Cache.Set(ctx, key, value, utils.PlayerCacheLifetime).Err()
|
||||
}
|
||||
|
||||
func SetScore(playerId uint, gameTag string, score float32) error {
|
||||
key := GetPlayerCacheKey(playerId, gameTag)
|
||||
return SetValue(key, score)
|
||||
}
|
||||
|
||||
func GetScore(playerId uint, gameTag string) (float32, error) {
|
||||
key := GetPlayerCacheKey(playerId, gameTag)
|
||||
val, err := GetValue(key)
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return -1.0, nil // cache miss
|
||||
} else if err != nil {
|
||||
return -1.0, err // cache error
|
||||
} else {
|
||||
return utils.StringToFloat(val), nil // cache hit
|
||||
}
|
||||
}
|
||||
|
||||
func DeleteScore(playerId uint, gameTag string) error {
|
||||
key := GetPlayerCacheKey(playerId, gameTag)
|
||||
return models.Cache.Del(ctx, key).Err()
|
||||
}
|
||||
|
||||
func PurgeCache() error {
|
||||
return models.Cache.FlushAll(ctx).Err()
|
||||
}
|
||||
|
||||
func GetPlayerCacheKey(playerId uint, gameTag string) string {
|
||||
return fmt.Sprintf("player:%d:game:%s", playerId, gameTag)
|
||||
}
|
||||
3
internal/cache/go.mod
vendored
Normal file
3
internal/cache/go.mod
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
module cache
|
||||
|
||||
go 1.21
|
||||
3
internal/session/go.mod
Normal file
3
internal/session/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module session
|
||||
|
||||
go 1.21
|
||||
43
internal/session/session.go
Normal file
43
internal/session/session.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gorilla/sessions"
|
||||
)
|
||||
|
||||
var store = sessions.NewCookieStore([]byte("f0q0qew0!)§(ds9713lsda231"))
|
||||
|
||||
const LoginSessionName = "session"
|
||||
|
||||
func GetUsername(c *gin.Context) (string, bool) {
|
||||
session, _ := getSession(c)
|
||||
username, ok := session.Values["username"].(string)
|
||||
return username, ok
|
||||
}
|
||||
|
||||
func GetAuthenticated(c *gin.Context) (bool, bool) {
|
||||
session, _ := getSession(c)
|
||||
auth, ok := session.Values["authenticated"].(bool)
|
||||
return auth, ok
|
||||
}
|
||||
|
||||
func getSession(c *gin.Context) (*sessions.Session, error) {
|
||||
return store.Get(c.Request, LoginSessionName)
|
||||
}
|
||||
|
||||
func InvalidateSession(c *gin.Context) error {
|
||||
session, _ := getSession(c)
|
||||
session.Options.MaxAge = -1
|
||||
err := session.Save(c.Request, c.Writer)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func SetLoginSession(username string, c *gin.Context) error {
|
||||
session, _ := getSession(c)
|
||||
session.Values["authenticated"] = true
|
||||
session.Values["username"] = username
|
||||
err := session.Save(c.Request, c.Writer)
|
||||
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user