Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Persist enabled state #39

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 41 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,11 @@

// Override setTimeout to allow getting the time left
let placeTimeout;
const _setTimeout = setTimeout;
const _clearTimeout = clearTimeout;
const _setTimeout = setTimeout;
const _clearTimeout = clearTimeout;
const zs_allTimeouts = {};
let zs_maxTimeout = 240000;

setTimeout = (callback, delay) => {
let id = _setTimeout(callback, delay);
zs_allTimeouts[id] = Date.now() + delay;
Expand All @@ -217,7 +217,7 @@
_clearTimeout(id);
zs_allTimeouts[id] = undefined;
}

const getTimeout = (id) => {
if (zs_allTimeouts[id]) {
return Math.max(
Expand All @@ -237,10 +237,10 @@
}

// Update the percentage
const percentage = 100 - Math.min(Math.max(Math.round((theTimeout/zs_maxTimeout) * 100), 0), 100);
const percentage = 100 - Math.min(Math.max(Math.round((theTimeout / zs_maxTimeout) * 100), 0), 100);
zs_startButton.style.setProperty("--zs_timeout", `${percentage}%`);
zs_startButtonTitle.innerText = `Zinnsoldat v${zs_version}`;
const nextTryInSeconds = Math.floor(theTimeout/1000);
const nextTryInSeconds = Math.floor(theTimeout / 1000);
if (theTimeout > 0) {
zs_startButtonSubTitle.innerText = `Nächster Pixel: ${nextTryInSeconds}s`;
} else {
Expand All @@ -252,9 +252,15 @@
// Basics
// ----------------------------------------

let zs_running = true;
const ZS_RUNNING_STORAGE_KEY = 'zs_running';
let zs_running = await GM.getValue(ZS_RUNNING_STORAGE_KEY, true);
let zs_initialized;

const zs_setRunning = async (running) => {
zs_running = running;
await GM.setValue(ZS_RUNNING_STORAGE_KEY, running);
}

const zs_version = "1.6";
let zs_accessToken;
let c2;
Expand Down Expand Up @@ -285,11 +291,11 @@
static getCanvasX = (x, y) => {
return Math.abs((x + 1500) % 1000);
}

static getCanvasY = (x, y) => {
return Canvas.getCanvasId(x, y) < 3 ? y + 1000 : y;
}

static placePixel = async (x, y, color) => {
console.log('Trying to place pixel at %s, %s in %s', x, y, color);
const response = await fetch('https://gql-realtime-2.reddit.com/query', {
Expand Down Expand Up @@ -363,11 +369,11 @@
Toaster.error('Fehler beim Platzieren des Pixels');
return { status: 'Failure', timestamp: null, reason: '' };
}

// Pixels placed counter
let pixelsPlacedThisSession = parseInt(localStorage.getItem('pixelsPlacedThisSession') ?? '0') + 1;
localStorage.setItem('pixelsPlacedThisSession', pixelsPlacedThisSession);

console.log('Did place pixel at %s, %s in %s', x, y, color);
Toaster.place(`Pixel (${x}, ${y}) platziert! (#${pixelsPlacedThisSession})`, x, y);

Expand Down Expand Up @@ -436,7 +442,7 @@
const url = usingOldReddit ? 'https://new.reddit.com/r/place/' : 'https://www.reddit.com/r/place/';
const response = await fetch(url);
const responseText = await response.text();

return responseText.match(/"accessToken":"(\\"|[^"]*)"/)[1];
}
}
Expand All @@ -450,7 +456,7 @@
const data = await ntpResponse.json()
const ntpDate = data['unixtime'] * 1000

if(ntpDate - Date.now() <= -60000){
if (ntpDate - Date.now() <= -60000) {
let error_message = `Deine Computerzeit weicht um >= 1 Minute von der Serverzeit ab!
Um die Server nicht mit Anfragen zu fluten stoppt der Zinnsoldat nun.
Bitte stelle sicher, dass die Zeit deines Computers richtig synchronisiert ist!
Expand Down Expand Up @@ -520,10 +526,10 @@
// Execute job
Canvas.placePixel(job.x, job.y, job.color - 1).then((placeResult) => {
const { status, reason, timestamp } = placeResult;
const nextTry = (timestamp ? timestamp - Date.now() : 5*60*1000) + 3000 + Math.floor(Math.random()*18000);
const nextTry = (timestamp ? timestamp - Date.now() : 5 * 60 * 1000) + 3000 + Math.floor(Math.random() * 18000);
// Replay acknoledgement
const token = CarpetBomber.getTokens()[0];
c2.send(JSON.stringify({ type: "JobStatusReport", tokens: { [token]: { type: status, reason, cooldown: nextTry } }}));
c2.send(JSON.stringify({ type: "JobStatusReport", tokens: { [token]: { type: status, reason, cooldown: nextTry } } }));
// Schedule next job
zs_maxTimeout = nextTry;
clearTimeout(placeTimeout);
Expand Down Expand Up @@ -554,9 +560,9 @@
c2.onopen = () => {
Toaster.info('Verbinde mit "Carpetbomber"...');
c2.send(JSON.stringify({ type: "Handshake", version: zs_version }));
setInterval(() => c2.send(JSON.stringify({ type: "Wakeup"})), 40*1000);
setInterval(() => c2.send(JSON.stringify({ type: "Wakeup" })), 40 * 1000);
}

c2.onerror = (error) => {
Toaster.error('Verbindung zum "Carpetbomber" fehlgeschlagen!');
console.error(error);
Expand Down Expand Up @@ -604,24 +610,32 @@
zs_startButton.appendChild(zs_startButtonSubTitle);
document.body.appendChild(zs_startButton);

const zs_startBot = () => {
if (zs_initialized) {
zs_running = true;
const zs_setStartButtonState = (running) => {
if (running) {
zs_startButton.classList.remove('zs-startbutton');
zs_startButton.classList.add('zs-stopbutton');
zs_startButtonSubTitle.innerText = 'Starten...'
} else {
zs_startButton.classList.remove('zs-stopbutton');
zs_startButton.classList.add('zs-startbutton');
zs_startButtonSubTitle.innerText = 'Deaktiviert'
}
}

const zs_startBot = () => {
if (zs_initialized) {
zs_setRunning(true);
zs_setStartButtonState(true);
CarpetBomber.startRequestLoop();
} else {
Toaster.error('Version nicht mehr unterstützt!');
}
}

const zs_stopBot = () => {
zs_running = false;
zs_setRunning(false);
clearTimeout(placeTimeout);
zs_startButton.classList.remove('zs-stopbutton');
zs_startButton.classList.add('zs-startbutton');
zs_startButtonSubTitle.innerText = 'Deaktiviert'
zs_setStartButtonState(false);
}

zs_startButton.onclick = () => {
Expand All @@ -635,11 +649,12 @@
// ----------------------------------------
// Run
// ----------------------------------------

Toaster.info('Erbitte Reddit um Zugriff...');
zs_accessToken = await RedditApi.getAccessToken();
Toaster.success('Zugriff gewährt!');

let isTimeSynchronized = await TimeChecker.checkTime();
if(isTimeSynchronized) { CarpetBomber.initCarpetbomberConnection(); }
if (isTimeSynchronized) { CarpetBomber.initCarpetbomberConnection(); }
zs_setStartButtonState(zs_running);
})();
Loading