diff --git a/main.go b/main.go index 9a7c633..a3f3823 100644 --- a/main.go +++ b/main.go @@ -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) { id := mux.Vars(r)["id"] @@ -780,6 +837,8 @@ func getMatch(w http.ResponseWriter, r *http.Request) { /match//weapons GET weapon-stats for match /match//rounds GET round-stats for match /match/parse/ GET parses sharecode provided +/matches GET returns 20 latest matches in DB +/matches/next/ GET returns 20 matches after time unix */ func main() { 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`, 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)) loggedRouter := handlers.LoggingHandler(os.Stdout, router) proxyRouter := handlers.ProxyHeaders(loggedRouter)