-
Notifications
You must be signed in to change notification settings - Fork 0
/
thcda.go
163 lines (135 loc) · 4.26 KB
/
thcda.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package analysis
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
yaml "gopkg.in/yaml.v2"
"github.com/gtosh4/WoWCDHelper/internal/pkg/ctx"
"github.com/gtosh4/WoWCDHelper/pkg/warcraftlogs"
"github.com/gtosh4/WoWCDHelper/pkg/warcraftlogs/events"
"github.com/gtosh4/WoWCDHelper/pkg/warcraftlogs/fight"
)
type (
THCDA struct {
*ctx.Ctx
WCL *warcraftlogs.Client
}
FightData struct {
*ctx.Ctx
Code string
Num int64
Fights fight.Fights
Fight *fight.Fight
Friendlies []fight.Friendly
Enemies []fight.Enemy
Phases *fight.Phases
}
)
func (t *THCDA) RegisterHTTPHandlers(router *mux.Router) {
// Backend
apiR := router.PathPrefix("/thcda/v1").Subrouter()
apiR.Use(
handlers.CORS(handlers.AllowedOrigins([]string{"*"})),
handlers.CompressHandler,
)
fightR := apiR.PathPrefix("/{code}/{fight:[0-9]+}").Subrouter()
// Raw-type endpoints
fightR.Path("/events").Methods("GET").HandlerFunc(t.handleGetEvents)
fightR.Path("/events/{type}").Methods("GET").HandlerFunc(t.handleGetEvents)
// Logic endpoints
fightR.Path("/raid_health").Methods("GET").HandlerFunc(t.handleGetRaidHealth)
fightR.Path("/damage_taken").Methods("GET").HandlerFunc(t.handleDamageTaken)
}
func (t *THCDA) fightData(reqCtx *ctx.Ctx, vars map[string]string) (fd *FightData, err error) {
fd = new(FightData)
fd.Code = vars["code"]
var n int64
n, err = strconv.ParseInt(vars["fight"], 10, 64)
if err != nil {
return
}
fd.Num = n
fd.Ctx = reqCtx.NewSubcontextWithFields(logrus.Fields{"code": fd.Code, "fight_num": fd.Num})
fights, err := t.WCL.Fights(fd.Code)
if err != nil {
err = errors.Errorf("Failed to load fights from %s", fd.Code)
return
}
fd.Fight, fd.Friendlies, fd.Enemies, fd.Phases = fights.Fight(fd.Num)
if fd.Fight == &fight.FightNotFound {
err = errors.Errorf("Could not find fight %d in report %s", fd.Num, fd.Code)
return
}
fd.Log.Infof("Found %d friends, %d enemies and phases: %+v", len(fd.Friendlies), len(fd.Enemies), fd.Phases)
return
}
func (t *THCDA) handleGetEvents(resp http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
etype := vars["type"]
fd, err := t.fightData(t.Ctx.NewSubcontextWithField("remote-addr", req.RemoteAddr), vars)
if err != nil {
t.Log.WithError(err).Warnf("Could not get fight data for report %s", fd.Code)
resp.WriteHeader(http.StatusInternalServerError)
return
}
var events []events.Event
if etype == "" {
events, err = t.WCL.AllEvents(fd.Code, fd.Fight)
} else {
events, err = t.WCL.TypeEvents(fd.Code, fd.Fight, etype)
}
if err != nil {
t.Log.WithError(err).Warnf("Could not get events for report %s", fd.Code)
resp.WriteHeader(http.StatusInternalServerError)
return
}
evFile, err := os.OpenFile(fmt.Sprintf("examples/events_%s_%d.yaml", fd.Code, fd.Num), os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
fd.Log.WithError(err).Warn("Could not open file")
} else {
defer evFile.Close()
yaml.NewEncoder(evFile).Encode(events)
}
resp.WriteHeader(http.StatusNoContent)
return
}
func (t *THCDA) handleGetRaidHealth(resp http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
fd, err := t.fightData(t.Ctx, vars)
if err != nil {
t.Log.WithError(err).Warnf("Could not get fight data for report %s", fd.Code)
resp.WriteHeader(http.StatusInternalServerError)
return
}
raidHealth, err := t.RaidHealth(fd)
if err != nil {
fd.Log.WithError(err).Warnf("Could not get raid health for report %s", fd.Code)
resp.WriteHeader(http.StatusInternalServerError)
return
}
json.NewEncoder(resp).Encode(raidHealth)
resp.Header().Set("Content-Type", "application/json")
}
func (t *THCDA) handleDamageTaken(resp http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
fd, err := t.fightData(t.Ctx, vars)
if err != nil {
t.Log.WithError(err).Warnf("Could not get fight data for report %s", fd.Code)
resp.WriteHeader(http.StatusInternalServerError)
return
}
raidHealth, err := t.RaidHealth(fd)
if err != nil {
fd.Log.WithError(err).Warnf("Could not get raid health for report %s", fd.Code)
resp.WriteHeader(http.StatusInternalServerError)
return
}
json.NewEncoder(resp).Encode(raidHealth)
resp.Header().Set("Content-Type", "application/json")
}