Last active
August 29, 2015 14:00
-
-
Save vpArth/11037403 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
function Miner(incr, dripK, delay) { | |
var that = this; | |
this.incr = incr || localStats.bps*1e3; | |
this.dripK = dripK || 0.5; | |
this.delay = delay || 100; | |
document.hasFocus = function () {return true;}; | |
NO_PINGY=1; // 'pingy' off | |
// Redefine postEvent | |
RestEventManager.prototype.postEventData = function(e,t,next) | |
{ | |
// you can see clean event here | |
var r=XORCipher.encode(DataSaver.key,JSON.stringify(e)); | |
return $.ajax({ | |
type:"POST", | |
async:!0, | |
url:GAME_URL+(loggedIn?"events":"eventsanon"), | |
data:r, | |
contentType:"text/plain", | |
success: function() { | |
var self = this; | |
that.lastCorrect = localStats.byteCount; // save byteCount with success response | |
t.apply(self, arguments); // callback trigger | |
setTimeout(function(){ | |
if(localStats.byteCount > localStats.memoryCapacity * that.dripK) | |
$('#btn-addGlobalMem').trigger('click'); | |
}, 0); | |
if(typeof next == 'function')next(); | |
}, | |
error: function(e) { | |
localStats.byteCount = that.lastCorrect; // restore lastCorrect byteCount | |
console.error(e.responseText); // show error text | |
if(typeof next == 'function') | |
next(); | |
}}) | |
} | |
} | |
Miner.prototype.postEvent = function(mem, time, next) { | |
var d = { | |
userid: networkUser.userId, | |
events: [{ | |
generatedMem: mem, | |
power: null, | |
timeElapsed: time, | |
type: 1 | |
}] | |
} | |
RestEventManager.prototype.postEventData(d, function(){}, next); | |
}; | |
Miner.prototype.start = function() { | |
var that = this; | |
this.stopped = false; | |
this.lastCorrect = localStats.byteCount; // save before the action | |
function post(){ | |
localStats.byteCount+=that.incr; // mine some bytes | |
that.postEvent(localStats.byteCount, 120000, function(){ // tell to server that 2minutes passed | |
if(!that.stopped) | |
that.sI = setTimeout(post, that.delay); // next iteration on response | |
}) | |
} | |
if(this.sI) clearTimeout(this.sI); | |
if(!this.stopped) | |
this.sI = setTimeout(post, this.delay); // first call | |
}; | |
Miner.prototype.stop = function() { | |
this.stopped = true; | |
}; | |
var miner = new Miner(1e9, 0.01, 100); | |
miner.start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment