feat: capture in-game battle damage statistics & add battle_report API endpoints

This commit is contained in:
CAtlas0784 committed 2026-10-17 13:08:13 +07:00
1 parent 5c4d0d4149
commit 365e66ffe0
5 files changed
+415

No files matched your search

+64
View File
@@ -48,6 +48,8 @@ func registerAPIRoutes(mux *http.ServeMux) {
mux.HandleFunc("/api/gachabase/fetch", corsWrapper(apiGachabaseFetch))
mux.HandleFunc("/api/command", corsWrapper(apiCommand))
mux.HandleFunc("/api/data-in-game", corsWrapper(apiDataInGame))
mux.HandleFunc("/api/battle_report", corsWrapper(apiBattleReport))
mux.HandleFunc("/api/battle_report/reset", corsWrapper(apiBattleReportReset))
}
func apiRoot(w http.ResponseWriter, r *http.Request) {
@@ -76,6 +78,7 @@ func apiRoot(w http.ResponseWriter, r *http.Request) {
"avatar_max_all": "POST /api/avatar/max_all",
"teleport": "POST /api/teleport",
"command": "POST /api/command",
"battle_report": "GET, POST /api/battle_report",
},
})
}
@@ -771,3 +774,64 @@ func apiCommand(w http.ResponseWriter, r *http.Request) {
})
}
// apiBattleReport handles GET (fetch latest in-game battle) and POST (import battle report)
func apiBattleReport(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
report, err := common.LoadLatestBattleReport()
if err != nil {
writeJSON(w, map[string]any{
"status": 200,
"has_report": false,
"message": "No battle report recorded yet",
})
return
}
writeJSON(w, map[string]any{
"status": 200,
"has_report": true,
"report": report,
})
return
}
if r.Method == http.MethodPost {
var report common.BattleReport
if err := json.NewDecoder(r.Body).Decode(&report); err != nil {
writeJSON(w, map[string]any{"error": "invalid json: " + err.Error(), "status": 400})
return
}
if report.Timestamp == 0 {
report.Timestamp = time.Now().Unix()
}
if err := common.SaveBattleReport(&report); err != nil {
writeJSON(w, map[string]any{"error": err.Error(), "status": 500})
return
}
writeJSON(w, map[string]any{
"status": 200,
"message": "Battle report saved successfully",
"report": report,
})
return
}
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
}
// apiBattleReportReset clears the current recorded battle report
func apiBattleReportReset(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost && r.Method != http.MethodGet {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
if err := common.ClearBattleReport(); err != nil {
writeJSON(w, map[string]any{"error": err.Error(), "status": 500})
return
}
writeJSON(w, map[string]any{
"status": 200,
"message": "Battle report cleared",
})
}