~xenrox/faceit_checker

fcd8b83886deabe3df3252debbb3255853183615 — Thorben Günther 1 year, 1 month ago 5f57678
Display date of last played match
2 files changed, 56 insertions(+), 10 deletions(-)

M faceit.go
M templates/player.html
M faceit.go => faceit.go +55 -9
@@ 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)

M templates/player.html => templates/player.html +1 -1
@@ 7,7 7,7 @@
  <body>
    Elo: {{.Elo}}<br />
    K/D: {{.Kd}}<br />
    Matches: {{.Matches}}<br />
    Matches: {{.Matches}} (last: {{.LastMatch.Format "02.01.2006"}})<br />
    Winrate: {{.Winrate}}%<br />
    Country: {{.Country}}<br />
    <a href="{{.Link}}">Faceit profile link</a><br />