Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var squares = {};
- var players = {};
- var squaresArray;
- 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"];
- var filterKeys = ["owner", "units", "rebels", "farms", "cities", "id", "graffiti", "perm", "domain"];
- var Player = function(name){
- this.name = name;
- this.units = 0;
- this.rebels = 0;
- this.farms = 0;
- this.cities = 0;
- this.squares = 0;
- this.squareList = {};
- this.incomeMultiplier = 1;
- this.income = 100;
- this.unitMultiplier = 1;
- this.unitProduction = 0;
- this.additionalEnergyIncome = 0;
- this.graffiti = {};
- for(var i=0;i<functionalGraffities.length;i++){
- this.graffiti[functionalGraffities[i]] = 0;
- }
- return this;
- }
- Player.prototype.update = function(){
- for(var square in this.squareList){
- var s = this.squareList[square];
- this.squares++;
- this.units += s.units;
- this.rebels += s.rebels;
- this.farms += s.farms;
- this.cities += s.cities;
- if(functionalGraffities.indexOf(s.graffiti) != -1){
- this.graffiti[s.graffiti]++;
- if(s.graffiti == "longsheng"){
- this.incomeMultiplier += 0.05;
- }
- else if(s.graffiti == "castle"){
- this.incomeMultiplier += 0.02;
- }
- }
- }
- this.incomeMultiplier = (~~this.incomeMultiplier*100)/100;
- for(var square in this.squareList){
- var s = this.squareList[square];
- s.update(this.incomeMultiplier);
- this.income += s.income;
- this.unitProduction += s.unitProduction;
- }
- }
- var Square = function(name, id, u, f, c, r, gr, p, d){
- this.owner = name;
- this.id = id;
- this.units = u;
- this.farms = f;
- this.cities = c;
- this.rebels = r;
- this.graffiti = gr;
- this.perm = p;
- this.domain = d;
- this.income = 0;
- this.unitProduction = 0;
- return this;
- }
- Square.prototype.update = function(m){
- this.income = Number(this.farms*50*m*(100-this.rebels)/100);
- this.unitProduction = (Number(this.cities*(100-this.rebels)/100));
- if(this.graffiti == "sword"){
- this.unitProduction *= 5;
- }
- }
- var parseGrid = function(){
- document.querySelector('#masterTable').querySelectorAll("td").forEach(function(s){
- var player, perm;
- var name = s.querySelector(".name").title.toLowerCase().trim()||"thegridadmin";
- if(players[name]||false){
- player = players[name]
- }
- else{
- player = new Player(name)
- players[name] = player;
- }
- if(s.style.borderStyle == "double"){
- perm = true;
- }
- else{
- perm = false;
- }
- var id = Number(s.querySelector(".numberBox").textContent.trim())||0;
- var units = Number(s.querySelector(".units").textContent.trim().split(",").join(""))||0;
- var farms = Number(s.querySelectorAll(".structures span").item(0).textContent.trim())||0;
- var cities = Number(s.querySelectorAll(".structures span").item(1).textContent.trim())||0;
- var rebels = Number(s.querySelectorAll(".structures span").item(2).textContent.trim())||0;
- var graffiti = s.querySelector(".countryName").textContent.toLowerCase().trim()||"";
- var domain = Math.ceil(id/42);
- var square = new Square(name, id, units, farms, cities, rebels, graffiti, perm, domain);
- squares[id] = square;
- player.squareList[id] = square;
- });
- };
- var updatePlayers = function(){
- for(var player in players){
- var p = players[player];
- p.update();
- }
- }
- var run = function(){
- squares = {};
- players = {};
- parseGrid();
- updatePlayers();
- squaresArray = objectToArray(squares);
- }
- var display = function(){
- console.group();
- for(var player in players){
- var p = players[player];
- console.groupCollapsed(player);
- console.group();
- console.log("Units: " + p.units, " Farms: " + p.farms, " Cities: " + p.cities, " Rebels: " + p.rebels, " Squares " + p.squares, " Income: " + p.income, " Unit Production: " + p.unitProduction);
- console.groupCollapsed("Graffiti");
- console.table(p.graffiti);
- console.groupEnd();
- console.groupCollapsed("Squares:");
- console.table(p.squareList);
- console.groupEnd();
- console.groupEnd();
- console.groupEnd();
- }
- console.groupEnd();
- }
- var filter = function(key, comp, value, square){
- if(comp === "=" && square[key] == value){
- return true;
- }
- else if(comp === "!=" && square[key] != value){
- return true;
- }
- else if(comp === "<=" && square[key] <= value){
- return true;
- }
- else if(comp === ">=" && square[key] >= value){
- return true;
- }
- else if(comp === "<" && square[key] < value){
- return true;
- }
- else if(comp === ">" && square[key] > value){
- return true;
- }
- else{
- return false;
- }
- }
- var filterSquares = function(sqs, parameters){
- var matches = sqs;
- var keys = Object.keys(parameters);
- //var filterKeys = ["owner", "units", "rebels", "farms", "cities", "id", "graffiti", "perm"];
- // {owner: "name", units: ">50", "farms: =50" }
- for(var i=0;i<keys.length;i++){
- if(filterKeys.indexOf(keys[i].toLowerCase()) == -1){
- console.warn("Error: Invalid filter key!");
- return null;
- }
- var value = parameters[keys[i]];
- if(keys[i] != "owner" && keys[i] !== "graffiti" && value.match(/[a-df-z]/i) !== null && value.match(/[=<>]{0,2}[0-9e]+/i) !== null ){
- console.warn("Error: Invalid filter value!");
- return null;
- }
- }
- for(var parameter in parameters){
- var m = []
- var comp;
- var value = parameters[parameter];
- if(parameter === "owner"){
- comp = "=";
- }
- else if(parameter === "graffiti"){
- comp = value.match(/[!=<>]{1,2}/)[0];
- value = value.match(/[a-z]+/i)[0].toLowerCase();;
- //console.log(comp, value);
- }
- else{
- comp = value.match(/[!=<>]{1,2}/)[0];
- value = value.match(/[0-9\.e]+/i)[0]
- }
- for(var i=0;i<matches.length;i++){
- //console.log(i);
- var result = filter(parameter, comp, value, matches[i]);
- if(result){
- m.push(matches[i]);
- }
- }
- matches = m;
- }
- return matches;
- }
- var objectToArray = function(obj){
- var arr = [];
- for(var key in obj){
- arr.push(obj[key]);
- }
- return arr;
- }
- run();
- squaresArray = objectToArray(squares);
- //display();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement