added /match/chat endpoint

This commit is contained in:
2022-01-29 21:03:10 +01:00
parent 3bdcdecb68
commit 2ab620ec54
3 changed files with 59 additions and 9 deletions

52
main.go
View File

@@ -7,6 +7,7 @@ import (
"csgowtfd/ent"
"csgowtfd/ent/match"
"csgowtfd/ent/matchplayer"
"csgowtfd/ent/messages"
"csgowtfd/ent/migrate"
"csgowtfd/ent/player"
"csgowtfd/utils"
@@ -619,6 +620,53 @@ func getMatchRounds(w http.ResponseWriter, r *http.Request) {
}
}
func getMatchChat(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
id := mux.Vars(r)["id"]
if id == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
matchId, err := strconv.ParseUint(id, 10, 64)
if err != nil {
log.Infof("[GMC] Error parsing matchID %s: %v", id, err)
w.WriteHeader(http.StatusBadRequest)
return
}
tStats, err := db.Messages.Query().Where(messages.HasMatchPlayerWith(matchplayer.HasMatchesWith(match.ID(matchId)))).All(context.Background())
if err != nil {
log.Infof("[GMC] match %d not found: %+v", matchId, err)
w.WriteHeader(http.StatusNotFound)
return
}
//resp := make([]*utils.ChatResponse, 0)
resp := map[string][]*utils.ChatResponse{}
for _, stat := range tStats {
steamid := strconv.FormatUint(stat.Edges.MatchPlayer.PlayerStats, 10)
if _, ok := resp[steamid]; !ok {
resp[steamid] = make([]*utils.ChatResponse, 0)
}
resp[steamid] = append(resp[steamid], &utils.ChatResponse{
Message: stat.Message,
AllChat: stat.AllChat,
Tick: stat.Tick,
})
}
err = utils.SendJSON(resp, w)
if err != nil {
log.Errorf("[GMC] JSON: %+v", err)
w.WriteHeader(http.StatusInternalServerError)
}
}
func getMatchWeapons(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
@@ -1046,8 +1094,8 @@ 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(`/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}}/chat`, func(writer http.ResponseWriter, request *http.Request) {}).Methods(http.MethodOptions)
router.HandleFunc(`/match/{id:\d{19}}/chat`, getMatchChat).Methods(http.MethodGet)
router.HandleFunc(`/matches`, func(writer http.ResponseWriter, request *http.Request) {}).Methods(http.MethodOptions)
router.HandleFunc(`/matches`, getMatches).Methods(http.MethodGet)