<commit_before>
var events = require("events");
var htmlparser = require("htmlparser2");
var moment = require("moment");
var Promise = require("promise");
var util = require("util");

var commands = require("../core/commands");

exports.initialize = function(bot) {
  var drivers = bot.shared.drivers;
  var races = bot.shared.events;

  var game = new Game(bot.database, drivers, races);
  game._initDatabase();

  var perms = bot.config.debug
    ? commands.Command.ALLOW_ALL
    : commands.Command.ALLOW_AUTHED;

  bot.addCommand("bet", perms, function(user, params) {
    return new Promise(function(resolve, reject) {
      if (!game.betsAllowed) {
        var datestr = moment.utc(game.betsOpen).format("MMMM D, HH:mm UTC");
        throw new Error(util.format("Bets will be allowed after %s, until qualifying!", datestr));
      }

      if (params.length != 3) {
        throw new Error("Need three names to bet");
      }

      params.forEach(function(name) {
        if (name.length < 3) {
          throw new Error("Name length has to be at least three characters");
        }
      });

      game.bet.apply(game, [user].concat(params)).then(function(reply) {
        resolve(reply);
      });
    });
  });

  bot.events.on("race.weekend.start", function() {
    bot.spam("Hello everybody! Bet window is now open and bets are allowed until qualifying starts!");

    var now = new Date;
    var deadline = races.nextQualifying.date;

    var notify = function() {
      bot.spam("Hello everybody! Remember to !bet for race podium before quali!");
    };

    var last_chance = function() {
      bot.spam("Last chance to !bet for race podium! Qualifying in 30 minutes!");
    };

    setTimeout(last_chance, deadline.subtract(30, "minutes").diff(now));
    setTimeout(notify, deadline.subtract(1.5, "hours").diff(now));
    setTimeout(notify, deadline.subtract(2.5, "hours").diff(now));
    setTimeout(notify, deadline.subtract(9, "hours").diff(now));
    setTimeout(notify, deadline.subtract(3, "hours").diff(now));
  });

  bot.events.on("race.weekend.end", function() {
    var round = races.nextRace.round - 1;
    var iid = setInterval(function() {
      var results = new RaceResults(bot.net, bot.config.betgame);

      results.fetchRace(round).then(function(result) {
        game.bets.round(round).then(function(bets) {
          (new PointsCalculator(result)).process(bets).then(function(scores) {
//             console.log("SCORES:", scores.map(o => o.nick + ": " + o.points).join("; "));
            game.bets.saveScores(round, scores).then(function() {
              clearInterval(iid);

              bot.spam("Betting scores updated!");
            }, function(err) {
              console.log(err);
            });
          });
        });
      });
    }, 1000 * 15);
    // CALCULATE SCORES
  });
};

var RaceResults = function(net, options) {
  this.net = net;
  this.url = options.source;

  this.sources = [];
  this.results = [];
};

RaceResults.prototype = {
  get: function(i) {
    if (arguments.length == 0) {
      throw new Error("Have to define which round to fetch");
    }

    var cache = this;
    return new Promise(function(resolve, reject) {
      if (i < cache.results.length) {
        return resolve(cache.results[i]);
      }
      cache.fetchRace(i).then(function(result) {
        resolve(result);
      });
    });
  },
  fetchRace: function(i) {
    var cache = this;<commit_msg>Refresh every 15 minutes, not 15 seconds<commit_after>
var events = require("events");
var htmlparser = require("htmlparser2");
var moment = require("moment");
var Promise = require("promise");
var util = require("util");

var commands = require("../core/commands");

exports.initialize = function(bot) {
  var drivers = bot.shared.drivers;
  var races = bot.shared.events;

  var game = new Game(bot.database, drivers, races);
  game._initDatabase();

  var perms = bot.config.debug
    ? commands.Command.ALLOW_ALL
    : commands.Command.ALLOW_AUTHED;

  bot.addCommand("bet", perms, function(user, params) {
    return new Promise(function(resolve, reject) {
      if (!game.betsAllowed) {
        var datestr = moment.utc(game.betsOpen).format("MMMM D, HH:mm UTC");
        throw new Error(util.format("Bets will be allowed after %s, until qualifying!", datestr));
      }

      if (params.length != 3) {
        throw new Error("Need three names to bet");
      }

      params.forEach(function(name) {
        if (name.length < 3) {
          throw new Error("Name length has to be at least three characters");
        }
      });

      game.bet.apply(game, [user].concat(params)).then(function(reply) {
        resolve(reply);
      });
    });
  });

  bot.events.on("race.weekend.start", function() {
    bot.spam("Hello everybody! Bet window is now open and bets are allowed until qualifying starts!");

    var now = new Date;
    var deadline = races.nextQualifying.date;

    var notify = function() {
      bot.spam("Hello everybody! Remember to !bet for race podium before quali!");
    };

    var last_chance = function() {
      bot.spam("Last chance to !bet for race podium! Qualifying in 30 minutes!");
    };

    setTimeout(last_chance, deadline.subtract(30, "minutes").diff(now));
    setTimeout(notify, deadline.subtract(1.5, "hours").diff(now));
    setTimeout(notify, deadline.subtract(2.5, "hours").diff(now));
    setTimeout(notify, deadline.subtract(9, "hours").diff(now));
    setTimeout(notify, deadline.subtract(3, "hours").diff(now));
  });

  bot.events.on("race.weekend.end", function() {
    var round = races.nextRace.round - 1;
    var iid = setInterval(function() {
      var results = new RaceResults(bot.net, bot.config.betgame);

      results.fetchRace(round).then(function(result) {
        game.bets.round(round).then(function(bets) {
          (new PointsCalculator(result)).process(bets).then(function(scores) {
//             console.log("SCORES:", scores.map(o => o.nick + ": " + o.points).join("; "));
            game.bets.saveScores(round, scores).then(function() {
              clearInterval(iid);

              bot.spam("Betting scores updated!");
            }, function(err) {
              console.log(err);
            });
          });
        });
      });
    }, 1000 * 60 * 15);
    // CALCULATE SCORES
  });
};

var RaceResults = function(net, options) {
  this.net = net;
  this.url = options.source;

  this.sources = [];
  this.results = [];
};

RaceResults.prototype = {
  get: function(i) {
    if (arguments.length == 0) {
      throw new Error("Have to define which round to fetch");
    }

    var cache = this;
    return new Promise(function(resolve, reject) {
      if (i < cache.results.length) {
        return resolve(cache.results[i]);
      }
      cache.fetchRace(i).then(function(result) {
        resolve(result);
      });
    });
  },
  fetchRace: function(i) {
    var cache = this;
{'fraction_non_alphanumeric': 0.15455861070911722, 'fraction_numerical': 0.006946454413892909, 'mean_word_length': 2.8501392757660167, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 5, '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': '5850089', 'n_tokens_mistral': 2190, 'n_tokens_neox': 2106, 'n_words': 480}