<commit_before>/* JavaScript tetris game.
 * File: game.js
 * This file contains all the game logic. */

/* Directional constants (arrow key codes for keydown event; D stands for direction).
 * The numbers are the ASCII values for the keys. */
var D_LEFT = 37;   // Left arrow
var D_ROTATE = 38; // Up arrow
var D_RIGHT = 39;  // Right arrow
var D_DOWN = 40;   // Down arrow
var D_LIST = [D_LEFT, D_RIGHT, D_DOWN];
/* Other input constants */
var K_PAUSE = 80;   // P
var K_QUIT = 27;    // Esc
var K_QUIT2 = 81;   // Q
var K_RESTART = 13; // Enter
var FK_LIST = [K_PAUSE, K_QUIT, K_QUIT2, K_RESTART]; // Function-keys list.
var K_LEVELUP = 76; // L
var K_HARDDROP = 32; // Spacebar

function handleInput(ev) {
    /* FIXME: REFACTOR THIS FUNCTION TO REDUCE REDUNDANCY AND IMPROVE PERFORMANCE!!! */

    /* Handles input from keyboard and mouse */
    ev = ev || window.event;
    
    var go = handleInput.gameObject;
    
    if (ev.type === "click") {
        /* Start or restart the game by clicking on the canvas.
         * We return afterwards because we don't respond to any further mouse events. */
        if (go !== undefined) { 
            // Game is in progress. End it.
            go.endGame();
        }
        startGame(handleInput);
        return;
    }
    var keyCode = ev.keyCode;
    if ((go !== undefined) && (go.gameStarted)) {
        if ((go.paused) && (FK_LIST.indexOf(keyCode) === -1)) {
            /* The pressed key was not found in the function keys list, and the game is paused.
             * Only the function keys can work when the game is paused. */
            return;
        }
        /* These ones can only work if the game is in progress */
        if ((D_LIST.indexOf(keyCode) !== -1) && (go.allowMovement)) {
            /* Movement keys. */
            var drop = go.moveBlock(keyCode);
            if (drop) {
                /* If the user pressed the down key twice in a row and there was a collision, we drop the block */
                go.softdrop++;
                if (go.softdrop === 2) {
                    /* Drop the block, clear the automatic movement timer, and then restart it.
                     * If we don't clear it first, there will be two active timers. */
                    go.dropBlock();
                    clearTimeout(go.autoMoveTimer);
                    go.restartAutoMove(true);
                    go.softdrop = 0;
                }
            }<commit_msg>Fixed bug where a click on the canvas while the game is paused would still start a new game.<commit_after>/* JavaScript tetris game.
 * File: game.js
 * This file contains all the game logic. */

/* Directional constants (arrow key codes for keydown event; D stands for direction).
 * The numbers are the ASCII values for the keys. */
var D_LEFT = 37;   // Left arrow
var D_ROTATE = 38; // Up arrow
var D_RIGHT = 39;  // Right arrow
var D_DOWN = 40;   // Down arrow
var D_LIST = [D_LEFT, D_RIGHT, D_DOWN];
/* Other input constants */
var K_PAUSE = 80;   // P
var K_QUIT = 27;    // Esc
var K_QUIT2 = 81;   // Q
var K_RESTART = 13; // Enter
var K_LEVELUP = 76; // L
var K_HARDDROP = 32; // Spacebar
// Function keys list
var FK_LIST = [K_PAUSE, K_QUIT, K_QUIT2, K_RESTART]

function handleInput(ev) {
    /* Handles input from keyboard and mouse.
     * Every key press and mouse click triggers this function. */
    ev = ev || window.event;
    
    var go = handleInput.gameObject;
    
    if (ev.type === "click") {
        /* Start or restart the game by clicking on the canvas */
        if (go !== undefined) { 
            /* Restart the game if it's not paused.
             * A click while paused could be an indication that
             * the canvas element lost focus, and the user is trying to refocus it,
             * so that they can unpause and continue the game. */
            if (!go.paused) {
                go.endGame();
                startGame(handleInput);
            }
        }
        else { 
            /* New game */
            startGame(handleInput);
        }
    }
    var keyCode = ev.keyCode;
    if ((go !== undefined) && (go.gameStarted)) {
        if (go.paused && FK_LIST.indexOf(keyCode) === -1) {
            /* The pressed key was not found in the function keys list, and the game is paused.
             * Only the function keys can work when the game is paused. */
            return;
        }
        /* These ones can only work if the game is in progress */
        if (D_LIST.indexOf(keyCode) !== -1 && go.allowMovement) {
            /* Movement keys. */
            var drop = go.moveBlock(keyCode);
            if (drop) {
                /* If the user pressed the down key twice in a row and there was a collision, we drop the block */
                go.softdrop++;
                if (go.softdrop === 2) {
                    /* Drop the block, clear the automatic movement timer, and then restart it.
                     * If we don't clear it first, there will be two active timers. */
                    go.dropBlock();
                    clearTimeout(go.autoMoveTimer);
                    go.restartAutoMove(true); // passing true means it'll spawn a new block
                    go.softdrop = 0;
                }
            }
{'fraction_non_alphanumeric': 0.10229007633587786, 'fraction_numerical': 0.009923664122137405, 'mean_word_length': 1.773015873015873, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3890811', 'n_tokens_mistral': 1561, 'n_tokens_neox': 1469, 'n_words': 589}