<commit_before>var Cell = require('./Cell');
var Packet = require('./packet');

function PacketHandler(gameServer, socket) {
    this.gameServer = gameServer;
    this.socket = socket;
}

module.exports = PacketHandler;

PacketHandler.prototype.handleMessage = function(message) {
    function stobuf(buf) {
        var length = buf.length;
        var arrayBuf = new ArrayBuffer(length);
        var view = new Uint8Array(arrayBuf);

        for (var i = 0; i < length; i++) {
            view[i] = buf[i];
        }

        return view.buffer;
    }

    var buffer = stobuf(message);
    var view = new DataView(buffer);
    var packetId = view.getUint8(0, true);

    switch (packetId) {
        case 0:
            // Set Nickname
            var nick = "";
            for (var i = 1; i < view.byteLength; i += 2) {
                var charCode = view.getUint16(i, true);
                if (charCode == 0) {
                    break;
                }

                nick += String.fromCharCode(charCode);
            }
            this.setNickname(nick);
            break;
        case 16:
            // Mouse Move
            var client = this.socket.playerTracker;
            client.setMouseX(view.getFloat64(1, true));
            client.setMouseY(view.getFloat64(9, true));
			
            for (var i = 0; i < client.cells.length; i++){
                var cell = client.cells[i];
				
                if (!cell) {
                    continue;
                }
				
                cell.calcMove(client.getMouseX(), client.getMouseY(), this.gameServer.border);
                
                // Check if cells nearby (Still buggy)
                var list = this.gameServer.getCellsInRange(cell);
                for (var j = 0; j < list.length ; j++) {
                    //Remove the cells
                    var n = list[j].getType();
                    
                    switch (n) {
                        case 3: // Ejected Mass
                            cell.mass += this.gameServer.config.ejectMassGain;
                            break;                      	
                        case 0: // Player Cell
                            cell.mass += list[j].mass;
                            break;
                        case 1: // Food
                            this.gameServer.currentFood--;
                            cell.mass += this.gameServer.config.foodMass;
                            break;
                        case 2: // Virus
                            this.gameServer.currentViruses--;
                            break;
                        default:
                            break;
                    }
                    this.gameServer.removeNode(list[j]); 
                }
            }
            break;
		case 17: // Space Press - Split cell
            var client = this.socket.playerTracker;
            var len = client.cells.length;
            for (var i = 0; i < len; i++) {
                var cell = client.cells[i];
				
                if (client.cells.length >= this.gameServer.config.playerMaxCells) {
                    // Player cell limit
                    continue;
                }

                if (!cell) {
                    continue;
                }
                
                if (cell.mass < this.gameServer.config.playerMinMassSplit) {
                    continue;
                }
				
                var deltaY = client.getMouseY() - cell.getPos().y;
                var deltaX = client.getMouseX() - cell.getPos().x;
                var angle = Math.atan2(deltaX,deltaY);
            	
                // Get starting position
                var size = cell.getSize();
                var startPos = {
                    x: cell.getPos().x + ( (size + this.gameServer.config.ejectMass) * Math.sin(angle) ), 
                    y: cell.getPos().y + ( (size + this.gameServer.config.ejectMass) * Math.cos(angle) )
                };
                // Calculate mass of splitting cell
                var newMass = cell.mass / 2;
                cell.mass = newMass;
                // Create cell
                split = new Cell(this.gameServer.getNextNodeId(), client, startPos, newMass, 0);
                split.setAngle(angle);
                split.setMoveEngineData(75, 5);
            	
                // Add to moving cells list
                this.gameServer.addMovingCell(split);
                this.gameServer.addNode(split);
				
                // Add to player screen
                this.socket.sendPacket(new Packet.AddNodes(split));
                client.addCell(split);
            }
            break;
        case 18: // Q Press (Adds mass for debugging ejecting/splitting)
        	var client = this.socket.playerTracker;
            for (var i = 0; i < client.cells.length; i++) {
                var cell = client.cells[i];
				
                if (!cell) {
                    continue;
                }
                
                cell.mass += 10;
            }                
            break;
        case 21: // W Press - Eject mass
            var client = this.socket.playerTracker;
            for (var i = 0; i < client.cells.length; i++) {
                var cell = client.cells[i];
				
                if (!cell) {
                    continue;
                }
                
                if (cell.mass < this.gameServer.config.playerMinMassEject) {<commit_msg>Fixed an issue with ejecting cells<commit_after>var Cell = require('./Cell');
var Packet = require('./packet');

function PacketHandler(gameServer, socket) {
    this.gameServer = gameServer;
    this.socket = socket;
}

module.exports = PacketHandler;

PacketHandler.prototype.handleMessage = function(message) {
    function stobuf(buf) {
        var length = buf.length;
        var arrayBuf = new ArrayBuffer(length);
        var view = new Uint8Array(arrayBuf);

        for (var i = 0; i < length; i++) {
            view[i] = buf[i];
        }

        return view.buffer;
    }

    var buffer = stobuf(message);
    var view = new DataView(buffer);
    var packetId = view.getUint8(0, true);

    switch (packetId) {
        case 0:
            // Set Nickname
            var nick = "";
            for (var i = 1; i < view.byteLength; i += 2) {
                var charCode = view.getUint16(i, true);
                if (charCode == 0) {
                    break;
                }

                nick += String.fromCharCode(charCode);
            }
            this.setNickname(nick);
            break;
        case 16:
            // Mouse Move
            var client = this.socket.playerTracker;
            client.setMouseX(view.getFloat64(1, true));
            client.setMouseY(view.getFloat64(9, true));
			
            for (var i = 0; i < client.cells.length; i++){
                var cell = client.cells[i];
				
                if (!cell) {
                    continue;
                }
				
                cell.calcMove(client.getMouseX(), client.getMouseY(), this.gameServer.border);
                
                // Check if cells nearby (Still buggy)
                var list = this.gameServer.getCellsInRange(cell);
                for (var j = 0; j < list.length ; j++) {
                    //Remove the cells
                    var n = list[j].getType();
                    
                    switch (n) {
                        case 3: // Ejected Mass
                            cell.mass += this.gameServer.config.ejectMassGain;
                            break;                      	
                        case 0: // Player Cell
                            cell.mass += list[j].mass;
                            break;
                        case 1: // Food
                            this.gameServer.currentFood--;
                            cell.mass += this.gameServer.config.foodMass;
                            break;
                        case 2: // Virus
                            this.gameServer.currentViruses--;
                            break;
                        default:
                            break;
                    }
                    this.gameServer.removeNode(list[j]); 
                }
            }
            break;
		case 17: // Space Press - Split cell
            var client = this.socket.playerTracker;
            var len = client.cells.length;
            for (var i = 0; i < len; i++) {
                var cell = client.cells[i];
				
                if (client.cells.length >= this.gameServer.config.playerMaxCells) {
                    // Player cell limit
                    continue;
                }

                if (!cell) {
                    continue;
                }
                
                if (cell.mass < this.gameServer.config.playerMinMassSplit) {
                    continue;
                }
				
                var deltaY = client.getMouseY() - cell.getPos().y;
                var deltaX = client.getMouseX() - cell.getPos().x;
                var angle = Math.atan2(deltaX,deltaY);
            	
                // Get starting position
                var size = cell.getSize() + 10;
                var startPos = {
                    x: cell.getPos().x + ( (size + this.gameServer.config.ejectMass) * Math.sin(angle) ), 
                    y: cell.getPos().y + ( (size + this.gameServer.config.ejectMass) * Math.cos(angle) )
                };
                // Calculate mass of splitting cell
                var newMass = cell.mass / 2;
                cell.mass = newMass;
                // Create cell
                split = new Cell(this.gameServer.getNextNodeId(), client, startPos, newMass, 0);
                split.setAngle(angle);
                split.setMoveEngineData(75, 5);
            	
                // Add to moving cells list
                this.gameServer.addMovingCell(split);
                this.gameServer.addNode(split);
				
                // Add to player screen
                this.socket.sendPacket(new Packet.AddNodes(split));
                client.addCell(split);
            }
            break;
        case 21: // W Press - Eject mass
            var client = this.socket.playerTracker;
            for (var i = 0; i < client.cells.length; i++) {
                var cell = client.cells[i];
				
                if (!cell) {
                    continue;
                }
                
                if (cell.mass < this.gameServer.config.playerMinMassEject) {
{'fraction_non_alphanumeric': 0.10077076791321725, 'fraction_numerical': 0.007517366067180512, 'mean_word_length': 1.0503316426063207, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 5, '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': '1824593', 'n_tokens_mistral': 2880, 'n_tokens_neox': 2560, 'n_words': 612}