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"); }
public ActionResult UpdateLink(Link link) { var response = new ResponseWithValidation(); JsonValidation.GetJsonWithValidation(ModelState, ref response); if (ModelState.IsValid) { _linkRepository.UpdateLink(link); } return(Json(JsonConvert.SerializeObject(response))); }
public ActionResult AddLink(Link link) { var response = new ResponseWithValidation(); JsonValidation.GetJsonWithValidation(ModelState, ref response); if (ModelState.IsValid) { Link addedLink = _linkRepository.AddLink(link); response.Data = addedLink; } return(Json(JsonConvert.SerializeObject(response))); }
public static JToken JsonCommand(JToken jtoken) { JsonValidation.ValidateJTokenRuleChecker(jtoken); //If object is a Board... try //Try validating board, then Score { JsonValidation.ValidateBoard(jtoken); return(JToken.Parse(RuleChecker.Score(jtoken.ToObject <string[][]>()).ToString())); } catch //figure out why board is invalid if this is a board { if (jtoken.Count() > 2) { JsonValidation.ValidateBoard(jtoken); } } JsonValidation.ValidateStone(jtoken.ElementAt(0)); JsonValidation.ValidatePass(jtoken.ElementAt(1)); //If object is [Stone, Move] and Move is "pass" try { if (jtoken.ElementAt(1).ToObject <string>() == "pass") //Pass this to RuleChecker { return(JToken.Parse(JsonConvert.SerializeObject( RuleChecker.Pass()))); } } catch { } //If object is [Stone, Move] and Move is a Play JsonValidation.ValidatePlay(jtoken.ElementAt(1)); JsonValidation.ValidatePoint(jtoken.ElementAt(1).ElementAt(0)); JsonValidation.ValidateBoards(jtoken.ElementAt(1).ElementAt(1)); try { return(JToken.Parse(JsonConvert.SerializeObject( RuleChecker.Play( jtoken.ElementAt(0).ToObject <string>(), jtoken.ElementAt(1).ElementAt(0).ToObject <string>(), jtoken.ElementAt(1).ElementAt(1).ToObject <string[][][]>())))); } catch { return(JToken.Parse(JsonConvert.SerializeObject(false))); } throw new InvalidJsonInputException("Unrecognized JSONCommand passed to RuleCheckerWrapper"); }
public static JToken JsonCommand(JToken jtoken) { JsonValidation.ValidateJTokenRuleChecker(jtoken); //If object is a Board... if (jtoken.Count() > 2) { return(JToken.Parse(RuleChecker.Score(jtoken.ToObject <string[][]>()).ToString())); } //If object is [Stone, Move] and Move is "pass" try { if (jtoken.ElementAt(1).ToObject <string>() == "pass") //Pass this to RuleChecker { return(JToken.Parse(JsonConvert.SerializeObject( RuleChecker.Pass()))); } } catch { } //If object is [Stone, Move] and Move is a Play JsonValidation.ValidatePlay(jtoken.ElementAt(1)); try { return(JToken.Parse(JsonConvert.SerializeObject( RuleChecker.Play( jtoken.ElementAt(0).ToObject <string>(), //Stone jtoken.ElementAt(1).ElementAt(0).ToObject <string>(), //Point jtoken.ElementAt(1).ElementAt(1).ToObject <string[][][]>())))); //Boards } catch (RuleCheckerException) { return(JToken.Parse(JsonConvert.SerializeObject(false))); } throw new InvalidJsonInputException("Unrecognized JSONCommand passed to RuleCheckerAdapter"); }
public static PlexAuth NewPlexAuthPin() { var j = Get.DownloadJson(PlexEndpoints.PlexPinsEndpoint, RestSharp.Method.POST); return(JsonValidation.IsValidJson(j) ? PlexAuth.FromJson(j) : null); }
public static PlexAuth FromPinEndpoint(PlexAuth auth) { var j = Get.DownloadJson(auth.PinEndpointUrl); return(JsonValidation.IsValidJson(j) ? PlexAuth.FromJson(j) : null); }
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"); }
public JToken JsonCommand(JToken jtoken) { JToken statement; BoardWrapper boardObject; JsonValidation.ValidateJTokenBoard(jtoken); boardObject = new BoardWrapper(jtoken.ElementAt(0).ToObject <string[][]>(), jtoken.ElementAt(0).Count()); statement = jtoken.ElementAt(1); switch (statement.ElementAt(0).ToObject <string>()) { case "occupied?": JsonValidation.ValidateStatementElements(statement, 1); return(JToken.Parse(JsonConvert.SerializeObject(boardObject.Occupied( statement.ElementAt(1).ToObject <string>())))); case "occupies?": JsonValidation.ValidateStatementElements(statement, 2); return(JToken.Parse(JsonConvert.SerializeObject(boardObject.Occupies( statement.ElementAt(1).ToObject <string>(), statement.ElementAt(2).ToObject <string>())))); case "reachable?": JsonValidation.ValidateStatementElements(statement, 2); return(JToken.Parse(JsonConvert.SerializeObject(boardObject.Reachable( statement.ElementAt(1).ToObject <string>(), statement.ElementAt(2).ToObject <string>())))); case "place": JsonValidation.ValidateStatementElements(statement, 2); try //PlaceStone may return an exception { return(JToken.Parse(JsonConvert.SerializeObject(boardObject.PlaceStone( statement.ElementAt(1).ToObject <string>(), statement.ElementAt(2).ToObject <string>())))); } catch (BoardException) { return(JToken.Parse(JsonConvert.SerializeObject("This seat is taken!"))); } case "remove": JsonValidation.ValidateStatementElements(statement, 2); try { //RemoveStone may return an exception return(JToken.Parse(JsonConvert.SerializeObject(boardObject.RemoveStone( statement.ElementAt(1).ToObject <string>(), statement.ElementAt(2).ToObject <string>())))); } catch (BoardException) { return(JToken.Parse(JsonConvert.SerializeObject("I am just a board! I cannot remove what is not there!"))); } case "get-points": JsonValidation.ValidateStatementElements(statement, 1); return(JToken.Parse(JsonConvert.SerializeObject(boardObject.GetPoints( statement.ElementAt(1).ToObject <string>())))); } throw new InvalidJsonInputException("Unrecognized query/command passed to BoardAdapter"); }