Showing tag poster and photos on tag page. Improved campaign fallback logic, fixes wrong ratio selected.

This commit is contained in:
2025-12-13 04:00:14 +01:00
parent bc26e07812
commit 84b9bbd1b6
8 changed files with 274 additions and 33 deletions

View File

@@ -33,7 +33,7 @@ function curateCampaign(campaign) {
};
}
function selectRandomCampaign(primaryCampaigns, entityCampaigns, preferredCampaigns, allCampaigns, options) {
function selectRandomCampaign(primaryCampaigns, entityCampaigns, preferredCampaigns) {
if (primaryCampaigns.length > 0) {
return primaryCampaigns[crypto.randomInt(primaryCampaigns.length)];
}
@@ -46,14 +46,10 @@ function selectRandomCampaign(primaryCampaigns, entityCampaigns, preferredCampai
return preferredCampaigns[crypto.randomInt(preferredCampaigns.length)];
}
if (allCampaigns.length > 0 && options.allowRandomFallback !== false) {
return allCampaigns[crypto.randomInt(allCampaigns.length)];
}
return null;
}
export async function getRandomCampaign(options = {}, context = {}) {
export async function getRandomCampaign(options = {}, context = {}, pass = 0) {
const campaigns = options.campaigns
|| await redis.hGetAll('traxxx:campaigns').then((rawCampaigns) => Object.values(rawCampaigns).map((rawCampaign) => JSON.parse(rawCampaign)));
@@ -103,6 +99,18 @@ export async function getRandomCampaign(options = {}, context = {}) {
const randomCampaign = selectRandomCampaign(primaryCampaigns, randomEntityCampaigns, validCampaigns, campaigns, options);
// no campaign found, gradually widen scope
if (!randomCampaign && pass === 0 && options.allowRandomFallback !== false) {
return getRandomCampaign({
minRatio: options.minRatio,
maxRatio: options.maxRatio,
}, context, pass + 1);
}
if (!randomCampaign && pass === 1 && options.allowRandomFallback !== false && options.allowRandomRatio) {
return getRandomCampaign({}, context, pass + 1);
}
return randomCampaign;
}