From fcd8b83886deabe3df3252debbb3255853183615 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Wed, 29 Mar 2023 20:23:24 +0200 Subject: [PATCH] Display date of last played match --- faceit.go | 64 +++++++++++++++++++++++++++++++++++++------ templates/player.html | 2 +- 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/faceit.go b/faceit.go index baf0207..2e740a3 100644 --- a/faceit.go +++ b/faceit.go @@ -73,14 +73,23 @@ type faceitBan struct { } `json:"payload"` } +type matchHistory struct { + Items []match `json:"items"` +} + +type match struct { + StartedAt int64 `json:"started_at"` +} + type FaceitPlayer struct { - Elo int - Kd string - Matches string - Winrate string - Country string - Link string - Status string + Elo int + Kd string + Matches string + Winrate string + Country string + Link string + Status string + LastMatch time.Time } type faceitChecker struct { @@ -204,6 +213,35 @@ func (fc *faceitChecker) checkBan(faceitID string) (bool, string, error) { return false, "", nil } +func (fc *faceitChecker) lastMatch(faceitID string) (time.Time, error) { + var lastMatch time.Time + url := "https://open.faceit.com/data/v4/players/" + faceitID + "/history?game=csgo&limit=1" + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return lastMatch, err + } + + req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", fc.bearerToken)) + resp, err := fc.client.Do(req) + if err != nil { + return lastMatch, err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return lastMatch, fmt.Errorf("received status code %d", resp.StatusCode) + } + + b, err := io.ReadAll(resp.Body) + if err != nil { + return lastMatch, err + } + + var history matchHistory + err = json.Unmarshal(b, &history) + return time.Unix(history.Items[0].StartedAt, 0), err +} + // getSteamID returns a steamID64 from a profile URL func getSteamID(url string) (string, error) { url += "?xml=1" @@ -373,9 +411,17 @@ func (fc *faceitChecker) requestHandler(w http.ResponseWriter, r *http.Request) status = "" } - player := FaceitPlayer{Elo: data.Games.CSGO.Elo, Kd: data2.Stats.KD, + lastMatch, err := fc.lastMatch(data.PlayerID) + if err != nil { + fc.logger.Errorf("failed to retrieve last match: %v", err) + } + + player := FaceitPlayer{ + Elo: data.Games.CSGO.Elo, Kd: data2.Stats.KD, Matches: data2.Stats.Matches, Winrate: data2.Stats.Winrate, - Link: data.FaceitURL, Status: status, Country: data.Country} + Link: data.FaceitURL, Status: status, Country: data.Country, + LastMatch: lastMatch, + } if err := fc.templates.ExecuteTemplate(w, "player.html", player); err != nil { fc.logger.Errorf("requestHandler: failed to execute template: %v", err) diff --git a/templates/player.html b/templates/player.html index 91055a3..da3e50d 100644 --- a/templates/player.html +++ b/templates/player.html @@ -7,7 +7,7 @@ Elo: {{.Elo}}
K/D: {{.Kd}}
- Matches: {{.Matches}}
+ Matches: {{.Matches}} (last: {{.LastMatch.Format "02.01.2006"}})
Winrate: {{.Winrate}}%
Country: {{.Country}}
Faceit profile link
-- 2.44.0