MediaWiki:TeamBingo.js: Difference between revisions

From Roat Pkz
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
// --- Team Bingo Board with Fixed Info Box & Captain Restriction ---
mw.hook('wikipage.content').add(function () {
mw.hook('wikipage.content').add(function () {
    const JSON_PAGE = "TeamBingoData"; // wiki page where JSON is stored
    const POLL_INTERVAL = 5000; // 5 seconds
    const username = mw.config.get("wgUserName");


     var containers = document.querySelectorAll(".teamBingoContainer");
     const infoBox = document.getElementById("teamBingoInfoBox");
 
    const containers = document.querySelectorAll(".teamBingoContainer");
     if (!containers.length) return;
     if (!containers.length) return;


     // Shared info box
     let boardData = {}; // current state of tiles for all teams
    var infoBox = document.getElementById("teamBingoInfoBox");


     containers.forEach(function(container) {
    // Initialize boards
     containers.forEach(container => {
         container.innerHTML = "";
         container.innerHTML = "";
 
         const team = container.id;
         // Captain username from attribute
         const captain = container.getAttribute("data-captain");
         var captain = container.getAttribute("data-captain");
         const itemsAttr = container.getAttribute("data-items");
 
         const items = itemsAttr.split("|").filter(i => i.trim() !== "");
        // Parse items
         const parsedItems = items.map((entry, idx) => {
         var itemsAttr = container.getAttribute("data-items");
             const parts = entry.split("::");
         if (!itemsAttr) return;
             return { name: parts[0].trim(), tooltip: parts[1] ? parts[1].trim() : parts[0].trim(), index: idx };
        var items = itemsAttr.split("|").filter(i => i.trim() !== "");
         if (!items.length) return;
 
        var parsedItems = items.map(entry => {
             var parts = entry.split("::");
             return {
                name: parts[0].trim(),
                tooltip: parts[1] ? parts[1].trim() : parts[0].trim()
            };
         });
         });


         // Build table
         const table = document.createElement("table");
        var table = document.createElement("table");
        table.style.width = "100%";
         table.style.tableLayout = "fixed";
         table.style.tableLayout = "fixed";
         table.style.border = "2px solid #596e96";
         table.style.border = "2px solid #596e96";
         table.style.borderCollapse = "collapse";
         table.style.borderCollapse = "collapse";
        table.style.borderRadius = "8px";
        table.style.width = "100%";
         container.appendChild(table);
         container.appendChild(table);


         var rows = Math.ceil(Math.sqrt(parsedItems.length));
         const rows = Math.ceil(Math.sqrt(parsedItems.length));
         var cols = Math.ceil(parsedItems.length / rows);
         const cols = Math.ceil(parsedItems.length / rows);


         // API call for images
         const allTemplates = parsedItems.map(item => `{{plinkp|${item.name}}}`).join("\n");
        var allTemplates = parsedItems.map(item => `{{plinkp|${item.name}}}`).join("\n");
        $.getJSON("/api.php", {
            action: "parse",
            text: allTemplates,
            format: "json"
        }).done(function(data) {
            if (!data.parse || !data.parse.text) return;


             var tempDiv = document.createElement("div");
        // Fetch images via API
        $.getJSON("/api.php", { action: "parse", text: allTemplates, format: "json" }).done(function(data){
             if(!data.parse || !data.parse.text) return;
            const tempDiv = document.createElement("div");
             tempDiv.innerHTML = data.parse.text["*"];
             tempDiv.innerHTML = data.parse.text["*"];
             var imgs = tempDiv.querySelectorAll("img");
             const imgs = tempDiv.querySelectorAll("img");


             for (let r = 0; r < rows; r++) {
             for (let r = 0; r < rows; r++){
                 let row = table.insertRow();
                 const row = table.insertRow();
                 for (let c = 0; c < cols; c++) {
                 for (let c = 0; c < cols; c++){
                     let index = r * cols + c;
                     const idx = r*cols + c;
                     if (index >= parsedItems.length) break;
                     if(idx >= parsedItems.length) break;


                     let item = parsedItems[index];
                     const item = parsedItems[idx];
                     let cell = row.insertCell();
                     const cell = row.insertCell();
                     cell.style.width = `${100 / cols}%`;
                     cell.style.position = "relative";
                    cell.style.padding = "10px";
                    cell.style.border = "1px solid #596e96";
                     cell.style.textAlign = "center";
                     cell.style.textAlign = "center";
                     cell.style.background = "#313e59";
                     cell.style.background = "#313e59";
                     cell.style.color = "#fff";
                     cell.style.border = "1px solid #596e96";
                     cell.style.cursor = "pointer";
                     cell.style.cursor = "pointer";
                     cell.style.transition = "all 0.2s ease";
                     cell.style.transition = "all 0.2s";
                     cell.style.userSelect = "none";
                     cell.style.padding = "10px";
                    cell.style.wordWrap = "break-word";
                    cell.style.overflow = "visible";
                    cell.style.position = "relative";
 
                    let id = `${container.id}_cell_${index}`;
                    cell.id = id;


                     // Container
                     const cellContainer = document.createElement("div");
                    let cellContainer = document.createElement("div");
                     cellContainer.style.position = "relative";
                     cellContainer.style.position = "relative";
                    cellContainer.style.width = "100%";
                    cellContainer.style.height = "100%";


                     // Image
                     // Image
                     if (imgs[index]) {
                     if(imgs[idx]){
                         let img = document.createElement("img");
                         const img = document.createElement("img");
                         img.src = imgs[index].src;
                         img.src = imgs[idx].src;
                         img.alt = item.name;
                         img.alt = item.name;
                         img.style.width = "40px";
                         img.style.width = "40px";
                        img.style.height = "auto";
                         img.style.display = "block";
                         img.style.display = "block";
                         img.style.margin = "0 auto";
                         img.style.margin = "0 auto";
Line 96: Line 73:


                     // Name
                     // Name
                     let nameDiv = document.createElement("div");
                     const nameDiv = document.createElement("div");
                     nameDiv.textContent = item.name;
                     nameDiv.textContent = item.name;
                     nameDiv.style.marginTop = "5px";
                     nameDiv.style.marginTop = "5px";
                    nameDiv.style.pointerEvents = "none";
                     cellContainer.appendChild(nameDiv);
                     cellContainer.appendChild(nameDiv);


                     // Checkmark
                     // Checkmark
                     let checkmark = document.createElement("div");
                     const checkmark = document.createElement("div");
                     checkmark.textContent = "✔";
                     checkmark.textContent = "✔";
                    checkmark.className = "bingoCheckmark";
                     checkmark.style.position = "absolute";
                     checkmark.style.position = "absolute";
                     checkmark.style.top = "2px";
                     checkmark.style.top = "2px";
                     checkmark.style.right = "2px";
                     checkmark.style.right = "2px";
                    checkmark.style.color = "#4CAF50";
                     checkmark.style.fontSize = "20px";
                     checkmark.style.fontSize = "20px";
                    checkmark.style.color = "#4CAF50";
                    checkmark.style.background = "none";
                     checkmark.style.display = "none";
                     checkmark.style.display = "none";
                     cellContainer.appendChild(checkmark);
                     cellContainer.appendChild(checkmark);


                     // Hover
                    cell.appendChild(cellContainer);
                     cell.addEventListener("mouseenter", function() {
 
                        if (localStorage.getItem(this.id) !== "true") this.style.background = "#3b4a6b";
                     // Hover info
                     cell.addEventListener("mouseenter", ()=>{
                         infoBox.innerHTML = "";
                         infoBox.innerHTML = "";
                         let header = document.createElement("div");
                         const header = document.createElement("div");
                         header.style.fontWeight = "bold";
                         header.style.fontWeight = "bold";
                         header.style.marginBottom = "5px";
                         header.style.marginBottom = "5px";
                         header.textContent = item.name;
                         header.textContent = item.name;
                         infoBox.appendChild(header);
                         infoBox.appendChild(header);
                         if (imgs[index]) {
 
                             let hoverImg = document.createElement("img");
                         if(imgs[idx]){
                             hoverImg.src = imgs[index].src;
                             const img = document.createElement("img");
                             hoverImg.style.width = "40px";
                             img.src = imgs[idx].src;
                             hoverImg.style.height = "auto";
                             img.style.width = "40px";
                            hoverImg.style.display = "block";
                             img.style.display = "block";
                             hoverImg.style.marginBottom = "5px";
                             img.style.marginBottom = "5px";
                             infoBox.appendChild(hoverImg);
                             infoBox.appendChild(img);
                         }
                         }
                         item.tooltip.split("%%").forEach(line => {
 
                             let div = document.createElement("div");
                         item.tooltip.split("%%").forEach(line=>{
                             const div = document.createElement("div");
                             div.innerHTML = line.trim();
                             div.innerHTML = line.trim();
                             infoBox.appendChild(div);
                             infoBox.appendChild(div);
                         });
                         });
                    });
                    cell.addEventListener("mouseleave", function() {
                        if (localStorage.getItem(this.id) !== "true") this.style.background = "#313e59";
                        // restore default info
                        infoBox.innerHTML = defaultInfo();
                     });
                     });


                    // Click (captain only)
                     cell.addEventListener("mouseleave", ()=>{
                     cell.addEventListener("click", function() {
                         infoBox.innerHTML = "Hover over a tile to see its info here.";
                         if (mw.config.get('wgUserName') !== captain) return; // only captain
                        let done = localStorage.getItem(this.id) === "true";
                        localStorage.setItem(this.id, !done);
                        this.style.background = !done ? "rgba(76,175,80,0.5)" : "#313e59";
                        checkmark.style.display = !done ? "block" : "none";
                        updateProgress(container, parsedItems.length);
                     });
                     });


                     // Load saved state
                     // Click only if captain
                     if (localStorage.getItem(id) === "true") {
                     cell.addEventListener("click", function(){
                         cell.style.background = "rgba(76,175,80,0.5)";
                         if(username !== captain) return;
                        checkmark.style.display = "block";
                    }


                     cell.appendChild(cellContainer);
                        const key = `${team}_tile_${idx}`;
                        boardData[key] = !boardData[key];
                        saveBoardData();
                        renderBoard();
                     });
                 }
                 }
             }
             }
        });
    });


            // Progress
    // Fetch board state from JSON page
            let progressDiv = document.createElement("div");
    function fetchBoardData(){
            progressDiv.style.marginTop = "10px";
        $.getJSON(`/api.php?action=parse&page=${JSON_PAGE}&format=json`).done(data=>{
             progressDiv.style.color = "#fff";
             try{
            progressDiv.className = "bingoProgress";
                const text = data.parse.text["*"];
             container.appendChild(progressDiv);
                boardData = JSON.parse(text);
                renderBoard();
             }catch(e){ console.error(e);}
        });
    }


            // Reset
    // Save board state to JSON page
            let resetBtn = document.createElement("button");
    function saveBoardData(){
            resetBtn.textContent = "Reset Bingo";
        const content = JSON.stringify(boardData);
             resetBtn.style.marginTop = "5px";
        $.post("/api.php", {
             resetBtn.style.cursor = "pointer";
             action:"edit",
             resetBtn.addEventListener("click", function () {
            title: JSON_PAGE,
                if (mw.config.get('wgUserName') !== captain) return; // only captain
             token: mw.user.tokens.get("csrfToken"),
                for (let i = 0; i < parsedItems.length; i++) {
             format: "json",
                    let cb = document.getElementById(`${container.id}_cell_${i}`);
            text: content,
                    if (cb) {
            summary: "Update bingo board"
                        localStorage.setItem(cb.id, false);
        });
                        cb.style.background = "#313e59";
    }
                        let check = cb.querySelector(".bingoCheckmark");
 
                        if (check) check.style.display = "none";
    // Update checkmarks based on boardData
                    }
    function renderBoard(){
                 }
        containers.forEach(container=>{
                updateProgress(container, parsedItems.length);
            const team = container.id;
            const cells = container.querySelectorAll("td");
            cells.forEach((cell, idx)=>{
                const key = `${team}_tile_${idx}`;
                const check = cell.querySelector(".bingoCheckmark");
                if(check) check.style.display = boardData[key] ? "block" : "none";
                 cell.style.background = boardData[key] ? "rgba(76,175,80,0.5)" : "#313e59";
             });
             });
            container.appendChild(resetBtn);
            updateProgress(container, parsedItems.length);
         });
         });
    }


        function updateProgress(container, total) {
    fetchBoardData();
            let completed = 0;
    setInterval(fetchBoardData, 5000); // poll every 5s
            for (let i = 0; i < total; i++) {
                let cb = document.getElementById(`${container.id}_cell_${i}`);
                if (cb && localStorage.getItem(cb.id) === "true") completed++;
            }
            let progressDiv = container.querySelector(".bingoProgress");
            if (progressDiv)
                progressDiv.textContent = `Completed ${completed} of ${total} items`;
        }
 
        function defaultInfo() {
            return `
<strong>Roat Pkz PVM Bingo #3</strong><br>
Collect drops from PVM or skilling to score points — each drop = 1 point. Screenshots required; no chests, crates, or boxes. Event ends <strong>June 28th, 11:59 PM GMT</strong>.<br><br>
<strong>Rewards:</strong><br>
- 500 CREDITS (25M+)<br>
- 300 CREDITS (15M+)<br>
- 200 CREDITS (10M+)<br><br>
Only drops <strong>after this announcement</strong> count. Submit sheets via the <strong>No Access</strong> channel or message <strong>@JAY</strong>.
            `;
        }
    });
});
});

Revision as of 15:43, 7 March 2026

mw.hook('wikipage.content').add(function () {
    const JSON_PAGE = "TeamBingoData"; // wiki page where JSON is stored
    const POLL_INTERVAL = 5000; // 5 seconds
    const username = mw.config.get("wgUserName");

    const infoBox = document.getElementById("teamBingoInfoBox");

    const containers = document.querySelectorAll(".teamBingoContainer");
    if (!containers.length) return;

    let boardData = {}; // current state of tiles for all teams

    // Initialize boards
    containers.forEach(container => {
        container.innerHTML = "";
        const team = container.id;
        const captain = container.getAttribute("data-captain");
        const itemsAttr = container.getAttribute("data-items");
        const items = itemsAttr.split("|").filter(i => i.trim() !== "");
        const parsedItems = items.map((entry, idx) => {
            const parts = entry.split("::");
            return { name: parts[0].trim(), tooltip: parts[1] ? parts[1].trim() : parts[0].trim(), index: idx };
        });

        const table = document.createElement("table");
        table.style.width = "100%";
        table.style.tableLayout = "fixed";
        table.style.border = "2px solid #596e96";
        table.style.borderCollapse = "collapse";
        container.appendChild(table);

        const rows = Math.ceil(Math.sqrt(parsedItems.length));
        const cols = Math.ceil(parsedItems.length / rows);

        const allTemplates = parsedItems.map(item => `{{plinkp|${item.name}}}`).join("\n");

        // Fetch images via API
        $.getJSON("/api.php", { action: "parse", text: allTemplates, format: "json" }).done(function(data){
            if(!data.parse || !data.parse.text) return;
            const tempDiv = document.createElement("div");
            tempDiv.innerHTML = data.parse.text["*"];
            const imgs = tempDiv.querySelectorAll("img");

            for (let r = 0; r < rows; r++){
                const row = table.insertRow();
                for (let c = 0; c < cols; c++){
                    const idx = r*cols + c;
                    if(idx >= parsedItems.length) break;

                    const item = parsedItems[idx];
                    const cell = row.insertCell();
                    cell.style.position = "relative";
                    cell.style.textAlign = "center";
                    cell.style.background = "#313e59";
                    cell.style.border = "1px solid #596e96";
                    cell.style.cursor = "pointer";
                    cell.style.transition = "all 0.2s";
                    cell.style.padding = "10px";

                    const cellContainer = document.createElement("div");
                    cellContainer.style.position = "relative";

                    // Image
                    if(imgs[idx]){
                        const img = document.createElement("img");
                        img.src = imgs[idx].src;
                        img.alt = item.name;
                        img.style.width = "40px";
                        img.style.display = "block";
                        img.style.margin = "0 auto";
                        cellContainer.appendChild(img);
                    }

                    // Name
                    const nameDiv = document.createElement("div");
                    nameDiv.textContent = item.name;
                    nameDiv.style.marginTop = "5px";
                    cellContainer.appendChild(nameDiv);

                    // Checkmark
                    const checkmark = document.createElement("div");
                    checkmark.textContent = "✔";
                    checkmark.style.position = "absolute";
                    checkmark.style.top = "2px";
                    checkmark.style.right = "2px";
                    checkmark.style.color = "#4CAF50";
                    checkmark.style.fontSize = "20px";
                    checkmark.style.display = "none";
                    cellContainer.appendChild(checkmark);

                    cell.appendChild(cellContainer);

                    // Hover info
                    cell.addEventListener("mouseenter", ()=>{
                        infoBox.innerHTML = "";
                        const header = document.createElement("div");
                        header.style.fontWeight = "bold";
                        header.style.marginBottom = "5px";
                        header.textContent = item.name;
                        infoBox.appendChild(header);

                        if(imgs[idx]){
                            const img = document.createElement("img");
                            img.src = imgs[idx].src;
                            img.style.width = "40px";
                            img.style.display = "block";
                            img.style.marginBottom = "5px";
                            infoBox.appendChild(img);
                        }

                        item.tooltip.split("%%").forEach(line=>{
                            const div = document.createElement("div");
                            div.innerHTML = line.trim();
                            infoBox.appendChild(div);
                        });
                    });

                    cell.addEventListener("mouseleave", ()=>{
                        infoBox.innerHTML = "Hover over a tile to see its info here.";
                    });

                    // Click only if captain
                    cell.addEventListener("click", function(){
                        if(username !== captain) return;

                        const key = `${team}_tile_${idx}`;
                        boardData[key] = !boardData[key];
                        saveBoardData();
                        renderBoard();
                    });
                }
            }
        });
    });

    // Fetch board state from JSON page
    function fetchBoardData(){
        $.getJSON(`/api.php?action=parse&page=${JSON_PAGE}&format=json`).done(data=>{
            try{
                const text = data.parse.text["*"];
                boardData = JSON.parse(text);
                renderBoard();
            }catch(e){ console.error(e);}
        });
    }

    // Save board state to JSON page
    function saveBoardData(){
        const content = JSON.stringify(boardData);
        $.post("/api.php", {
            action:"edit",
            title: JSON_PAGE,
            token: mw.user.tokens.get("csrfToken"),
            format: "json",
            text: content,
            summary: "Update bingo board"
        });
    }

    // Update checkmarks based on boardData
    function renderBoard(){
        containers.forEach(container=>{
            const team = container.id;
            const cells = container.querySelectorAll("td");
            cells.forEach((cell, idx)=>{
                const key = `${team}_tile_${idx}`;
                const check = cell.querySelector(".bingoCheckmark");
                if(check) check.style.display = boardData[key] ? "block" : "none";
                cell.style.background = boardData[key] ? "rgba(76,175,80,0.5)" : "#313e59";
            });
        });
    }

    fetchBoardData();
    setInterval(fetchBoardData, 5000); // poll every 5s
});