#4 - Rework stat banner

This commit is contained in:
2022-09-19 21:08:49 +02:00
parent 8c4553f9f2
commit 4107a759c0
19 changed files with 442 additions and 72 deletions
+70
View File
@@ -0,0 +1,70 @@
const MAX_EXECUTIONS = 12;
const ROTATION_INTERVAL = 30000;
const executions = {};
function banner(mapping) {
Object.keys(mapping).forEach(
(cur) => {
bannerGroup(mapping[cur], cur);
}
);
}
function bannerGroup(group, grpName) {
resortGroup(group);
executions[grpName] = executions[grpName] ? executions[grpName] + 1 : 1;
const amount = group.placeholders.length;
const rval = group.banners.prio.slice(0, amount);
if (rval.length < amount) {
rval.push(
...group.banners.generic.slice(0, amount - rval.length)
);
}
if (rval.length > 0) {
$.ajax({
url: "stats/bannerMultiImpression.php",
method: "POST",
data: {
banners: rval.map(e => e.id).join(","),
source: document.URL
},
dataType: "html"
});
rval.forEach(v => {
v.views++;
});
const enough = rval.length == amount;
group.placeholders.forEach(
(target) => {
if (rval.length > 0) {
const ele = rval.shift();
$('#'+target).html(ele.media);
}
}
);
if (enough && executions[grpName] <= MAX_EXECUTIONS) {
setTimeout(
() => {
bannerGroup(group, grpName);
},
ROTATION_INTERVAL
);
}
}
}
function resortGroup(group) {
group.banners.prio.sort(compareEle)
group.banners.generic.sort(compareEle)
}
function compareEle(a, b) {
return a.views >= b.views;
}