<commit_before>import React, { Component } from 'react';
// import Header from './Header';
import Menu from './Menu';

export default class App extends Component {
    constructor(props) {
        super(props);
        this.state = {
            tabSelected: 1
        }
    }

    shouldComponentUpdate(nextProps, nextState) {
        return !((nextProps === this.props) && (nextState === this.state));
    }

    componentDidMount() {
        this.setState({
            tabSelected: this.props.selectedTab
        });
    }

    handleTabClick = event => {
        let tab = 1;
        if (event.target.classList.contains('two')) { tab = 2; }
        this.setState({
            tabSelected: tab
        });
        this.props.onTabSelect(tab);
    }
    
    findMeet = thisMeetIndex => {
        return this.props.tips.find(tipsMeet => { return tipsMeet.meetId === this.props.raceMeets[thisMeetIndex].meetId });
    }

    findRace = (tipMeet, thisMeetIndex, thisRaceIndex) => {
        return tipMeet.races.find(tipsRace => { return tipsRace.number === this.props.raceMeets[thisMeetIndex].races[thisRaceIndex].number });
    }

    findPunter = punterId => {
        return this.props.punters.find(punter => { return punter.punterId === punterId });
    }

    findPuntersStats = (stats, punterId) => {
        return stats.find(punter => { return punter.punterId === punterId });
    }

    findPuntersStatsMeet = (puntersStats, meetId) => {
        return puntersStats.meets.find(meet => { return meet.meetId === meetId });
    }

    calculateStats = () => {
        let stats = [],
            a = this.props.punters.length,
            b = this.props.raceMeets.length,
            meetsArray = [],
            c, d, placings, tipMeet, tips, puntersStats, puntersStatsMeet, raceScore;

        // Create a meets array to hold the score for each meet.  This will be added to each punters stats.
        while (b--) {
            meetsArray.push({ "meetId": this.props.raceMeets[b].meetId, "score": 0 });
        }

        // Load each punter into the points array with a score of 0
        while (a--) {
            stats.push({
                "punterId": this.props.punters[a].punterId,
                "trifectas": 0,
                "quinellas": 0,
                "firsts": 0,
                "seconds": 0,
                "thirds": 0,
                "meets": meetsArray.map(a => ({...a})) // Use map and the spread operator to clone the array with cloned values, instead of references
            });
        }

        // Reset the length of the race meets after the first while loop
        b = this.props.raceMeets.length

        // For each meet
        while (b--) {
            c = this.props.raceMeets[b].races.length;
            // For each race in meet
            while (c--) {
                // Set placings to the placings for this race
                placings = this.props.raceMeets[b].races[c].placings;
                // Find race meet in tips, and then get the associated tips
                tipMeet = this.findMeet(b);
                tips = this.findRace(tipMeet, b, c);
                d = tips.punters.length;
                // For each punters tips
                while (d--) {
                    // Reset the race score
                    raceScore = 0;
                    // Get the current punter's stats from the stats array
                    puntersStats = this.findPuntersStats(stats, tips.punters[d].punterId);
                    // Get the current race meet from the current punter's stats
                    puntersStatsMeet = this.findPuntersStatsMeet(puntersStats, this.props.raceMeets[b].meetId);
                    // Update the stats of that punter
                    if (tips.punters[d].tips.includes(placings.first)) { puntersStats.firsts++; raceScore += 3; }
                    if (tips.punters[d].tips.includes(placings.second)) { puntersStats.seconds++; raceScore += 2; }
                    if (tips.punters[d].tips.includes(placings.third)) { puntersStats.thirds++; raceScore += 1; }
                    if (raceScore === 6) { puntersStats.trifectas++; }
                    if (raceScore === 5) { puntersStats.quinellas++; }
                    // Add the race score to this meets score
                    puntersStatsMeet.score += raceScore;
                }
            }
        }

        return stats;
    }

    createStatArray = (stats, arrayToUpdate, arrayToUpdateName) => {
        let stat, punter;
        
        for (let i = 0, len = stats.length; i < len; i++) {
            // Set the correct stat data according to the array to update
            switch (arrayToUpdateName) {
                case 'trifectas': stat = stats[i].trifectas; break;
                case 'quinellas': stat = stats[i].quinellas; break;
                case 'firsts': stat = stats[i].firsts; break;
                case 'seconds': stat = stats[i].seconds; break;
                case 'thirds': stat = stats[i].thirds; break;
                default: break;
            }
            // Get the current punters details
            punter = this.findPunter(stats[i].punterId);
            // Create the stat item and add to the array to update
            arrayToUpdate.push(<div key={i} className="stat-item">
                                   <img src={'pics/' + punter.pic} alt="Profile pic" className="pic" />
                                   <div className="name">{punter.name.first}</div>
                                   <div className="stat">{stat}</div>
                               </div>);
        }
    }

    render() {
        let stats = this.calculateStats(),
            trifectas = [], quinellas = [], firsts = [], seconds = [], thirds = [],
            tabBtn1Class, tabBtn2Class, tab1Class, tab2Class;
        console.log(stats);

        // Sort punters in descending order by trifectas
        stats.sort((a, b) => { return b.trifectas - a.trifectas; });

        // Create list of punters in order of trifectas
        this.createStatArray(stats, trifectas, 'trifectas');

        // Sort punters in descending order by quinellas
        stats.sort((a, b) => { return b.quinellas - a.quinellas; });

        // Create list of punters in order of quinellas
        this.createStatArray(stats, quinellas, 'quinellas');

        // Sort punters in descending order by firsts
        stats.sort((a, b) => { return b.firsts - a.firsts; });

        // Create list of punters in order of firsts
        this.createStatArray(stats, firsts, 'firsts');

        // Sort punters in descending order by seconds
        stats.sort((a, b) => { return b.seconds - a.seconds; });

        // Create list of punters in order of firsts
        this.createStatArray(stats, seconds, 'seconds');

        // Sort punters in descending order by thirds
        stats.sort((a, b) => { return b.thirds - a.thirds; });

        // Create list of punters in order of firsts
        this.createStatArray(stats, thirds, 'thirds');

        // Set the tab status and hide/show the tabs
        if (this.state.tabSelected === 1) {
            tabBtn1Class = 'btn tab-btn one selected';
            tabBtn2Class = 'btn tab-btn two';
            tab1Class = '';
            tab2Class = 'hide';
        }
        else {
            tabBtn1Class = 'btn tab-btn one';
            tabBtn2Class = 'btn tab-btn two selected';
            tab1Class = 'hide';
            tab2Class = '';
        }

        return (
            <div className="app">
                {/* <Header page="Statistics" path={this.props.path} punters={this.props.punters} user={this.props.user} onReloadData={this.props.onReloadData} isAdmin={this.props.isAdmin} text="Here you can find various statistics, such as the placings for each race meet, number of trifectas, quinellas, 1sts, 2nds, 3rds, etc." /> */}
                <div className="tab-btns">
                    <button className={tabBtn1Class} onClick={this.handleTabClick}>Meets</button>
                    <button className={tabBtn2Class} onClick={this.handleTabClick}>Other</button>
                </div>
                <div className={tab1Class}>
                    <div>This is the Meets tab.</div>
                </div>
                <div className={tab2Class}>
                    <div className="bold mt-20 mb-10">Trifectas</div>
                    <div className="stat-container">{trifectas}</div>
                    <div className="bold mt-20 mb-10">Quinellas</div>
                    <div className="stat-container">{quinellas}</div>
                    <div className="bold mt-20 mb-10">Firsts</div>
                    <div className="stat-container">{firsts}</div>
                    <div className="bold mt-20 mb-10">Seconds</div>
                    <div className="stat-container">{seconds}</div>
                    <div className="bold mt-20 mb-10">Thirds</div>
                    <div className="stat-container">{thirds}</div>
                </div><commit_msg>Got the scores for every meet displaying on the Statistics page.<commit_after>import React, { Component } from 'react';
import Header from './Header';
import Menu from './Menu';

export default class App extends Component {
    constructor(props) {
        super(props);
        this.state = {
            tabSelected: 1
        }
    }

    shouldComponentUpdate(nextProps, nextState) {
        return !((nextProps === this.props) && (nextState === this.state));
    }

    componentDidMount() {
        this.setState({
            tabSelected: this.props.selectedTab
        });
    }

    handleTabClick = event => {
        let tab = 1;
        if (event.target.classList.contains('two')) { tab = 2; }
        this.setState({
            tabSelected: tab
        });
        this.props.onTabSelect(tab);
    }
    
    findMeet = thisMeetIndex => {
        return this.props.tips.find(tipsMeet => { return tipsMeet.meetId === this.props.raceMeets[thisMeetIndex].meetId });
    }

    findRace = (tipMeet, thisMeetIndex, thisRaceIndex) => {
        return tipMeet.races.find(tipsRace => { return tipsRace.number === this.props.raceMeets[thisMeetIndex].races[thisRaceIndex].number });
    }

    findPunter = punterId => {
        return this.props.punters.find(punter => { return punter.punterId === punterId });
    }

    findPuntersStats = (stats, punterId) => {
        return stats.find(punter => { return punter.punterId === punterId });
    }

    findPuntersStatsMeet = (puntersStats, meetId) => {
        return puntersStats.meets.find(meet => { return meet.meetId === meetId });
    }

    calculateStats = () => {
        let stats = [],
            a = this.props.punters.length,
            b = this.props.raceMeets.length,
            meetsArray = [],
            c, d, placings, tipMeet, tips, puntersStats, puntersStatsMeet, raceScore;

        // Create a meets array to hold the score for each meet.  This will be added to each punters stats.
        while (b--) {
            meetsArray.push({ "meetId": this.props.raceMeets[b].meetId, "score": 0 });
        }

        // Load each punter into the points array with a score of 0
        while (a--) {
            stats.push({
                "punterId": this.props.punters[a].punterId,
                "trifectas": 0,
                "quinellas": 0,
                "firsts": 0,
                "seconds": 0,
                "thirds": 0,
                "meets": meetsArray.map(a => ({...a})) // Use map and the spread operator to clone the array with cloned values, instead of references
            });
        }

        // Reset the length of the race meets after the first while loop
        b = this.props.raceMeets.length

        // For each meet
        while (b--) {
            c = this.props.raceMeets[b].races.length;
            // For each race in meet
            while (c--) {
                // Set placings to the placings for this race
                placings = this.props.raceMeets[b].races[c].placings;
                // Find race meet in tips, and then get the associated tips
                tipMeet = this.findMeet(b);
                tips = this.findRace(tipMeet, b, c);
                d = tips.punters.length;
                // For each punters tips
                while (d--) {
                    // Reset the race score
                    raceScore = 0;
                    // Get the current punter's stats from the stats array
                    puntersStats = this.findPuntersStats(stats, tips.punters[d].punterId);
                    // Get the current race meet from the current punter's stats
                    puntersStatsMeet = this.findPuntersStatsMeet(puntersStats, this.props.raceMeets[b].meetId);
                    // Update the stats of that punter
                    if (tips.punters[d].tips.includes(placings.first)) { puntersStats.firsts++; raceScore += 3; }
                    if (tips.punters[d].tips.includes(placings.second)) { puntersStats.seconds++; raceScore += 2; }
                    if (tips.punters[d].tips.includes(placings.third)) { puntersStats.thirds++; raceScore += 1; }
                    if (raceScore === 6) { puntersStats.trifectas++; }
                    if (raceScore === 5) { puntersStats.quinellas++; }
                    // Add the race score to this meets score
                    puntersStatsMeet.score += raceScore;
                }
            }
        }

        return stats;
    }

    createStatArray = (stats, arrayToUpdate, arrayToUpdateName) => {
        let stat, punter;
        
        for (let i = 0, len = stats.length; i < len; i++) {
            // Set the correct stat data according to the array to update
            switch (arrayToUpdateName) {
                case 'trifectas': stat = stats[i].trifectas; break;
                case 'quinellas': stat = stats[i].quinellas; break;
                case 'firsts': stat = stats[i].firsts; break;
                case 'seconds': stat = stats[i].seconds; break;
                case 'thirds': stat = stats[i].thirds; break;
                case 'meet': stat = stats[i].score; break;
                default: break;
            }
            // Get the current punters details
            punter = this.findPunter(stats[i].punterId);
            // Create the stat item and add to the array to update
            arrayToUpdate.push(<div key={i} className="stat-item">
                                   <img src={'pics/' + punter.pic} alt="Profile pic" className="pic" />
                                   <div className="name">{punter.name.first}</div>
                                   <div className="stat">{stat}</div>
                               </div>);
        }
    }

    render() {
        let stats = this.calculateStats(),
            caulGuineasTemp = [], caulCupTemp = [], coxPlateTemp = [], derbyDayTemp = [], melbCupTemp = [], oaksDayTemp = [], stakesDayTemp = [],
            caulGuineas = [], caulCup = [], coxPlate = [], derbyDay = [], melbCup = [], oaksDay = [], stakesDay = [],
            trifectas = [], quinellas = [], firsts = [], seconds = [], thirds = [],
            tabBtn1Class, tabBtn2Class, tab1Class, tab2Class;
        //console.log(stats);

        // Set the tab status and hide/show the tabs
        if (this.state.tabSelected === 1) {
            tabBtn1Class = 'btn tab-btn one selected';
            tabBtn2Class = 'btn tab-btn two';
            tab1Class = '';
            tab2Class = 'hide';
        }
        else {
            tabBtn1Class = 'btn tab-btn one';
            tabBtn2Class = 'btn tab-btn two selected';
            tab1Class = 'hide';
            tab2Class = '';
        }

        // Populate each of the temporary meet arrays with each punters score for that meet
        for (let i = 0, len = stats.length; i < len; i++) {
            for (let j = 0, leng = stats[i].meets.length; j < leng; j++) {
                if (stats[i].meets[j].meetId === 'CAULGUINEAS') { caulGuineasTemp.push({ "punterId": stats[i].punterId, "score": stats[i].meets[j].score }); }
                if (stats[i].meets[j].meetId === 'CAULCUP') { caulCupTemp.push({ "punterId": stats[i].punterId, "score": stats[i].meets[j].score }); }
                if (stats[i].meets[j].meetId === 'COXPLATE') { coxPlateTemp.push({ "punterId": stats[i].punterId, "score": stats[i].meets[j].score }); }
                if (stats[i].meets[j].meetId === 'DERBYDAY') { derbyDayTemp.push({ "punterId": stats[i].punterId, "score": stats[i].meets[j].score }); }
                if (stats[i].meets[j].meetId === 'MELBCUP') { melbCupTemp.push({ "punterId": stats[i].punterId, "score": stats[i].meets[j].score }); }
                if (stats[i].meets[j].meetId === 'OAKSDAY') { oaksDayTemp.push({ "punterId": stats[i].punterId, "score": stats[i].meets[j].score }); }
                if (stats[i].meets[j].meetId === 'STAKESDAY') { stakesDayTemp.push({ "punterId": stats[i].punterId, "score": stats[i].meets[j].score }); }
            }
        }
        
        // Sort punters in descending order by their score in the Caulfield Guineas
        caulGuineasTemp.sort((a, b) => { return b.score - a.score; });

        // Create list of punters for the Caulfield Guineas
        this.createStatArray(caulGuineasTemp, caulGuineas, 'meet');
        
        // Sort punters in descending order by their score in the Caulfield Cup
        caulCupTemp.sort((a, b) => { return b.score - a.score; });

        // Create list of punters for the Caulfield Cup
        this.createStatArray(caulCupTemp, caulCup, 'meet');
        
        // Sort punters in descending order by their score in the Cox Plate
        coxPlateTemp.sort((a, b) => { return b.score - a.score; });

        // Create list of punters for the Cox Plate
        this.createStatArray(coxPlateTemp, coxPlate, 'meet');
        
        // Sort punters in descending order by their score in the Derby Day
        derbyDayTemp.sort((a, b) => { return b.score - a.score; });

        // Create list of punters for the Derby Day
        this.createStatArray(derbyDayTemp, derbyDay, 'meet');
        
        // Sort punters in descending order by their score in the Melbourne Cup
        melbCupTemp.sort((a, b) => { return b.score - a.score; });

        // Create list of punters for the Melbourne Cup
        this.createStatArray(melbCupTemp, melbCup, 'meet');
        
        // Sort punters in descending order by their score in the Oaks Day
        oaksDayTemp.sort((a, b) => { return b.score - a.score; });

        // Create list of punters for the Oaks Day
        this.createStatArray(oaksDayTemp, oaksDay, 'meet');
        
        // Sort punters in descending order by their score in the Stakes Day
        stakesDayTemp.sort((a, b) => { return b.score - a.score; });

        // Create list of punters for the Stakes Day
        this.createStatArray(stakesDayTemp, stakesDay, 'meet');



        // Sort punters in descending order by trifectas
        stats.sort((a, b) => { return b.trifectas - a.trifectas; });

        // Create list of punters in order of trifectas
        this.createStatArray(stats, trifectas, 'trifectas');

        // Sort punters in descending order by quinellas
        stats.sort((a, b) => { return b.quinellas - a.quinellas; });

        // Create list of punters in order of quinellas
        this.createStatArray(stats, quinellas, 'quinellas');

        // Sort punters in descending order by firsts
        stats.sort((a, b) => { return b.firsts - a.firsts; });

        // Create list of punters in order of firsts
        this.createStatArray(stats, firsts, 'firsts');

        // Sort punters in descending order by seconds
        stats.sort((a, b) => { return b.seconds - a.seconds; });

        // Create list of punters in order of firsts
        this.createStatArray(stats, seconds, 'seconds');

        // Sort punters in descending order by thirds
        stats.sort((a, b) => { return b.thirds - a.thirds; });

        // Create list of punters in order of firsts
        this.createStatArray(stats, thirds, 'thirds');



        return (
            <div className="app">
                <Header page="Statistics" path={this.props.path} punters={this.props.punters} user={this.props.user} onReloadData={this.props.onReloadData} isAdmin={this.props.isAdmin} text="Here you can find various statistics, such as the placings for each race meet, number of trifectas, quinellas, 1sts, 2nds, 3rds, etc." />
                <div className="tab-btns">
                    <button className={tabBtn1Class} onClick={this.handleTabClick}>Meets</button>
                    <button className={tabBtn2Class} onClick={this.handleTabClick}>Other</button>
                </div>
                <div className={tab1Class}>
                    <div className="bold mt-20 mb-10">Caulfield Guineas</div>
                    <div className="stat-container">{caulGuineas}</div>
                    <div className="bold mt-20 mb-10">Caulfield Cup</div>
                    <div className="stat-container">{caulCup}</div>
                    <div className="bold mt-20 mb-10">Cox Plate</div>
                    <div className="stat-container">{coxPlate}</div>
                    <div className="bold mt-20 mb-10">Derby Day</div>
                    <div className="stat-container">{derbyDay}</div>
                    <div className="bold mt-20 mb-10">Melbourne Cup</div>
                    <div className="stat-container">{melbCup}</div>
                    <div className="bold mt-20 mb-10">Oaks Day</div>
                    <div className="stat-container">{oaksDay}</div>
                    <div className="bold mt-20 mb-10">Stakes Day</div>
                    <div className="stat-container">{stakesDay}</div>
                </div>
                <div className={tab2Class}>
                    <div className="bold mt-20 mb-10">Trifectas</div>
                    <div className="stat-container">{trifectas}</div>
                    <div className="bold mt-20 mb-10">Quinellas</div>
                    <div className="stat-container">{quinellas}</div>
                    <div className="bold mt-20 mb-10">Firsts</div>
                    <div className="stat-container">{firsts}</div>
                    <div className="bold mt-20 mb-10">Seconds</div>
                    <div className="stat-container">{seconds}</div>
                    <div className="bold mt-20 mb-10">Thirds</div>
                    <div className="stat-container">{thirds}</div>
                </div>
{'fraction_non_alphanumeric': 0.12830188679245283, 'fraction_numerical': 0.006392896781354051, 'mean_word_length': 1.8531982267257758, 'pattern_counts': {'":': 32, '<': 115, '<?xml version=': 0, '>': 156, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6318813', 'n_tokens_mistral': 6783, 'n_tokens_neox': 6344, 'n_words': 1646}