コード例 #1
0
        protected override SucoExpression deduceTypes(SucoTypeEnvironment env, SucoContext context)
        {
            var valueExpr = ValueExpression.DeduceTypes(env, context);
            var innerExpr = InnerExpression.DeduceTypes(env.DeclareVariable(VariableName, valueExpr.Type), context);

            return(new SucoLetExpression(StartIndex, EndIndex, VariableName, valueExpr, innerExpr, innerExpr.Type));
        }
コード例 #2
0
ファイル: ZingaPuzzlePublish.cs プロジェクト: Timwi/Zinga
        public HttpResponse PuzzlePublish(HttpRequest req)
        {
            if (req.Method != HttpMethod.Post)
            {
                return(HttpResponse.Empty(HttpStatusCode._405_MethodNotAllowed));
            }

            var jsonRaw = req.Post["puzzle"].Value;

            if (jsonRaw == null || !JsonDict.TryParse(jsonRaw, out var json))
            {
                return(HttpResponse.PlainText("The data transmitted is not valid JSON.", HttpStatusCode._400_BadRequest));
            }

            try
            {
                var puzzle = new Puzzle();

                puzzle.Title   = json["title"].GetString();
                puzzle.Author  = json["author"].GetString();
                puzzle.Rules   = json["rules"].GetString();
                puzzle.UrlName = MD5.ComputeUrlName(jsonRaw);

                var givens = json["givens"].GetList().Select((v, ix) => (v, ix)).Where(tup => tup.v != null).Select(tup => (cell: tup.ix, value: tup.v.GetInt())).ToArray();
                if (givens.Any(given => given.cell < 0 || given.cell >= 81))
                {
                    return(HttpResponse.PlainText($"At least one given is out of range (cell {puzzle.Givens.First(given => given.cell < 0 || given.cell >= 81).cell}).", HttpStatusCode._400_BadRequest));
                }
                puzzle.Givens = givens.Length > 0 ? givens : null;

                var constraints           = json["constraints"].GetList();
                var customConstraintTypes = json["customConstraintTypes"].GetList();

                using var tr = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.Serializable });
                using var db = new Db();

                if (db.Puzzles.Any(p => p.UrlName == puzzle.UrlName))
                {
                    return(HttpResponse.PlainText(puzzle.UrlName));
                }

                var dbConstraintTypes = constraints.Select(c => c["type"].GetInt()).Where(c => c >= 0).Distinct().ToArray()
                                        .Apply(cIds => db.Constraints.Where(c => cIds.Contains(c.ConstraintID))).AsEnumerable().ToDictionary(c => c.ConstraintID);
                var faultyId = constraints.Select(c => c["type"].GetInt()).Where(c => c >= 0).FirstOrNull(c => !dbConstraintTypes.ContainsKey(c));
                if (faultyId != null)
                {
                    return(HttpResponse.PlainText($"Unknown constraint type ID: {faultyId.Value}.", HttpStatusCode._400_BadRequest));
                }

                // Add the custom constraint types into the database
                foreach (var constraint in constraints)
                {
                    if (constraint["type"].GetInt() is int typeId && typeId < 0 && !dbConstraintTypes.ContainsKey(typeId))
                    {
                        if (~typeId >= customConstraintTypes.Count || customConstraintTypes[~typeId] == null)
                        {
                            return(HttpResponse.PlainText($"Undefined custom constraint type: {typeId}. List has {customConstraintTypes.Count} entries.", HttpStatusCode._400_BadRequest));
                        }
                        var cType = customConstraintTypes[~typeId];
                        var kind  = EnumStrong.Parse <ConstraintKind>(cType["kind"].GetString());

                        // Some verifications:
                        // Make sure the variable types parse as valid Suco types
                        var env = new SucoTypeEnvironment();
                        foreach (var(varName, varType) in cType["variables"].GetDict())
                        {
                            if (!SucoType.TryParse(varType.GetString(), out var type))
                            {
                                return(HttpResponse.PlainText($"Unrecognized Suco type: {varType.GetString()}.", HttpStatusCode._400_BadRequest));
                            }
                            env = env.DeclareVariable(varName, type);
                        }