SHARE
TWEET

Untitled

a guest Sep 13th, 2017 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private function checkMix():Boolean
  2. {
  3.     if(_selectedProductData)
  4.     {
  5.         //product selected
  6.         return checkProductData();
  7.     }else if(!_selectedProductData && _processManager.processes.length)
  8.     {
  9.         //no product selected, but processes have been added
  10.         return checkProductFreeData();
  11.         return false;
  12.     }else
  13.     {
  14.         //no product selected, no processes added
  15.         return false;
  16.     }
  17.     _checkMix.visible = false;
  18. }
  19.  
  20. private function checkProductData():Boolean
  21. {
  22.     var selectedProcesses:Array = _selectedProductData['processes'];
  23.     var processes:Array = _processManager.processes;
  24.     var checkProcesses:Boolean = checkProcessData(selectedProcesses, processes, true);
  25.     if(checkProcesses)
  26.     {
  27.         //processes are correct
  28.         for(var i:int=0;i<processes.length;i++)
  29.         {
  30.             var process:Object = processes[i];
  31.             var checkIngredients:Boolean = checkIngredientData(process, _selectedProductData['ingredients'], true);
  32.             if(checkIngredients)
  33.             {
  34.                 //ingredients correct
  35.                 addSuccessNotification("Congratulations, your mix is correct!");
  36.             }else
  37.             {
  38.                 //ingredients incorrect
  39.                 return false;
  40.             }
  41.         }
  42.         return true;
  43.     }else
  44.     {
  45.         return false;
  46.     }
  47. }
  48.  
  49. private function checkProductFreeData():Boolean
  50. {
  51.     var processes:Array = _processManager.processes;
  52.     processCheck: for(var i:int=0;i<_products.length;i++)
  53.     {
  54.         var product:Object = _products[i];
  55.         var selectedProcesses:Array = product['processes'];
  56.         var checkProcesses:Boolean = checkProcessData(selectedProcesses, processes, false);
  57.         if(checkProcesses)
  58.         {
  59.             //processes are correct
  60.             var matches:int = 0;
  61.             ingredientCheck: for(var j:int=0;j<processes.length;j++)
  62.             {
  63.                 var process:Object = processes[j];
  64.                 var checkIngredients:Boolean = checkIngredientData(process, product['ingredients'], false);
  65.                 if(checkIngredients)
  66.                 {
  67.                     matches++;
  68.                 }
  69.             }
  70.             if(matches === processes.length)
  71.             {
  72.                 var classString:String = product['title'].replace(" ", "")..replace(" ", "");
  73.                 var productIcon:Sprite = addIcons([classString], "com.assets.structure.primary.products.ui.")[classString];
  74.                 addSuccessNotification("Excellent! You have successfully made a dairy product.");
  75.                 //dispatch free product made
  76.                 dispatchEvent(new ProductEvent("PRODUCT_CREATED", true, false, product['title'], null, productIcon, product));
  77.                 _selectedProductData = product;
  78.                 return true;
  79.                 break processCheck;
  80.             }
  81.         }
  82.     }
  83.     addErrorNotification("Mix does not match any product.");
  84.     return false;
  85. }
  86.  
  87. private function checkProcessData(processes:Array, currentProcesses:Array, useNotifications:Boolean):Boolean
  88. {
  89.     var matchingProcesses:int = 0;
  90.     if(currentProcesses.length && currentProcesses.length === processes.length)
  91.     {
  92.         //processes exist and number of processes is correct
  93.         productCheck: for(var i:int=0;i<processes.length;i++)
  94.         {
  95.             var currentProcessData:Object = currentProcesses[i];
  96.             var currentProcess:Process = currentProcesses[i]['instance'];
  97.             var process:Object = processes[i];
  98.             if(currentProcessData['name'] !== (process['primarytitle'] + " " + process['subtitle']) && currentProcessData['name'] !== process['title'])
  99.             {
  100.                 //wrong process found
  101.                 if(useNotifications)
  102.                 {
  103.                     addErrorNotification("You have selected a process that is not used to make this dairy product.");
  104.                 }
  105.                 return false;
  106.                 break productCheck;
  107.             }
  108.         }
  109.         //process has correct number of processes + order, and has corect number of ingredients + order
  110.         return true;
  111.     }else
  112.     {
  113.         //processes don't exist or incorrect number of processes added
  114.         if(useNotifications)
  115.         {
  116.             addErrorNotification("You have used too many or not enough processes to make this dairy product. Add more processes or clear the screen and try again!");
  117.         }
  118.         return false;
  119.     }
  120. }
  121.  
  122. private function checkIngredientData(currentProcessData:Object, ingredients:Array, useNotifications:Boolean):Boolean
  123. {
  124.     var currentIngredients:Array = currentProcessData['instance']['ingredients'];
  125.     var dataIngredients:Array = getApplicableIngredients(currentProcessData['name']);
  126.     var processIngredients:Array = getProcessIngredients(dataIngredients, ingredients);
  127.     if(processIngredients.length === currentIngredients.length)
  128.     {
  129.         //correct # of ingredients
  130.         var orderCheck:Boolean = checkIngredientOrder(processIngredients, currentIngredients);
  131.         if(orderCheck)
  132.         {
  133.             //correct ingredients
  134.         }else if(!orderCheck)
  135.         {
  136.             //incorrect ingredients
  137.             if(useNotifications)
  138.             {
  139.                 addErrorNotification("You have added an ingredient that is not used to make this dairy product.");
  140.             }
  141.             return false;
  142.         }
  143.     }else if(processIngredients.length !== currentIngredients.length)
  144.     {
  145.         //not enough ingredients
  146.         if(useNotifications)
  147.         {
  148.             addErrorNotification("The dairy product has too many or not enough ingredients. Add more ingredients or take some away.");
  149.         }
  150.         return false;
  151.     }
  152.     return true;
  153. }
  154.  
  155. private function getApplicableIngredients(processName:String):Array
  156. {
  157.     var temp:Array = [];
  158.     var ingredients:Array = _ingredientList.slice(0);
  159.     for(var i:int=0;i<ingredients.length;i++)
  160.     {
  161.         var ingredient:Object = ingredients[i];
  162.         if(ingredient['process'] === processName)
  163.         {
  164.             temp.push(ingredient);
  165.         }
  166.         ingredient = null;
  167.     }
  168.     ingredients = null;
  169.     return temp;
  170. }
  171.  
  172. private function getProcessIngredients(dataIngredients:Array, ingredients:Array):Array
  173. {
  174.     var temp:Array = [];
  175.     var i1:Array = dataIngredients.slice(0);
  176.     var i2:Array = ingredients.slice(0);
  177.     dataCheck: for(var i:int=0;i<i1.length;i++)
  178.     {
  179.         var dataIngredient:Object = i1[i];
  180.         titleCheck: for(var j:int=0;j<i2.length;j++)
  181.         {
  182.             var ingredient:Object = i2[j];
  183.             if(dataIngredient['title'] === ingredient['title'])
  184.             {
  185.                 temp.push(dataIngredient);
  186.                 ingredient = null;
  187.                 break titleCheck;
  188.             }
  189.         }
  190.         dataIngredient = null;
  191.     }
  192.     i1 = null, i2 = null;
  193.     return temp;
  194. }
  195.  
  196. private function checkIngredientOrder(ingredients:Array, processIngredients:Array):Boolean
  197. {
  198.     var i1:Array = ingredients.slice(0);
  199.     var i2:Array = processIngredients.slice(0);
  200.     var temp:Array = [];
  201.     ingredientCheck: for(var i:int=0;i<i1.length;i++)
  202.     {
  203.         var ingredient:Object = i1[i];
  204.         titleCheck: for(var j:int=0;j<i2.length;j++)
  205.         {
  206.             var processIngredient:Object = i2[j];
  207.             if(processIngredient['title'] === ingredient['title'])
  208.             {
  209.                 temp.push(ingredient);
  210.                 break titleCheck;
  211.             }
  212.         }
  213.         if(temp.length <= i)
  214.         {
  215.             return false;
  216.             break ingredientCheck;
  217.         }
  218.     }
  219.     return true;
  220. }
RAW Paste Data
Top