127 lines
3.8 KiB
Go
127 lines
3.8 KiB
Go
package controllers
|
|
|
|
import (
|
|
"InfantrySkillCalculator/models"
|
|
"InfantrySkillCalculator/utils"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
)
|
|
|
|
type AddCacheInput struct {
|
|
PlayerID uint `json:"player_id" binding:"required"`
|
|
Score float32 `json:"score" gorm:"default:-1.0"`
|
|
GameTag string `json:"game_tag" binding:"required"`
|
|
}
|
|
|
|
type UpdateCacheInput struct {
|
|
Score float32 `json:"score" gorm:"default:-1.0"`
|
|
}
|
|
|
|
// GetCacheByPlayerID GET /cache/:player_id
|
|
func GetCacheByPlayerID(c *gin.Context) {
|
|
playerId := utils.StringToUint(c.Param("player_id"))
|
|
|
|
val, err := models.PlayerCache.GetScore(playerId)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
|
utils.Logger.Warnf("[CACHE] Record not found! Error: %s", err.Error())
|
|
} else {
|
|
c.JSON(http.StatusOK, val)
|
|
}
|
|
}
|
|
|
|
// AddCache POST /cache
|
|
func AddCache(c *gin.Context) {
|
|
var input AddCacheInput
|
|
var game models.Game
|
|
|
|
if err := c.BindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
utils.Logger.Warnf("[CACHE] Failed to bind JSON! Error: %s", err.Error())
|
|
return
|
|
}
|
|
|
|
if err := models.DB.Where("tag = ?", input.GameTag).First(&game).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Game not found!"})
|
|
utils.Logger.Warnf("[CACHE] Game not found! Error: %s", err.Error())
|
|
return
|
|
}
|
|
|
|
err := models.PlayerCache.SetScore(input.PlayerID, input.Score)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Cache update failed! Error: " + err.Error()})
|
|
utils.Logger.Warnf("[CACHE] Cache update failed! Error: %s", err.Error())
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, nil)
|
|
}
|
|
|
|
// UpdateCacheByPlayerID PATCH /cache/:id
|
|
func UpdateCacheByPlayerID(c *gin.Context) {
|
|
playerID := utils.StringToUint(c.Param("id"))
|
|
score := utils.StringToFloat(c.PostForm("score"))
|
|
|
|
err := models.PlayerCache.SetScore(playerID, score)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Cache update failed! Error: " + err.Error()})
|
|
utils.Logger.Warnf("[CACHE] Cache update failed! Error: %s", err.Error())
|
|
} else {
|
|
c.JSON(http.StatusOK, nil)
|
|
}
|
|
}
|
|
|
|
// DeleteCacheByPlayerID DELETE /cache/:id
|
|
func DeleteCacheByPlayerID(c *gin.Context) {
|
|
playerID := utils.StringToUint(c.Param("id"))
|
|
|
|
if err := models.PlayerCache.DeleteScore(playerID); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
utils.Logger.Warnf("[CACHE] Cache deletion failed! Error: %s", err.Error())
|
|
} else {
|
|
c.JSON(http.StatusOK, nil)
|
|
}
|
|
}
|
|
|
|
// DeleteAllCaches DELETE /cache
|
|
func DeleteAllCaches(c *gin.Context) {
|
|
if err := models.PlayerCache.PurgeCache(); err != nil {
|
|
c.String(http.StatusBadRequest, err.Error())
|
|
utils.Logger.Warnf("[CACHE] Cache purge failed! Error: %s", err.Error())
|
|
} else {
|
|
c.String(http.StatusOK, "Purged all caches!")
|
|
}
|
|
}
|
|
|
|
func UpdateCacheAfterExpiry(key string) {
|
|
utils.Logger.Infof("[KeepUpdated] Updating cache for key %s", key)
|
|
|
|
playerId, err := models.PlayerCache.GetPlayerIdFromCacheKey(key)
|
|
if err != nil {
|
|
utils.Logger.Fatal(err)
|
|
}
|
|
|
|
var player models.Player
|
|
if err := models.DB.
|
|
Preload("Clan").
|
|
Where("id = ?", playerId).
|
|
First(&player).Error; err != nil {
|
|
|
|
utils.Logger.Errorf("[KeepUpdated] Failed to find player with ID %d! Error: %s", playerId, err.Error())
|
|
return
|
|
}
|
|
|
|
if player.Clan.KeepUpdated {
|
|
score, statusCode := GetPlayerScore(player.Name)
|
|
if score == score && score != -1 { // not NaN
|
|
if err := models.PlayerCache.SetScore(player.ID, score); err != nil {
|
|
utils.Logger.Warnf("[KeepUpdated] Failed to update cache for player %s! Error: %s", player.Name, err.Error())
|
|
} else {
|
|
utils.Logger.Infof("[KeepUpdated] Updated cache for player %s", player.Name)
|
|
}
|
|
} else {
|
|
utils.Logger.Warnf("[KeepUpdated] Failed to calculate score for player %s! Status code: %d", player.Name, statusCode)
|
|
}
|
|
}
|
|
}
|