コード例 #1
0
        public JToken JsonCommand(JToken jtoken, string name = "no name", string AIType = "dumb", int n = 1)
        {
            JsonValidation.ValidateJTokenPlayer(jtoken);

            switch (jtoken.ElementAt(0).ToObject <string>())
            {
            case "register":
                if (AIType == "less dumb")
                {
                    _player = new PlayerWrapper(name, AIType, n);
                }
                else
                {
                    _player = new PlayerWrapper(name, AIType);
                }
                return(JToken.Parse(JsonConvert.SerializeObject(name)));

            case "receive-stones":
                _player.ReceiveStones(jtoken.ElementAt(1).ToObject <string>());
                return(JToken.Parse(JsonConvert.SerializeObject(null)));

            case "make-a-move":
                try
                {
                    return(JToken.Parse(JsonConvert.SerializeObject(_player.MakeAMove(
                                                                        jtoken.ElementAt(1).ToObject <string[][][]>()))));
                }
                catch (PlayerException e)
                {
                    return(JToken.Parse(JsonConvert.SerializeObject(e.Message)));
                }
            }

            throw new InvalidJsonInputException("Unrecognized JSONCommand passed to PlayerWrapper");
        }
コード例 #2
0
        public JToken JsonCommand(JToken jtoken, string name = "no name")
        {
            JsonValidation.ValidateJTokenPlayer(jtoken);

            try
            {
                switch (jtoken.ElementAt(0).ToObject <string>())
                {
                case "register":
                    try
                    {
                        name = _player.Register(name);
                        return(JToken.Parse(JsonConvert.SerializeObject(name)));
                    }
                    catch (JsonSerializationException e)
                    {
                        throw new InvalidJsonInputException(e.Message);
                    }

                case "receive-stones":
                    try
                    {
                        _player.ReceiveStones(jtoken.ElementAt(1).ToObject <string>());
                    }
                    catch (JsonSerializationException e)
                    {
                        throw new InvalidJsonInputException(e.Message);
                    }
                    catch (ArgumentException e)
                    {
                        throw new InvalidJsonInputException(e.Message);
                    }
                    return(JToken.Parse(JsonConvert.SerializeObject(null)));

                case "make-a-move":
                    try
                    {
                        return(JToken.Parse(JsonConvert.SerializeObject(_player.MakeAMove(
                                                                            jtoken.ElementAt(1).ToObject <string[][][]>()))));
                    }
                    catch (PlayerException e)
                    {
                        return(JToken.Parse(JsonConvert.SerializeObject(e.Message)));
                    }
                    catch (PlayerProxyException e)
                    {
                        return(JToken.Parse(JsonConvert.SerializeObject(e.Message)));
                    }
                    catch (JsonSerializationException e)
                    {
                        throw new InvalidJsonInputException(e.Message);
                    }
                }
            }

            catch (WrapperException e)
            {
                throw new InvalidJsonInputException("A wrapper exception occurred", e);
            }
            throw new InvalidJsonInputException("Unrecognized JSONCommand passed to PlayerWrapper");
        }