public void Handle(CalculateNodeStatisticsQuery options)
        {
            if (options.IsLongRunning)
            {
                _applicationDatabase.Database.SetCommandTimeout(new TimeSpan(0, 10, 0));
            }
            Console.WriteLine("\tCalculateNodeStatisticsQueryHandler {0}", DateTime.Now);
            var nodesWithLatency = new List <Tuple <int, Node> >();

            foreach (var node in _applicationDatabase.Nodes)
            {
                var sum = _applicationDatabase.Statistics
                          .Where(s => s.NodeId == node.Id && s.Timestamp > options.MinTimestamp)
                          .Select(s => s.IsSuccess ? 1 : 0)
                          .Sum();
                var count = _applicationDatabase.Statistics
                            .Where(s => s.NodeId == node.Id && s.Timestamp > options.MinTimestamp)
                            .Count();
                var latency = _applicationDatabase.Statistics
                              .Where(s => s.NodeId == node.Id && s.Timestamp > options.MinTimestamp)
                              .Select(s => s.Latency)
                              .Sum();
                if (sum >= (count - sum))
                {
                    nodesWithLatency.Add(new Tuple <int, Node>(latency, node));
                }
                else
                {
                    node.IsActive = false;
                    _applicationDatabase.SaveChanges();
                }
            }
            foreach (var node in nodesWithLatency
                     .OrderBy(t => t.Item1)
                     .Take(options.MaxActives)
                     .Select(t => t.Item2)
                     )
            {
                node.IsActive            = true;
                node.LastActiveTimestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
                _applicationDatabase.SaveChanges();
            }
            foreach (var node in nodesWithLatency
                     .OrderBy(t => t.Item1)
                     .Skip(options.MaxActives)
                     .Select(t => t.Item2)
                     )
            {
                node.IsActive = false;
                _applicationDatabase.SaveChanges();
            }
        }
Example #2
0
 /// <summary>
 /// This method seeds the SpriteTypes table
 /// Since these were added manually, we have to do that too.
 /// </summary>
 private static void SeedSpriteTypeTable()
 {
     using (ApplicationDatabase ctxt = new ApplicationDatabase())
     {
         List <SpriteType> spriteTypesInDb = ctxt.SpriteTypes.ToList();
         if (!spriteTypesInDb.Any(o => o.Id == 1 && o.spriteTypeName == "sprite"))
         {
             ctxt.SpriteTypes.Add(new SpriteType()
             {
                 Id = 1, spriteTypeName = "sprite"
             });
         }
         if (!spriteTypesInDb.Any(o => o.Id == 2 && o.spriteTypeName == "stage"))
         {
             ctxt.SpriteTypes.Add(new SpriteType()
             {
                 Id = 2, spriteTypeName = "stage"
             });
         }
         if (!spriteTypesInDb.Any(o => o.Id == 3 && o.spriteTypeName == "procDef"))
         {
             ctxt.SpriteTypes.Add(new SpriteType()
             {
                 Id = 3, spriteTypeName = "procDef"
             });
         }
         ctxt.SaveChanges();
     }
 }
Example #3
0
 private void SaveAuthorsToDatabase(List <Author> toAddToDatabase)
 {
     using (ApplicationDatabase ctxt = new ApplicationDatabase())
     {
         foreach (Author authorToAdd in toAddToDatabase)
         {
             if (!ctxt.Authors.Any(pa => pa.Id == authorToAdd.Id))
             {
                 Console.Write("☻");
                 try
                 {
                     ctxt.Authors.Add(authorToAdd);
                 }
                 catch (Exception e)
                 {
                     Console.WriteLine(e.Message);
                 }
             }
             else
             {
                 Console.Write("☺");
             }
         }
         Console.Write('\n');
         ctxt.SaveChanges();
     }
 }
Example #4
0
        public void Handle(ClearNodeStatisticsQuery options)
        {
            var toDelete = _applicationDatabase.Statistics
                           .Where(s => s.Timestamp < options.MinTimestamp);

            _applicationDatabase.Statistics.RemoveRange(toDelete);
            _applicationDatabase.SaveChanges();
        }
        public bool Handle(MarkMainChainQuery query)
        {
            if (query.IsLongRunning)
            {
                _applicationDatabase.Database.SetCommandTimeout(new TimeSpan(0, 10, 0));
            }
            using (var transaction = _applicationDatabase.Database.BeginTransaction()) {
                try
                {
                    _applicationDatabase.BlockChain
                    .Where(b => b.IsInMainChain && b.BlockHash != BlockchainConsensus.GenesisBlock.BlockHash)
                    .ToList()
                    .ForEach(b => b.IsInMainChain = false);
                    _applicationDatabase.SaveChangesAsync();

                    var headBlock = _queryHandlerGetHeadBlock.Handle(new GetHeadBlockQuery()
                    {
                    });
                    if (headBlock.IsInMainChain == false)
                    {
                        _applicationDatabase.BlockChain.Update(headBlock);
                        headBlock.IsInMainChain = true;
                        _applicationDatabase.SaveChanges();
                    }
                    string blockHashPrevious = headBlock.BlockHashPrevious;
                    while (!string.IsNullOrEmpty(blockHashPrevious) && blockHashPrevious != BlockchainConsensus.GenesisBlock.BlockHash)
                    {
                        var block = _applicationDatabase.BlockChain.Single(b => b.BlockHash == blockHashPrevious);
                        blockHashPrevious   = block.BlockHashPrevious;
                        block.IsInMainChain = true;
                        _applicationDatabase.SaveChanges();
                    }
                    transaction.Commit();
                    return(true);
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    return(false);
                }
            }
        }
Example #6
0
        public void ParseProject(string projectJson, int projectId)
        {
            if (projectJson.StartsWith("PK"))
            {
                return;
            }

            JObject projectObject;

            try { projectObject = JObject.Parse(projectJson); }
            catch (Exception ex) { Console.WriteLine(ex.Message); return; }

            projectObject.TryGetValue("targets", out JToken targetToken);
            if (targetToken == null)
            {
                return;
            }

            using (ApplicationDatabase ctxt = new ApplicationDatabase())
            {
                foreach (var obj in (JArray)targetToken)
                {
                    DecomposedSb3Target project = DecomposeTarget(obj as JObject, projectId);
                    foreach (KeyValuePair <Script, List <Block> > script in project.AllScriptsAndBlocks)
                    {
                        script.Key.ProjectId   = projectId;
                        script.Key.TotalBlocks = script.Value.Count();

                        ctxt.Scripts.Add(script.Key);
                        ctxt.SaveChanges();

                        foreach (Block b in script.Value)
                        {
                            JSONReader.SaveBlockWithParameters(b, b.parameters);
                        }
                    }
                    ctxt.Procedures.AddRange(project.AllProcedures);
                    ctxt.SaveChanges();
                }
            }
        }
Example #7
0
        public String ClearCanvas([FromQuery] string psd)
        {
            var password = ComputeSha256Hah(psd, _config["SHA256:Salt"]);

            if (password == _config["SHA256:Password"])
            {
                var canvas = _context.Canvas
                             .Include(c => c.ColorData)
                             .OrderByDescending(c => c.CanvasID)
                             .FirstOrDefault();
                foreach (var tableRow in canvas.ColorData)
                {
                    tableRow.Hex = "#FFFFFF";
                }
                _context.SaveChanges();
                return("All done");
            }
            else
            {
                return("Nice try but you are unauthorized");
            }
        }
        /// <summary>
        /// Return empty database with genesis block
        /// </summary>
        /// <param name="seed"></param>
        /// <returns></returns>
        public static ApplicationDatabase GetDatabase(Action <ApplicationDatabase> seed)
        {
            var options = new DbContextOptionsBuilder <ApplicationDatabase>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .ConfigureWarnings(w => w.Ignore(InMemoryEventId.TransactionIgnoredWarning))
                          .Options;
            var context = new ApplicationDatabase(options);

            context.BlockChain.Add(BlockchainConsensus.GenesisBlock);
            context.SaveChanges();

            seed(context);
            return(context);
        }
        public object PostUpateNote([FromBody] UserNote Body)
        {
            int UserID = GetUserIDFromClaims();

            if (ModelState.IsValid)
            {
                var Note = _db.User
                           .Include(u => u.UserNote)
                           .First(u => u.UserID == UserID)
                           .UserNote
                           .Single(n => n.NoteID == Body.NoteID);

                Note.Heading = Body.Heading;
                Note.Note    = Body.Note;

                _db.SaveChanges();
                return(new { message = "successful" });
            }
            else
            {
                return(new { message = "Invalid Request" });
            }
        }
Example #10
0
        private static void ImportSb2BlockTypes()
        {
            string[] sb2BlockTypes  = File.ReadAllLines(@"[DOES NOT EXIST] Assets\Sb2BlockTypes");
            string[] sb3BlockInputs = File.ReadAllLines(@"[DOES NOT EXIST] Assets\Sb3BlockInputs");
            using (ApplicationDatabase ctxt = new ApplicationDatabase())
            {
                ctxt.OpCodes.RemoveRange(ctxt.OpCodes);
                ctxt.SaveChanges();

                foreach (string blockType in sb2BlockTypes)
                {
                    string[] opCodeInfo = blockType.Split(',');
                    OpCode   newOpCode  = new OpCode()
                    {
                        OpcodeBlockType    = opCodeInfo[0].Trim(),
                        OpCodeSymbolLegacy = opCodeInfo[1].Trim(),
                        OpCodeSymbolSb3    = "".Trim(),
                        ShapeName          = opCodeInfo[2].Trim(),
                        IsInput            = (opCodeInfo[3] == "Yes").ToString().Trim()
                    };
                    ctxt.OpCodes.Add(newOpCode);
                }
                //Now put in sb3 block input types. They don't have an sb2 counterpart.
                foreach (string blockInput in sb3BlockInputs)
                {
                    OpCode newOpCode = new OpCode()
                    {
                        OpcodeBlockType    = "MenuInput",
                        IsInput            = "True",
                        OpCodeSymbolLegacy = "",
                        OpCodeSymbolSb3    = blockInput
                    };
                    ctxt.OpCodes.Add(newOpCode);
                }
                ctxt.SaveChanges();
            }
        }
Example #11
0
        public bool Handle(AddBlockQuery query)
        {
            if (query.IsLongRunning)
            {
                _applicationDatabase.Database.SetCommandTimeout(new TimeSpan(1, 0, 0));
            }
            try
            {
                var dbNewBlock = new Database.Block()
                {
                    BlockHash         = query.NewBlock.BlockHash,
                    BlockHashPrevious = query.NewBlock.BlockHashPrevious,
                    Length            = query.NewBlock.Length,
                    Nonce             = query.NewBlock.Nonce,
                    Timestamp         = query.NewBlock.Timestamp
                };
                _applicationDatabase.BlockChain.AddAsync(dbNewBlock);
                _applicationDatabase.SaveChanges();

                foreach (var bm in query.NewBlock.Messages)
                {
                    var msg = _applicationDatabase.Messages.Single(e => e.Id == bm.Id);
                    _applicationDatabase.BlockMessages.Add(new BlockMessages()
                    {
                        StoreId   = msg.StoreId,
                        BlockHash = dbNewBlock.BlockHash
                    });
                }
                _applicationDatabase.SaveChanges();
                return(true);
            }
            catch (Exception ex)
            {
                Console.WriteLine("AddBlockQueryHandler: {0} \n{1}", ex.Message, ex.StackTrace);
                return(false);
            }
        }
Example #12
0
 public void DeleteTeacher(int TeacherID)
 {
     try
     {
         var teacher = _context.Teachers.FirstOrDefault(s => s.TeacherID == TeacherID);
         _context.Teachers.Remove(teacher);
         _context.SaveChanges();
         return;
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex);
         return;
     }
 }
 public void DeleteSubject(int SubjectID)
 {
     try
     {
         var subject = _context.Subjects.FirstOrDefault(s => s.SubjectID == SubjectID);
         _context.Subjects.Remove(subject);
         _context.SaveChanges();
         return;
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex);
         return;
     }
 }
 public void DeleteChapter(int ChapterID)
 {
     try
     {
         var chapter = _context.Chapters.FirstOrDefault(s => s.ChapterID == ChapterID);
         _context.Chapters.Remove(chapter);
         _context.SaveChanges();
         return;
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex);
         return;
     }
 }
 public void DeleteTutorial(int TutorialID)
 {
     try
     {
         var tutorial = _context.Tutorials.FirstOrDefault(s => s.TutorialID == TutorialID);
         _context.Tutorials.Remove(tutorial);
         _context.SaveChanges();
         return;
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex);
         return;
     }
 }
Example #16
0
 public void DeleteAssignment(int AssignmentID)
 {
     try
     {
         var assignment = _context.Assignments.FirstOrDefault(s => s.AssignmentlID == AssignmentID);
         _context.Assignments.Remove(assignment);
         _context.SaveChanges();
         return;
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex);
         return;
     }
 }
Example #17
0
 private void UpdateAuthor(Author toUpdate)
 {
     using (ApplicationDatabase _dbContext = new ApplicationDatabase())
     {
         var entity = _dbContext.Authors.Where(c => c.Id == toUpdate.Id).AsQueryable().FirstOrDefault();
         if (entity == null)
         {
             _dbContext.Authors.Add(toUpdate);
         }
         else
         {
             _dbContext.Entry(entity).CurrentValues.SetValues(toUpdate);
         }
         _dbContext.SaveChanges();
     }
 }
Example #18
0
        public void CreateNewCanvas()
        {
            var matrix = new ColorData[SIZE * SIZE];

            for (int i = 0; i < SIZE; i++)
            {
                for (int j = 0; j < SIZE; j++)
                {
                    matrix[SIZE * i + j] = new ColorData
                    {
                        RowIndex    = i,
                        ColumnIndex = j,
                        Hex         = "#FFFFFF"
                    };
                }
            }
            _context.Canvas.Add(new Models.Canvas {
                ColorData = matrix
            });
            _context.SaveChanges();
        }
Example #19
0
        public static void Map(bool remapCompletely = false)
        {
            //This is simply a file that contains on every line an sb2 opcode and its equivalent sb3 opcode, separated by a ~ character
            //Example: someSb2Opcode ~ someSb3Opcode
            if (remapCompletely)
            {
                ImportSb2BlockTypes();
            }
            string[] sb2BlocksAndTheirSb3Mappings = File.ReadAllLines(@"[DOES NOT EXIST] Assets\blockMappingSb2toSb3");
            using (ApplicationDatabase ctxt = new ApplicationDatabase())
            {
                foreach (OpCode opCode in ctxt.OpCodes) //To avoid nasty errors, trim everything to sanitize the input somewhat.
                {
                    opCode.OpCodeSymbolSb3    = opCode.OpCodeSymbolSb3.Trim();
                    opCode.OpCodeSymbolLegacy = opCode.OpCodeSymbolLegacy.Trim();
                }

                foreach (string mappedOpCode in sb2BlocksAndTheirSb3Mappings)
                {
                    string[] splitOpCode = mappedOpCode.Split('~');
                    if (splitOpCode.Length != 2)
                    {
                        Console.WriteLine($"Anomaly in sb2 to sb3 mapping: {mappedOpCode} was not able to be parsed.");
                        continue;
                    }
                    string sb2OpCode = splitOpCode.First().Trim();
                    string sb3OpCode = splitOpCode.Last().Trim();

                    OpCode opCodeInDatabase = ctxt.OpCodes.Where(o => o.OpCodeSymbolLegacy.ToLower() == sb2OpCode.ToLower()).FirstOrDefault();
                    if (opCodeInDatabase == null)
                    {
                        Console.WriteLine($"Anomaly in sb2 to sb3 mapping: {sb2OpCode} was not found in the database");
                        continue;
                    }
                    opCodeInDatabase.OpCodeSymbolSb3 = sb3OpCode;
                }
                ctxt.SaveChanges();
            }
        }
Example #20
0
        public object Post([FromBody] UserCredencialModel Body)
        {
            if (ModelState.IsValid && _db.User.Where(u => u.UserName == Body.UserName).ToList().Count == 0)
            {
                List <UserNote> InitialNote = new List <UserNote> {
                    new UserNote {
                        Heading = "<h1>Heading</h1>", Note = "<p> </p>"
                    }
                };

                _db.User.Add(new User {
                    UserName = Body.UserName,
                    Password = ComputeSha256Hah(Body.UserName, Body.Password, _config["SHA256:Salt"]),
                    UserNote = InitialNote
                });
                _db.SaveChanges();

                return(new { message = "successful" });
            }
            else
            {
                return(new { message = "not successful" });
            }
        }
Example #21
0
        private List <Block> RecurseInto(JArray arrayToRecurse, ref int order, int indent, int ScriptId)
        {
            if (indent > 1000)
            {
                return(new List <Block>());
            }
            if (order > 2000)
            {
                return(new List <Block>());
            }
            List <Block> encounteredBlocks = new List <Block>();

            if (IsArrayOfArrays(arrayToRecurse))
            {
                foreach (var array in arrayToRecurse)
                {
                    encounteredBlocks.AddRange(RecurseInto((JArray)array, ref order, indent + 1, ScriptId));
                }
            }
            else if (IsProcedureDefinition(arrayToRecurse))
            {
                Block encounteredBlock = new Block()
                {
                    BlockRank = order, OpCodeId = GetOpCodeIdByName("procDef"), Indent = indent, IsPartOfProcDef = true
                };
                encounteredBlocks.Add(encounteredBlock);

                using (ApplicationDatabase ctxt = new ApplicationDatabase())
                {
                    Procedure encounteredProcedure = new Procedure()
                    {
                        ProcedureName = arrayToRecurse[1].ToString(),
                        ScriptId      = ScriptId,
                        TotalArgs     = arrayToRecurse[1].ToString().Split('%').Count() - 1 //Easier than counting the actual parameters of the method.
                    };
                    ctxt.Procedures.Add(encounteredProcedure);
                    ctxt.SaveChanges();
                }
            }
            else if (IsRegularBlock(arrayToRecurse))
            {
                order += 1;
                Block encounteredBlock = new Block()
                {
                    BlockRank = order, OpCodeId = GetOpCodeIdByName(arrayToRecurse[0].ToString()), Indent = indent
                };
                if (encounteredBlock.OpCodeId == 2201)
                {
                    int x = 0;
                }
                encounteredBlocks.Add(encounteredBlock);

                List <string> parametersOfBlock = new List <string>();
                if (!IsArrayOfPrimitives(arrayToRecurse))
                {
                    foreach (var element in arrayToRecurse.Skip(1)) //Skip the field where the opCode name was
                    {
                        if (element is JArray)
                        {
                            encounteredBlocks.AddRange(RecurseInto((JArray)element, ref order, indent + 1, ScriptId));
                        }
                        else
                        {
                            parametersOfBlock.Add(element.ToString()); //If it is a primitive type, then it's most certainly a parameter
                        }
                    }
                    encounteredBlock.parameters = parametersOfBlock.ToArray();
                }
                else
                {
                    //If it's an array of primitives, there will be no more nested blocks and therefore they must all be parameters.
                    encounteredBlock.parameters = arrayToRecurse.Skip(1).Select(o => o.ToString()).ToArray();
                }
            }
            return(encounteredBlocks);
        }
Example #22
0
        public override void Scrape()
        {
            Console.WriteLine("Starting project scraping...");
            const int maxThreads = 8;
            ConcurrentStack <Author> toHandle = new ConcurrentStack <Author>();

            using (ApplicationDatabase ctxt = new ApplicationDatabase())
            {
                if (!Directory.Exists(Path.Combine(this.WorkingDirectoryPath, "projects")))
                {
                    Console.WriteLine($"Creating initial projects directory at: {Path.Combine(this.WorkingDirectoryPath, "projects")}");
                    try
                    {
                        Directory.CreateDirectory(Path.Combine(this.WorkingDirectoryPath, "projects"));
                    }
                    catch (Exception ex)
                    {
                        Console.Write(ex.Message);
                        return;
                    }
                }

                Console.WriteLine("Enumerating existing projects... (this may take a very long time!)");
                string[] fileNames = Directory.GetFiles(Path.Combine(this.WorkingDirectoryPath, "projects")).Select(o => Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(o))).ToArray();
                Console.WriteLine($"Enumeration completed. Found {fileNames.Length} already scraped projects.\nGenerating projects cache...");
                downloadedProjectsCache = new ConcurrentDictionary <string, string>(fileNames.ToDictionary(x => x.ToString(), x => ""));
                Console.WriteLine("Generating projects cache completed.\nBuilding stack of authors to scrape...");

                foreach (Author a in ctxt.Authors.Where(o => o.Projects.Count() <= 0).OrderBy(o => o.Id))
                {
                    toHandle.Push(a);
                }
                Console.WriteLine("Building stack done. Starting scrapers...");
            }
            System.Timers.Timer titleBarUpdateTimer = new System.Timers.Timer();
            titleBarUpdateTimer.Elapsed += (sender, args) => Console.Title = $"{toHandle.Count()}";
            titleBarUpdateTimer.Interval = 10000;
            titleBarUpdateTimer.Start();


            int maxToHandle = toHandle.Count();

            for (int i = 0; i < maxThreads; i++)
            {
                ThreadPool.QueueUserWorkItem((Action) =>
                {
                    Console.WriteLine($"Project scraper {Guid.NewGuid().ToString()} of {maxThreads} active.");
                    using (ApplicationDatabase ctxt2 = new ApplicationDatabase())
                    {
                        while (true)
                        {
                            Author handling = null;
                            while (handling == null)
                            {
                                toHandle.TryPop(out handling);
                            }
                            foreach (Project project in GetProjectsByUsername(handling.Username, false))
                            {
                                if (!ctxt2.Projects.AsNoTracking().Any(o => o.Id == project.Id))
                                {
                                    project.AuthorId = handling.Id;
                                    ctxt2.Projects.Add(project);
                                }
                                DownloadProjectToFile(project.Id.ToString());
                            }
                            ctxt2.SaveChanges();
                        }
                    }
                }, null);
            }
        }
Example #23
0
        internal static void ProcessUnregisteredProjects(string pathToUnregisteredProjects, ProjectScraper p)
        {
            // Create a timer with a two second interval.
            System.Timers.Timer aTimer = new System.Timers.Timer(20000);
            // Hook up the Elapsed event for the timer.
            aTimer.Elapsed  += (Object source, ElapsedEventArgs e) => { Say("Not done yet.."); };
            aTimer.AutoReset = true;
            aTimer.Enabled   = true;


            Say($"Enumerating unregistered project files in {Path.Combine(pathToUnregisteredProjects, "UnregisteredProjects.txt")}.");
            string[] unregisteredProjectIds = File.ReadAllLines(Path.Combine(pathToUnregisteredProjects, "UnregisteredProjects.txt")).Distinct <string>().ToArray();
            Say($"Enumerating unregistered project files done");
            aTimer.Stop(); aTimer.Start();

            Say($"Enumerating existing project files in {pathToUnregisteredProjects}. This could take a very long time...");
            string[] fileNames = Directory.GetFiles(pathToUnregisteredProjects).Select(o => Path.GetFileName(o)).ToArray();
            Say($"Enumerating existing project files done.");
            aTimer.Stop(); aTimer.Start();

            Say($"Creating projects cache. This could take a very long time...");
            Dictionary <string, string> projectCache = new Dictionary <string, string>(fileNames.ToDictionary(x => x.Substring(0, x.IndexOf('.')), x => $".{x.Substring(x.IndexOf('.') + 1)}"));

            Say($"Creating projects cache done.");

            fileNames      = null; //Otherwise, millions of strings will be hanging around for no reason.
            aTimer.Enabled = false;
            aTimer         = null;
            GC.Collect();

            using (ApplicationDatabase ctxt = new ApplicationDatabase())
            {
                foreach (string projectId in unregisteredProjectIds)
                {
                    if (!Int32.TryParse(projectId, out int projectIdAsInt))
                    {
                        continue;
                    }
                    if (ctxt.Projects.AsNoTracking().Any(o => o.Id == projectIdAsInt))
                    {
                        continue;
                    }

                    string baseUrl         = "https://api.scratch.mit.edu/projects/{0}";
                    string projectInfoJson = JSONGetter.GetAsJSONString(string.Format(baseUrl, projectId));

                    JObject       projectObject = JObject.Parse(projectInfoJson);
                    ProjectAuthor author        = Newtonsoft.Json.JsonConvert.DeserializeObject <ProjectAuthor>(projectObject["author"].ToString());
                    if (ctxt.Authors.AsNoTracking().Any(o => o.Id == author.id))       //If the author is known...
                    {
                        projectCache.TryGetValue(projectId, out string fileExtension); //Validate if it exists as a file...
                        if (string.IsNullOrEmpty(fileExtension))
                        {
                            p.DownloadProjectToFile(projectId);
                        }

                        Project newProject = p.ParseProject(projectInfoJson, false);
                        newProject.AuthorId = author.id;
                        ctxt.Projects.Add(newProject);
                        ctxt.SaveChanges();

                        //TODO: Optionally immediately parse the actual project and its blocks.
                    }
                    else
                    {
                        Say($"Found project from unknown author: {author.id}");
                    }
                    projectCache.Remove(projectId); //This way, the cache will immediately get rid of now useless entries
                }
            }
        }
Example #24
0
        private List <Script> GetAllScripts(string projectJson, int projectId)
        {
            JObject       projectObject = JObject.Parse(projectJson);
            List <Script> toReturn      = new List <Script>();

            projectObject.TryGetValue("scripts", out JToken stageScriptToken);
            using (ApplicationDatabase ctxt = new ApplicationDatabase())
            {
                if (stageScriptToken != null)
                {
                    foreach (var obj in stageScriptToken.Children())
                    {
                        string scriptCoordinates = $"{obj[0].Value<Int32>()}-{obj[1].Value<Int32>()}"; //Get X and Y coordinates
                        Script currentScript     = new Script()
                        {
                            Coordinates = scriptCoordinates, ProjectId = projectId, SpriteTypeId = 2, SpriteName = "stage"
                        };

                        currentScript = ctxt.Scripts.Add(currentScript);
                        ctxt.SaveChanges();

                        List <Block> blocksInScript = GetAllBlockFromScript((JArray)obj, currentScript.ScriptId);
                        currentScript.TotalBlocks = blocksInScript.Count();

                        foreach (Block b in blocksInScript)
                        {
                            b.ScriptId = currentScript.ScriptId;
                            JSONReader.SaveBlockWithParameters(b, b.parameters);
                        }
                        ctxt.SaveChanges();
                    }
                }

                projectObject.TryGetValue("children", out JToken childrenToken);
                if (childrenToken != null)
                {
                    foreach (JObject sprite in childrenToken.Children())
                    {
                        string spriteName = sprite.Value <string>("objName");
                        sprite.TryGetValue("scripts", out JToken spriteScriptsToken);
                        if (spriteScriptsToken != null)
                        {
                            foreach (var obj in spriteScriptsToken.Children())
                            {
                                string scriptCoordinates = $"{obj[0].Value<Int32>()}-{obj[1].Value<Int32>()}"; //Get X and Y coordinates
                                Script currentScript     = new Script()
                                {
                                    Coordinates = scriptCoordinates, ProjectId = projectId, SpriteTypeId = 1, SpriteName = spriteName
                                };
                                currentScript = ctxt.Scripts.Add(currentScript);
                                ctxt.SaveChanges();

                                List <Block> blocksInScript = GetAllBlockFromScript((JArray)obj, currentScript.ScriptId);
                                currentScript.TotalBlocks = blocksInScript.Count();

                                foreach (Block b in blocksInScript)
                                {
                                    b.ScriptId = currentScript.ScriptId;
                                    JSONReader.SaveBlockWithParameters(b, b.parameters);
                                }
                                if (blocksInScript.Any(o => o.IsPartOfProcDef)) //Just check if we found any block that was procDef , which can only occur if the entire script is a procDef
                                {
                                    currentScript.SpriteTypeId = 3;
                                }
                                ctxt.SaveChanges();
                            }
                        }
                    }
                }
            }
            return(toReturn);
        }
Example #25
0
 //Save database
 private void SaveDb()
 {
     ApplicationDatabase.SaveChanges();
 }