Nooble9781 / Tetris activelearn

// ==UserScript==
// @name         Tetris activelearn
// @namespace    www.pearsonactivelearn.com/*
// @version      0.1
// @description  tetris?
// @author       Kieran Aslett
// @match        www.pearsonactivelearn.com/app/library/series/view/666684
// @grant        none
// @copyright 2019, Nooble9781 (https://openuserjs.org/users/Nooble9781)
// @license MIT
// ==/UserScript==


(function() {
    let dom = document.getElementsByClassName("clearfix")[0].children[1].children[0];
    dom.href = "";
    dom.onclick = function() {
        var highscore = 0;
        function getCookie(name) {
            var value = "; " + document.cookie;
            var parts = value.split("; " + name + "=");
            if (parts.length == 2) return parts.pop().split(";").shift();
        }

        if (!(highscore = getCookie("highscore"))) {
            highscore = 0;
        } else {
            if (highscore % 100 != 0) highscore = 0;
        }

        var images = [];
        var occupied = [
            [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
            [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
            [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
            [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
            [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
            [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
            [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
            [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
            [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
            [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
            [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
            [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
            [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
            [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
            [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
            [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
            [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
            [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
            [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
            [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined]
        ];
        var player = [
            [],
            [],
            [],
            []
        ];
        var pieces = [
            {image: 0, blocks: [[[0, 0], [1, 0], [0, 1], [1, 1]], [[0, 0], [1, 0], [0, 1], [1, 1]], [[0, 0], [1, 0], [0, 1], [1, 1]], [[0, 0], [1, 0], [0, 1], [1, 1]]], width: 2, height: 2},
            {image: 1, blocks: [[[0, 1], [1, 0], [1, 1], [2, 1]], [[0, 0], [0, 1], [0, 2], [1, 1]], [[0, 0], [1, 0], [2, 0], [1, 1]], [[0, 1], [1, 0], [1, 1], [1, 2]]], width: 3, height: 2},
            {image: 2, blocks: [[[0, 0], [0, 1], [0, 2], [0, 3]], [[0, 0], [1, 0], [2, 0], [3, 0]], [[0, 0], [0, 1], [0, 2], [0, 3]], [[0, 0], [1, 0], [2, 0], [3, 0]]], width: 1, height: 4},
            {image: 3, blocks: [[[0, 0], [0, 1], [0, 2], [1, 2]], [[0, 0], [1, 0], [2, 0], [0, 1]], [[0, 0], [1, 0], [1, 1], [1, 2]], [[0, 1], [1, 1], [2, 1], [2, 0]]], width: 2, height: 3},
            {image: 4, blocks: [[[1, 0], [1, 1], [1, 2], [0, 2]], [[0, 0], [0, 1], [1, 1], [2, 1]], [[0, 0], [1, 0], [0, 1], [0, 2]], [[0, 0], [1, 0], [2, 0], [2, 1]]], width: 2, height: 3},
            {image: 5, blocks: [[[0, 0], [1, 0], [1, 1], [2, 1]], [[1, 0], [1, 1], [0, 1], [0, 2]], [[0, 0], [1, 0], [1, 1], [2, 1]], [[1, 0], [1, 1], [0, 1], [0, 2]]], width: 3, height: 2},
            {image: 6, blocks: [[[0, 1], [1, 0], [2, 0], [1, 1]], [[0, 0], [0, 1], [1, 1], [1, 2]], [[0, 1], [1, 0], [2, 0], [1, 1]], [[0, 0], [0, 1], [1, 1], [1, 2]]], width: 3, height: 2}
        ];
        var playerImage = undefined;
        var offsetX = 0;
        var offsetY = 0;
        var piece = 0;
        var score = 0;
        var held = Math.floor(Math.random() * (pieces.length));
        var next = Math.floor(Math.random() * (pieces.length));
        // var next = 6;
        var rotation = 0;
        var running = true;
        var tetrisLast = false;
        var els = document.getElementsByClassName("book-image");
        for (let el in els) {
            try {
                images.push(els[el].children[0].src);
            } catch(e) {

            }
        }

        window.addEventListener("resize", draw);

        function keys(e) {
            switch (e.which) {
                case 40:
                    window.moveDown();
                    break;
                case 37:
                    window.moveLR(-1);
                    break;
                case 39:
                    window.moveLR(1);
                    break;
                case 38:
                    window.rotate(1);
                    break;
                case 16:
                    var temp = held;
                    held = next;
                    next = temp;
                    draw();
                    break;
                case 32:
                    window.moveDown(1);
                    break;
                case 27:
                    if (running) {
                        running = false;
                        clearInterval(interval);
                    } else {
                        running = true;
                        interval = setInterval(window.moveDown, 500);
                    }
                    break;
            }
        }

        window.addEventListener("keydown", keys);

        window.spawnPiece = function() {
            offsetY = 0;
            offsetX = 3;
            piece = next;
            next = Math.floor(Math.random() * (pieces.length));
            rotation = Math.floor(Math.random() * (3));
            for (let i = 0; i < pieces[piece].blocks[0].length; i++) {
                player[i] = [].concat(pieces[piece].blocks[rotation][i]);
            }
            for (let i = 0; i < player.length; i++) {
                player[i][0] += offsetX;
                player[i][1] += offsetY;
            }
            playerImage = pieces[piece].image;
        }

        window.spawnPiece();

        window.rotate = function(dir) {
            let broke = false;
            rotation += dir;
            if (rotation == 4) {
                rotation = 0;
            } else if (rotation == -1) {
                rotation = 3;
            }
            for (let i = 0; i < pieces[piece].blocks[0].length; i++) {
                player[i] = [].concat(pieces[piece].blocks[rotation][i]);
            }
            for (let i = 0; i < player.length; i++) {
                player[i][0] += offsetX;
                player[i][1] += offsetY;
                if (occupied[player[i][1]][player[i][0]] != undefined || player[i][0] >= 10 || player[i][0] < 0 || player[i][1] >= 20) {
                    broke = true;
                }
            }
            if (broke) {
                window.rotate(-dir);
            } else {
                draw();
            }
        }

        window.moveDown = function(speed) {
            let broke = false;
            offsetY += 1;
            if (speed) {
                while (!broke) {
                    for (let i = 0; i < player.length; i++) {
                        player[i][1] = player[i][1] + 1;
                        if (player[i][1] >= 20 || occupied[player[i][1]][player[i][0]] != undefined) {
                            broke = true;
                        }
                    }
                }
            } else {
                for (let i = 0; i < player.length; i++) {
                    player[i][1] = player[i][1] + 1;
                    if (player[i][1] >= 20 || occupied[player[i][1]][player[i][0]] != undefined) {
                        broke = true;
                    }
                }
            }
            if (broke) {
                let win = false;
                for (let i = 0; i < player.length; i++) {
                    player[i][1] = player[i][1] - 1;
                    occupied[player[i][1]][player[i][0]] = playerImage;;
                    if (player[i][1] == 0) {
                        win = true;
                    }
                }
                if (!win) {
                    window.spawnPiece(0);
                    let lines = 0;
                    for (let i = 0; i < 20; i++) {
                        let full = true;
                        for (let j = 0; j < 10; j++) {
                            if (occupied[i][j] == undefined) {
                                full = false;
                            }
                        }
                        if (full) {
                            window.moveScreenDown(i);
                            lines++;
                        }
                    }
                    switch (lines) {
                        case 0:
                            tetrisLast = false;
                            break;
                        case 1:
                            score += 100;
                            tetrisLast = false;
                            break;
                        case 2:
                            score += 200;
                            tetrisLast = false;
                            break;
                        case 3:
                            score += 400;
                            tetrisLast = false;
                            break;
                        case 4:
                            if (tetrisLast) {
                                score += 1200;
                            } else {
                                score += 800;
                            }
                            tetrisLast = true;
                            break;
                    }
                } else {
                    reset();
                    window.spawnPiece(0);
                }
            }
            draw();
        }

        function reset() {
            if (score > highscore) {
                document.cookie = 'highscore=' + score + '; expires=Fri, 20 April 42069 04:20:00 UTC; path=/';
                highscore = score;
            }
            alert(score);
            occupied = [
                [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
                [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
                [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
                [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
                [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
                [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
                [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
                [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
                [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
                [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
                [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
                [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
                [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
                [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
                [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
                [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
                [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
                [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
                [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined],
                [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined]
            ];
            player = [
                [],
                [],
                [],
                []
            ];
            offsetX = 0;
            offsetY = 0;
            piece = 0;
            score = 0;
            held = Math.floor(Math.random() * (pieces.length));
            next = Math.floor(Math.random() * (pieces.length));
            rotation = 0;
            running = true;
            tetrisLast = false;
            window.spawnPiece()
        }

        window.moveScreenDown = function(index) {
            for (let i = index; i > 0; i--) {
                for (let j = 0; j < 10; j++) {
                    occupied[i][j] = occupied[i - 1][j];
                }
            }
            for (let j = 0; j < 10; j++) {
                occupied[0][j] = undefined;
            }
            draw();
        }

        window.moveLR = function(dir) {
            let broke = false;
            offsetX += dir;
            for (let i = 0; i < player.length; i++) {
                player[i][0] = player[i][0] + dir;
                if ((player[i][0] >= 10 || player[i][0] < 0) || occupied[player[i][1]][player[i][0]] != undefined) {
                    broke = true;
                }
            }
            if (broke) {
                offsetX -= dir;
                for (let i = 0; i < player.length; i++) {
                    player[i][0] = player[i][0] - dir;
                }
            }
            draw();
        }

        function draw() {
            var body = document.getElementsByClassName("panel-body student-book-panel")[0];
            body.style.height = body.clientHeight + "px";
            body.innerHTML = "";
            let heightLim = body.clientHeight / 20;
            let widthLim = body.clientWidth / 15;
            var dim = 0;
            var offsetY = 0;
            var offsetX = 0;
            if (heightLim < widthLim) {
                dim = heightLim;
                offsetX = (body.clientWidth - (15 * dim)) / 2;
            } else {
                dim = widthLim;
                offsetY = (body.clientHeight - (20 * dim)) / 2;
            }
            body.style.position = "relative";
            let box = document.createElement("div");
            box.style.position = "absolute";
            box.style.left = offsetX - 1 + "px";
            box.style.top = offsetY - 1 + "px";
            box.style.width = 10 * dim + 2 + "px";
            box.style.height = 20 * dim + 2 + "px";
            box.style.border = "1px solid white";
            box.className = "box";
            body.appendChild(box);
            let hold = document.createElement("div");
            hold.style.position = "absolute";
            hold.style.left = offsetX + 2 + 10 * dim + "px";
            hold.style.top = offsetY - 1 + "px";
            hold.style.width = 5 * dim + 2 + "px";
            hold.style.height = 5 * dim + 2 + "px";
            hold.style.border = "1px solid white";
            hold.className = "box";
            body.appendChild(hold);
            let nextB = document.createElement("div");
            nextB.style.position = "absolute";
            nextB.style.left = offsetX + 2 + 10 * dim + "px";
            nextB.style.top = offsetY - 1 + 5 * dim + 2 + "px";
            nextB.style.width = 5 * dim + 2 + "px";
            nextB.style.height = 5 * dim + 2 + "px";
            nextB.style.border = "1px solid white";
            nextB.className = "box";
            body.appendChild(nextB);
            let spare = document.createElement("div");
            spare.style.position = "absolute";
            spare.style.left = offsetX + 2 + 10 * dim + "px";
            spare.style.top = offsetY - 1 + 5 * dim + 2 + 5 * dim + 2 + "px";
            spare.style.width = 5 * dim + 2 + "px";
            spare.style.height = 10 * dim - 2 + "px";
            spare.style.border = "1px solid white";
            spare.style.textAlign = "center";
            spare.innerHTML = "<div style='margin-top: 66%'>" + score + "<br> Highscore:<br>" + highscore + "</div>";
            spare.style.overflowWrap = "break-work";
            spare.className = "box";
            body.appendChild(spare);
            for (let i = 0; i < 20; i++) {
                for (let j = 0; j < 10; j++) {
                    if (occupied[i][j] != undefined) {
                        let dom = document.createElement("img");
                        dom.src = images[occupied[i][j]];
                        dom.style.width = dim + "px";
                        dom.style.height = dim + "px";
                        dom.style.position = "absolute";
                        dom.style.left = offsetX + (j * dim) + "px";
                        dom.style.top = offsetY + (i * dim) + "px";
                        dom.style.borderRadius = "5%";
                        dom.style.border = "1px solid white";
                        dom.className = "piece";
                        body.appendChild(dom);
                    } else {
                        for (let k = 0; k < player.length; k++) {
                            if (player[k][0] == j && player[k][1] == i) {
                                let dom = document.createElement("img");
                                dom.src = images[playerImage];
                                dom.style.width = dim + "px";
                                dom.style.height = dim + "px";
                                dom.style.position = "absolute";
                                dom.style.left = offsetX + (j * dim) + "px";
                                dom.style.top = offsetY + (i * dim) + "px";
                                dom.style.borderRadius = "5%";
                                dom.style.border = "1px solid white";
                                dom.className = "piece";
                                body.appendChild(dom);
                            }
                        }
                    }

                }
            }
            let offX = 0;
            let offY = 0;
            switch (pieces[held].width) {
                case 1:
                    offX = dim;
                    break;
                case 2:
                    offX = dim * 0.5;
                    break;
            }
            switch (pieces[held].height) {
                case 2:
                    offY = dim * 0.5;
                    break;
                case 4:
                    offY = - dim * 0.5;
                    break;
            }
            for (let i = 0; i < pieces[0].blocks[0].length; i++) {
                let dom = document.createElement("img");
                dom.src = images[pieces[held].image];
                dom.style.width = dim + "px";
                dom.style.height = dim + "px";
                dom.style.position = "absolute";
                dom.style.left = offsetX + (11 * dim) + (pieces[held].blocks[0][i][0] * dim) + offX + "px";
                dom.style.top = offsetY + (1 * dim) + (pieces[held].blocks[0][i][1] * dim) + offY + "px";
                dom.style.borderRadius = "5%";
                dom.style.border = "1px solid white";
                dom.className = "piece";
                body.appendChild(dom);
            }

            offX = 0;
            offY = 0;
            switch (pieces[next].width) {
                case 1:
                    offX = dim;
                    break;
                case 2:
                    offX = dim * 0.5;
                    break;
            }
            switch (pieces[next].height) {
                case 2:
                    offY = dim * 0.5;
                    break;
                case 4:
                    offY = - dim * 0.5;
                    break;
            }
            for (let i = 0; i < pieces[0].blocks[0].length; i++) {
                let dom = document.createElement("img");
                dom.src = images[pieces[next].image];
                dom.style.width = dim + "px";
                dom.style.height = dim + "px";
                dom.style.position = "absolute";
                dom.style.left = offsetX + (11 * dim) + (pieces[next].blocks[0][i][0] * dim) + offX + "px";
                dom.style.top = offsetY + (6 * dim) + (pieces[next].blocks[0][i][1] * dim) + offY + "px";
                dom.style.borderRadius = "5%";
                dom.style.border = "1px solid white";
                dom.className = "piece";
                body.appendChild(dom);
            }
        }

        draw();
        var interval = setInterval(window.moveDown, 500);
    }
})();