private RequestHandlingResult GetPlayersStatistic(string request)
        {
            var splitRequest = request.Split('/');
            var name         = splitRequest[0];
            var games        = GameServers
                               .Where(x => x.Scoreboard.Any(y => y.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)))
                               .ToArray();

            if (games.Length == 0)
            {
                return(RequestHandlingResult.Fail(HttpStatusCode.NotFound));
            }
            var serializStatistic = jsonSerializer.Serialize(statistic.GetPlayerStatistic(name, games));

            return(RequestHandlingResult.Successfull(serializStatistic.GetBytesInAscii()));
        }
        private RequestHandlingResult PutMatches(string endpoint, string body, string date)
        {
            GameServer gameServer;

            if (!AdvertiseServers.ContainsKey(endpoint) ||
                !CheckMatchField(body) ||
                !jsonSerializer.TryDeserialize(body, out gameServer) ||
                !CheckGameMode(endpoint, gameServer.GameMode))
            {
                return(RequestHandlingResult.Fail(HttpStatusCode.BadRequest));
            }
            gameServer.Endpoint    = endpoint;
            gameServer.DateAndTime = DateTime.Parse(date, null, DateTimeStyles.RoundtripKind);
            GameServers.Add(gameServer);
            dataBase.AddGameServer(gameServer);
            return(RequestHandlingResult.Successfull(new byte[0]));
        }
        private RequestHandlingResult PutInfo(string endpoint, string body)
        {
            Information info;

            if (!jsonSerializer.TryDeserialize(body, out info) || !CheckInfoField(body))
            {
                return(RequestHandlingResult.Fail(HttpStatusCode.BadRequest));
            }
            var advertRequest = new AdvertiseQueryServer(endpoint, info);

            if (AdvertiseServers.ContainsKey(endpoint))
            {
                dataBase.UpdateAdvertServer(advertRequest);
            }
            else
            {
                dataBase.AddAdvertServer(advertRequest);
            }
            AdvertiseServers.AddOrUpdate(endpoint, advertRequest, ((s, server) => advertRequest));
            return(RequestHandlingResult.Successfull(new byte[0]));
        }
        private RequestHandlingResult ProcessPutRequest(string requestString, string body)
        {
            var splitedString = PutRequestRegex.Split(requestString);

            if (splitedString.Length <= 3 || body.Length == 0)
            {
                return(RequestHandlingResult.Fail(HttpStatusCode.BadRequest));
            }

            switch (splitedString[3])
            {
            case "info":
                return(PutInfo(splitedString[2], body));

            case "matches":
                return(PutMatches(splitedString[2], body, splitedString[4]));

            default:
                return(RequestHandlingResult.Fail(HttpStatusCode.BadRequest));
            }
        }
        private RequestHandlingResult GetAdvertMatch(string date, GameServer[] games)
        {
            DateTime dateTime;

            try
            {
                dateTime = DateTime.Parse(date, null, DateTimeStyles.RoundtripKind);
            }
            catch (Exception)
            {
                return(RequestHandlingResult.Fail(HttpStatusCode.BadRequest));
            }
            var match = games.Where(x => x.DateAndTime == dateTime).ToArray();

            if (match.Length == 0)
            {
                return(RequestHandlingResult.Fail(HttpStatusCode.NotFound));
            }
            var serializeMatch = jsonSerializer.Serialize(match[0]);

            return(RequestHandlingResult.Successfull(serializeMatch.GetBytesInAscii()));
        }
        private RequestHandlingResult GetServerInformation(string request)
        {
            var splitedRequest = ServerInfoRegex.Split(request);

            if (splitedRequest[1] == "info")
            {
                var serializeServers = jsonSerializer.Serialize(AdvertiseServers.Values.ToArray());
                return(RequestHandlingResult.Successfull(serializeServers.GetBytesInAscii()));
            }
            var endpoint           = splitedRequest[1];
            var neededGames        = GameServers.Where(x => x.Endpoint == endpoint).ToArray();
            var neededAdvertServer = AdvertiseServers.Where(x => x.Key == endpoint).ToArray();

            if (!neededAdvertServer.Any() && neededGames.Length == 0)
            {
                return(RequestHandlingResult.Fail(HttpStatusCode.NotFound));
            }
            object informations;

            switch (splitedRequest[2])
            {
            case "info":
                informations = neededAdvertServer[0].Value.Info;
                break;

            case "matches":
                return(GetAdvertMatch(splitedRequest[3], neededGames));

            case "stats":
                informations = statistic.GetServerStatistic(neededGames);
                break;

            default:
                return(RequestHandlingResult.Fail(HttpStatusCode.BadRequest));
            }
            var serializeInformation = jsonSerializer.Serialize(informations);

            return(RequestHandlingResult.Successfull(serializeInformation.GetBytesInAscii()));
        }
        private RequestHandlingResult GetReport(string request)
        {
            var splitRequest = ReportRegex.Split(request)
                               .Where(x => !string.IsNullOrEmpty(x))
                               .ToArray();
            var n = 5;

            if (splitRequest.Length > 1)
            {
                n = GetRightCountOfItems(int.Parse(splitRequest[1]));
            }
            if (n == 0 || GameServers.Count == 0)
            {
                var emptyAnswer = jsonSerializer.Serialize(new string[0]);
                return(RequestHandlingResult.Successfull(emptyAnswer.GetBytesInAscii()));
            }
            object reportResult;

            switch (splitRequest[0])
            {
            case "recent-matches":
                reportResult = statistic.GetRecentMatches(n, GameServers);
                break;

            case "best-players":
                reportResult = statistic.GetBestPlayers(n, GameServers);
                break;

            case "popular-servers":
                reportResult = statistic.GetPopularServers(n, AdvertiseServers, GameServers);
                break;

            default:
                return(RequestHandlingResult.Fail(HttpStatusCode.BadRequest));
            }
            var serializeReport = jsonSerializer.Serialize(reportResult);

            return(RequestHandlingResult.Successfull(serializeReport.GetBytesInAscii()));
        }
        private RequestHandlingResult ProcessGetRequest(string requestString)
        {
            var splitedRequest = GetRequestRegex.Split(requestString);

            if (splitedRequest.Length < 2)
            {
                return(RequestHandlingResult.Fail(HttpStatusCode.BadRequest));
            }

            switch (splitedRequest[1])
            {
            case "servers":
                return(GetServerInformation(splitedRequest[2]));

            case "players":
                return(GetPlayersStatistic(splitedRequest[2]));

            case "reports":
                return(GetReport(splitedRequest[2]));

            default:
                return(RequestHandlingResult.Fail(HttpStatusCode.BadRequest));
            }
        }