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 Captains and Shared JSON + Live Updates ---
// --- Team Bingo Board with Captains, Shared JSON, and Live Updates ---
mw.hook('wikipage.content').add(function () {
mw.hook('wikipage.content').add(function () {


Line 34: Line 34:
     // Render the board for a container
     // Render the board for a container
     function renderBoard(container, teamKey, boardData) {
     function renderBoard(container, teamKey, boardData) {
         container.innerHTML = "";
         container.innerHTML = "";


         const captain = container.getAttribute("data-captain");
         const captain = container.getAttribute("data-captain");
         const itemsAttr = container.getAttribute("data-items");
         const itemsAttr = container.getAttribute("data-items");
         if (!itemsAttr) return;
         if (!itemsAttr) return;
         const items = itemsAttr.split("|").filter(i => i.trim() !== "");
         const items = itemsAttr.split("|").filter(i => i.trim() !== "");
        if (!items.length) return;


         const parsedItems = items.map(entry => {
         const parsedItems = items.map(entry => {
Line 48: Line 47:
         });
         });


        // Build table
         const table = document.createElement("table");
         const table = document.createElement("table");
         table.style.tableLayout = "fixed";
         table.style.tableLayout = "fixed";
Line 102: Line 100:
                     cellContainer.style.height = "100%";
                     cellContainer.style.height = "100%";


                    // Image
                     if (imgs[index]) {
                     if (imgs[index]) {
                         const img = document.createElement("img");
                         const img = document.createElement("img");
Line 114: Line 111:
                     }
                     }


                    // Name
                     const nameDiv = document.createElement("div");
                     const nameDiv = document.createElement("div");
                     nameDiv.textContent = item.name;
                     nameDiv.textContent = item.name;
Line 121: Line 117:
                     cellContainer.appendChild(nameDiv);
                     cellContainer.appendChild(nameDiv);


                    // Checkmark
                     const checkmark = document.createElement("div");
                     const checkmark = document.createElement("div");
                     checkmark.textContent = "✔";
                     checkmark.textContent = "✔";
Line 134: Line 129:
                     cellContainer.appendChild(checkmark);
                     cellContainer.appendChild(checkmark);


                     // Hover info
                     // Hover updates info box and keeps green if completed
                     cell.addEventListener("mouseenter", function() {
                     cell.addEventListener("mouseenter", function() {
                         if (!boardData[teamKey][`tile_${index}`]) cell.style.background = "#3b4a6b";
                         const done = boardData[teamKey][`tile_${index}`];
                        cell.style.background = done ? "rgba(76,175,80,0.5)" : "#3b4a6b";


                         infoBox.innerHTML = "";
                         infoBox.innerHTML = "";
Line 163: Line 159:


                     cell.addEventListener("mouseleave", function() {
                     cell.addEventListener("mouseleave", function() {
                         // Restore background based on completion
                         const done = boardData[teamKey][`tile_${index}`];
                        cell.style.background = boardData[teamKey][`tile_${index}`] ? "rgba(76,175,80,0.5)" : "#313e59";
                        cell.style.background = done ? "rgba(76,175,80,0.5)" : "#313e59";
 
                         infoBox.innerHTML = `
                         infoBox.innerHTML = `
<strong>Roat Pkz PVM Bingo #3</strong><br>
<strong>Roat Pkz PVM Bingo #3</strong><br>
Line 186: Line 181:
                         cell.style.background = !done ? "rgba(76,175,80,0.5)" : "#313e59";
                         cell.style.background = !done ? "rgba(76,175,80,0.5)" : "#313e59";


                        // Save back to MediaWiki page
                         $.post(mw.util.wikiScript('api'), {
                         $.post(mw.util.wikiScript('api'), {
                             action: 'edit',
                             action: 'edit',
Line 193: Line 187:
                             format: 'json',
                             format: 'json',
                             text: JSON.stringify(boardData, null, 4)
                             text: JSON.stringify(boardData, null, 4)
                         }).done(() => console.log("Board updated for everyone!"));
                         }).done(function(){
                            console.log("Board updated for everyone!");
                        });
                     });
                     });


Line 218: Line 214:
     }
     }


     // Initialize boards after fetching JSON
     // Initialize boards
     fetchBoardData(function(boardData) {
     fetchBoardData(function(boardData) {
         containers.forEach(container => {
         containers.forEach(container => {
Line 233: Line 229:
                 const cells = container.querySelectorAll("td");
                 const cells = container.querySelectorAll("td");
                 cells.forEach((cell, index) => {
                 cells.forEach((cell, index) => {
                    const done = boardData[teamKey][`tile_${index}`];
                     const checkmark = cell.querySelector(".bingoCheckmark");
                     const checkmark = cell.querySelector(".bingoCheckmark");
                     if (checkmark) {
                     if (checkmark) checkmark.style.display = done ? "block" : "none";
                        const done = boardData[teamKey][`tile_${index}`];
                    cell.style.background = done ? "rgba(76,175,80,0.5)" : "#313e59";
                        checkmark.style.display = done ? "block" : "none";
                        cell.style.background = done ? "rgba(76,175,80,0.5)" : "#313e59";
                    }
                 });
                 });
             });
             });
         });
         });
     }, 5000); // every 5 seconds
     }, 5000); // every 5 seconds
});
});

Revision as of 16:09, 7 March 2026

// --- Team Bingo Board with Captains, Shared JSON, and Live Updates ---
mw.hook('wikipage.content').add(function () {

    const currentUser = mw.config.get('wgUserName'); // Logged-in user

    // Select all team bingo containers
    const containers = document.querySelectorAll(".teamBingoContainer");
    if (!containers.length) return;

    // Shared info box
    const infoBox = document.getElementById("teamBingoInfoBox");

    // Fetch the JSON data from the Module page
    function fetchBoardData(callback) {
        $.getJSON(mw.util.wikiScript('api'), {
            action: 'query',
            titles: 'Module:TeamBingo/data.json',
            prop: 'revisions',
            rvprop: 'content',
            format: 'json',
            formatversion: 2
        }).done(function(data){
            if (!data.query.pages[0].revisions) return;
            const content = data.query.pages[0].revisions[0].content;
            try {
                const boardData = JSON.parse(content);
                callback(boardData);
            } catch(e) {
                console.error("Failed to parse JSON from Module:TeamBingo/data.json", e);
            }
        });
    }

    // Render the board for a container
    function renderBoard(container, teamKey, boardData) {
        container.innerHTML = "";

        const captain = container.getAttribute("data-captain");
        const itemsAttr = container.getAttribute("data-items");
        if (!itemsAttr) return;
        const items = itemsAttr.split("|").filter(i => i.trim() !== "");
        if (!items.length) return;

        const parsedItems = items.map(entry => {
            const parts = entry.split("::");
            return { name: parts[0].trim(), tooltip: parts[1] ? parts[1].trim() : parts[0].trim() };
        });

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

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

        // Single API call for images
        const 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;

            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 index = r * cols + c;
                    if (index >= parsedItems.length) break;

                    const item = parsedItems[index];
                    const cell = row.insertCell();
                    cell.style.width = `${100 / cols}%`;
                    cell.style.padding = "10px";
                    cell.style.border = "1px solid #596e96";
                    cell.style.textAlign = "center";
                    cell.style.background = boardData[teamKey][`tile_${index}`] ? "rgba(76,175,80,0.5)" : "#313e59";
                    cell.style.color = "#fff";
                    cell.style.cursor = "pointer";
                    cell.style.transition = "all 0.2s ease";
                    cell.style.userSelect = "none";
                    cell.style.wordWrap = "break-word";
                    cell.style.overflow = "visible";
                    cell.style.position = "relative";

                    const id = `${container.id}_cell_${index}`;
                    cell.id = id;

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

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

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

                    const checkmark = document.createElement("div");
                    checkmark.textContent = "✔";
                    checkmark.className = "bingoCheckmark";
                    checkmark.style.position = "absolute";
                    checkmark.style.top = "2px";
                    checkmark.style.right = "2px";
                    checkmark.style.fontSize = "20px";
                    checkmark.style.color = "#4CAF50";
                    checkmark.style.background = "none";
                    checkmark.style.display = boardData[teamKey][`tile_${index}`] ? "block" : "none";
                    cellContainer.appendChild(checkmark);

                    // Hover updates info box and keeps green if completed
                    cell.addEventListener("mouseenter", function() {
                        const done = boardData[teamKey][`tile_${index}`];
                        cell.style.background = done ? "rgba(76,175,80,0.5)" : "#3b4a6b";

                        infoBox.innerHTML = "";
                        const header = document.createElement("div");
                        header.style.fontWeight = "bold";
                        header.style.marginBottom = "5px";
                        header.textContent = item.name;
                        infoBox.appendChild(header);

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

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

                    cell.addEventListener("mouseleave", function() {
                        const done = boardData[teamKey][`tile_${index}`];
                        cell.style.background = done ? "rgba(76,175,80,0.5)" : "#313e59";
                        infoBox.innerHTML = `
<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>.
                        `;
                    });

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

                        const done = boardData[teamKey][`tile_${index}`];
                        boardData[teamKey][`tile_${index}`] = !done;
                        checkmark.style.display = !done ? "block" : "none";
                        cell.style.background = !done ? "rgba(76,175,80,0.5)" : "#313e59";

                        $.post(mw.util.wikiScript('api'), {
                            action: 'edit',
                            title: 'Module:TeamBingo/data.json',
                            token: mw.user.tokens.get('csrfToken'),
                            format: 'json',
                            text: JSON.stringify(boardData, null, 4)
                        }).done(function(){
                            console.log("Board updated for everyone!");
                        });
                    });

                    cell.appendChild(cellContainer);
                }
            }

            // Progress
            const progressDiv = document.createElement("div");
            progressDiv.style.marginTop = "10px";
            progressDiv.style.color = "#fff";
            progressDiv.className = "bingoProgress";
            container.appendChild(progressDiv);

            function updateProgress() {
                let completed = 0;
                for (let i = 0; i < parsedItems.length; i++) {
                    if (boardData[teamKey][`tile_${i}`]) completed++;
                }
                progressDiv.textContent = `Completed ${completed} of ${parsedItems.length} items`;
            }
            updateProgress();
        });
    }

    // Initialize boards
    fetchBoardData(function(boardData) {
        containers.forEach(container => {
            const teamKey = container.getAttribute("data-team");
            renderBoard(container, teamKey, boardData);
        });
    });

    // --- Poll JSON every 5 seconds for live updates ---
    setInterval(() => {
        fetchBoardData(function(boardData){
            containers.forEach(container => {
                const teamKey = container.getAttribute("data-team");
                const cells = container.querySelectorAll("td");
                cells.forEach((cell, index) => {
                    const done = boardData[teamKey][`tile_${index}`];
                    const checkmark = cell.querySelector(".bingoCheckmark");
                    if (checkmark) checkmark.style.display = done ? "block" : "none";
                    cell.style.background = done ? "rgba(76,175,80,0.5)" : "#313e59";
                });
            });
        });
    }, 5000); // every 5 seconds
});