<commit_before>import React, { PropTypes, Component } from 'react';
import { connect } from 'react-redux';
import Paper from 'material-ui/Paper';
import RaisedButton from 'material-ui/RaisedButton';
import Divider from 'material-ui/Divider';
import ImmutablePropTypes from 'react-immutable-proptypes';
import Chip from 'material-ui/Chip';
import SvgIconFace from 'material-ui/svg-icons/action/face';
import Avatar from 'material-ui/Avatar';
import _ from 'lodash';
import PeopleList from '../peopleList';
import { addOuting } from '../../../reducers/outing';
import { getAllPeople } from '../../../reducers/people';

class AddOuting extends Component {
  constructor(props) {
    super(props);
    this.state = {
      canSubmit: false,
      notSplurging: props.allPeople.toArray().map(person => person.toJS()).map(p => ({
        key: p.id,
        label: p.name,
        numCoffeeDrank: p.number_coffee_drank,
        numCoffeePaid: p.number_coffee_paid,
        coffeePrice: p.coffee_price || 0,
        selected: false,
      })),
      splurgingHard: [],
      luckyWinner: null,
      totalCost: 0,
    };
    this.enableButton = this.enableButton.bind(this);
    this.disableButton = this.disableButton.bind(this);
    this.submitForm = this.submitForm.bind(this);
    this.addPerson = this.addPerson.bind(this);
    this.removePerson = this.removePerson.bind(this);
    this.getWinner = this.getWinner.bind(this);
  }

  getWinner() {
    const { splurgingHard } = this.state;
    let groupings = null;

    if (_(splurgingHard).some(p => p.numCoffeePaid === 0)) {
      groupings = _(splurgingHard).filter(p => p.numCoffeePaid === 0).groupBy(p => p.numCoffeeDrank).value();
    } else {
      groupings = _(splurgingHard).groupBy(p => p.numCoffeeDrank / p.numCoffeePaid).value();
    }
    const winningGroupKey = _(Object.keys(groupings)).maxBy(key => ~~key);
    const winningIndex = Math.floor(Math.random() * groupings[winningGroupKey].length);
    const winner = groupings[winningGroupKey][winningIndex];
    let totalCost = 0;
    splurgingHard.forEach(p => (totalCost += p.coffeePrice));
    winner && (winner.selected = true);

    this.setState({
      luckyWinner: winner,
      totalCost,
    });
  }

  enableButton() {
    this.setState({
      canSubmit: true,
    });
  }

  disableButton() {
    this.setState({
      canSubmit: false,
    });
  }

  submitForm() {
    const { luckyWinner, totalCost, splurgingHard } = this.state;
    this.props.addOuting(luckyWinner.key, totalCost, splurgingHard.map(p => p.key));<commit_msg>simplified winner algorithm using difference instead of ratio, which seems to fix whatever bug was showing the wrong winner sometimes.<commit_after>import React, { PropTypes, Component } from 'react';
import { connect } from 'react-redux';
import Paper from 'material-ui/Paper';
import RaisedButton from 'material-ui/RaisedButton';
import Divider from 'material-ui/Divider';
import ImmutablePropTypes from 'react-immutable-proptypes';
import Chip from 'material-ui/Chip';
import SvgIconFace from 'material-ui/svg-icons/action/face';
import Avatar from 'material-ui/Avatar';
import _ from 'lodash';
import PeopleList from '../peopleList';
import { addOuting } from '../../../reducers/outing';
import { getAllPeople } from '../../../reducers/people';

class AddOuting extends Component {
  constructor(props) {
    super(props);
    this.state = {
      canSubmit: false,
      notSplurging: props.allPeople.toArray().map(person => person.toJS()).map(p => ({
        key: p.id,
        label: p.name,
        numCoffeeDrank: p.number_coffee_drank,
        numCoffeePaid: p.number_coffee_paid,
        coffeePrice: p.coffee_price || 0,
        selected: false,
      })),
      splurgingHard: [],
      luckyWinner: null,
      totalCost: 0,
    };
    this.enableButton = this.enableButton.bind(this);
    this.disableButton = this.disableButton.bind(this);
    this.submitForm = this.submitForm.bind(this);
    this.addPerson = this.addPerson.bind(this);
    this.removePerson = this.removePerson.bind(this);
    this.getWinner = this.getWinner.bind(this);
  }

  getWinner() {
    const { splurgingHard } = this.state;
    const groupings = _(splurgingHard).groupBy(p => p.numCoffeeDrank - p.numCoffeePaid).value();

    const winningGroupKey = _(Object.keys(groupings)).maxBy(key => ~~key);
    const winningIndex = Math.floor(Math.random() * groupings[winningGroupKey].length);
    const winner = groupings[winningGroupKey][winningIndex];

    winner && (winner.selected = true);

    let totalCost = 0;
    splurgingHard.forEach(p => (totalCost += p.coffeePrice));

    this.setState({
      luckyWinner: winner,
      totalCost,
    });
  }

  enableButton() {
    this.setState({
      canSubmit: true,
    });
  }

  disableButton() {
    this.setState({
      canSubmit: false,
    });
  }

  submitForm() {
    const { luckyWinner, totalCost, splurgingHard } = this.state;
    this.props.addOuting(luckyWinner.key, totalCost, splurgingHard.map(p => p.key));
{'fraction_non_alphanumeric': 0.1412631998406057, 'fraction_numerical': 0.0015939430165371588, 'mean_word_length': 3.4307149161518096, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 18, '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': '4659900', 'n_tokens_mistral': 1642, 'n_tokens_neox': 1607, 'n_words': 322}