~xenrox/faceit_checker

218874e0c9d2e30a2df28118ce80c7be4cd0d059 — Thorben Günther 1 year, 5 days ago 7575810
Don't error out if a single account fails
2 files changed, 55 insertions(+), 43 deletions(-)

M faceit.go
M templates/players.html
M faceit.go => faceit.go +30 -24
@@ 90,6 90,8 @@ type FaceitPlayer struct {
	Link      string
	Status    string
	LastMatch time.Time
	SteamLink string
	Error     string
}

type faceitChecker struct {


@@ 380,37 382,39 @@ func (fc *faceitChecker) requestHandler(w http.ResponseWriter, r *http.Request) 
		return
	}

	// TODO: Allow partial errors and display found accounts
	var playerList []*FaceitPlayer
	for _, steamID := range strings.Split(steamIDs, " ") {
	steamIDList := strings.Split(steamIDs, " ")
	length := len(steamIDList)

	for _, steamID := range steamIDList {
		player := &FaceitPlayer{}
		playerList = append(playerList, player)

		// Information is only needed if multiple accounts are displayed
		if length > 1 {
			player.SteamLink = "https://steamcommunity.com/profiles/" + steamID
		}

		data, err := fc.getFaceitPlayer(steamID)
		if err != nil {
			fc.logger.Errorf("failed to get Faceit player: %v", err)
			if err := fc.templates.ExecuteTemplate(w, "error.html", err); err != nil {
				fc.logger.Errorf("requestHandler: failed to execute template: %v", err)
				http.Error(w, internalError, http.StatusInternalServerError)
			}
			return
			player.Error = err.Error()
			continue
		}

		var data2 faceitCSGO
		err = fc.checkStats(data.PlayerID, &data2)
		if err != nil {
			fc.logger.Errorf("failed to check stats: %v", err)
			if err := fc.templates.ExecuteTemplate(w, "error.html", fmt.Sprintf("Error getting Faceit stats: %v", err)); err != nil {
				fc.logger.Errorf("requestHandler: failed to execute template: %v", err)
				http.Error(w, internalError, http.StatusInternalServerError)
			}
			return
			player.Error = err.Error()
			continue
		}

		banned, reason, err := fc.checkBan(data.PlayerID)
		if err != nil {
			fc.logger.Errorf("failed to check ban status: %v", err)
			if err := fc.templates.ExecuteTemplate(w, "error.html", fmt.Sprintf("Error checking Ban status: %v", err)); err != nil {
				fc.logger.Errorf("requestHandler: failed to execute template: %v", err)
				http.Error(w, internalError, http.StatusInternalServerError)
			}
			player.Error = err.Error()
			continue
		}

		var status string


@@ 423,16 427,18 @@ func (fc *faceitChecker) requestHandler(w http.ResponseWriter, r *http.Request) 
		lastMatch, err := fc.lastMatch(data.PlayerID)
		if err != nil {
			fc.logger.Errorf("failed to retrieve last match: %v", err)
			player.Error = err.Error()
			continue
		}

		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,
			LastMatch: lastMatch,
		}

		playerList = append(playerList, &player)
		player.Elo = data.Games.CSGO.Elo
		player.Kd = data2.Stats.KD
		player.Matches = data2.Stats.Matches
		player.Winrate = data2.Stats.Winrate
		player.Link = data.FaceitURL
		player.Status = status
		player.Country = data.Country
		player.LastMatch = lastMatch
	}

	if err := fc.templates.ExecuteTemplate(w, "players.html", playerList); err != nil {

M templates/players.html => templates/players.html +25 -19
@@ 5,25 5,31 @@
        <link rel="stylesheet" href="css/main.css" type="text/css" />
    </head>
    <body>
        {{range .}} Elo: {{.Elo}}<br />
        K/D: {{.Kd}}<br />
        Matches: {{.Matches}} (last: {{.LastMatch.Format "02.01.2006"}})<br />
        Winrate: {{.Winrate}}%<br />
        Country: {{.Country}}<br />
        <a href="{{.Link}}">Faceit profile link</a><br />
        <br />
        {{ if .Status }}
        <div class="banned">
            {{.Status}}
            <br />
        </div>
        {{else}}
        <div class="clean">
            No Faceit ban.
            <br />
        </div>
        {{end}}
        <hr />
        {{range .}}
            {{if .SteamLink}}
                <a href="{{.SteamLink}}">{{.SteamLink}} </a><br />
            {{end}} 
            {{ if .Error }}
                Failed to get stats: {{.Error}}<br />
            {{else}}
                Elo: {{.Elo}}<br />
                K/D: {{.Kd}}<br />
                Matches: {{.Matches}} (last: {{.LastMatch.Format "02.01.2006"}})<br />
                Winrate: {{.Winrate}}%<br />
                Country: {{.Country}}<br />
                <a href="{{.Link}}">Faceit profile link</a><br />
                <br />
                {{ if .Status }}
                    <div class="banned">
                    {{.Status}}<br />
                    </div>
                {{else}}
                    <div class="clean">
                    No Faceit ban.<br />
                    </div>
                {{end}}
            {{end}}
            <hr />
        {{end}}
    </body>
</html>