From 2ab620ec546e4c8b8cb4b80c785150cd85ed3121 Mon Sep 17 00:00:00 2001 From: Giovanni Harting <539@idlegandalf.com> Date: Sat, 29 Jan 2022 21:03:10 +0100 Subject: [PATCH] added /match/chat endpoint --- go.sum | 5 ----- main.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++-- utils/utils.go | 11 +++++++++-- 3 files changed, 59 insertions(+), 9 deletions(-) diff --git a/go.sum b/go.sum index 461d6ef..a239c5f 100644 --- a/go.sum +++ b/go.sum @@ -302,7 +302,6 @@ github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= -github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= @@ -333,7 +332,6 @@ github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+ github.com/oklog/oklog v0.3.2/go.mod h1:FCV+B7mhrz4o+ueLpx+KqkyXRGMWOYEvfiXtdGtbWGs= github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo= -github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= @@ -412,10 +410,8 @@ github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9 github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= github.com/sony/gobreaker v0.4.1/go.mod h1:ZKptC7FHNvhBz7dN2LGjPVBz2sZJmc0/PkyDJOjmxWY= github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= -github.com/spf13/cobra v1.3.0 h1:R7cSvGu+Vv+qX0gW5R/85dx2kmmJT5z5NM8ifdYjdn0= github.com/spf13/pflag v1.0.1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= -github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw= github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw= github.com/streadway/handy v0.0.0-20190108123426-d5acb3125c2a/go.mod h1:qNTQ5P5JnDBl6z3cMAg/SywNDC5ABu5ApDIw6lUbRmI= @@ -624,7 +620,6 @@ golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roY golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= -golang.org/x/tools v0.1.9-0.20211216111533-8d383106f7e7 h1:M1gcVrIb2lSn2FIL19DG0+/b8nNVKJ7W7b4WcAGZAYM= golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/main.go b/main.go index 9c5e522..845ed4b 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/utils/utils.go b/utils/utils.go index 350399f..866322c 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -76,7 +76,7 @@ type CommunityXML struct { VanityURL string `xml:"customURL"` } -type shareCodeResponse struct { +type ShareCodeResponse struct { Result struct { Code string `json:"nextcode"` } `json:"result"` @@ -155,6 +155,13 @@ type MateResponse struct { Total int `json:"total,omitempty"` } +type ChatResponse struct { + Player *PlayerResponse `json:"player,omitempty"` + Message string `json:"message"` + AllChat bool `json:"teamChat"` + Tick int `json:"tick"` +} + type WeaponDmg struct { Eq int `json:"eq"` Dmg uint `json:"dmg"` @@ -563,7 +570,7 @@ func getNextShareCode(lastCode string, apiKey string, authCode string, steamId u return "", err } - rJson := new(shareCodeResponse) + rJson := new(ShareCodeResponse) err = json.Unmarshal(bJson, rJson) if err != nil { return "", err