Skip to content

Commit 02b1796

Browse files
committed
[bpo] Rename fields
1 parent cb14b69 commit 02b1796

File tree

1 file changed

+68
-72
lines changed

1 file changed

+68
-72
lines changed

3rd-party/PTP - Bonus Point Optimization.user.js

Lines changed: 68 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ==UserScript==
22
// @name Bonus Point Optimization
3-
// @version 1.8
3+
// @version 2
44
// @description Chameleon's take on the BP optimization script
55
// @author Chameleon
66
// @include http*://*passthepopcorn.me/bonus.php*
@@ -12,11 +12,11 @@
1212

1313
'use strict';
1414

15-
const calculateBPYearGB = ({ bpyear, size }) => bpyear / (size / (1024 ** 3));
16-
const calculateBPYearGBCoj = (torrent, cojyears) =>
17-
commonFormula(torrent, 365.2422 * cojyears, 1 / cojyears);
18-
const calculateMightychefYears = (torrent, mightychefdays) =>
19-
commonFormula(torrent, mightychefdays, 1);
15+
const calculateBPYearGB = ({ bpYear, size }) => bpYear / (size / (1024 ** 3));
16+
const calculateBPYearGBCoj = (torrent, cojYears) =>
17+
commonFormula(torrent, 365.2422 * cojYears, 1 / cojYears);
18+
const calculateBPDaysGBMightychef = (torrent, mightychefDays) =>
19+
commonFormula(torrent, mightychefDays, 1);
2020

2121
const commonFormula = ({ gp, seedTimeDays: t, seeders }, d, algQ) => {
2222
const s = 2.4 * (seeders ** -0.6);
@@ -78,7 +78,7 @@ const loadDataListener = elContent => e => {
7878
};
7979

8080
const loadData = async (elContent, elMessage) => {
81-
const torrentData = JSON.parse(window.localStorage.bpopt);
81+
const torrentData = JSON.parse(window.localStorage.bpopt2);
8282
torrentData.torrents = [];
8383

8484
let bpRatePagesTotal;
@@ -116,7 +116,7 @@ const loadData = async (elContent, elMessage) => {
116116
if (!snatchlistPagesTotal) throw new Error('Unexpected number of pages on snatchlist.php');
117117
} while (snatchlistPageNum <= snatchlistPagesTotal);
118118

119-
window.localStorage.bpopt = JSON.stringify(torrentData);
119+
window.localStorage.bpopt2 = JSON.stringify(torrentData);
120120
showOptimization(elContent, torrentData);
121121
};
122122

@@ -148,16 +148,17 @@ const parseBPRatePage = (elPage, torrentData) => {
148148
size: parseSize(tds[2].innerHTML.replaceAll(',', '')),
149149
seeders: parseInt(tds[3].innerHTML.replaceAll(',', ''), 10),
150150
ratio: parseFloat(tds[6].innerHTML.replaceAll(',', '')),
151-
bpyear: parseFloat(tds[9].innerHTML.replaceAll(',', '')),
152-
bphour: parseFloat(tds[5].innerHTML.replaceAll(',', '')),
151+
bpYear: parseFloat(tds[9].innerHTML.replaceAll(',', '')),
152+
bpHour: parseFloat(tds[5].innerHTML.replaceAll(',', '')),
153153
seedTimeSeconds: tds[4].getAttribute('data-seed-seconds'),
154154
hidden: false,
155155
};
156156

157157
torrent.seedTimeDays = torrent.seedTimeSeconds / (60 * 60 * 24);
158-
torrent.bpyeargb = calculateBPYearGB(torrent);
159-
torrent.cojbpyeargb = calculateBPYearGBCoj(torrent, torrentData.cojyears);
160-
torrent.mightychefyeargb = calculateMightychefYears(torrent, torrentData.mightychefdays);
158+
torrent.bpYearGB = calculateBPYearGB(torrent);
159+
torrent.bpYearGBCoj = calculateBPYearGBCoj(torrent, torrentData.cojYears);
160+
torrent.bpDaysGBMightychef =
161+
calculateBPDaysGBMightychef(torrent, torrentData.mightychefDays);
161162

162163
torrentData.torrents.push(torrent);
163164
}
@@ -190,7 +191,7 @@ const calcStats = torrentData => {
190191
const target = t.hidden ? hidden : shown;
191192
target.total++;
192193
target.size += t.size;
193-
target.bpYear += torrentData.useMightychef ? t.mightychefyeargb : t.bpyear;
194+
target.bpYear += torrentData.useMightychef ? t.bpDaysGBMightychef : t.bpYear;
194195
}
195196

196197
const total = {
@@ -204,34 +205,29 @@ const calcStats = torrentData => {
204205

205206
// eslint-disable-next-line complexity
206207
const showOptimization = (elContent, torrentData) => {
207-
if (!torrentData.torrents?.length > 0) {
208-
window.localStorage.removeItem('bpopt');
209-
firstRun(elContent);
210-
return;
211-
}
212-
213208
const { total, hidden, shown } = calcStats(torrentData);
214209

215-
const target = torrentData.mightychefTarget;
216-
const reachedTarget = Math.round(total.bpYear * 100) === target * 100;
217-
if (torrentData.useMightychef && target !== -1 && !reachedTarget && torrentData.loops < 20) {
210+
const targetBP = torrentData.mightychefTargetBP;
211+
const reachedTarget = Math.round(total.bpYear * 100) === targetBP * 100;
212+
if (torrentData.useMightychef && targetBP !== -1 && !reachedTarget && torrentData.loops < 20) {
213+
console.log(`in the weird loop; loops=${torrentData.loops}`);
218214
const elBonus = document.querySelector('#nav_bonus a');
219215
const currentBP = Number(elBonus.innerHTML.match(/\d/g).join(''));
220-
const target2 = target > currentBP ? target - currentBP : target;
216+
const targetBP2 = targetBP > currentBP ? targetBP - currentBP : targetBP;
221217

222-
torrentData.mightychefdays /= total.bpYear / target2;
218+
torrentData.mightychefDays /= total.bpYear / targetBP2;
223219
for (const t of torrentData.torrents) {
224-
t.mightychefyeargb = calculateMightychefYears(t, torrentData.mightychefdays);
220+
t.bpDaysGBMightychef = calculateBPDaysGBMightychef(t, torrentData.mightychefDays);
225221
}
226222

227223
torrentData.loops = torrentData.loops ? torrentData.loops + 1 : 1;
228-
window.localStorage.bpopt = JSON.stringify(torrentData);
224+
window.localStorage.bpopt2 = JSON.stringify(torrentData);
229225
showOptimization(elContent, torrentData);
230226
return;
231227
}
232228

233-
const mightychefPeriod = torrentData.mightychefdays === 1 ?
234-
'Day' : `${printNumber(torrentData.mightychefdays, true)} days`;
229+
const mightychefPeriod = torrentData.mightychefDays === 1 ?
230+
'Day' : `${printNumber(torrentData.mightychefDays, true)} days`;
235231
const partPeriod = torrentData.useMightychef ? mightychefPeriod : 'Year';
236232

237233
elContent.setAttribute('style', 'text-align: center;');
@@ -296,14 +292,14 @@ const showOptimization = (elContent, torrentData) => {
296292
<br>
297293
<span class="bpoLabel">coj Years averaged over:</span>
298294
<span class="bpoCont">
299-
<input type="number" value="${torrentData.cojyears}">
295+
<input type="number" value="${torrentData.cojYears}">
300296
<a id="applyCojYears" href="#">Apply</a>
301297
</span>
302298
<br>
303299
<span class="bpoLabel">mightychef days:</span>
304300
<span class="bpoCont">
305-
<input type="number" value="${torrentData.mightychefdays}">
306-
<a id="applyMightychefYears" href="#">Apply</a>
301+
<input type="number" value="${torrentData.mightychefDays}">
302+
<a id="applyMightychefDays" href="#">Apply</a>
307303
</span>
308304
<br>
309305
<span class="bpoLabel">BP/Year/GB divisor (2500 for GB/Year/GB):</span>
@@ -314,8 +310,8 @@ const showOptimization = (elContent, torrentData) => {
314310
<br>
315311
<span class="bpoLabel">mightychef target:</span>
316312
<span class="bpoCont">
317-
<input type="number" value="${torrentData.mightychefTarget}">
318-
<a id="applyMightychefTarget" href="#">Apply</a>
313+
<input type="number" value="${torrentData.mightychefTargetBP}">
314+
<a id="applyMightychefTargetBP" href="#">Apply</a>
319315
</span>
320316
<br>
321317
<span class="bpoLabel">Minimum seed time (days):</span>
@@ -357,7 +353,7 @@ const showOptimization = (elContent, torrentData) => {
357353
el.addEventListener('click', e => {
358354
e.preventDefault();
359355
linkListeners[el.id](torrentData);
360-
window.localStorage.bpopt = JSON.stringify(torrentData);
356+
window.localStorage.bpopt2 = JSON.stringify(torrentData);
361357
showOptimization(elContent, torrentData);
362358
});
363359
});
@@ -368,7 +364,7 @@ const showOptimization = (elContent, torrentData) => {
368364
e.preventDefault();
369365
if (Number.isNaN(input.valueAsNumber)) return;
370366
inputListeners[el.id](torrentData, input.valueAsNumber);
371-
window.localStorage.bpopt = JSON.stringify(torrentData);
367+
window.localStorage.bpopt2 = JSON.stringify(torrentData);
372368
showOptimization(elContent, torrentData);
373369
});
374370
});
@@ -380,7 +376,7 @@ const showOptimization = (elContent, torrentData) => {
380376
e.preventDefault();
381377
const sortBy = el.innerHTML.replace(/[/, ]/g, '');
382378
sortTorrents(torrentData, sortBy);
383-
window.localStorage.bpopt = JSON.stringify(torrentData);
379+
window.localStorage.bpopt2 = JSON.stringify(torrentData);
384380
showOptimization(elContent, torrentData);
385381
});
386382
});
@@ -391,11 +387,11 @@ const showOptimization = (elContent, torrentData) => {
391387
const remaining = Math.round((mST - days) * 100) / 100;
392388
const needToSeed = (mST > 0 && days < mST) || (mST <= 0 && days > -mST);
393389

394-
const bpyearRawValue = torrentData.useMightychef ?
395-
t.mightychefyeargb * (t.size / (1024 ** 3)) : t.bpyear;
396-
const bpyeargbRawValue = torrentData.useMightychef ?
397-
t.mightychefyeargb :
398-
torrentData.useCoj ? t.cojbpyeargb : t.bpyeargb;
390+
const bpPeriodRawValue = torrentData.useMightychef ?
391+
t.bpDaysGBMightychef * (t.size / (1024 ** 3)) : t.bpYear;
392+
const bpPeriodGBRawValue = torrentData.useMightychef ?
393+
t.bpDaysGBMightychef :
394+
torrentData.useCoj ? t.bpYearGBCoj : t.bpYearGB;
399395

400396
elTable.insertAdjacentHTML('beforeend', `
401397
<div class="bpoRow bpoHover ${t.hidden ? 'bpoHidden' : ''} ${needToSeed ? 'bpoNeedToSeed' : ''}" ${mST > 0 && days < mST ? `title="Seed time remaining: ${remaining}"` : ''}>
@@ -412,9 +408,9 @@ const showOptimization = (elContent, torrentData) => {
412408
</span><span class="bpoCell" style="width: 55px;">
413409
${printNumber(t.seeders, true)}
414410
</span><span class="bpoCell" style="width: 75px;">
415-
${printNumber(bpyearRawValue)}
411+
${printNumber(bpPeriodRawValue)}
416412
</span><span class="bpoCell" style="width: 75px;">
417-
${printNumber(bpyeargbRawValue / torrentData.divisor)}
413+
${printNumber(bpPeriodGBRawValue / torrentData.divisor)}
418414
</span><span class="bpoCell" style="width: 30px;">
419415
<a href="javascript:void(0);">X</a>
420416
</span>
@@ -447,11 +443,11 @@ const getSortFunc = (newSortBy, { sortBy, useCoj, useMightychef }) => {
447443
}
448444

449445
if (useMightychef && newSortBy.match(/(BP|GB)\d.*GB/)) {
450-
return sortFuncs.MightychefBPYearGB(reverse);
446+
return sortFuncs.MightychefBPDaysGB(reverse);
451447
}
452448

453449
if (useMightychef && newSortBy.match(/BP\d/)) {
454-
return mightychefbpyearSort(reverse);
450+
return mightychefBPDaysSort(reverse);
455451
}
456452

457453
return sortFuncs[newSortBy](reverse);
@@ -471,42 +467,42 @@ const sortFuncs = {
471467
GP: makeKeySortFunc('gp', -1),
472468
Size: makeKeySortFunc('size', -1),
473469
Seeders: makeKeySortFunc('seeders', -1),
474-
BPYear: makeKeySortFunc('bpyear'),
475-
BPYearGB: makeKeySortFunc('bpyeargb'),
476-
GBYearGB: makeKeySortFunc('bpyeargb'),
477-
MightychefBPYearGB: makeKeySortFunc('mightychefyeargb'),
478-
CojBPYearGB: makeKeySortFunc('cojbpyeargb'),
470+
BPYear: makeKeySortFunc('bpYear'),
471+
BPYearGB: makeKeySortFunc('bpYearGB'),
472+
GBYearGB: makeKeySortFunc('bpYearGB'),
473+
MightychefBPDaysGB: makeKeySortFunc('bpDaysGBMightychef'),
474+
CojBPYearGB: makeKeySortFunc('bpYearGBCoj'),
479475
Hide: makeKeySortFunc('hidden'),
480476
SeedTime: makeKeySortFunc('seedTimeDays'),
481477
Ratio: reverse => (a, b) => reverse * (getRatioKey(b.ratio) - getRatioKey(a.ratio)),
482478
Title: reverse => (a, b) => reverse * a.title.localeCompare(b.title),
483479
};
484480

485-
const mightychefbpyearSort = reverse => (a, b) => {
486-
const getKey = x => x.mightychefyeargb * (x.size / (1024 ** 3));
481+
const mightychefBPDaysSort = reverse => (a, b) => {
482+
const getKey = x => x.bpDaysGBMightychef * (x.size / (1024 ** 3));
487483
return reverse * (getKey(a) - getKey(b));
488484
};
489485

490486
// ----- Event listeners (input) -----
491487

492488
const inputListeners = {
493489
applyCojYears: (torrentData, value) => {
494-
torrentData.cojyears = value;
490+
torrentData.cojYears = value;
495491
for (const torrent of torrentData.torrents) {
496-
torrent.cojbpyeargb = calculateBPYearGBCoj(torrent, value);
492+
torrent.bpYearGBCoj = calculateBPYearGBCoj(torrent, value);
497493
}
498494
},
499-
applyMightychefYears: (torrentData, value) => {
500-
torrentData.mightychefdays = value;
495+
applyMightychefDays: (torrentData, value) => {
496+
torrentData.mightychefDays = value;
501497
for (const torrent of torrentData.torrents) {
502-
torrent.mightychefyeargb = calculateMightychefYears(torrent, value);
498+
torrent.bpDaysGBMightychef = calculateBPDaysGBMightychef(torrent, value);
503499
}
504500
},
505501
applyDivisor: (torrentData, value) => {
506502
torrentData.divisor = value;
507503
},
508-
applyMightychefTarget: (torrentData, value) => {
509-
torrentData.mightychefTarget = value;
504+
applyMightychefTargetBP: (torrentData, value) => {
505+
torrentData.mightychefTargetBP = value;
510506
torrentData.loops = 0;
511507
},
512508
applyMinimumSeedTime: (torrentData, value) => {
@@ -516,7 +512,7 @@ const inputListeners = {
516512

517513
const hideTorrent = (torrent, elContent, torrentData) => {
518514
torrent.hidden = !torrent.hidden;
519-
window.localStorage.bpopt = JSON.stringify(torrentData);
515+
window.localStorage.bpopt2 = JSON.stringify(torrentData);
520516
showOptimization(elContent, torrentData);
521517
};
522518

@@ -591,14 +587,14 @@ const parseHTML = html => new DOMParser().parseFromString(html, 'text/html');
591587

592588
const getPeriod = torrentData => {
593589
if (!torrentData.useMightychef) return 'year';
594-
if (torrentData.mightychefdays === 1) return 'day';
590+
if (torrentData.mightychefDays === 1) return 'day';
595591

596-
const hours = (torrentData.mightychefdays % 1) * 24;
592+
const hours = (torrentData.mightychefDays % 1) * 24;
597593
const minutes = (hours % 1) * 60;
598594
const seconds = Math.round((minutes % 1) * 60);
599595
return hours === 0 && minutes === 0 && seconds === 0 ?
600-
`${torrentData.mightychefdays} days` :
601-
`${Math.floor(torrentData.mightychefdays)} days, ${Math.floor(hours)} hours, ${Math.floor(minutes)} minutes, and ${seconds} seconds`;
596+
`${torrentData.mightychefDays} days` :
597+
`${Math.floor(torrentData.mightychefDays)} days, ${Math.floor(hours)} hours, ${Math.floor(minutes)} minutes, and ${seconds} seconds`;
602598
};
603599

604600
const printNumber = (number, asInteger) => number.toLocaleString('en-US', {
@@ -624,8 +620,8 @@ const addCSVtoLink = (a, torrentData) => {
624620
a.innerHTML = `Generating ${torrentData.torrents.length} rows`;
625621

626622
const fields = [
627-
'id', 'link', 'title', 'gp', 'size', 'seeders', 'ratio', 'bpyear', 'bphour',
628-
'seedtimeseconds', 'bpyeargb', 'cojbpyeargb', 'mightychefyeargb', 'seedTimeDays',
623+
'id', 'link', 'title', 'gp', 'size', 'seeders', 'ratio', 'bpYear', 'bpHour',
624+
'seedtimeseconds', 'bpYearGB', 'bpYearGBCoj', 'bpDaysGBMightychef', 'seedTimeDays',
629625
'hidden', 'seedTimeLeft',
630626
];
631627
let text = `${fields.map(f => `"${f}"`).join(',')}\n`;
@@ -662,7 +658,7 @@ const main = () => {
662658
// Add links to the new page on both the bonus.php and bprate.php pages
663659
addLinks();
664660

665-
const torrentDataStored = window.localStorage.bpopt;
661+
const torrentDataStored = window.localStorage.bpopt2;
666662
const torrentData = torrentDataStored ?
667663
{
668664
...JSON.parse(torrentDataStored),
@@ -672,14 +668,14 @@ const main = () => {
672668
{
673669
firstRun: true,
674670
loops: 1,
675-
cojyears: 3,
676-
mightychefdays: 365,
671+
cojYears: 3,
672+
mightychefDays: 365,
677673
divisor: 1,
678-
mightychefTarget: -1,
674+
mightychefTargetBP: -1,
679675
minimumSeedTime: 2,
680676
};
681677

682-
window.localStorage.bpopt = JSON.stringify(torrentData);
678+
window.localStorage.bpopt2 = JSON.stringify(torrentData);
683679

684680
if (!window.location.href.includes('optimization=true')) return;
685681

0 commit comments

Comments
 (0)