public static bool TryParse(string encoded, GamePlayer owner, out LobbySeek seek)
        {
            string[] parts = encoded.Split(';');
            if (parts.Length != 4)
            {
                seek = null;
                return(false);
            }

            int secondsInitial;

            if (!int.TryParse(parts[0], out secondsInitial))
            {
                seek = null;
                return(false);
            }
            if ((secondsInitial != 30 && secondsInitial != 45 && secondsInitial != 90) && (secondsInitial % 60 != 0 || secondsInitial > 30 * 60 || secondsInitial < 0))
            {
                seek = null;
                return(false);
            }

            int secondsIncrement;

            if (!int.TryParse(parts[1], out secondsIncrement))
            {
                seek = null;
                return(false);
            }

            if (secondsIncrement < 0 || secondsIncrement > 30 || (secondsIncrement == 0 && secondsInitial == 0))
            {
                seek = null;
                return(false);
            }

            string[] allowedVariants = new string[] { "Antichess", "Atomic", "Crazyhouse", "Horde", "KingOfTheHill", "RacingKings", "ThreeCheck" };
            string   variant         = parts[2];

            if (!allowedVariants.Contains(parts[2]))
            {
                seek = null;
                return(false);
            }

            if (parts[3] != "true" && parts[3] != "false")
            {
                seek = null;
                return(false);
            }
            bool symmetrical = parts[3] == "true";

            seek = new LobbySeek(new TimeControl(secondsInitial, secondsIncrement), variant, symmetrical, owner);
            return(true);
        }
Beispiel #2
0
        async Task HandleReceived(string text)
        {
            SocketMessage message = new SocketMessage(text);

            if (!message.Okay)
            {
                await Send("{\"t\":\"error\",\"d\":\"invalid message\"}");

                return;
            }
            switch (message.Type)
            {
            case "create":
                LobbySeek seek;
                bool      isValid = LobbySeek.TryParse(message.Data, client, out seek);
                if (!isValid)
                {
                    await Send("{\"t\":\"error\",\"d\":\"invalid seek\"}");

                    return;
                }
                string seekId = await seekRepository.Add(seek);
                await Send("{\"t\":\"ack\",\"d\":\"" + seekId + "\"}");

                break;

            case "remove":
                await seekRepository.Remove(message.Data, client);

                break;

            case "bump":
                seekRepository.Bump(message.Data, client);
                break;

            case "join":
                LobbySeek joined = seekRepository.Get(message.Data);
                if (joined == null)
                {
                    await Send("{\"t\":\"error\",\"d\":\"seek does not exist\"}");

                    return;
                }
                await seekRepository.Remove(joined.ID, joined.Owner);

                bool hostIsWhite = randomProvider.RandomBool();
                int  nWhite;
                int  nBlack;
                int  max = joined.Variant != "RacingKings" ? 960 : 1440;
                nWhite = randomProvider.RandomPositiveInt(max);
                if (joined.Symmetrical)
                {
                    nBlack = nWhite;
                }
                else
                {
                    nBlack = randomProvider.RandomPositiveInt(max);
                }
                Game game = new Game(gameRepository.GenerateId(), hostIsWhite ? joined.Owner : client, hostIsWhite ? client : joined.Owner, joined.Variant, joined.FullVariantName, nWhite, nBlack, joined.Symmetrical, joined.TimeControl, DateTime.UtcNow, 0);
                gameRepository.Add(game);
                string redirectJson = "{\"t\":\"redirect\",\"d\":\"" + game.ID + "\"}";
                await Send(redirectJson);

                await handlerRepository.SendTo(joined.Owner, redirectJson);

                break;

            case "init":
                List <LobbySeek> seeks = seekRepository.GetShallowCopy();
                foreach (LobbySeek s in seeks)
                {
                    Dictionary <string, object> msg = new Dictionary <string, object>();
                    msg.Add("t", "add");
                    msg.Add("d", s.SeekJson(userRepository));
                    await Send(JsonConvert.SerializeObject(msg));
                }
                break;

            case "keepAlive":
                await Send("{\"t\":\"keepAlive\"}");

                break;

            default:
                await Send("{\"t\":\"error\",\"d\":\"invalid message\"}");

                break;
            }
        }
        public static bool TryParse(string encoded, GamePlayer owner, out LobbySeek seek)
        {
            string[] parts = encoded.Split(';');
            if (parts.Length != 7)
            {
                seek = null;
                return(false);
            }

            if (!int.TryParse(parts[0], out int secondsInitial))
            {
                seek = null;
                return(false);
            }
            if ((secondsInitial != 30 && secondsInitial != 45 && secondsInitial != 90) && (secondsInitial % 60 != 0 || secondsInitial > 30 * 60 || secondsInitial < 0))
            {
                seek = null;
                return(false);
            }

            if (!int.TryParse(parts[1], out int secondsIncrement))
            {
                seek = null;
                return(false);
            }

            if (secondsIncrement < 0 || secondsIncrement > 30 || (secondsIncrement == 0 && secondsInitial == 0))
            {
                seek = null;
                return(false);
            }

            string[] allowedVariants = new string[] { "Antichess", "Atomic", "Crazyhouse", "Horde", "KingOfTheHill", "RacingKings", "ThreeCheck" };
            string   variant         = parts[2];

            if (!allowedVariants.Contains(parts[2]))
            {
                seek = null;
                return(false);
            }

            if (parts[3] != "random" && parts[3] != "number")
            {
                seek = null;
                return(false);
            }
            Position position = parts[3] == "random" ? Position.Random : Position.FromNumbers;

            if (parts[4] != "true" && parts[4] != "false")
            {
                seek = null;
                return(false);
            }
            bool symmetrical = parts[4] == "true";

            if (!int.TryParse(parts[5], out int whitePosition) || whitePosition <0 || whitePosition> (variant != "RacingKings" ? 959 : 1439))
            {
                seek = null;
                return(false);
            }

            if (!int.TryParse(parts[6], out int blackPosition) || blackPosition <0 || blackPosition> (variant != "RacingKings" ? 959 : 1439))
            {
                seek = null;
                return(false);
            }

            if (whitePosition == 518 && blackPosition == 518 && variant != "RacingKings")
            {
                seek = null;
                return(false);
            }

            seek = new LobbySeek(new TimeControl(secondsInitial, secondsIncrement), variant, position, symmetrical, whitePosition, blackPosition, owner);
            return(true);
        }