// Share
$(‘#share’).addEventListener(‘click’, ()=>{
const nm = get(‘name’,’Friend’);
const msg = encodeURIComponent(`Roll & Win at Crazy Plastics — Black Friday! I just played. Try your luck: https://wa.me/27100210177?text=ROLL`);
const url = `https://api.whatsapp.com/send?text=${msg}`;
window.open(url, ‘_blank’);
});
// Accessibility
function focusSpin(){ try{ spinBtn.focus(); }catch(e){} }
// UI helpers
function hideResults(){ [resWin,resNear,resTry].forEach(x=>x.classList.remove(‘show’)); }
function show(el){ el.classList.add(‘show’); }
function formatDate(d){ return d.toLocaleDateString(undefined,{year:’numeric’,month:’short’,day:’numeric’}); }
function canPlay(){
const cnt = playCount();
if(cnt >= WEEKEND_CAP){ cooldown.innerText = `You\’ve used your ${WEEKEND_CAP} plays for the weekend. Good luck in the grand draw!`; cooldown.hidden=false; return false; }
const last = lastPlayAt();
if(last){
const diffH = (Date.now() – new Date(last).getTime())/36e5;
if(diffH < DAILY_LIMIT_HOURS){
const left = Math.max(0, DAILY_LIMIT_HOURS - diffH);
cooldown.innerText = `You\'ve already rolled today. Try again in ~${left.toFixed(1)}h.`; cooldown.hidden=false; return false;
}
}
cooldown.hidden=true; return true;
} function updateCooldownUI(){ canPlay(); } // Simple reel animation (emoji shuffle)
function sleep(ms){ return new Promise(r=>setTimeout(r,ms)); }
async function animateReels(){
const cycles = [16, 22, 28]; // staggered stops
for(let i=0;i
const code = generateVoucher();
voucherEl.textContent = code;
show(resWin);
} else if(result === ‘NEAR_MISS’){
reels[0].textContent=’🧻’; reels[1].textContent=’🎲’; reels[2].textContent=’🧻’;
show(resNear);
} else {
reels.forEach(r=>r.textContent=’🎲’);
show(resTry);
}
}
// Weighted random pick
function weightedPick(w){
const entries = Object.entries(w);
const total = entries.reduce((s,[,v])=>s+v,0);
const r = Math.random()*total;
let acc=0;
for(const [k,v] of entries){ acc+=v; if(r<=acc) return k; }
return entries[entries.length-1][0];
} // Voucher code demo (client-side). Replace with server code generation.
function generateVoucher(){
const phone = 'web';
const ts = Date.now().toString(36).toUpperCase();
const seed = (phone + ts).split('').reduce((a,c)=>((a<<5)-a + c.charCodeAt(0))|0,0);
const base = Math.abs(seed).toString(36).toUpperCase().padStart(6,'0').slice(0,6);
return `CP-BF-${base}`;
} // Auto-show game if user already filled gate previously
if(savedName && savedStore){ gate.hidden=true; game.hidden=false; storeNameEl.textContent=savedStore; focusSpin(); updateCooldownUI(); }
})();
