Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var squares = {};
  2. var players = {};
  3. var squaresArray;
  4.  
  5. var functionalGraffities = ["wall", "spire", "domlord", "seer", "castle", "gnome", "longsheng", "myst", "nospawn", "pil-a", "pil-n", "pil-s", "monk", "rebel", "samurai", "sea", "seer", "spawn", "sword", "thug", "wildcard", "windmill"];
  6. var filterKeys = ["owner", "units", "rebels", "farms", "cities", "id", "graffiti", "perm", "domain"];
  7.  
  8. var Player = function(name){
  9.     this.name = name;
  10.     this.units = 0;
  11.     this.rebels = 0;
  12.     this.farms = 0;
  13.     this.cities = 0;
  14.     this.squares = 0;
  15.     this.squareList = {};
  16.    
  17.     this.incomeMultiplier = 1;
  18.     this.income = 100;
  19.     this.unitMultiplier = 1;
  20.     this.unitProduction = 0;
  21.     this.additionalEnergyIncome = 0;
  22.    
  23.     this.graffiti = {};
  24.    
  25.     for(var i=0;i<functionalGraffities.length;i++){
  26.         this.graffiti[functionalGraffities[i]] = 0;
  27.     }
  28.    
  29.     return this;
  30. }
  31. Player.prototype.update = function(){
  32.     for(var square in this.squareList){
  33.         var s = this.squareList[square];
  34.         this.squares++;
  35.         this.units += s.units;
  36.         this.rebels += s.rebels;
  37.         this.farms += s.farms;
  38.         this.cities += s.cities;
  39.        
  40.         if(functionalGraffities.indexOf(s.graffiti) != -1){
  41.             this.graffiti[s.graffiti]++;
  42.            
  43.             if(s.graffiti == "longsheng"){
  44.                 this.incomeMultiplier += 0.05;
  45.             }
  46.             else if(s.graffiti == "castle"){
  47.                 this.incomeMultiplier += 0.02;
  48.             }
  49.         }
  50.     }
  51.     this.incomeMultiplier = (~~this.incomeMultiplier*100)/100;
  52.     for(var square in this.squareList){
  53.         var s = this.squareList[square];
  54.         s.update(this.incomeMultiplier);
  55.         this.income += s.income;
  56.         this.unitProduction += s.unitProduction;
  57.     }
  58. }
  59.  
  60. var Square = function(name, id, u, f, c, r, gr, p, d){
  61.     this.owner = name;
  62.     this.id = id;
  63.     this.units = u;
  64.     this.farms = f;
  65.     this.cities = c;
  66.     this.rebels = r;
  67.     this.graffiti = gr;
  68.     this.perm = p;
  69.     this.domain = d;
  70.    
  71.     this.income = 0;
  72.     this.unitProduction = 0;
  73.  
  74.     return this;
  75. }
  76. Square.prototype.update = function(m){
  77.     this.income = Number(this.farms*50*m*(100-this.rebels)/100);
  78.    
  79.     this.unitProduction = (Number(this.cities*(100-this.rebels)/100));
  80.     if(this.graffiti == "sword"){
  81.         this.unitProduction *= 5;
  82.     }
  83. }
  84.  
  85. var parseGrid = function(){
  86.     document.querySelector('#masterTable').querySelectorAll("td").forEach(function(s){
  87.         var player, perm;
  88.         var name = s.querySelector(".name").title.toLowerCase().trim()||"thegridadmin";
  89.        
  90.         if(players[name]||false){
  91.             player = players[name]
  92.         }
  93.         else{
  94.             player = new Player(name)
  95.             players[name] = player;
  96.         }
  97.         if(s.style.borderStyle == "double"){
  98.             perm = true;
  99.         }
  100.         else{
  101.             perm = false;
  102.         }
  103.         var id = Number(s.querySelector(".numberBox").textContent.trim())||0;
  104.         var units = Number(s.querySelector(".units").textContent.trim().split(",").join(""))||0;
  105.         var farms = Number(s.querySelectorAll(".structures span").item(0).textContent.trim())||0;
  106.         var cities = Number(s.querySelectorAll(".structures span").item(1).textContent.trim())||0;
  107.         var rebels = Number(s.querySelectorAll(".structures span").item(2).textContent.trim())||0;
  108.         var graffiti = s.querySelector(".countryName").textContent.toLowerCase().trim()||"";
  109.         var domain = Math.ceil(id/42);
  110.        
  111.         var square = new Square(name, id, units, farms, cities, rebels, graffiti, perm, domain);
  112.         squares[id] = square;
  113.         player.squareList[id] = square;
  114.     });
  115. };
  116.  
  117. var updatePlayers = function(){
  118.     for(var player in players){
  119.         var p = players[player];
  120.         p.update();
  121.     }
  122. }
  123.  
  124. var run = function(){
  125.     squares = {};
  126.     players = {};
  127.    
  128.     parseGrid();
  129.     updatePlayers();
  130.    
  131.     squaresArray = objectToArray(squares);
  132.  
  133. }
  134.  
  135. var display = function(){
  136.     console.group();
  137.     for(var player in players){
  138.         var p = players[player];
  139.         console.groupCollapsed(player);
  140.             console.group();
  141.                 console.log("Units: " + p.units, "      Farms: " + p.farms, "       Cities: " + p.cities, "     Rebels: " + p.rebels, "     Squares " + p.squares, "    Income: " + p.income, "     Unit Production: " + p.unitProduction);
  142.                
  143.                 console.groupCollapsed("Graffiti");
  144.                     console.table(p.graffiti);
  145.                 console.groupEnd();
  146.                
  147.                 console.groupCollapsed("Squares:");
  148.                     console.table(p.squareList);
  149.                 console.groupEnd();
  150.             console.groupEnd();
  151.         console.groupEnd();
  152.     }
  153.     console.groupEnd();
  154. }
  155. var filter = function(key, comp, value, square){
  156.     if(comp === "=" && square[key] == value){
  157.         return true;
  158.     }
  159.     else if(comp === "!=" && square[key] != value){
  160.         return true;
  161.     }
  162.     else if(comp === "<=" && square[key] <= value){
  163.         return true;
  164.     }
  165.     else if(comp === ">=" && square[key] >= value){
  166.         return true;
  167.     }
  168.     else if(comp === "<" && square[key] < value){
  169.         return true;
  170.     }
  171.     else if(comp === ">" && square[key] > value){
  172.         return true;
  173.     }
  174.     else{
  175.         return false;
  176.     }
  177. }
  178. var filterSquares = function(sqs, parameters){
  179.     var matches = sqs;
  180.     var keys = Object.keys(parameters);
  181.     //var filterKeys = ["owner", "units", "rebels", "farms", "cities", "id", "graffiti", "perm"];
  182.     // {owner: "name", units: ">50", "farms: =50" }
  183.     for(var i=0;i<keys.length;i++){
  184.         if(filterKeys.indexOf(keys[i].toLowerCase()) == -1){
  185.             console.warn("Error: Invalid filter key!");
  186.             return null;
  187.         }
  188.         var value = parameters[keys[i]];
  189.         if(keys[i] != "owner" && keys[i] !== "graffiti" && value.match(/[a-df-z]/i) !== null && value.match(/[=<>]{0,2}[0-9e]+/i) !== null ){
  190.             console.warn("Error: Invalid filter value!");
  191.             return null;
  192.         }
  193.     }
  194.    
  195.     for(var parameter in parameters){
  196.         var m = []
  197.         var comp;
  198.         var value = parameters[parameter];
  199.         if(parameter === "owner"){
  200.             comp = "=";
  201.         }
  202.         else if(parameter === "graffiti"){
  203.             comp = value.match(/[!=<>]{1,2}/)[0];
  204.             value = value.match(/[a-z]+/i)[0].toLowerCase();;
  205.             //console.log(comp, value);
  206.         }
  207.         else{
  208.             comp = value.match(/[!=<>]{1,2}/)[0];
  209.             value = value.match(/[0-9\.e]+/i)[0]
  210.         }
  211.         for(var i=0;i<matches.length;i++){
  212.             //console.log(i);
  213.             var result = filter(parameter, comp, value, matches[i]);
  214.             if(result){
  215.                 m.push(matches[i]);
  216.             }
  217.         }
  218.        
  219.         matches = m;
  220.     }
  221.     return matches;
  222. }
  223.  
  224. var objectToArray = function(obj){
  225.     var arr = [];
  226.     for(var key in obj){
  227.         arr.push(obj[key]);
  228.     }
  229.     return arr;
  230. }
  231. run();
  232.  
  233. squaresArray = objectToArray(squares);
  234.  
  235.  
  236.  
  237. //display();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement