schmic / ac-server-ctrl

Assetto Corsa Server Control
Other
4 stars 0 forks source link

Laptimes parser: adding cuts to lap. #2

Open Pitbi opened 9 years ago

Pitbi commented 9 years ago

Hello guys,

First, I want to tell you that ac-server-ctrl and ac-monitor is a really good job. I have a proposition for adding track driver cuts in lap times parser. I think is a important information.

Sorry for using issue but I did not create a branch for a pull-request.

/libs/parsers/laptimes.js

 var acEvents    = require('../server-events');

var acRE = {};
acRE.newLap  = /^Car.onLapCompleted$/;
acRE.endLap  = /^Car.onLapCompleted END$/;
acRE.lapTime = /^LAP (.*) (\d+:\d+:\d+)$/;
acRE.cuts = /^Result.OnLapCompleted.\sCuts:\s(\d)/;

var lap = {};

module.exports = function (server, line, cb) {
    var toSeconds = function (timeStr) {
        var timeParts = timeStr.split(':');
        return parseInt(timeParts[0]) * 60 + parseInt(timeParts[1]) + parseInt(timeParts[2]) / 1000;
    };

    var driverByName = function(session, name) {
        var driver = undefined;
        Object.keys(session.drivers).some(function(k) {
            var nameRE = new RegExp(name, 'i');
            if(session.drivers[k].DRIVERNAME.match(nameRE)) {
                driver = session.drivers[k];
                return true;
            }
        });
        return driver;
    };

    if (acRE.newLap.test(line)) {
        lap = {};
    }

    if (acRE.lapTime.test(line)) {
        var matches = line.match(acRE.lapTime);
        var lapTime = toSeconds(matches.pop());
        var driver = driverByName(server.session, matches.pop());

        lap.driver      = driver.DRIVERNAME;
        lap.car         = driver.MODEL;
        lap.track       = server.session.track;
        lap.trackConfig = server.session.trackConfig;
        lap.laptime     = lapTime;
        lap.time        = new Date().toISOString();
        lap.session     = server.session.type;

        var bestLap = server.session.laptimes[driver.SID];

        if (bestLap === undefined || bestLap.laptime > lap.laptime) {
            server.session.laptimes[driver.SID] = lap;
            server.emit(acEvents.lap.best, lap);
        }
    }

    if (acRE.cuts.test(line)) {
        lap.cuts = line[line.length - 1];
    }

    if (acRE.endLap.test(line)) {
        server.emit(acEvents.lap.time, lap);
        lap = {};
    }

    cb(null, line);
};

Hoping that you like it.

Bye.

Pitbi commented 9 years ago

Hey, i do this too late in the night.

The method to get cuts number is not good. it would be better to match number. Because if cuts are 10, my method return 0.

I have not tested but it should be better:

...

acRE.cuts = /^Result.OnLapCompleted.\sCuts:\s(\d+)$/;

...

if (acRE.cuts.test(line)) {
    var match = line.match(acRE.cuts);
    lap.cuts = match.pop();
}

Bye.

nemaides commented 8 years ago

Hey Pitbi, you seem to know your javascript, i'm trying to ask you since the original developer haven't been active on github for over 6 months now, i have this issue: https://github.com/schmic/ac-monitor/issues/21 which i posted on the ac-monitor as i didn't notice it had it's own seperate git, but do you have any idea why i'm having these issues? And does yours work fine, if you're still using it ?

Pitbi commented 8 years ago

Hey,

I don't know if this is a known issue. I do not use ac-monitor since a long time. I stopped my project for AC. Sorry can not help you.

Good luck!