Esempio n. 1
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;
            }
        }