<commit_before>﻿function arena(args) {
    args = args || {};
    var playerId;
    var players;
    var arenaHandler;
    var playerMoving;
    var singOut;
    var input;
    var gfx;
    var canvas, backgroundCanvas;
    var lastPlayerListUpdate;
    var viewport = { x: 0, y: 0 };
    var arena;
    var playerInput = { keysInput: 0, facingDirection: { X: 0, Y: 0 } },
        previousPlayerInput = { keysInput: 0, facingDirection: { X: 0, Y: 0 } };
    var connected;

    var gameObjects = [];

    var setPlayer = function (p) {
        playerId = p;
        gfx.resizeCanvas();
        connected = true;
        requestAnimationFrame(processFrame);
        input.startListening();
    };

    var setupArena = function (a) {
        arena = a;
    };

    var playerMoved = function (gameObjectsFromServer) {
        var currentPlayer = getCurrentPlayer();
        gameObjects = gameObjectsFromServer;
        if (playerId && !getCurrentPlayer()) {
            playerId = null;
            singOut(currentPlayer);
        }
    };

    var isCurrentPlayerPredicate = function (g) { return g.Id == playerId; };

    var getCurrentPlayer = function () {
        return gameObjects.find(isCurrentPlayerPredicate);
    }

    var inputChange = function (input) {
        var player = getCurrentPlayer();
        if (player == undefined) return;

        var mouseX = Math.round(input.mouse.x / gfx.scale() - viewport.x);
        var mouseY = Math.round(-input.mouse.y / gfx.scale() - viewport.y);

        calculateFacingDirection(player, mouseX, mouseY);

        playerInput.keysInput = input.direction;
        playerInput.facingDirection = player.FacingDirection;
    };

    function calculateFacingDirection(player, mouseX, mouseY) {
        player.FacingDirection.X = Math.round(mouseX - player.Position.X);
        player.FacingDirection.Y = Math.round(mouseY - player.Position.Y);
    }

    var lastTimestamp;
    var processFrame = function (timestamp) {
        if (!connected)
            return;

        if (!lastPlayerListUpdate) {
            lastPlayerListUpdate = timestamp;
        }

        lastTimestamp = lastTimestamp || timestamp;
        var currentPlayer = getCurrentPlayer();
        
        if (playerInput &&
            (playerInput.keysInput != previousPlayerInput.keysInput ||
             playerInput.facingDirection.X != previousPlayerInput.facingDirection.X ||
             playerInput.facingDirection.Y != previousPlayerInput.facingDirection.Y)) {
            previousPlayerInput.keysInput = playerInput.keysInput;
            previousPlayerInput.facingDirection = playerInput.facingDirection;
            playerMoving(playerInput);

            //if (currentPlayer)
            //    updatePlayerSpeed(currentPlayer, playerInput.keysInput);
        }

        var timeDiff = (timestamp - lastTimestamp) / 1000;
        updateObjectsPositions(timeDiff);

        if (currentPlayer !== undefined) {
            viewport.x =  gfx.originalSize.x / 2 - currentPlayer.Position.X;
            viewport.y = -gfx.originalSize.y / 2 - currentPlayer.Position.Y;
        }

        gfx.drawArena(gameObjects);

        if (timestamp - lastPlayerListUpdate > 2000) {
            lastPlayerListUpdate = timestamp;
            players.updateLeaderboard(gameObjects);
        }

        lastTimestamp = timestamp;

        if (connected)
            requestAnimationFrame(processFrame);
    };

    var updatePlayerSpeed = function (player, keys) {
        var speedX = 0,
            speedY = 0;

        if (keys & keysInput.Up)
            speedY = 1;
        if (keys & keysInput.Down)
            speedY = -1;
        if (keys & keysInput.Right)
            speedX = 1;
        if (keys & keysInput.Left)
            speedX = -1;

        if (speedX != 0 || speedY != 0) {
            var speedLength = Math.sqrt(speedX * speedX + speedY * speedY)<commit_msg>update server about player status at least at 20fps<commit_after>﻿function arena(args) {
    args = args || {};
    var playerId;
    var players;
    var arenaHandler;
    var playerMoving;
    var singOut;
    var input;
    var gfx;
    var canvas, backgroundCanvas;
    var lastPlayerListUpdate;
    var viewport = { x: 0, y: 0 };
    var arena;
    var playerInput = { keysInput: 0, facingDirection: { X: 0, Y: 0 } },
        previousPlayerInput = { keysInput: 0, facingDirection: { X: 0, Y: 0 } };
    var connected;
    var communicationDelay = 1000 / 20;

    var gameObjects = [];

    var setPlayer = function (p) {
        playerId = p;
        gfx.resizeCanvas();
        connected = true;
        requestAnimationFrame(processFrame);
        input.startListening();
    };

    var setupArena = function (a) {
        arena = a;
    };

    var playerMoved = function (gameObjectsFromServer) {
        var currentPlayer = getCurrentPlayer();
        gameObjects = gameObjectsFromServer;
        if (playerId && !getCurrentPlayer()) {
            playerId = null;
            singOut(currentPlayer);
        }
    };

    var isCurrentPlayerPredicate = function (g) { return g.Id == playerId; };

    var getCurrentPlayer = function () {
        return gameObjects.find(isCurrentPlayerPredicate);
    }

    var inputChange = function (input) {
        var player = getCurrentPlayer();
        if (player == undefined) return;

        var mouseX = Math.round(input.mouse.x / gfx.scale() - viewport.x);
        var mouseY = Math.round(-input.mouse.y / gfx.scale() - viewport.y);

        calculateFacingDirection(player, mouseX, mouseY);

        playerInput.keysInput = input.direction;
        playerInput.facingDirection = player.FacingDirection;
    };

    function calculateFacingDirection(player, mouseX, mouseY) {
        player.FacingDirection.X = Math.round(mouseX - player.Position.X);
        player.FacingDirection.Y = Math.round(mouseY - player.Position.Y);
    }

    var lastTimestamp;
    var lastCommunicationTime;
    var processFrame = function (timestamp) {
        if (!connected)
            return;

        if (!lastPlayerListUpdate) {
            lastPlayerListUpdate = timestamp;
            lastCommunicationTime = timestamp;
        }

        lastTimestamp = lastTimestamp || timestamp;
        var currentPlayer = getCurrentPlayer();
        
        if (playerInputChanged() || timestamp - lastCommunicationTime > communicationDelay) {
            previousPlayerInput.keysInput = playerInput.keysInput;
            previousPlayerInput.facingDirection = playerInput.facingDirection;
            lastCommunicationTime = timestamp;

            playerMoving(playerInput);

            //if (currentPlayer)
            //    updatePlayerSpeed(currentPlayer, playerInput.keysInput);
        }

        var timeDiff = (timestamp - lastTimestamp) / 1000;
        updateObjectsPositions(timeDiff);

        if (currentPlayer !== undefined) {
            viewport.x =  gfx.originalSize.x / 2 - currentPlayer.Position.X;
            viewport.y = -gfx.originalSize.y / 2 - currentPlayer.Position.Y;
        }

        gfx.drawArena(gameObjects);

        if (timestamp - lastPlayerListUpdate > 2000) {
            lastPlayerListUpdate = timestamp;
            players.updateLeaderboard(gameObjects);
        }

        lastTimestamp = timestamp;

        if (connected)
            requestAnimationFrame(processFrame);
    };

    function playerInputChanged() {
        return playerInput &&
            (playerInput.keysInput != previousPlayerInput.keysInput ||
             playerInput.facingDirection.X != previousPlayerInput.facingDirection.X ||
             playerInput.facingDirection.Y != previousPlayerInput.facingDirection.Y)
    }

    var updatePlayerSpeed = function (player, keys) {
        var speedX = 0,
            speedY = 0;

        if (keys & keysInput.Up)
            speedY = 1;
        if (keys & keysInput.Down)
            speedY = -1;
        if (keys & keysInput.Right)
            speedX = 1;
        if (keys & keysInput.Left)
            speedX = -1;

        if (speedX != 0 || speedY != 0) {
            var speedLength = Math.sqrt(speedX * speedX + speedY * speedY)
{'fraction_non_alphanumeric': 0.10067692307692308, 'fraction_numerical': 0.0073846153846153844, 'mean_word_length': 2.1904201020808793, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '332133', 'n_tokens_mistral': 2296, 'n_tokens_neox': 2176, 'n_words': 506}