~xenrox/faceit_checker

c19ee39f7c262a22c05bceb11099682bf7719a8c — Thorben Günther 3 months ago 302830b
api: Add endpoint for CS2 elo
2 files changed, 61 insertions(+), 4 deletions(-)

M api.go
M faceit.go
M api.go => api.go +54 -3
@@ 8,8 8,8 @@ import (
	"strings"
)

func (fc *faceitChecker) getElo(w http.ResponseWriter, r *http.Request) {
	logger := fc.logger.With(slog.String("handler", "/api/elo"))
func (fc *faceitChecker) getCSGOElo(w http.ResponseWriter, r *http.Request) {
	logger := fc.logger.With(slog.String("handler", "/api/csgo/elo"))

	w.Header().Set("Content-Type", "application/json")



@@ 18,7 18,7 @@ func (fc *faceitChecker) getElo(w http.ResponseWriter, r *http.Request) {
		return
	}

	steamID64 := strings.TrimPrefix(r.URL.Path, "/api/elo/")
	steamID64 := strings.TrimPrefix(r.URL.Path, "/api/csgo/elo/")

	data, err := fc.getFaceitPlayer(r.Context(), steamID64)
	if err != nil {


@@ 58,3 58,54 @@ func (fc *faceitChecker) getElo(w http.ResponseWriter, r *http.Request) {
			slog.String("error", err.Error()))
	}
}

func (fc *faceitChecker) getCS2Elo(w http.ResponseWriter, r *http.Request) {
	logger := fc.logger.With(slog.String("handler", "/api/cs2/elo"))

	w.Header().Set("Content-Type", "application/json")

	if r.Method != http.MethodGet {
		http.Error(w, "Only GET allowed", http.StatusMethodNotAllowed)
		return
	}

	steamID64 := strings.TrimPrefix(r.URL.Path, "/api/cs2/elo/")

	data, err := fc.getFaceitPlayer(r.Context(), steamID64)
	if err != nil {
		// Validate steamid; there are two possibilies: User entered correct steamID
		// but has no Faceit account -> give him default elo or user entered wrong steamID
		url := "https://steamcommunity.com/profiles/" + steamID64
		steamProfile, errSteam := fc.getSteamProfile(r.Context(), url)
		if errSteam == nil && steamProfile.ID64 == steamID64 {
			if err == ErrNoCSGOFaceit {
				// Set default elo
				data.Games.CSGO.Elo = 1000
			} else {
				logger.Error("Internal error",
					slog.String("error", err.Error()))
				w.WriteHeader(http.StatusInternalServerError)
				_, err = io.WriteString(w, `{"Msg": Internal Server Error}`)
				if err != nil {
					logger.Error("Internal error",
						slog.String("error", err.Error()))
				}
				return
			}
		} else {
			w.WriteHeader(404)
			_, err := io.WriteString(w, `{"Msg": Faulty steamID64}`)
			if err != nil {
				logger.Error("Internal error",
					slog.String("error", err.Error()))
			}
			return
		}
	}

	_, err = io.WriteString(w, fmt.Sprintf(`{"Elo": %d}`, data.Games.CS2.Elo))
	if err != nil {
		logger.Error("Internal error",
			slog.String("error", err.Error()))
	}
}

M faceit.go => faceit.go +7 -1
@@ 59,6 59,9 @@ type faceitInfo struct {
		CSGO struct {
			Elo int `json:"faceit_elo"`
		} `json:"csgo"`
		CS2 struct {
			Elo int `json:"faceit_elo"`
		} `json:"cs2"`
	} `json:"games"`
}



@@ 144,7 147,8 @@ func main() {

	r := http.NewServeMux()
	r.HandleFunc("/", fc.faceitHandler)
	r.HandleFunc("/api/elo/", fc.getElo)
	r.HandleFunc("/api/csgo/elo/", fc.getCSGOElo)
	r.HandleFunc("/api/cs2/elo/", fc.getCS2Elo)
	fs := http.FileServer(http.FS(css))
	r.Handle("/css/", fs)



@@ 331,6 335,8 @@ func (fc *faceitChecker) getSteamProfile(ctx context.Context, url string) (*xmlS
func (fc *faceitChecker) getFaceitPlayer(ctx context.Context, steamID64 string) (faceitInfo, error) {
	var info faceitInfo

	// Querying for CS:GO gets both CS:GO and CS2 elo.
	// TODO: Nevertheless change to CS2 in the future.
	url := "https://open.faceit.com/data/v4/players?game=csgo&game_player_id=" + steamID64
	req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
	if err != nil {