frMatchViewHelper.js 2.84 KB
angular.module('fr.matchViewHelper', [])

.service('matchViewHelper', ['$interval', function ($interval) {
    function startMinutesJob(match) {
        var job;

        if (match.statusType === 'inprogress' || match.statusType === 'notstarted') {
            job = $interval(function () {
                setMinutes(match);
            }, 30000);

            setMinutes(match);
        }

        return job;
    }

    function stopMinutesJob(job) {
        if (job) {
            $interval.cancel(job);
        }
    }

    function setMinutes(match) {
        if (match.timerStart) {
            match.minute = Math.floor((new Date() - new Date(match.timerStart)) / 1000 / 60) + 1;

            if (match.status == '1st' && match.minute > 45) {
                match.minute = '45\'+';
            } else if (match.status == '2nd' && (match.minute += 45) > 90) {
                match.minute = '90\'+';
            } else {
                match.minute += '\'';
            }
        }
    }

    function setMainIncidents(match) {
        match.mainIncidents = [];
        match.homeRedCards = 0;
        match.awayRedCards = 0;

        _(match.incidents).each(function (incident) {
            if (incident.code.category === 'g' || incident.code.id === 'r' || incident.code.id === 'y2') {
                match.mainIncidents.push(incident);

                if (incident.code.id == 'r' || incident.code.id == 'y2') {
                    if (incident.side == 'home') {
                        match.homeRedCards++;
                    } else {
                        match.awayRedCards++;
                    }
                }
            }
        });
    }

    function arrangeIncidents(match) {
        var output = {
            homeGoals: [],
            awayGoals: [],
            homeCards: [],
            awayCards: [],
        };

        _(match.incidents).each(function (incident) {
            if (incident.code.id === 'ps' || incident.code.id === 'psm') {
                incident.elapsed = 'PEN';
            }

            switch (incident.code.category) {
                case 'g':
                    output[incident.side + 'Goals'].push(incident);
                    break;

                case 'c':
                    output[incident.side + 'Cards'].push(incident);
                    break;

                default:
                    break;
            }
        });

        return output;
    }

    function setScore(match) {
        if (match.homeScore === undefined) {
            match.homeScore = 0;
        }

        if (match.awayScore === undefined) {
            match.awayScore = 0;
        }
    }

    return {
        startMinutesJob: startMinutesJob,
        stopMinutesJob: stopMinutesJob,
        setMinutes: setMinutes,
        setMainIncidents: setMainIncidents,
        arrangeIncidents: arrangeIncidents,
        setScore: setScore,
    };
}]);