Exemplo n.º 1
0
        public async Task <IActionResult> RegisterPuzzleForEditing(string fen, string variant, int checksByWhite, int checksByBlack)
        {
            fen    += " - 0 1";
            variant = Utilities.NormalizeVariantNameCapitalization(variant);
            if (!Array.Exists(supportedVariants, x => x == variant))
            {
                return(Json(new { success = false, error = "Unsupported variant." }));
            }
            if (variant == "ThreeCheck")
            {
                if (checksByWhite > 2 || checksByWhite < 0 || checksByBlack > 2 || checksByBlack < 0)
                {
                    return(Json(new { success = false, error = "Invalid amount of checks." }));
                }

                fen += String.Format(" +{0}+{1}", checksByWhite, checksByBlack);
            }
            Puzzle possibleDuplicate = await puzzleRepository.FindByFenAndVariantAsync(fen, variant);

            if (possibleDuplicate != null && possibleDuplicate.Approved)
            {
                return(Json(new { success = false, error = "Duplicate; same FEN and variant: " + Url.Action("TrainId", "Puzzle", new { id = possibleDuplicate.ID }) }));
            }
            ChessGame game   = gameConstructor.Construct(variant, fen);
            Puzzle    puzzle = new Puzzle();

            Task <int?> aid = loginHandler.LoggedInUserIdAsync(HttpContext);

            puzzle.Game       = game;
            puzzle.InitialFen = fen;
            puzzle.Variant    = variant;
            puzzle.Solutions  = new List <string>();
            do
            {
                puzzle.ID = Guid.NewGuid().GetHashCode();
            } while (puzzlesBeingEdited.Contains(puzzle.ID));
            puzzle.Author = (await aid).Value;
            puzzlesBeingEdited.Add(puzzle);
            return(Json(new { success = true, id = puzzle.ID }));
        }
Exemplo n.º 2
0
        public IActionResult RegisterPuzzleForEditing(string fen, string variant)
        {
            variant = Utilities.NormalizeVariantNameCapitalization(variant);
            if (!Array.Exists(supportedVariants, x => x == variant))
            {
                return(Json(new { success = false, error = "Unsupported variant." }));
            }
            ChessGame game   = gameConstructor.Construct(variant, fen);
            Puzzle    puzzle = new Puzzle();

            puzzle.Game       = game;
            puzzle.InitialFen = fen;
            puzzle.Variant    = variant;
            puzzle.Author     = loginHandler.LoggedInUserId(HttpContext).Value;
            puzzle.Solutions  = new List <string>();
            do
            {
                puzzle.ID = Guid.NewGuid().GetHashCode();
            } while (puzzlesBeingEdited.Contains(puzzle.ID));
            puzzlesBeingEdited.Add(puzzle);
            return(Json(new { success = true, id = puzzle.ID }));
        }