diff --git a/server.js b/server.js index e72fb8e..b662b60 100644 --- a/server.js +++ b/server.js @@ -311,6 +311,23 @@ async function handleApi(req,res,pathname){ try{ const r=await Promise.race([chain.memberPublic(id),new Promise((_,rej)=>setTimeout(()=>rej(new Error('Blockchain lookup timed out — try again.')),20000))]); if(r.registered)r.referralUrl=`${getConfig().dappReferralBaseUrl}${encodeURIComponent(id)}`; + // align next-in-line with the human-curated rotation: prefer the ACTIVE + // rotation sponsor when they sit in this member's leg and need directs; + // else the first chain-order position that's a queue participant; else + // keep the chain's pure structural pick (covers legs outside the queue). + if(r.registered&&r.subtree){ + try{ + const sponsors=getSponsors(); + const act=activeSponsor(sponsors); + const participants=new Set(sponsors.filter(s=>s.status!=='qualified').map(s=>String(s.id))); + const bfs=[];const q=[r.subtree.left,r.subtree.right].filter(Boolean); + while(q.length){const n=q.shift();bfs.push(n);if(n.left)q.push(n.left);if(n.right)q.push(n.right);} + let pick=null; + if(act)pick=bfs.find(n=>String(n.id)===String(act.id)&&(n.directCount||0)<2); + if(!pick)pick=bfs.find(n=>participants.has(String(n.id))&&(n.directCount||0)<2); + if(pick)r.nextInLine={id:pick.id,directCount:pick.directCount||0,levelName:pick.levelName}; + }catch(e){} + } if(r.nextInLine)r.nextInLine.referralUrl=`${getConfig().dappReferralBaseUrl}${encodeURIComponent(r.nextInLine.id)}`; memberCache.set(id,{data:r,ts:Date.now()}); if(memberCache.size>500)memberCache.delete(memberCache.keys().next().value);