示例#1
0
        public ScriptErrorLogger SetupViewer(string script)
        {
            List <string> AllAddedNames = new List <string>();

            CodeBlocks.Clear();
            Script newscript = new Script();

            newscript.Analyse(script);
            if (newscript.Logger.hasErrors())
            {
                return(newscript.Logger);
            }
            foreach (ICodeStruct codeStruct in newscript.Code)
            {
                Assignation item    = (Assignation)codeStruct;
                CodeElement element = new CodeElement
                {
                    Occurence = AllAddedNames.Count(n => n == item.Assignee),
                    Name      = item.Assignee
                };
                CodeBlock block = item.Value as CodeBlock;
                element.Childrens = block != null?RunInBlock(block, AllAddedNames) :
                                        new List <CodeElement>();

                AllAddedNames.Add(element.Name);
                CodeBlocks.Add(element);
            }
            OnPropertyChanged("CodeBlocks");
            return(null);
        }
        // GET: People
        public ActionResult Index()
        {
            int idGame     = (int)this.Session["idGame"];
            int idAssigner = (int)this.Session["idAssigner"];

            Assignation assignation = new Assignation {
                AssignerID = idAssigner, GameID = idGame
            };
            List <Assignation> allAssignations = db.Assignations.Where((a => a.GameID == idGame)).ToList();

            if (this.isGameStarted())
            {
                assignation = allAssignations.Where(a => a.AssignerID == idAssigner).First();
                if (assignation.Character == null)
                {
                    return(View("SetCharacter", assignation));
                }
            }

            if (allAssignations.Where(a => a.AssignerID == idAssigner).Count() == 0)
            {
                db.Assignations.Add(assignation);
                db.SaveChanges();

                this.Assign(idGame);
            }

            return(View(db.Assignations.Where((a => a.GameID == idGame && a.AssignedID != idAssigner)).ToList()));
        }
        public ActionResult SetCharacter(string character)
        {
            int idGame     = (int)this.Session["idGame"];
            int idAssigner = (int)this.Session["idAssigner"];

            Assignation assignation = db.Assignations.Where((a => a.GameID == idGame && a.AssignerID == idAssigner)).ToList().First();

            assignation.Character = character;
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
示例#4
0
        private static List <CodeElement> RunInBlock(CodeBlock block, ICollection <string> AllAddedNames)
        {
            List <CodeElement> newlist = new List <CodeElement>();

            foreach (ICodeStruct codeStruct in block.Code.Where(t => t is Assignation))
            {
                Assignation item    = (Assignation)codeStruct;
                CodeElement element = new CodeElement
                {
                    Occurence = AllAddedNames.Count(n => n == item.Assignee),
                    Name      = item.Assignee
                };
                CodeBlock codeBlock = item.Value as CodeBlock;
                element.Childrens = codeBlock != null?RunInBlock(codeBlock, AllAddedNames) :
                                        new List <CodeElement>();

                AllAddedNames.Add(element.Name);
                newlist.Add(element);
            }
            return(newlist);
        }
示例#5
0
        public void GoodScriptTest()
        {
            //Arrange
            Script script = new Script();

            //Act
            script.Analyse(File.ReadAllText("usa.txt"));
            //Test code value
            CodeValue value       = script.FindValue("tag");
            string    valueString = value.Parse();
            //Test Assignation
            Assignation assign       = script.FindAssignation("modifier");
            string      assignString = assign.Parse();
            //Test Extraction
            ICodeStruct extracted       = script.Extract("default");
            string      extractedString = extracted.Parse();
            //Test find all focuses
            List <ICodeStruct> assigns = script.FindAllValuesOfType <CodeBlock>("focus");
            //Test try parse
            string tag = script.TryParse(script, "tag");

            //Assert
            Assert.IsTrue(script.Code.Any());
            Assert.IsFalse(script.Logger.hasErrors());
            Assert.IsTrue(valueString == "USA");
            Assert.IsTrue(!string.IsNullOrEmpty(assignString));
            Assert.IsTrue(extractedString.Contains("yes"));
            //Should not find a tag after extraction
            Assert.IsNull(script.FindValue("default"));
            Assert.IsTrue(assigns.Any());
            Assert.IsTrue(tag == "USA");
            //Test broken parse
            Assert.IsNull(script.TryParse(script, "default", null, false));
            script.TryParse(script, "default");
            Assert.IsTrue(script.Logger.hasErrors());
        }
 internal void Assignation(int TagId, int AssignationId, Assignation AssigrationType)
 {
     sql.SetParameterValue("@AssignationId", AssignationId);
     sql.SetParameterValue("@AssigrationType", AssigrationType);
 }
 internal void Assignation(int TagId, int AssignationId, Assignation AssigrationType)
 {
     sql.SetParameterValue("@AssignationId", AssignationId);
     sql.SetParameterValue("@AssigrationType", AssigrationType);
 }
        private static AssignationModel BlockToModel(ICodeStruct currentBlock, RelayCommand <object> deleteCommand)
        {
            //Should never loop inside something other than an assignation, check type
            Assignation assignation = currentBlock as Assignation;

            if (assignation != null)
            {
                Assignation block = assignation;
                //Check if simply empty, consider an empty block
                if (block.Value == null)
                {
                    //Simple assignation, return corresponding model
                    return(new AssignationModel
                    {
                        BackgroundColor = BLOCKS_COLORS[0],
                        BorderColor = BLOCKS_COLORS[1],
                        Color = BLOCKS_COLORS[2],
                        IsNotEditable = false,
                        CanHaveChild = true,
                        Code = Regex.Replace(block.Assignee, @"\t|\n|\r|\s", "") + " "
                               + block.Operator + " {}",
                        LocalizationKey = getAssignationName(block.Assignee),
                        DeleteNodeCommand = deleteCommand
                    });
                }
                //check if a code value
                CodeValue value = block.Value as CodeValue;
                if (value != null)
                {
                    //Simple assignation, return corresponding model
                    return(new AssignationModel()
                    {
                        BackgroundColor = ASSIGNATIONS_COLORS[0],
                        BorderColor = ASSIGNATIONS_COLORS[1],
                        Color = ASSIGNATIONS_COLORS[2],
                        IsNotEditable = false,
                        CanHaveChild = false,
                        Code = Regex.Replace(block.Assignee, @"\t|\n|\r|\s", "") + " " + block.Operator
                               + " " + Regex.Replace(value.Value, @"\t|\n|\r|\s", ""),
                        LocalizationKey = "Scripter_Assignation_Custom",
                        DeleteNodeCommand = deleteCommand
                    });
                    //Ignore childs even if there is some
                }
                //Otherwise, the child is a codeblock
                CodeBlock codeBlock = block.Value as CodeBlock;
                if (codeBlock != null)
                {
                    AssignationModel newAssignation = new AssignationModel()
                    {
                        BackgroundColor = getColorArray(block.Assignee)[0],
                        BorderColor     = getColorArray(block.Assignee)[1],
                        Color           = getColorArray(block.Assignee)[2],
                        IsNotEditable   = false,
                        CanHaveChild    = true,
                        Code            = Regex.Replace(block.Assignee, @"\t|\n|\r|\s", "") +
                                          " " + block.Operator + " {}",
                        LocalizationKey   = getAssignationName(block.Assignee),
                        DeleteNodeCommand = deleteCommand
                    };
                    foreach (ICodeStruct code in codeBlock.Code)
                    {
                        newAssignation.Childrens.Add(BlockToModel(code, deleteCommand));
                    }
                    return(newAssignation);
                }
                //Error, return nothing.
                return(null);
            }
            //If the current block is a code Value (Corrsponding to block = { Value }
            if (currentBlock is CodeValue)
            {
                //Print the value
                return(new AssignationModel()
                {
                    BackgroundColor = ASSIGNATIONS_COLORS[0],
                    BorderColor = ASSIGNATIONS_COLORS[1],
                    Color = ASSIGNATIONS_COLORS[2],
                    IsNotEditable = false,
                    CanHaveChild = false,
                    Code = Regex.Replace(currentBlock.Parse(), @"\t|\n|\r|\s", ""),
                    LocalizationKey = "Scripter_Assignation_Custom",
                    DeleteNodeCommand = deleteCommand
                });
            }
            return(null);
        }
 /// <summary>
 /// Recursive method that loop inside the given model childrens an create the required block
 /// </summary>
 /// <param name="model">The model to loop into</param>
 /// <param name="level">The current level of the script</param>
 /// <returns>a well formed ICodeStruct made from the model's data.</returns>
 private static ICodeStruct ModelToBlock(AssignationModel model, int level = 0)
 {
     if (model.Childrens.Any())
     {
         Assignation newBlock;
         //If contains equals
         if (model.Code.Contains("="))
         {
             //Empty code block, create as such
             newBlock = new Assignation(level)
             {
                 Assignee = model.Code.Split('=')[0].Trim(),
                 Operator = "=",
                 Value    = new CodeBlock(level + 1)
             };
         }
         //If contains lesser than, but also does not contains a block
         else if (model.Code.Contains("<") && !model.Code.Contains("{") &&
                  !model.Code.Contains("}"))
         {
             //If yes, assignation
             return(new Assignation(level)
             {
                 Assignee = model.Code.Split('<')[0].Trim(),
                 Operator = "<",
                 Value = new CodeValue(model.Code.Split('<')[1].Trim())
             });
         }
         //If contains bigger than, but also does not contains a block
         else if (model.Code.Contains(">") && !model.Code.Contains("{") &&
                  !model.Code.Contains("}"))
         {
             //If yes, assignation
             return(new Assignation(level)
             {
                 Assignee = model.Code.Split('>')[0].Trim(),
                 Operator = ">",
                 Value = new CodeValue(model.Code.Split('>')[1].Trim())
             });
         }
         else
         {
             //Weird block, just return its code
             newBlock = new Assignation(level)
             {
                 Assignee = model.Code,
                 Operator = "=",
                 Value    = new CodeBlock(level + 1)
             };
         }
         foreach (AssignationModel child in model.Childrens)
         {
             ((CodeBlock)newBlock.Value).Code.Add(ModelToBlock(child, level + 1));
         }
         return(newBlock);
     }
     //Should be a code value, since there is no children
     //Check if it contains an equal, but no code block
     if (model.Code.Contains("=") && !model.Code.Contains("{") && !model.Code.Contains("}"))
     {
         //If yes, assignation
         return(new Assignation(level)
         {
             Assignee = model.Code.Split('=')[0].Trim(),
             Operator = "=",
             Value = new CodeValue(model.Code.Split('=')[1].Trim())
         });
     }
     //If contains lesser than, but also does not contains a block
     if (model.Code.Contains("<") && !model.Code.Contains("{") && !model.Code.Contains("}"))
     {
         //If yes, assignation
         return(new Assignation(level)
         {
             Assignee = model.Code.Split('<')[0].Trim(),
             Operator = "<",
             Value = new CodeValue(model.Code.Split('<')[1].Trim())
         });
     }
     //If contains bigger than, but also does not contains a block
     if (model.Code.Contains(">") && !model.Code.Contains("{") && !model.Code.Contains("}"))
     {
         //If yes, assignation
         return(new Assignation(level)
         {
             Assignee = model.Code.Split('>')[0].Trim(),
             Operator = ">",
             Value = new CodeValue(model.Code.Split('>')[1].Trim())
         });
     }
     //If contains equals, but also contains a block
     if (model.Code.Contains("="))
     {
         //Empty code block, create as such
         return(new Assignation(level)
         {
             Assignee = model.Code.Split('=')[0].Trim(),
             Operator = "="
         });
     }
     //Code value in block
     return(new CodeValue(model.Code));
 }
示例#10
0
        public static FocusGridModel CreateTreeFromScript(string fileName, Script script)
        {
            if (script.Logger.hasErrors())
            {
                return(null);
            }
            FocusGridModel container = new FocusGridModel(script.TryParse(script, "id"));
            //Get content of Modifier block
            Assignation modifier = script.FindAssignation("modifier");

            container.TAG = script.TryParse(modifier, "tag", null, false);
            if (container.TAG != null)
            {
                container.AdditionnalMods = modifier.GetContentAsScript(new[] { "add", "tag" })
                                            .Parse(script.Comments, 0);
            }
            //Run through all foci
            foreach (ICodeStruct codeStruct in script.FindAllValuesOfType <CodeBlock>("focus"))
            {
                CodeBlock block = (CodeBlock)codeStruct;
                try
                {
                    //Create the focus
                    FocusModel newFocus = new FocusModel
                    {
                        UniqueName = script.TryParse(block, "id"),
                        Text       = script.TryParse(block, "text", null, false),
                        Image      = script.TryParse(block, "icon").Replace("GFX_", ""),
                        X          = int.Parse(script.TryParse(block, "x")),
                        Y          = int.Parse(script.TryParse(block, "y")),
                        Cost       = GetDouble(script.TryParse(block, "cost"), 10)
                    };
                    //Get all core scripting elements
                    Script InternalFocusScript = block.
                                                 GetContentAsScript(ALL_PARSED_ELEMENTS.ToArray(), script.Comments);
                    newFocus.InternalScript = InternalFocusScript;
                    if (script.Logger.hasErrors())
                    {
                        new ViewModelLocator().ErrorDawg.AddError(
                            string.Join("\n", script.Logger.getErrors()));
                        continue;
                    }
                    container.FociList.Add(newFocus);
                }
                catch (Exception)
                {
                    //TODO: Add language support
                    new ViewModelLocator().ErrorDawg.AddError(script.Logger.ErrorsToString());
                    new ViewModelLocator().ErrorDawg.AddError("Invalid syntax for focus "
                                                              + script.TryParse(block, "id") + ", please double-check the syntax.");
                }
            }
            //Run through all foci again for mutually exclusives and prerequisites
            foreach (ICodeStruct codeStruct in script.FindAllValuesOfType <CodeBlock>("focus"))
            {
                CodeBlock block = (CodeBlock)codeStruct;
                string    id    = block.FindValue("id") != null?block.FindValue("id").Parse() : "";

                FocusModel newFocus = container.FociList.FirstOrDefault(f => f.UniqueName == id);
                if (newFocus == null)
                {
                    //Check if we removed this focus because of a syntax error
                    continue;
                }
                //Try to find its relative to
                string relativeToId = script.TryParse(block, "relative_position_id", null, false);
                if (!string.IsNullOrEmpty(relativeToId))
                {
                    newFocus.CoordinatesRelativeTo = container.FociList.FirstOrDefault(f =>
                                                                                       f.UniqueName == relativeToId);
                }
                //Recreate its mutually exclusives
                foreach (ICodeStruct exclusives in block.FindAllValuesOfType <ICodeStruct>
                             ("mutually_exclusive"))
                {
                    foreach (ICodeStruct focuses in exclusives
                             .FindAllValuesOfType <ICodeStruct>("focus"))
                    {
                        //Check if focus exists in list
                        FocusModel found = container.FociList.FirstOrDefault(f =>
                                                                             f.UniqueName == focuses.Parse());
                        //If we have found something
                        if (found == null)
                        {
                            continue;
                        }
                        MutuallyExclusiveSetModel set =
                            new MutuallyExclusiveSetModel(newFocus, found);
                        //Check if the set already exists in this focus
                        if (newFocus.MutualyExclusive.Contains(set) ||
                            found.MutualyExclusive.Contains(set))
                        {
                            continue;
                        }
                        newFocus.MutualyExclusive.Add(set);
                        found.MutualyExclusive.Add(set);
                    }
                }
                //Recreate its prerequisites
                foreach (ICodeStruct prerequistes in block
                         .FindAllValuesOfType <ICodeStruct>("prerequisite"))
                {
                    if (!prerequistes.FindAllValuesOfType <ICodeStruct>("focus").Any())
                    {
                        break;
                    }
                    PrerequisitesSetModel set = new PrerequisitesSetModel(newFocus);
                    foreach (ICodeStruct focuses in prerequistes.FindAllValuesOfType <ICodeStruct>
                                 ("focus"))
                    {
                        //Add the focus as a prerequisites in the current existing focuses
                        FocusModel search = container.FociList.FirstOrDefault((f) =>
                                                                              f.UniqueName == focuses.Parse());
                        if (search != null)
                        {
                            set.FociList.Add(search);
                        }
                    }
                    //If any prerequisite was added (Poland has empty prerequisite blocks...)
                    if (set.FociList.Any())
                    {
                        newFocus.Prerequisite.Add(set);
                    }
                }
            }
            return(container);
        }
 private static AssignationModel BlockToModel(ICodeStruct currentBlock, RelayCommand <object> deleteCommand)
 {
     //Should never loop inside something other than an assignation, check type
     if (currentBlock is Assignation)
     {
         Assignation block = currentBlock as Assignation;
         //Check if simply empty, consider an empty block
         if (block.Value == null)
         {
             ResourceDictionary resourceLocalization = new ResourceDictionary();
             resourceLocalization.Source = new Uri(Configurator.getLanguageFile(), UriKind.Relative);
             //Simple assignation, return corresponding model
             return(new AssignationModel()
             {
                 BackgroundColor = BLOCKS_COLORS[0],
                 BorderColor = BLOCKS_COLORS[1],
                 Color = BLOCKS_COLORS[2],
                 IsNotEditable = false,
                 CanHaveChild = true,
                 Code = Regex.Replace(block.Assignee, @"\t|\n|\r|\s", "") + " " + block.Operator + " {}",
                 Text = getAssignationName(block.Assignee),
                 DeleteNodeCommand = deleteCommand
             });
         }
         //check if a code value
         if (block.Value is CodeValue)
         {
             ResourceDictionary resourceLocalization = new ResourceDictionary();
             resourceLocalization.Source = new Uri(Configurator.getLanguageFile(), UriKind.Relative);
             //Simple assignation, return corresponding model
             return(new AssignationModel()
             {
                 BackgroundColor = ASSIGNATIONS_COLORS[0],
                 BorderColor = ASSIGNATIONS_COLORS[1],
                 Color = ASSIGNATIONS_COLORS[2],
                 IsNotEditable = false,
                 CanHaveChild = false,
                 Code = Regex.Replace(block.Assignee, @"\t|\n|\r|\s", "") + " " + block.Operator
                        + " " + Regex.Replace(((CodeValue)block.Value).Value, @"\t|\n|\r|\s", ""),
                 Text = resourceLocalization["Scripter_Assignation_Custom"] as string,
                 DeleteNodeCommand = deleteCommand
             });
             //Ignore childs even if there is some
         }
         //Otherwise, the child is a codeblock
         else if (block.Value is CodeBlock)
         {
             AssignationModel newAssignation = new AssignationModel()
             {
                 BackgroundColor = getColorArray(block.Assignee)[0],
                 BorderColor     = getColorArray(block.Assignee)[1],
                 Color           = getColorArray(block.Assignee)[2],
                 IsNotEditable   = false,
                 CanHaveChild    = true,
                 Code            = Regex.Replace(block.Assignee, @"\t|\n|\r|\s", "") +
                                   " " + block.Operator + " {}",
                 Text = getAssignationName(block.Assignee),
                 DeleteNodeCommand = deleteCommand
             };
             foreach (ICodeStruct code in ((CodeBlock)block.Value).Code)
             {
                 newAssignation.Childrens.Add(BlockToModel(code, deleteCommand));
             }
             return(newAssignation);
         }
         else
         {
             //An error in the code, log it
             RecursiveCodeException e = new RecursiveCodeException();
             throw e.AddToRecursiveChain("Invalid assignation", block.Assignee, block.Line.ToString());
         }
     }
     return(null);
 }