209 lines
5.8 KiB
Go
209 lines
5.8 KiB
Go
package controllers
|
|
|
|
import (
|
|
"InfantrySkillCalculator/models"
|
|
"InfantrySkillCalculator/utils"
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
"html/template"
|
|
"net/http"
|
|
)
|
|
|
|
type AddPlayerInput struct {
|
|
Name string `json:"name" binding:"required"`
|
|
ClanID uint `json:"clan_id" binding:"required"`
|
|
}
|
|
|
|
type UpdatePlayerInput struct {
|
|
Name string `json:"name"`
|
|
ClanID uint `json:"clan_id"`
|
|
}
|
|
|
|
// GetAllPlayers GET /player
|
|
func GetAllPlayers(c *gin.Context) {
|
|
var players []models.Player
|
|
if err := models.DB.Find(&players).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
|
utils.Logger.Errorf("[PLAYER] Record not found! Error: %s", err.Error())
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, players)
|
|
}
|
|
|
|
// GetPlayersByClanHTML GET /players_html
|
|
func GetPlayersByClanHTML(c *gin.Context) {
|
|
var players []models.Player
|
|
clanId := c.Request.URL.Query().Get("clan_id")
|
|
if clanId == "NaN" {
|
|
c.String(http.StatusBadRequest, "")
|
|
return
|
|
}
|
|
if err := models.DB.
|
|
Where("clan_id = ?", utils.StringToUint(clanId)).
|
|
Find(&players).Error; err != nil {
|
|
|
|
c.String(http.StatusBadRequest, "")
|
|
utils.Logger.Errorf("[PLAYER] Clan not found! Error: %s", err.Error())
|
|
return
|
|
}
|
|
|
|
game, err := GetActiveGame(c)
|
|
if err != nil {
|
|
c.String(http.StatusBadRequest, "")
|
|
utils.Logger.Errorf("[PLAYER] No active game not found! Error: %s", err.Error())
|
|
return
|
|
}
|
|
|
|
var playerIDs []uint
|
|
for _, player := range players {
|
|
playerIDs = append(playerIDs, player.ID)
|
|
}
|
|
|
|
scores, err := models.PlayerCache.GetScores(playerIDs, game.Tag)
|
|
if err != nil {
|
|
c.String(http.StatusBadRequest, "")
|
|
utils.Logger.Errorf("[PLAYER] Could not get scores! Error: %s", err.Error())
|
|
return
|
|
}
|
|
|
|
userRole := GetUserRoleByCtx(c)
|
|
|
|
var data []map[string]interface{}
|
|
for i, player := range players {
|
|
|
|
score := scores[i]
|
|
var scoreStr string
|
|
if score == -1.0 {
|
|
scoreStr = "<i class=\"bi bi-dash me-2\" style=\"margin-left:0.7rem;\"></i>"
|
|
} else {
|
|
scoreStr = fmt.Sprintf("%.2f", score)
|
|
}
|
|
|
|
data = append(data, map[string]interface{}{
|
|
"PlayerName": player.Name,
|
|
"Score": template.HTML(scoreStr),
|
|
"PlayerID": player.ID,
|
|
"UserRole": userRole,
|
|
})
|
|
}
|
|
|
|
err = utils.PlayerItemTemplate.Execute(c.Writer, data)
|
|
if err != nil {
|
|
utils.Logger.Errorf("[PLAYER] Could not execute template! Error: %s", err.Error())
|
|
}
|
|
}
|
|
|
|
// AddPlayer POST /player
|
|
func AddPlayer(c *gin.Context) {
|
|
var input AddPlayerInput
|
|
var player models.Player
|
|
|
|
if err := c.BindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
utils.Logger.Errorf("[PLAYER] Could not bind JSON! Error: %s", err.Error())
|
|
return
|
|
}
|
|
|
|
personaID, nucleusID, statusCode := GetPlayerIds(input.Name)
|
|
if personaID == 0 || nucleusID == 0 {
|
|
c.JSON(statusCode, gin.H{"error": "Player not found!"})
|
|
utils.Logger.Errorf("[PLAYER] Could not find player! Name: %s, Status: %d", input.Name, statusCode)
|
|
return
|
|
}
|
|
|
|
player = models.Player{Name: input.Name, ClanID: input.ClanID, PersonaID: personaID, NucleusID: nucleusID}
|
|
if err := models.DB.Create(&player); err.Error != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error.Error()})
|
|
utils.Logger.Errorf("[PLAYER] Could not create player! Error: %s", err.Error.Error())
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, player)
|
|
|
|
utils.Logger.Infof("[PLAYER] Added player '%s' in clan %d", input.Name, input.ClanID)
|
|
}
|
|
|
|
// GetPlayerByID GET /player/:id
|
|
func GetPlayerByID(c *gin.Context) {
|
|
var player models.Player
|
|
|
|
if err := models.DB.Where("id = ?", c.Param("id")).First(&player).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
|
utils.Logger.Errorf("[PLAYER] Could not find player! Error: %s", err.Error())
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, player)
|
|
}
|
|
|
|
// UpdatePlayerByID PATCH /player/:id
|
|
func UpdatePlayerByID(c *gin.Context) {
|
|
var input UpdatePlayerInput
|
|
var player models.Player
|
|
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
utils.Logger.Errorf("[PLAYER] Could not bind JSON! Error: %s", err.Error())
|
|
return
|
|
}
|
|
|
|
if err := models.DB.First(&player, c.Param("id")).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
|
|
utils.Logger.Errorf("[PLAYER] Could not find player! Error: %s", err.Error())
|
|
return
|
|
}
|
|
|
|
oldValues := models.Player{
|
|
Name: player.Name,
|
|
ClanID: player.ClanID,
|
|
}
|
|
|
|
player.Name = input.Name
|
|
player.ClanID = input.ClanID
|
|
|
|
if err := models.DB.Save(&player).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
utils.Logger.Errorf("[PLAYER] Could not update player! Error: %s", err.Error())
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, nil)
|
|
|
|
utils.Logger.Infof("[PLAYER] Updated player: %s -> %s", oldValues.Name, player.Name)
|
|
}
|
|
|
|
// DeletePlayerByID DELETE /player/:id
|
|
func DeletePlayerByID(c *gin.Context) {
|
|
var player models.Player
|
|
|
|
if err := models.DB.Clauses(clause.Returning{}).Where("id = ?", c.Param("id")).Delete(&player).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
utils.Logger.Errorf("[PLAYER] Could not delete player! Error: %s", err.Error())
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, true)
|
|
|
|
utils.Logger.Infof("[PLAYER] Deleted player: %s", player.Name)
|
|
}
|
|
|
|
// DeleteAllPlayers DELETE /admin/player
|
|
func DeleteAllPlayers(c *gin.Context) {
|
|
var players []models.Player
|
|
if err := models.DB.
|
|
Session(&gorm.Session{AllowGlobalUpdate: true}).
|
|
Clauses(clause.Returning{}).
|
|
Delete(&players).Error; err != nil {
|
|
|
|
c.String(http.StatusBadRequest, "Purge failed! Error: "+err.Error())
|
|
utils.Logger.Errorf("[PLAYER] Could not purge players! Error: %s", err.Error())
|
|
return
|
|
}
|
|
|
|
c.String(http.StatusOK, "Purged "+utils.UintToString(uint(len(players)))+" players!")
|
|
utils.Logger.Infof("[PLAYER] Purged %d players!", len(players))
|
|
}
|