diff --git a/main.go b/main.go index 377f034..8f667ab 100644 --- a/main.go +++ b/main.go @@ -148,6 +148,20 @@ func housekeeping() { func getPlayer(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", conf.Httpd.CORSAllowDomains) id := mux.Vars(r)["id"] + 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() + } + tPlayer, err := utils.GetPlayer(db, id, conf.Steam.APIKey, nil) if err != nil { log.Infof("[GP] Player not found: %+v", err) @@ -169,9 +183,16 @@ func getPlayer(w http.ResponseWriter, r *http.Request) { response.VACDate = &tPlayer.VacDate } - db.Lock.RLock() - tMatches, err := tPlayer.QueryMatches().Order(ent.Desc(match.FieldDate)).Limit(10).All(context.Background()) - db.Lock.RUnlock() + var tMatches []*ent.Match + if !offsetTime.IsZero() { + db.Lock.RLock() + tMatches, err = tPlayer.QueryMatches().Where(match.DateLT(offsetTime)).Order(ent.Desc(match.FieldDate)).Limit(10).All(context.Background()) + db.Lock.RUnlock() + } else { + db.Lock.RLock() + tMatches, err = tPlayer.QueryMatches().Order(ent.Desc(match.FieldDate)).Limit(10).All(context.Background()) + db.Lock.RUnlock() + } if err != nil || len(tMatches) == 0 { log.Debugf("[GP] No matches found for player %s", id) err := utils.SendJSON(response, w) @@ -762,12 +783,13 @@ func main() { // routes router = mux.NewRouter().StrictSlash(true) router.HandleFunc("/player/{id}", getPlayer).Methods(http.MethodGet, http.MethodOptions) + router.HandleFunc(`/player/{id}/after/{time:\d+}`, getPlayer).Methods(http.MethodGet, http.MethodOptions) router.HandleFunc("/player/{id}/track", postPlayerTrack).Methods(http.MethodPost, http.MethodOptions) router.HandleFunc("/player/{id}/track", deletePlayerTrack).Methods(http.MethodOptions, http.MethodDelete) router.HandleFunc("/match/parse/{sharecode}", getMatchParse).Methods(http.MethodGet, http.MethodOptions) - router.HandleFunc("/match/{id:[0-9]{19}}", getMatch).Methods(http.MethodGet, http.MethodOptions) - router.HandleFunc("/match/{id:[0-9]{19}}/weapons", getMatchWeapons).Methods(http.MethodGet, http.MethodOptions) - router.HandleFunc("/match/{id:[0-9]{19}}/rounds", getMatchRounds).Methods(http.MethodGet, http.MethodOptions) + router.HandleFunc(`/match/{id:\d{19}}`, getMatch).Methods(http.MethodGet, http.MethodOptions) + router.HandleFunc(`/match/{id:\d{19}}/weapons`, getMatchWeapons).Methods(http.MethodGet, http.MethodOptions) + router.HandleFunc(`/match/{id:\d{19}}/rounds`, getMatchRounds).Methods(http.MethodGet, http.MethodOptions) router.Use(mux.CORSMethodMiddleware(router)) loggedRouter := handlers.LoggingHandler(os.Stdout, router) proxyRouter := handlers.ProxyHeaders(loggedRouter)