<commit_before>var app = require('http').createServer(handler), io = require('socket.io').listen(app), fs = require('fs');

var renderFPS = 60;
var worldFPS = 180;
var fieldState;
var beyList = new Array();
var maxBeys = 10;
var fieldSize = 500;
var speed = 3;
var repulse = 100;
var friction = 1;
var restCoeff = 1; //W

//xCIuWFNg
var BeyObject = function (point, size, session) {
    this.point = point;
    this.size = size;
    this.session = session;
    this.accel = [0, 0];
    this.sensor = { 'x': 0, 'y': 0, 'z': 0 };
    this.weight = 100;
}

function sessionExistsInBeyList(session) {
    for (i = 0; i < beyList.length; i++) {
        if (session == beyList[i].session) {
            return true;
        }
    }
    return false;
}

//xCo
function createBey(session, size) {
    var emergePoint = false;
    if (beyList.length >= maxBeys || sessionExistsInBeyList(session)) {
        return false;
    }
    //đ̃xCƂԂȂʒuȂ瑱s
    //n񎎍să_Ȃ߂
    for (i = 0; i < 100; i++) {
        var radius = Math.random() * fieldSize * 0.5;
        var theta = Math.random() * Math.PI * 2;
        var pointByRect = [radius * Math.sin(theta), radius * Math.cos(theta)];
        var emergeFlag = true;
        for (j = 0; j < beyList.length; j++) {
            if (distanceBetween(beyList[j].point, pointByRect) < beyList[j].size + size) {
                emergeFlag = false;
                break;
            }
        }
        if (emergeFlag) {
            emergePoint = pointByRect;
            break;
        }
    }
    if (emergePoint == false) {
        return false;
    }
    var bey = new BeyObject(emergePoint, size, session);
    beyList.push(bey);
    return true;
}

//xC
function removeBey(session) {
    for (i = 0; i < beyList.length; i++) {
        if (session == beyList[i].session) {
            beyList.splice(i, 1);
            return true;
        }
    }
    return false;
}

//ZT̍XV
function updateSensorInfo(session, sensor) {
    for (i = 0; i < beyList.length; i++) {
        if (session == beyList[i].session) {
            beyList[i].sensor = sensor;
            return true;
        }
    }
    return false;
}

function distanceBetween(p1, p2) {
    return Math.sqrt((p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]));
}

function vectorAddition(v1, v2, weight) {
    return [v1[0] + v2[0] * weight, v1[1] + v2[1] * weight];
}

function polarToRect(r, theta) {
    return [r * Math.sin(theta), r * Math.cos(theta)];
}

function rectToPolar(point) {
    return { "r": distanceBetween(point, [0, 0]), "theta": (point[1] == 0) ? 0 : ((point[1] > 0) ? Math.atan(point[0] / point[1]) : (Math.atan(point[0] / point[1]) + Math.PI)) };
}

//t@CǂݍŃX|X
var writeFromFile = function (req, res, locate) {
    fs.readFile(__dirname + locate, function (err, data) {
        if (err) {
            res.writeHead(500, {
                'Content-Type': 'text/plain'
            });
            res.write('Error loading ' + locate);
            res.end();
            console.log("not found " + locate);
            return;
        }

        res.writeHead(200, {
            'Content-Type': 'text/html; charset=utf-8'
        });
        res.write(data);
        res.end();
        console.log("served " + locate);
    });
}

app.listen(8080);

//Kvȃt@C
function handler (req, res) {
    var urlinfo = require('url').parse(req.url, true);
    if (urlinfo.pathname === "/") writeFromFile(req, res, "/index.html");
    else if (urlinfo.pathname === "/js/client.js") writeFromFile(req, res, "/js/client.js");
    else if (urlinfo.pathname === "/monitor") writeFromFile(req, res, "/monitor.html");
    else if (urlinfo.pathname === "/js/monitor.js") writeFromFile(req, res, "/js/monitor.js");
}

//ڑ

//Z_[
io.of('/send').on('connection', function (socket) {
    console.log('sender: ' + socket.id + ': connected');
    //ėpbZ[W
    socket.on('info', function (message) {
        console.log('sender: ' + socket.id + ': ' + JSON.stringify(message));
    });
    //oNGXg
    socket.on('emerge', function (message) {
        console.log('sender: ' + socket.id + ': emerge request: ' + JSON.stringify(message));
        if (createBey(socket.id, 30)) {
            console.log('sender: ' + socket.id + ': emerge request accepted: ' + JSON.stringify(message));
            socket.emit('responce', true);
        } else {
            console.log('sender: ' + socket.id + ': emerge request denied: ' + JSON.stringify(message));
            socket.emit('responce', false);
        }
    });
    //NGXg
    socket.on('exit', function (message) {
        console.log('sender: ' + socket.id + ': exit request: ' + JSON.stringify(message));
        if (removeBey(socket.id)) {
            console.log('sender: ' + socket.id + ': exit request accepted: ' + JSON.stringify(message));
            socket.emit('responce', true);
        } else {
            console.log('sender: ' + socket.id + ': exit request denied: ' + JSON.stringify(message));
            socket.emit('responce', false);
        }
    });
    //ZT̍XV
    socket.on('sensor', function (sensor) {
        //console.log('sender: ' + socket.id + ': recieved sensor info: ' + JSON.stringify(sensor));
        updateSensorInfo(socket.id, JSON.parse(sensor));
    })
    //ؒf
    socket.on('disconnect', function () {
        console.log('sender: ' + socket.id + ': disconnected');
        removeBey(socket.id);
    });
});

//j^[
io.of('/monitor').on('connection', function (socket) {
    console.log('monitor: ' + socket.id + ': connected');
    //ėpbZ[W
    socket.on('info', function (message) {
        //console.log('monitor: ' + socket.id + ': ' + JSON.stringify(message));
    });
    //ؒf
    socket.on('disconnect', function () {
        console.log('monitor: ' + socket.id + ': disconnected');
    });
});

//M[v
var renderRoop = function () {
    setInterval(function () {
        sendToMonitor();
    }, 1000.0 / renderFPS);
};

//xCXV[v
var worldRoop = function () {
    setInterval(function () {
        updateBeys();
    }, 1000.0 / worldFPS);
};


//xC̈ʒuXV
var updateBeys = function () {
    beyList.forEach(function (bey) {
        var accelPolar = rectToPolar(bey.accel);
        //Cɂ錸
        if (accelPolar.r >= friction) bey.accel = polarToRect(accelPolar.r - friction, accelPolar.theta);
        else bey.accel = [0, 0];
        //ZT[l𔽉fĉ
        bey.accel[0] += -bey.sensor.x * speed;
        bey.accel[1] += bey.sensor.y * speed;
        bey.point[0] += bey.accel[0] / worldFPS;
        bey.point[1] += bey.accel[1] / worldFPS;

        //Փˏ
        beyList.forEach(function (objBey) {
            if (bey.session > objBey.session) {
                if (distanceBetween(bey.point, objBey.point) < bey.size + objBey.size) {
                    // bey猩objBeÿʒu
                    var objDirection = rectToPolar([objBey.point[0] - bey.point[0], objBey.point[1] - bey.point[1]]);
                    //x@ƕsɕ
                    var resolvedBeyAcc = polarToRect(rectToPolar(bey.accel).r, rectToPolar(bey.accel).theta - objDirection.theta);
                    var resolvedObjAcc = polarToRect(rectToPolar(objBey.accel).r, rectToPolar(objBey.accel).theta - objDirection.theta);
                    //
                    var repulsePower = polarToRect(repulse, objDirection.theta);

                    //objBeŷďdԂ
                    objBey.point = [bey.point[0] + (objBey.point[0] - bey.point[0]) / objDirection.r * (bey.size + objBey.size) * 1.02,
                        bey.point[1] + (objBey.point[1] - bey.point[1]) / objDirection.r * (bey.size + objBey.size) * 1.02];

                    /*
                    Փˌ̑xȉ̐ŉZB
                    \[\begin{cases}m_{1}v_{1}'+m_{2}v_{2}'=m_{1}v_{1}+m_{2}v_{2}\\\left(v_{1}'-v_{2}'\right)=-e\left(v_{1}-v_{2}\right)\end{cases}\]
                    \[\begin{cases}v_{1}'=v_{1}-\left(v_{1}-v_{2}\right)\left(1+e\right)\frac{m_{2}}{m_{1}+m_{2}}\\v_{2}'=v_{2}+\left(v_{1}-v_{2}\right)\left(1+e\right)\frac{m_{1}}{m_{1}+m_{2}}\end{cases}\]
                    */

                    var newResolvedBeyAccY = resolvedBeyAcc[1] - (resolvedBeyAcc[1] - resolvedObjAcc[1]) * (1 + restCoeff) * (objBey.weight / (bey.weight + objBey.weight));
                    var newResolvedObjAccY = resolvedObjAcc[1] + (resolvedBeyAcc[1] - resolvedObjAcc[1]) * (1 + restCoeff) * (bey.weight / (bey.weight + objBey.weight));

                    resolvedBeyAcc[1] = newResolvedBeyAccY;
                    resolvedObjAcc[1] = newResolvedObjAccY;

                    bey.accel = polarToRect(rectToPolar(resolvedBeyAcc).r, rectToPolar(resolvedBeyAcc).theta + objDirection.theta);
                    objBey.accel = polarToRect(rectToPolar(resolvedObjAcc).r, rectToPolar(resolvedObjAcc).theta + objDirection.theta);
                }
            }
        })
        if (distanceBetween(bey.point, [0, 0]) > fieldSize - bey.size) {
            bey.point = polarToRect((fieldSize - bey.size) * 2 - distanceBetween(bey.point, [0, 0]), rectToPolar(bey.point).theta);
            bey.accel = polarToRect(distanceBetween(bey.accel, [0, 0]), 2 * rectToPolar(bey.point).theta - rectToPolar(bey.accel).theta + Math.PI);
        }
    })
};

//j^ɑM
var sendToMonitor = function () {<commit_msg>変数名を置き換え<commit_after>var app = require('http').createServer(handler), io = require('socket.io').listen(app), fs = require('fs');

var renderFPS = 60;
var worldFPS = 180;
var fieldState;
var beyList = new Array();
var maxBeys = 10;
var fieldSize = 500;
var speed = 3;
var repulse = 100;
var friction = 1;
var restCoeff = 1; //W

//xCIuWFNg
var BeyObject = function (point, size, session) {
    this.point = point;
    this.size = size;
    this.session = session;
    this.speed = [0, 0];
    this.sensor = { 'x': 0, 'y': 0, 'z': 0 };
    this.weight = 100;
}

function sessionExistsInBeyList(session) {
    for (i = 0; i < beyList.length; i++) {
        if (session == beyList[i].session) {
            return true;
        }
    }
    return false;
}

//xCo
function createBey(session, size) {
    var emergePoint = false;
    if (beyList.length >= maxBeys || sessionExistsInBeyList(session)) {
        return false;
    }
    //đ̃xCƂԂȂʒuȂ瑱s
    //n񎎍să_Ȃ߂
    for (i = 0; i < 100; i++) {
        var radius = Math.random() * fieldSize * 0.5;
        var theta = Math.random() * Math.PI * 2;
        var pointByRect = [radius * Math.sin(theta), radius * Math.cos(theta)];
        var emergeFlag = true;
        for (j = 0; j < beyList.length; j++) {
            if (distanceBetween(beyList[j].point, pointByRect) < beyList[j].size + size) {
                emergeFlag = false;
                break;
            }
        }
        if (emergeFlag) {
            emergePoint = pointByRect;
            break;
        }
    }
    if (emergePoint == false) {
        return false;
    }
    var bey = new BeyObject(emergePoint, size, session);
    beyList.push(bey);
    return true;
}

//xC
function removeBey(session) {
    for (i = 0; i < beyList.length; i++) {
        if (session == beyList[i].session) {
            beyList.splice(i, 1);
            return true;
        }
    }
    return false;
}

//ZT̍XV
function updateSensorInfo(session, sensor) {
    for (i = 0; i < beyList.length; i++) {
        if (session == beyList[i].session) {
            beyList[i].sensor = sensor;
            return true;
        }
    }
    return false;
}

function distanceBetween(p1, p2) {
    return Math.sqrt((p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]));
}

function vectorAddition(v1, v2, weight) {
    return [v1[0] + v2[0] * weight, v1[1] + v2[1] * weight];
}

function polarToRect(r, theta) {
    return [r * Math.sin(theta), r * Math.cos(theta)];
}

function rectToPolar(point) {
    return { "r": distanceBetween(point, [0, 0]), "theta": (point[1] == 0) ? 0 : ((point[1] > 0) ? Math.atan(point[0] / point[1]) : (Math.atan(point[0] / point[1]) + Math.PI)) };
}

//t@CǂݍŃX|X
var writeFromFile = function (req, res, locate) {
    fs.readFile(__dirname + locate, function (err, data) {
        if (err) {
            res.writeHead(500, {
                'Content-Type': 'text/plain'
            });
            res.write('Error loading ' + locate);
            res.end();
            console.log("not found " + locate);
            return;
        }

        res.writeHead(200, {
            'Content-Type': 'text/html; charset=utf-8'
        });
        res.write(data);
        res.end();
        console.log("served " + locate);
    });
}

app.listen(8080);

//Kvȃt@C
function handler (req, res) {
    var urlinfo = require('url').parse(req.url, true);
    if (urlinfo.pathname === "/") writeFromFile(req, res, "/index.html");
    else if (urlinfo.pathname === "/js/client.js") writeFromFile(req, res, "/js/client.js");
    else if (urlinfo.pathname === "/monitor") writeFromFile(req, res, "/monitor.html");
    else if (urlinfo.pathname === "/js/monitor.js") writeFromFile(req, res, "/js/monitor.js");
}

//ڑ

//Z_[
io.of('/send').on('connection', function (socket) {
    console.log('sender: ' + socket.id + ': connected');
    //ėpbZ[W
    socket.on('info', function (message) {
        console.log('sender: ' + socket.id + ': ' + JSON.stringify(message));
    });
    //oNGXg
    socket.on('emerge', function (message) {
        console.log('sender: ' + socket.id + ': emerge request: ' + JSON.stringify(message));
        if (createBey(socket.id, 30)) {
            console.log('sender: ' + socket.id + ': emerge request accepted: ' + JSON.stringify(message));
            socket.emit('responce', true);
        } else {
            console.log('sender: ' + socket.id + ': emerge request denied: ' + JSON.stringify(message));
            socket.emit('responce', false);
        }
    });
    //NGXg
    socket.on('exit', function (message) {
        console.log('sender: ' + socket.id + ': exit request: ' + JSON.stringify(message));
        if (removeBey(socket.id)) {
            console.log('sender: ' + socket.id + ': exit request accepted: ' + JSON.stringify(message));
            socket.emit('responce', true);
        } else {
            console.log('sender: ' + socket.id + ': exit request denied: ' + JSON.stringify(message));
            socket.emit('responce', false);
        }
    });
    //ZT̍XV
    socket.on('sensor', function (sensor) {
        //console.log('sender: ' + socket.id + ': recieved sensor info: ' + JSON.stringify(sensor));
        updateSensorInfo(socket.id, JSON.parse(sensor));
    })
    //ؒf
    socket.on('disconnect', function () {
        console.log('sender: ' + socket.id + ': disconnected');
        removeBey(socket.id);
    });
});

//j^[
io.of('/monitor').on('connection', function (socket) {
    console.log('monitor: ' + socket.id + ': connected');
    //ėpbZ[W
    socket.on('info', function (message) {
        //console.log('monitor: ' + socket.id + ': ' + JSON.stringify(message));
    });
    //ؒf
    socket.on('disconnect', function () {
        console.log('monitor: ' + socket.id + ': disconnected');
    });
});

//M[v
var renderRoop = function () {
    setInterval(function () {
        sendToMonitor();
    }, 1000.0 / renderFPS);
};

//xCXV[v
var worldRoop = function () {
    setInterval(function () {
        updateBeys();
    }, 1000.0 / worldFPS);
};


//xC̈ʒuXV
var updateBeys = function () {
    beyList.forEach(function (bey) {
        var speedPolar = rectToPolar(bey.speed);
        //Cɂ錸
        if (speedPolar.r >= friction) bey.speed = polarToRect(speedPolar.r - friction, speedPolar.theta);
        else bey.speed = [0, 0];
        //ZT[l𔽉fĉ
        bey.speed[0] += -bey.sensor.x * speed;
        bey.speed[1] += bey.sensor.y * speed;
        bey.point[0] += bey.speed[0] / worldFPS;
        bey.point[1] += bey.speed[1] / worldFPS;

        //Փˏ
        beyList.forEach(function (objBey) {
            if (bey.session > objBey.session) {
                if (distanceBetween(bey.point, objBey.point) < bey.size + objBey.size) {
                    // bey猩objBeÿʒu
                    var objDirection = rectToPolar([objBey.point[0] - bey.point[0], objBey.point[1] - bey.point[1]]);
                    //x@ƕsɕ
                    var resolvedBeySpd = polarToRect(rectToPolar(bey.speed).r, rectToPolar(bey.speed).theta - objDirection.theta);
                    var resolvedObjSpd = polarToRect(rectToPolar(objBey.speed).r, rectToPolar(objBey.speed).theta - objDirection.theta);
                    //
                    var repulsePower = polarToRect(repulse, objDirection.theta);

                    //objBeŷďdԂ
                    objBey.point = [bey.point[0] + (objBey.point[0] - bey.point[0]) / objDirection.r * (bey.size + objBey.size) * 1.02,
                        bey.point[1] + (objBey.point[1] - bey.point[1]) / objDirection.r * (bey.size + objBey.size) * 1.02];

                    /*
                    Փˌ̑xȉ̐ŉZB
                    \[\begin{cases}m_{1}v_{1}'+m_{2}v_{2}'=m_{1}v_{1}+m_{2}v_{2}\\\left(v_{1}'-v_{2}'\right)=-e\left(v_{1}-v_{2}\right)\end{cases}\]
                    \[\begin{cases}v_{1}'=v_{1}-\left(v_{1}-v_{2}\right)\left(1+e\right)\frac{m_{2}}{m_{1}+m_{2}}\\v_{2}'=v_{2}+\left(v_{1}-v_{2}\right)\left(1+e\right)\frac{m_{1}}{m_{1}+m_{2}}\end{cases}\]
                    */

                    var newResolvedBeySpdY = resolvedBeySpd[1] - (resolvedBeySpd[1] - resolvedObjSpd[1]) * (1 + restCoeff) * (objBey.weight / (bey.weight + objBey.weight));
                    var newResolvedObjSpdY = resolvedObjSpd[1] + (resolvedBeySpd[1] - resolvedObjSpd[1]) * (1 + restCoeff) * (bey.weight / (bey.weight + objBey.weight));

                    resolvedBeySpd[1] = newResolvedBeySpdY;
                    resolvedObjSpd[1] = newResolvedObjSpdY;

                    bey.speed = polarToRect(rectToPolar(resolvedBeySpd).r, rectToPolar(resolvedBeySpd).theta + objDirection.theta);
                    objBey.speed = polarToRect(rectToPolar(resolvedObjSpd).r, rectToPolar(resolvedObjSpd).theta + objDirection.theta);
                }
            }
        })
        if (distanceBetween(bey.point, [0, 0]) > fieldSize - bey.size) {
            bey.point = polarToRect((fieldSize - bey.size) * 2 - distanceBetween(bey.point, [0, 0]), rectToPolar(bey.point).theta);
            bey.speed = polarToRect(distanceBetween(bey.speed, [0, 0]), 2 * rectToPolar(bey.point).theta - rectToPolar(bey.speed).theta + Math.PI);
        }
    })
};

//j^ɑM
var sendToMonitor = function () {
{'fraction_non_alphanumeric': 0.17666539985884142, 'fraction_numerical': 0.01835061621152071, 'mean_word_length': 2.248104390760007, 'pattern_counts': {'":': 4, '<': 17, '<?xml version=': 0, '>': 13, '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': '5460042', 'n_tokens_mistral': 6699, 'n_tokens_neox': 6355, 'n_words': 1154}