added /matches

This commit is contained in:
2021-11-09 20:40:56 +01:00
parent 1d6396abfd
commit 2d8b989d3a

66
main.go
View File

@@ -661,6 +661,63 @@ func getMatchWeapons(w http.ResponseWriter, r *http.Request) {
} }
} }
func getMatches(w http.ResponseWriter, r *http.Request) {
t := mux.Vars(r)["time"]
var offsetTime time.Time
if t != "" {
unixOffset, err := strconv.ParseInt(t, 10, 64)
if err != nil {
log.Infof("[GP] offset not an int: %v", err)
w.WriteHeader(http.StatusBadRequest)
return
}
offsetTime = time.Unix(unixOffset, 0).UTC()
}
var mResponse []*utils.MatchResponse
var err error
var tMatches []*ent.Match
if !offsetTime.IsZero() {
tMatches, err = db.Match.Query().Where(match.DateLT(offsetTime)).Order(ent.Desc(match.FieldDate)).Limit(20).All(context.Background())
} else {
tMatches, err = db.Match.Query().Order(ent.Desc(match.FieldDate)).Limit(20).All(context.Background())
}
if err != nil || len(tMatches) == 0 {
log.Debug("[GMS] No matches found")
err := utils.SendJSON(mResponse, w)
if err != nil {
log.Errorf("[GP] Unable to marshal JSON: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
return
}
for _, iMatch := range tMatches {
mResponse = append(mResponse, &utils.MatchResponse{
MatchId: iMatch.ID,
Map: iMatch.Map,
Date: iMatch.Date.Unix(),
Score: [2]int{iMatch.ScoreTeamA, iMatch.ScoreTeamB},
Duration: iMatch.Duration,
MatchResult: iMatch.MatchResult,
MaxRounds: iMatch.MaxRounds,
Parsed: iMatch.DemoParsed,
VAC: iMatch.VacPresent,
GameBan: iMatch.GamebanPresent,
})
}
err = utils.SendJSON(mResponse, w)
if err != nil {
log.Errorf("[GM] JSON: %+v", err)
w.WriteHeader(http.StatusInternalServerError)
}
}
func getMatch(w http.ResponseWriter, r *http.Request) { func getMatch(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"] id := mux.Vars(r)["id"]
@@ -780,6 +837,8 @@ func getMatch(w http.ResponseWriter, r *http.Request) {
/match/<id>/weapons GET weapon-stats for match <id> /match/<id>/weapons GET weapon-stats for match <id>
/match/<id>/rounds GET round-stats for match <id> /match/<id>/rounds GET round-stats for match <id>
/match/parse/<sharecode> GET parses sharecode provided /match/parse/<sharecode> GET parses sharecode provided
/matches GET returns 20 latest matches in DB
/matches/next/<unix> GET returns 20 matches after time unix
*/ */
func main() { func main() {
killSignals := make(chan os.Signal, 1) killSignals := make(chan os.Signal, 1)
@@ -899,6 +958,13 @@ func main() {
router.HandleFunc(`/match/{id:\d{19}}/rounds`, func(writer http.ResponseWriter, request *http.Request) {}).Methods(http.MethodOptions) router.HandleFunc(`/match/{id:\d{19}}/rounds`, func(writer http.ResponseWriter, request *http.Request) {}).Methods(http.MethodOptions)
router.HandleFunc(`/match/{id:\d{19}}/rounds`, getMatchRounds).Methods(http.MethodGet) router.HandleFunc(`/match/{id:\d{19}}/rounds`, getMatchRounds).Methods(http.MethodGet)
router.HandleFunc(`/matches`, func(writer http.ResponseWriter, request *http.Request) {}).Methods(http.MethodOptions)
router.HandleFunc(`/matches`, getMatches).Methods(http.MethodGet)
router.HandleFunc(`/matches/next/{time:\d+}`, func(writer http.ResponseWriter, request *http.Request) {}).Methods(http.MethodOptions)
router.HandleFunc(`/matches/next/{time:\d+}`, getMatches).Methods(http.MethodGet)
router.Use(mux.CORSMethodMiddleware(router)) router.Use(mux.CORSMethodMiddleware(router))
loggedRouter := handlers.LoggingHandler(os.Stdout, router) loggedRouter := handlers.LoggingHandler(os.Stdout, router)
proxyRouter := handlers.ProxyHeaders(loggedRouter) proxyRouter := handlers.ProxyHeaders(loggedRouter)