<commit_before>/* eslint no-param-reassign: 0 */

require('dotenv').config();
const db = require('../db');
const ObjectID = require('mongodb').ObjectID;

function betResult(game) {
  const homeResult = game.homeScore + game.homeSpread;
  const awayResult = game.awayScore + game.awaySpread;

  if (!game.selectedTeam) { return 'Loss'; }
  if (homeResult === game.awayScore) { return 'Push'; }
  if ((game.selectedTeam === game.awayTeam && awayResult > game.homeScore) ||
     (game.selectedTeam === game.homeTeam && homeResult > game.awayScore)) {
    return 'Win';
  }
  return 'Loss';
}

function updateScores(err, duelWeeks) {
  if (err) { throw err; }
  duelWeeks.forEach((duelWeek) => {
    db.get().collection('results').findOne(
      { year: duelWeek.year, weekNum: duelWeek.weekNum }
      , (resultErr, weekResult) => {
        if (resultErr) { throw resultErr; }
        const updatedGames = duelWeek.games.map((game) => {
          const gameResult = weekResult.scores.find(score => score.gameId === game.id);
          if (gameResult) {
            game.awayScore = gameResult.awayScore;
            game.homeScore = gameResult.homeScore;
            game.result = betResult(game);
          }
          return game;
        });
        db.get().collection('duelweeks').updateOne(
          { _id: new ObjectID(duelWeek._id) },
          { $set: { games: updatedGames } },
          (updateErr) => {
            if (updateErr) { throw updateErr; }
            process.exit();
          });
      });
  });
}

db.connect(process.env.MONGODB_URI, (err) => {
  if (err) { throw err; }
  db.get().collection('duelweeks').find({ 'games.result': null }).toArray(updateScores);
});
<commit_msg>Handle updating duel weeks that do not have results<commit_after>/* eslint no-param-reassign: 0 */

require('dotenv').config();
const db = require('../db');
const ObjectID = require('mongodb').ObjectID;

function betResult(game) {
  const homeResult = game.homeScore + game.homeSpread;
  const awayResult = game.awayScore + game.awaySpread;

  if (!game.selectedTeam) { return 'Loss'; }
  if (homeResult === game.awayScore) { return 'Push'; }
  if ((game.selectedTeam === game.awayTeam && awayResult > game.homeScore) ||
     (game.selectedTeam === game.homeTeam && homeResult > game.awayScore)) {
    return 'Win';
  }
  return 'Loss';
}

function updateScores(err, duelWeeks) {
  if (err) { throw err; }
  duelWeeks.forEach((duelWeek) => {
    db.get().collection('results').findOne(
      { year: duelWeek.year, weekNum: duelWeek.weekNum }
      , (resultErr, weekResult) => {
        if (resultErr) {
          console.error(`Error finding result for duel week: ${resultErr}`); // eslint-disable-line no-console
          return;
        }
        if (!weekResult) { return; }
        const updatedGames = duelWeek.games.map((game) => {
          const gameResult = weekResult.scores.find(score => score.gameId === game.id);
          if (gameResult) {
            game.awayScore = gameResult.awayScore;
            game.homeScore = gameResult.homeScore;
            game.result = betResult(game);
          }
          return game;
        });
        db.get().collection('duelweeks').updateOne(
          { _id: new ObjectID(duelWeek._id) },
          { $set: { games: updatedGames } },
          (updateErr) => {
            if (updateErr) { throw updateErr; }
            process.exit();
          });
      });
  });
}

db.connect(process.env.MONGODB_URI, (err) => {
  if (err) { throw err; }
  db.get().collection('duelweeks').find({ 'games.result': null }).toArray(updateScores);
});

{'fraction_non_alphanumeric': 0.16050069541029208, 'fraction_numerical': 0.0005563282336578582, 'mean_word_length': 2.7110423116615068, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '3681557', 'n_tokens_mistral': 1148, 'n_tokens_neox': 1124, 'n_words': 225}