Last active
December 3, 2019 07:20
-
-
Save RealTrisT/79db9b2b05e0138d9011e16d344e6918 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
R_CRYSTAL = "crystal"; | |
R_DARK_MATTER = "darkmatter"; | |
R_DEUT = "deuterium"; | |
R_METAL = "metal"; | |
R_ENERGY = "energy"; | |
R_NAMES = [R_METAL, R_CRYSTAL, R_DEUT, R_DARK_MATTER, R_ENERGY]; | |
class Resource{ | |
production; | |
amount; | |
max; | |
/*CurrentAmount(origin_time){ | |
var time_disparity = Date.now() - origin_time; | |
var amount_since = Resource_AmountForTime(name, time_disparity/1000); | |
return this.actual + amount_since; | |
} | |
//time is in seconds | |
AmountForTime(time){ | |
if(typeof this.production === 'undefined') | |
return false; | |
return this.production * time; | |
} | |
//returns the time it takes to produce a certain amount of resource | |
TimeForAmount(amount){ | |
// 1 minute -> [production] | |
// amount minutes -> x | |
// | |
// x = amount / production * 1 | |
if(typeof this.production === 'undefined') | |
return false; | |
return amount / this.production; | |
}*/ | |
} | |
//-------------------------------------------------------------------------------- | |
class Planet{ | |
constructor(){ | |
this.resources = {}; | |
for (var i = 0; i < R_NAMES.length; i++) | |
this.resources[R_NAMES[i]] = new Resource(); | |
this.technologies = {}; | |
} | |
fetch_server_time; | |
id; | |
name; | |
coord; | |
resources; | |
technologies; | |
} | |
//-------------------------------------------------------------------------------- | |
class Client{ | |
planets = {}; | |
metas = {}; | |
//virtual_dom.getElementById("resources_metal").getAttribute("data-raw") | |
i_ProcessPlanets(virtual_dom){ | |
var planet_list = virtual_dom.getElementById("planetList"); | |
for (var i = 0; i < planet_list.children.length; i++) { | |
let ID = planet_list.children[i].id.substr(7); | |
let planet = (ID in this.planets)?this.planets[ID]:this.planets[ID] = new Planet(); | |
let planet_dom = planet_list.children[i]; | |
planet.id = ID; | |
planet.name = planet_dom.getElementsByClassName("planet-name")[0].innerText; | |
planet.coord = planet_dom.getElementsByClassName("planet-koords")[0].innerText; | |
} | |
}; | |
i_ProcessMetas(virtual_dom){ | |
var dom_metas = virtual_dom.getElementsByTagName("meta"); | |
for(var i = 0; i < dom_metas.length; i++){ | |
let str = dom_metas[i].name; | |
if(!(!str || (/^\s*$/.test(str)))){ | |
this.metas[str] = dom_metas[i].content; | |
} | |
} | |
}; | |
i_ProcessResourceHeader(planet, timestamp_on_arrival){ | |
planet.fetch_server_time = this.metas["ogame-timestamp"]*1000; | |
} | |
i_ProcessResourceFrontPage(planet, virtual_dom){ | |
for (var i = 0; i < R_NAMES.length; i++) { | |
let r_val = virtual_dom.getElementById("resources_" + R_NAMES[i]).getAttribute("data-raw"); | |
planet.resources[R_NAMES[i]].amount = parseInt(r_val); | |
} | |
this.i_ProcessTechnologies.call(this, planet, virtual_dom); | |
}; | |
i_ProcessTechnologies(planet, virtual_dom){ | |
var each_tech = virtual_dom.getElementsByClassName("technology"); | |
for (var i = 0; i < each_tech.length; i++) { | |
let tech_id = each_tech[i].getAttribute("data-technology"); | |
let tech_value = each_tech[i].getElementsByClassName("level"); | |
planet.technologies[tech_id] = (tech_value.length) | |
? tech_value[0].innerText | |
: each_tech[i].getElementsByClassName("amount")[0].innerText | |
; | |
} | |
} | |
//----------------------------------------------------------------- | |
i_GetPagePromise(page){ | |
return new Promise(function (resolve, reject) { | |
var ajax = new XMLHttpRequest(); | |
ajax.onreadystatechange = function(){ | |
if(this.readyState == 4){ | |
if(this.status == 200){ | |
resolve(this.responseText); | |
}else{ | |
reject(false); | |
} | |
} | |
}; | |
ajax.onerror = function(){ | |
reject(false); | |
} | |
ajax.open("GET", page, true); | |
ajax.send(); | |
}); | |
} | |
i_RequestMultiplePages(pages){ | |
for(let i = 0; i < pages.length; i++) | |
pages[i] = this.i_GetPagePromise.call(this, pages[i]); | |
return Promise.all(pages); | |
} | |
//--------------------------------------------------------------- | |
async RunIt(){ | |
var self = this; | |
let response = await this.i_GetPagePromise( | |
"https://s141-pt.ogame.gameforge.com/game/index.php?page=ingame&component=supplies" | |
); | |
var parser = new DOMParser(); | |
var virtual_dom = parser.parseFromString(response, "text/html"); | |
self.i_ProcessPlanets.call(self, virtual_dom); | |
for (const [id, planet] of Object.entries(self.planets)) { | |
console.log("running planet " + planet.name); | |
let responses = await self.i_RequestMultiplePages.call(self, [ | |
"https://s141-pt.ogame.gameforge.com/game/index.php?page=ingame&component=supplies&cp=" + id, | |
"https://s141-pt.ogame.gameforge.com/game/index.php?page=fetchResources&ajax=1&cp=" + id | |
]); | |
var parser = new DOMParser(); | |
var virtual_dom = parser.parseFromString(responses[0], "text/html"); | |
self.i_ProcessMetas.call(self, virtual_dom); | |
self.i_ProcessResourceHeader.call(self, planet); | |
self.i_ProcessResourceFrontPage.call(self, planet, virtual_dom); | |
var resources_data = JSON.parse(responses[1]); | |
for(const [material, data] of Object.entries(resources_data)){ | |
if(material == "honorScore")continue; | |
planet.resources[material].production = data.production; | |
planet.resources[material].max = data.max; | |
} | |
} | |
console.log("finished"); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment