Advertisement
Temporelucis

statParser.js

Jun 27th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //THIS INCLUDES /lib/util.js METHODS
  2. //Statistics constructor
  3. var pokemonStatistics = function (statistics, pokemonName) {
  4.     this["statistics"] = statistics;
  5.     this["pokemonName"] = pokemonName.toLowerCase();
  6.     this["abilitiesArray"] = new Array();
  7.     this["itemsArray"] = new Array();
  8.     this["spreadsArray"] = new Array();
  9.     this["movesArray"] = new Array();
  10.     this["teammatesArray"] = new Array();
  11.     this["handleProcessingData_string"] = "this.default()";
  12.     this["handleNotFound_string"] = "this.default()";
  13.     this["handleDone_string"] = "this.default()";
  14. };
  15. pokemonStatistics.prototype = Object.create(function () { });   //Statistics prototype
  16. pokemonStatistics.prototype.constructor = pokemonStatistics;    //Creates constructor
  17. pokemonStatistics.prototype.prepareStatistics = function () {   //Prepares the statistics
  18.     this["statistics"] = this["statistics"]
  19.     .replace(/\s+/g, " ")
  20.     .toLowerCase();
  21. }
  22. pokemonStatistics.prototype.parseStatistics = function () {
  23.     this.handleProcessingData();
  24.     this.prepareStatistics();
  25.     statistics_copy = this.statistics;
  26.     desired_pokemon_start = "+----------------------------------------+ | "+this["pokemonName"]+" | +----------------------------------------+";
  27.     desired_pokemon_end = "+----------------------------------------+ +----------------------------------------+";
  28.     desired_pokemon_start_index = statistics_copy.indexOf(desired_pokemon_start);
  29.     if (desired_pokemon_start_index != -1) {                                                //If the pokemon is on the list
  30.         statistics_copy = statistics_copy.substring(desired_pokemon_start_index, statistics_copy.length);
  31.         desired_pokemon_end_index = statistics_copy.indexOf(desired_pokemon_end);
  32.         if (desired_pokemon_end_index != -1) {                                              //Makes a substring of the statistics with only the pokemon desired
  33.             statistics_copy = statistics_copy.substring(0, desired_pokemon_end_index);
  34.         } else {
  35.             statistics_copy = statistics_copy.substring(0, statistics_copy.length);
  36.         }
  37.         this["abilitiesArray"] = this.getAbilities(statistics_copy);    //Gets the value of each Array
  38.         this["itemsArray"] = this.getItems(statistics_copy);                    //Passes down the substring with the pokemon statistics
  39.         this["spreadsArray"] = this.getSpreads(statistics_copy);
  40.         this["movesArray"] = this.getMoves(statistics_copy);
  41.         this["teammatesArray"] = this.getTeammates(statistics_copy);
  42.         this.handleDone();
  43.     } else {                                                                                                                //The pokemon wasn't found on the list
  44.         this.handleNotFound();
  45.     }
  46. };
  47. //Get array methods
  48. pokemonStatistics.prototype.getAbilities =  function (stats) {
  49.     search_start = stats.indexOf("abilities") + "abilities | ".length;  //Makes a substring based on the pokemon string
  50.     stats = stats.substring(search_start, stats.length);
  51.     search_end = stats.indexOf(" +----------------------------------------+ ");
  52.     stats = stats.substring(0, search_end);
  53.     abilities = new Array();                                                                                        //Creates the arrays we are gonna need
  54.     percentage = new Array();
  55.     element = "";
  56.     percentage_element = "";
  57.     lcount = 0;
  58.     for (var c1 = 0; c1 < stats.length; c1++) {                                                 //Goes through the string char by char
  59.         if (stats[c1] == "|")
  60.             lcount++;
  61.         if (lcount%2 != 0) {
  62.             if (!util.isNumber(stats[c1]) && stats[c1] != "%" && stats[c1] != ".")
  63.                 element += stats[c1];
  64.             else
  65.                 percentage_element += stats[c1];
  66.         } else {
  67.             abilities.push(element);
  68.             percentage.push(percentage_element);
  69.             element = "";
  70.             percentage_element = "";
  71.         }
  72.     }
  73.     util.trim(abilities);
  74.     util.trim(percentage);
  75.     for (var c1 = 0; c1 < abilities.length; c1++) {                                     //Removes useless characters
  76.         abilities[c1] = abilities[c1].substring(2, abilities[c1].length-2);
  77.         percentage[c1] = percentage[c1].substring(0, percentage[c1].length-1);
  78.     }
  79.     return { "abilities" : abilities, "percentage" : percentage };      //Returns the array
  80. }
  81. pokemonStatistics.prototype.getItems =  function (stats) {
  82.     search_start = stats.indexOf("items") + "items | ".length;  //Makes a substring based on the pokemon string
  83.     stats = stats.substring(search_start, stats.length);
  84.     search_end = stats.indexOf(" +----------------------------------------+ ");
  85.     stats = stats.substring(0, search_end);
  86.     items = new Array();                                                                                        //Creates the arrays we are gonna need
  87.     percentage = new Array();
  88.     element = "";
  89.     percentage_element = "";
  90.     lcount = 0;
  91.     for (var c1 = 0; c1 < stats.length; c1++) {                                                 //Goes through the string char by char
  92.         if (stats[c1] == "|")
  93.             lcount++;
  94.         if (lcount%2 != 0) {
  95.             if (!util.isNumber(stats[c1]) && stats[c1] != "%" && stats[c1] != ".")
  96.                 element += stats[c1];
  97.             else
  98.                 percentage_element += stats[c1];
  99.         } else {
  100.             items.push(element);
  101.             percentage.push(percentage_element);
  102.             element = "";
  103.             percentage_element = "";
  104.         }
  105.     }
  106.     util.trim(items);
  107.     util.trim(percentage);
  108.     for (var c1 = 0; c1 < items.length; c1++) {                                     //Removes useless characters
  109.         items[c1] = items[c1].substring(2, items[c1].length-2);
  110.         percentage[c1] = percentage[c1].substring(0, percentage[c1].length-1);
  111.     }
  112.     return { "items" : items, "percentage" : percentage };      //Returns the array
  113. }
  114. pokemonStatistics.prototype.getMoves =  function (stats) {
  115.     search_start = stats.indexOf("moves") + "moves | ".length;  //Makes a substring based on the pokemon string
  116.     stats = stats.substring(search_start, stats.length);
  117.     search_end = stats.indexOf(" +----------------------------------------+ ");
  118.     stats = stats.substring(0, search_end);
  119.     moves = new Array();                                                                                        //Creates the arrays we are gonna need
  120.     percentage = new Array();
  121.     element = "";
  122.     percentage_element = "";
  123.     lcount = 0;
  124.     for (var c1 = 0; c1 < stats.length; c1++) {                                                 //Goes through the string char by char
  125.         if (stats[c1] == "|")
  126.             lcount++;
  127.         if (lcount%2 != 0) {
  128.             if (!util.isNumber(stats[c1]) && stats[c1] != "%" && stats[c1] != ".")
  129.                 element += stats[c1];
  130.             else
  131.                 percentage_element += stats[c1];
  132.         } else {
  133.             moves.push(element);
  134.             percentage.push(percentage_element);
  135.             element = "";
  136.             percentage_element = "";
  137.         }
  138.     }
  139.     util.trim(moves);
  140.     util.trim(percentage);
  141.     for (var c1 = 0; c1 < moves.length; c1++) {                                     //Removes useless characters
  142.         moves[c1] = moves[c1].substring(2, moves[c1].length-2);
  143.         percentage[c1] = percentage[c1].substring(0, percentage[c1].length-1);
  144.     }
  145.     return { "moves" : moves, "percentage" : percentage };      //Returns the array
  146. }
  147. pokemonStatistics.prototype.getTeammates =  function (stats) {
  148.     search_start = stats.indexOf("teammates") + "teammates | ".length;  //Makes a substring based on the pokemon string
  149.     stats = stats.substring(search_start, stats.length);
  150.     search_end = stats.indexOf(" +----------------------------------------+ ");
  151.     stats = stats.substring(0, search_end);
  152.     teammates = new Array();                                                                                        //Creates the arrays we are gonna need
  153.     percentage = new Array();
  154.     element = "";
  155.     percentage_element = "";
  156.     lcount = 0;
  157.     for (var c1 = 0; c1 < stats.length; c1++) {                                                 //Goes through the string char by char
  158.         if (stats[c1] == "|")
  159.             lcount++;
  160.         if (lcount%2 != 0) {
  161.             if (!util.isNumber(stats[c1]) && stats[c1] != "%" && stats[c1] != "." && stats[c1] != "+")
  162.                 element += stats[c1];
  163.             else if (stats[c1] != "+")
  164.                 percentage_element += stats[c1];
  165.         } else {
  166.             teammates.push(element);
  167.             percentage.push(percentage_element);
  168.             element = "";
  169.             percentage_element = "";
  170.         }
  171.     }
  172.     util.trim(teammates);
  173.     util.trim(percentage);
  174.     for (var c1 = 0; c1 < teammates.length; c1++) {                                     //Removes useless characters
  175.         teammates[c1] = teammates[c1].substring(2, teammates[c1].length-2);
  176.         percentage[c1] = percentage[c1].substring(0, percentage[c1].length-1);
  177.     }
  178.     return { "teammates" : teammates, "percentage" : percentage };      //Returns the array
  179. }
  180. pokemonStatistics.prototype.getSpreads =  function (stats) {
  181.     search_start = stats.indexOf("spreads") + "spreads | ".length;  //Makes a substring based on the pokemon string
  182.     stats = stats.substring(search_start, stats.length);
  183.     search_end = stats.indexOf(" +----------------------------------------+ ");
  184.     stats = stats.substring(0, search_end);
  185.     natures = new Array();                                                                                      //Creates the arrays we are gonna need
  186.     spreads_array = new Array();
  187.     spreads = new Array();
  188.     element = "";
  189.     spreads_element = "";
  190.     lcount = 0;
  191.     for (var c1 = 0; c1 < stats.length; c1++) {                                                 //Goes through the string char by char
  192.         if (stats[c1] == "|")
  193.             lcount++;
  194.         if (lcount%2 != 0) {
  195.             if (!util.isNumber(stats[c1]) && stats[c1] != "%" && stats[c1] != "." && stats[c1] != "/" && stats[c1] != ":" && stats[c1] != " ")
  196.                 element += stats[c1];
  197.             else if (stats[c1] != ":" && stats[c1] != "%")
  198.                 spreads_element += stats[c1];
  199.         } else {
  200.             natures.push(element);
  201.             spreads_array.push(spreads_element);
  202.             element = "";
  203.             spreads_element = "";
  204.         }
  205.     }
  206.     util.trim(natures);
  207.     util.trim(spreads_array);
  208.     for (var c1 = 0; c1 < natures.length; c1++) {                                       //Removes useless characters
  209.         natures[c1] = natures[c1].substring(1, natures[c1].length);
  210.         spreads_array[c1] = spreads_array[c1].substring(0, spreads_array[c1].length-1);
  211.         spreads_substring = util.trim(spreads_array[c1].split(" "));    //Splits a string that looks like this 252/0/252/4/0/0 98.78
  212.         spreads.push(util.trim(spreads_substring[0].split("/")));           //Returns the ev's inside an array
  213.         percentage.push(spreads_substring[1]);                                              //Returns the percentage
  214.     }
  215.     spreads.pop();
  216.     percentage.pop();
  217.     natures.pop();
  218.     return { "natures" : natures, "spreads": spreads,  "percentage" : percentage };     //Returns the array
  219. }
  220. //HANDLE Progress
  221. pokemonStatistics.prototype.handleProcessingData = function () {
  222.     eval(this["handleProcessingData_string"]);
  223. };
  224. pokemonStatistics.prototype.setHandleProcessingData = function (functionName) {
  225.     this["handleProcessingData_string"] = functionName;
  226. };
  227. pokemonStatistics.prototype.handleNotFound = function () {
  228.     eval(this["handleNotFound_string"]);
  229. };
  230. pokemonStatistics.prototype.setHandleNotFound = function (functionName) {
  231.     this["handleNotFound_string"] = functionName;
  232. };
  233. pokemonStatistics.prototype.handleDone = function () {
  234.     eval(this["handleDone_string"]);
  235. };
  236. pokemonStatistics.prototype.setHandleDone = function (functionName) {
  237.     this["handleDone_string"] = functionName;
  238. };
  239. pokemonStatistics.prototype.default = function () { };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement