Implement KeepUpdated. Switch to Logrus logger. Relocate score-related functions.
This commit is contained in:
@@ -3,9 +3,7 @@ package controllers
|
||||
import (
|
||||
"InfantrySkillCalculator/models"
|
||||
"InfantrySkillCalculator/utils"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
@@ -95,61 +93,33 @@ func DeleteAllCaches(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// GetScoreByPlayerID GET /score/:player_id
|
||||
func GetScoreByPlayerID(c *gin.Context) {
|
||||
var player models.Player
|
||||
var playerId = utils.StringToUint(c.Param("player_id"))
|
||||
func UpdateCacheAfterExpiry(key string) {
|
||||
utils.Logger.Println("[KeepUpdated] Cache expired: " + key)
|
||||
|
||||
playerId, err := models.PlayerCache.GetPlayerIdFromCacheKey(key)
|
||||
if err != nil {
|
||||
utils.Logger.Fatal(err)
|
||||
}
|
||||
|
||||
var player models.Player
|
||||
if err := models.DB.
|
||||
Model(&models.Player{}).
|
||||
Preload("Clan").
|
||||
Where("id = ?", playerId).
|
||||
First(&player).Error; err != nil {
|
||||
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Player not found!"})
|
||||
return
|
||||
utils.Logger.Println("Failed to find player: " + err.Error())
|
||||
}
|
||||
|
||||
game, err := GetActiveGame(c)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "No active game available!"})
|
||||
return
|
||||
}
|
||||
|
||||
score, err := models.PlayerCache.GetScore(player.ID, game.Tag)
|
||||
if err != nil || score == -1 {
|
||||
score = utils.CalcPlayerScore(player.Name, game.Tag)
|
||||
if player.Clan.KeepUpdated {
|
||||
score := CalcPlayerScore(player.Name, "BF2042")
|
||||
if score == score && score != -1 { // not NaN
|
||||
if err := models.PlayerCache.SetScore(player.ID, game.Tag, score); err != nil {
|
||||
log.Fatal(err)
|
||||
if err := models.PlayerCache.SetScore(player.ID, "BF2042", score); err != nil {
|
||||
utils.Logger.Println("Failed to update cache: " + err.Error())
|
||||
} else {
|
||||
utils.Logger.Println("[KeepUpdated] Updated cache for player " + player.Name)
|
||||
}
|
||||
} else {
|
||||
utils.Logger.Println("[KeepUpdated] Received NaN for player " + player.Name)
|
||||
}
|
||||
}
|
||||
|
||||
if score != score || score == -1 { // NaN
|
||||
c.String(http.StatusOK, "<i class=\"bi bi-person-x-fill me-2 text-danger fs-5\" style=\"margin-left: 0.69rem;\"></i>")
|
||||
} else if score != -1 {
|
||||
c.String(http.StatusOK, fmt.Sprintf("%.2f", score))
|
||||
} else {
|
||||
c.JSON(http.StatusBadRequest, "Invalid request!")
|
||||
}
|
||||
}
|
||||
|
||||
// GetScoreByPlayerName POST /score/:player_name
|
||||
func GetScoreByPlayerName(c *gin.Context) {
|
||||
playerName := c.Param("player_name")
|
||||
game, err := GetActiveGame(c)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "No active game available!"})
|
||||
return
|
||||
}
|
||||
|
||||
score := utils.CalcPlayerScore(playerName, game.Tag)
|
||||
|
||||
if score != score || score == -1 { // NaN
|
||||
c.String(http.StatusNotFound, "Spieler nicht gefunden!")
|
||||
} else if score != -1 {
|
||||
c.String(http.StatusOK, fmt.Sprintf("%.2f", score))
|
||||
} else {
|
||||
c.String(http.StatusBadRequest, "Ungültige Abfrage!")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user