<commit_before>require 'mechanize'
require 'chronic'

module CCCB::Core::GiantMultiplayerRobot
  extend Module::Requirements
  needs :bot, :commands

  def update_gmr_game(id)
    giant_multiplayer_robot.games[id] ||= OpenStruct.new
    debug "Checking GMR game ##{id}"
    m = Mechanize.new
    turns = m.get("http://multiplayerrobot.com/Game/GameDetailTurns?id=#{id}")
    last_turn_data = turns.search("div.turn-finished/div.turn-cell-text").to_a.first
    last_turn_text = last_turn_data.text.split(/\r\n/).reverse.join.gsub(/\s+/, ' ')
    last_turn = Chronic.parse( last_turn_text )
    # Convert to GMT
    last_turn = (last_turn + last_turn.gmt_offset).utc
    spam "#{last_turn}"
    
    spam "#{giant_multiplayer_robot.games[id].last_turn} != #{last_turn}"
    giant_multiplayer_robot.games[id].updated = Time.now
    if giant_multiplayer_robot.games[id].last_turn != last_turn
      players = m.post("http://multiplayerrobot.com/Game/Details?id=#{id}")
      next_player = players.search("div.game-host/a/img").attribute("title").text

      giant_multiplayer_robot.games[id].last_turn = last_turn
      giant_multiplayer_robot.games[id].next_player = next_player
      return true
    else 
      return false
    end
  end

  def module_load
    giant_multiplayer_robot.games ||= {}
    giant_multiplayer_robot.channel_updated ||= Hash.new(0)
    giant_multiplayer_robot.channel_next_player ||= {}
    add_setting :channel, "gmr_games"
    default_setting 86400, "options", "gmr_nag_frequency"
    default_setting 60, "options", "gmr_update_frequency"
  end

  def module_unload
    giant_multiplayer_robot.thread.kill
  end

  def module_start
    giant_multiplayer_robot.thread = Thread.new do 
      loop do
        networking.networks.each do |name,network|
          spam "Checking GMR games on #{name}"
          network.channels.each do |ch_name, channel|
            giant_multiplayer_robot.channel_updated[channel] ||= Chronic.parse("Jan 01 1970 00:00:00 GMT")
            spam "Checking on #{channel}"
            begin
              channel.get_setting("gmr_games").each do |game_name, game_id|
                spam "Check GMR: #{channel} #{game_name}"
                game = giant_multiplayer_robot.games[game_id]
                frequency = channel.get_setting("options", "gmr_update_frequency").to_i
                if game.nil? or Time.now - game.updated  > frequency
                  debug "Update GMR #{channel} #{game_name}/#{game_id}"
                  update_gmr_game(game_id)
                  game = giant_multiplayer_robot.games[game_id]
                  spam "Game #{game}"
                end

                channel_elapsed = Time.now - giant_multiplayer_robot.channel_updated[channel]

                next_player_map = (giant_multiplayer_robot.channel_next_player[channel] ||= {})

                if next_player_map[game_id].nil? or next_player_map[game_id] != game.next_player or 
                  (Time.now - giant_multiplayer_robot.channel_updated[channel]) > channel.get_setting("options","gmr_nag_frequency").to_i
                then
                  waiting = elapsed_time( Time.now - game.last_turn )
                  channel.msg "GMR Game #{game_name} (##{game_id}): Next player is #{game.next_player}. Waiting #{waiting}"
                  schedule_hook :event, event: :gmr_game_nag, game: game_name, id: game_id, next_player: game.next_player, updated: true
                  giant_multiplayer_robot.channel_updated[channel] = Time.now<commit_msg>Fix a bug with reloading before the GMR check fires<commit_after>require 'mechanize'
require 'chronic'

module CCCB::Core::GiantMultiplayerRobot
  extend Module::Requirements
  needs :bot, :commands

  def update_gmr_game(id)
    giant_multiplayer_robot.games[id] ||= OpenStruct.new
    debug "Checking GMR game ##{id}"
    m = Mechanize.new
    turns = m.get("http://multiplayerrobot.com/Game/GameDetailTurns?id=#{id}")
    last_turn_data = turns.search("div.turn-finished/div.turn-cell-text").to_a.first
    last_turn_text = last_turn_data.text.split(/\r\n/).reverse.join.gsub(/\s+/, ' ')
    last_turn = Chronic.parse( last_turn_text )
    # Convert to GMT
    last_turn = (last_turn + last_turn.gmt_offset).utc
    spam "#{last_turn}"
    
    spam "#{giant_multiplayer_robot.games[id].last_turn} != #{last_turn}"
    giant_multiplayer_robot.games[id].updated = Time.now
    if giant_multiplayer_robot.games[id].last_turn != last_turn
      players = m.post("http://multiplayerrobot.com/Game/Details?id=#{id}")
      next_player = players.search("div.game-host/a/img").attribute("title").text

      giant_multiplayer_robot.games[id].last_turn = last_turn
      giant_multiplayer_robot.games[id].next_player = next_player
      return true
    else 
      return false
    end
  end

  def module_load
    giant_multiplayer_robot.games ||= {}
    giant_multiplayer_robot.channel_updated ||= Hash.new(0)
    giant_multiplayer_robot.channel_next_player ||= {}
    add_setting :channel, "gmr_games"
    default_setting 86400, "options", "gmr_nag_frequency"
    default_setting 60, "options", "gmr_update_frequency"
  end

  def module_unload
    giant_multiplayer_robot.thread.kill
  end

  def module_start
    giant_multiplayer_robot.thread = Thread.new do 
      loop do
        networking.networks.each do |name,network|
          spam "Checking GMR games on #{name}"
          network.channels.each do |ch_name, channel|
            giant_multiplayer_robot.channel_updated[channel] ||= Chronic.parse("Jan 01 1970 00:00:00 GMT")
            spam "Checking on #{channel}"
            begin
              channel.get_setting("gmr_games").each do |game_name, game_id|
                spam "Check GMR: #{channel} #{game_name}"
                game = giant_multiplayer_robot.games[game_id]
                frequency = channel.get_setting("options", "gmr_update_frequency").to_i
                if game.nil? or game.updated.nil? or Time.now - game.updated  > frequency
                  debug "Update GMR #{channel} #{game_name}/#{game_id}"
                  update_gmr_game(game_id)
                  game = giant_multiplayer_robot.games[game_id]
                  spam "Game #{game}"
                end

                channel_elapsed = Time.now - giant_multiplayer_robot.channel_updated[channel]

                next_player_map = (giant_multiplayer_robot.channel_next_player[channel] ||= {})

                if next_player_map[game_id].nil? or next_player_map[game_id] != game.next_player or 
                  (Time.now - giant_multiplayer_robot.channel_updated[channel]) > channel.get_setting("options","gmr_nag_frequency").to_i
                then
                  waiting = elapsed_time( Time.now - game.last_turn )
                  channel.msg "GMR Game #{game_name} (##{game_id}): Next player is #{game.next_player}. Waiting #{waiting}"
                  schedule_hook :event, event: :gmr_game_nag, game: game_name, id: game_id, next_player: game.next_player, updated: true
                  giant_multiplayer_robot.channel_updated[channel] = Time.now
{'fraction_non_alphanumeric': 0.11126820302559028, 'fraction_numerical': 0.005655308921249823, 'mean_word_length': 2.947544642857143, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '2483321', 'n_tokens_mistral': 2345, 'n_tokens_neox': 2260, 'n_words': 392}