"""Easter egg features for Snipe. Three features: 1. Konami code → Snipe Mode — JS detector sets ?snipe_mode=1 URL param, Streamlit detects it on rerun. Audio is synthesised client-side via Web Audio API (no bundled file; local-first friendly). Disabled by default for accessibility / autoplay-policy reasons; requires explicit sidebar opt-in. 2. The Steal shimmer — a listing with trust ≥ 90, price 15–30 % below market, and no suspicious_price flag gets a gold shimmer banner. 3. Auction de-emphasis — auctions with > 1 h remaining show a soft notice because live prices are misleading until the final minutes. """ from __future__ import annotations import json from datetime import datetime, timezone from typing import Optional import streamlit as st from app.db.models import Listing, TrustScore # --------------------------------------------------------------------------- # 1. Konami → Snipe Mode # --------------------------------------------------------------------------- _KONAMI_JS = """ """ _SNIPE_AUDIO_JS = """ """ _SNIPE_BANNER_CSS = """ """ def inject_konami_detector() -> None: """Inject the JS Konami sequence detector into the page (once per load).""" st.components.v1.html(_KONAMI_JS, height=0) def check_snipe_mode() -> bool: """Return True if ?snipe_mode=1 is present in the URL query params.""" return st.query_params.get("snipe_mode", "") == "1" def render_snipe_mode_banner(audio_enabled: bool) -> None: """Render the Snipe Mode activation banner and optionally play the audio cue.""" st.markdown(_SNIPE_BANNER_CSS, unsafe_allow_html=True) st.markdown( '
', unsafe_allow_html=True, ) if audio_enabled: st.components.v1.html(_SNIPE_AUDIO_JS, height=0) # --------------------------------------------------------------------------- # 2. The Steal shimmer # --------------------------------------------------------------------------- _STEAL_CSS = """ """ def inject_steal_css() -> None: """Inject the steal-shimmer CSS (idempotent — Streamlit deduplicates).""" st.markdown(_STEAL_CSS, unsafe_allow_html=True) def is_steal(listing: Listing, trust: Optional[TrustScore], market_price: Optional[float]) -> bool: """Return True when this listing qualifies as 'The Steal'. Criteria (all must hold): - trust composite ≥ 90 - no suspicious_price flag - price is 15–30 % below the market median (deeper discounts are suspicious, not steals) """ if trust is None or market_price is None or market_price <= 0: return False if trust.composite_score < 90: return False flags = json.loads(trust.red_flags_json or "[]") if "suspicious_price" in flags: return False discount = (market_price - listing.price) / market_price return 0.15 <= discount <= 0.30 def render_steal_banner() -> None: """Render the gold shimmer steal banner above a listing row.""" st.markdown( '', unsafe_allow_html=True, ) # --------------------------------------------------------------------------- # 3. Auction de-emphasis # --------------------------------------------------------------------------- def auction_hours_remaining(listing: Listing) -> Optional[float]: """Return hours remaining for an auction listing, or None for fixed-price / no data.""" if listing.buying_format != "auction" or not listing.ends_at: return None try: ends = datetime.fromisoformat(listing.ends_at) delta = ends - datetime.now(timezone.utc) return max(delta.total_seconds() / 3600, 0.0) except (ValueError, TypeError): return None def render_auction_notice(hours: float) -> None: """Render a soft de-emphasis notice for auctions with significant time remaining.""" if hours >= 1.0: h = int(hours) label = f"{h}h left" if h < 24 else f"{h // 24}d {h % 24}h left" st.caption( f"⏰ Auction · {label} — price not final until last few minutes" )