<commit_before>package handlers

import (
	"encoding/json"
	"net/http"

	"github.com/sirupsen/logrus"

	"github.com/ivan1993spb/snake-server/connections"
)

const URLRouteGetGames = "/games"

const MethodGetGames = http.MethodGet

// TODO: Create width and height?
type responseGetGamesEntity struct {
	ID    int `json:"id"`
	Limit int `json:"limit"`
	Count int `json:"count"`
}

type responseGetGamesHandler struct {
	Games []responseGetGamesEntity `json:"games"`
}

type getGamesHandler struct {
	logger       logrus.FieldLogger
	groupManager *connections.ConnectionGroupManager
}

type ErrGetGamesHandler string

func (e ErrGetGamesHandler) Error() string {
	return "get game handler error: " + string(e)
}

func NewGetGamesHandler(logger logrus.FieldLogger, groupManager *connections.ConnectionGroupManager) http.Handler {
	return &getGamesHandler{
		logger:       logger,
		groupManager: groupManager,
	}
}

func (h *getGamesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	h.logger.Info("get games handler start")
	defer h.logger.Info("get games handler end")

	response := responseGetGamesHandler{
		Games: []responseGetGamesEntity{},
	}
	for id, group := range h.groupManager.Groups() {
		response.Games = append(response.Games, responseGetGamesEntity{
			ID:    id,
			Limit: group.GetLimit(),
			Count: group.GetCount(),
		})
	}

	w.Header().Add("Content-Type", "application/json; charset=utf-8")

	if err := json.NewEncoder(w).Encode(response); err != nil {
		h.logger.Error(ErrGetGamesHandler(err.Error()))
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}
}
<commit_msg>Create limit, count, width and height info to get games handler<commit_after>package handlers

import (
	"encoding/json"
	"net/http"

	"github.com/sirupsen/logrus"

	"github.com/ivan1993spb/snake-server/connections"
)

const URLRouteGetGames = "/games"

const MethodGetGames = http.MethodGet

type responseGetGamesEntity struct {
	ID     int `json:"id"`
	Limit  int `json:"limit"`
	Count  int `json:"count"`
	Width  int `json:"width"`
	Height int `json:"height"`
}

type responseGetGamesHandler struct {
	Games []responseGetGamesEntity `json:"games"`
	Limit int                      `json:"limit"`
	Count int                      `json:"count"`
}

type getGamesHandler struct {
	logger       logrus.FieldLogger
	groupManager *connections.ConnectionGroupManager
}

type ErrGetGamesHandler string

func (e ErrGetGamesHandler) Error() string {
	return "get game handler error: " + string(e)
}

func NewGetGamesHandler(logger logrus.FieldLogger, groupManager *connections.ConnectionGroupManager) http.Handler {
	return &getGamesHandler{
		logger:       logger,
		groupManager: groupManager,
	}
}

func (h *getGamesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	groupCount := h.groupManager.GroupCount()

	response := responseGetGamesHandler{
		Games: make([]responseGetGamesEntity, 0, groupCount),
		Limit: h.groupManager.GroupLimit(),
		Count: groupCount,
	}

	for id, group := range h.groupManager.Groups() {
		response.Games = append(response.Games, responseGetGamesEntity{
			ID:     id,
			Limit:  group.GetLimit(),
			Count:  group.GetCount(),
			Width:  int(group.GetWorldWidth()),
			Height: int(group.GetWorldHeight()),
		})
	}

	w.Header().Set("Content-Type", "application/json; charset=utf-8")

	if err := json.NewEncoder(w).Encode(response); err != nil {
		h.logger.Error(ErrGetGamesHandler(err.Error()))
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}
}

{'fraction_non_alphanumeric': 0.12242626599888703, 'fraction_numerical': 0.003060656649972176, 'mean_word_length': 4.715421303656598, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5190512', 'n_tokens_mistral': 1225, 'n_tokens_neox': 1154, 'n_words': 252}