feat(#22): gamificação RPG — XP, quests, conquistas, cosméticos e personagem SVG pixel art
Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"financeiro-carvalho/internal/service"
|
||||
)
|
||||
|
||||
type GameHandler struct {
|
||||
svc *service.GameService
|
||||
}
|
||||
|
||||
func NewGameHandler(svc *service.GameService) *GameHandler {
|
||||
return &GameHandler{svc: svc}
|
||||
}
|
||||
|
||||
func (h *GameHandler) Summary(w http.ResponseWriter, r *http.Request) {
|
||||
s, err := h.svc.GetSummary(r.Context())
|
||||
if err != nil {
|
||||
respondError(w, http.StatusInternalServerError, "failed to get game summary")
|
||||
return
|
||||
}
|
||||
respondJSON(w, http.StatusOK, s)
|
||||
}
|
||||
|
||||
func (h *GameHandler) AwardXP(w http.ResponseWriter, r *http.Request) {
|
||||
var body struct {
|
||||
EventType string `json:"event_type"`
|
||||
Amount int `json:"amount"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
respondError(w, http.StatusBadRequest, "invalid JSON")
|
||||
return
|
||||
}
|
||||
p, err := h.svc.AwardXP(r.Context(), body.EventType, body.Amount, body.Description)
|
||||
if err != nil {
|
||||
respondError(w, http.StatusInternalServerError, "failed to award xp")
|
||||
return
|
||||
}
|
||||
respondJSON(w, http.StatusOK, p)
|
||||
}
|
||||
|
||||
func (h *GameHandler) ClaimQuest(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.Atoi(chi.URLParam(r, "id"))
|
||||
if err != nil {
|
||||
respondError(w, http.StatusBadRequest, "invalid id")
|
||||
return
|
||||
}
|
||||
if err := h.svc.ClaimQuest(r.Context(), id); err != nil {
|
||||
respondError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (h *GameHandler) Achievements(w http.ResponseWriter, r *http.Request) {
|
||||
list, err := h.svc.ListAllAchievements(r.Context())
|
||||
if err != nil {
|
||||
respondError(w, http.StatusInternalServerError, "failed to list achievements")
|
||||
return
|
||||
}
|
||||
respondJSON(w, http.StatusOK, list)
|
||||
}
|
||||
|
||||
func (h *GameHandler) Cosmetics(w http.ResponseWriter, r *http.Request) {
|
||||
list, err := h.svc.ListCosmetics(r.Context())
|
||||
if err != nil {
|
||||
respondError(w, http.StatusInternalServerError, "failed to list cosmetics")
|
||||
return
|
||||
}
|
||||
respondJSON(w, http.StatusOK, list)
|
||||
}
|
||||
|
||||
func (h *GameHandler) EquipCosmetic(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.Atoi(chi.URLParam(r, "id"))
|
||||
if err != nil {
|
||||
respondError(w, http.StatusBadRequest, "invalid id")
|
||||
return
|
||||
}
|
||||
if err := h.svc.EquipCosmetic(r.Context(), id); err != nil {
|
||||
respondError(w, http.StatusInternalServerError, "failed to equip cosmetic")
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
@@ -11,11 +11,12 @@ import (
|
||||
const maxUploadSize = 10 << 20 // 10 MB
|
||||
|
||||
type ImportHandler struct {
|
||||
svc *service.ImportService
|
||||
svc *service.ImportService
|
||||
gameSvc *service.GameService
|
||||
}
|
||||
|
||||
func NewImportHandler(svc *service.ImportService) *ImportHandler {
|
||||
return &ImportHandler{svc: svc}
|
||||
func NewImportHandler(svc *service.ImportService, gameSvc *service.GameService) *ImportHandler {
|
||||
return &ImportHandler{svc: svc, gameSvc: gameSvc}
|
||||
}
|
||||
|
||||
// Preview parses the uploaded file and returns rows with duplicate flags.
|
||||
@@ -78,5 +79,6 @@ func (h *ImportHandler) Confirm(w http.ResponseWriter, r *http.Request) {
|
||||
respondError(w, http.StatusInternalServerError, "failed to save transactions")
|
||||
return
|
||||
}
|
||||
h.gameSvc.NotifyAction(r.Context(), "import_confirmed", map[string]any{"count": result.Imported})
|
||||
respondJSON(w, http.StatusOK, result)
|
||||
}
|
||||
|
||||
@@ -14,11 +14,12 @@ import (
|
||||
)
|
||||
|
||||
type TransactionHandler struct {
|
||||
svc *service.TransactionService
|
||||
svc *service.TransactionService
|
||||
gameSvc *service.GameService
|
||||
}
|
||||
|
||||
func NewTransactionHandler(svc *service.TransactionService) *TransactionHandler {
|
||||
return &TransactionHandler{svc: svc}
|
||||
func NewTransactionHandler(svc *service.TransactionService, gameSvc *service.GameService) *TransactionHandler {
|
||||
return &TransactionHandler{svc: svc, gameSvc: gameSvc}
|
||||
}
|
||||
|
||||
func (h *TransactionHandler) List(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -42,6 +43,7 @@ func (h *TransactionHandler) Create(w http.ResponseWriter, r *http.Request) {
|
||||
respondError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
h.gameSvc.NotifyAction(r.Context(), "transaction_created", map[string]any{"category_id": out.CategoryID})
|
||||
respondJSON(w, http.StatusCreated, out)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user