<commit_before>module RunTracker
  module SeedDB
    ##
    # TODO this needs to be significantly refactored after it works
    # Gathers all of the runners, their runs, their current stats
    # At the same time it also counts the moderators verified runs, and last verified run date
    # Also determines the current stats for each category
    # Will pull all current data from a game's leaderboard
    # categoryList and modList are expected to be hashes keyed with their respective SRC ids
    def self.getGameRunners(gameID, gameName, categoryList, modList)
      currentRunnerList = PostgresDB.getCurrentRunners # NOTE unverified implementation
      newRunnerList = {}

      count = 0
      RTBot.send_message(DevChannelID, 'Archiving Existing Runs...This can Take a While...')

      categoryList.each do |_key, category| # Loop through every category
        currentWRTime = Util::MaxInteger
        currentWRID = ''
        currentWRDate = nil

        numSubmittedRuns = 0
        numSubmittedWRs = 0

        longestHeldWR = 0
        longestHeldWRID = ''

        requestLink = "#{SrcAPI::API_URL}runs" \
                      "?game=#{gameID}" \
                      "&category=#{category.category_id.split('-').first}" \
                      '&orderby=date&direction=asc&max=200'

        loop do
          # This inefficiency makes me so depressed, the way SRC has added subcategories makes me cry
          # as you cannot do an API call for just a subcategories' runs
          # Remove the subcategory component of the key
          requestResults = Util.jsonRequest(requestLink)
          categoryRuns = requestResults['data']
          pagination = requestResults['pagination']

          categoryRuns.each do |run|
            # Add to runner information
            # NOTE this causes a problem if the runner ever gets a SRC account in the future
            # the fix is probably to check on the heartbeat updating if their name overwrite the previous guest name and then update accordingly
            runnerKey = ''
            runnerName = ''
            if run['players'].first['rel'].downcase.casecmp('guest').zero?
              runnerKey = run['players'].first['name']
              runnerName = 'guest'
            else
              runnerKey = run['players'].first['id']
            end
            # If we havnt started tracking this runner before, init
            runner = nil
            if !currentRunnerList.key?(runnerKey) && !newRunnerList.key?(runnerKey)
              unless runnerName.downcase.casecmp('guest').zero? # only call API for name if new runner and not a guest
                runnerName = SrcAPI.getUserName(runnerKey)
              end
              runner = Runner.new(runnerKey, runnerName)
              runner.historic_runs[gameID] = RunnerGame.new(gameID, gameName)
              runner.historic_runs[gameID].categories[category.category_id] = RunnerCategory.new(category.category_id, category.category_name)
              newRunnerList[runnerKey] = runner<commit_msg>fixed issues with guest runners being inserted a duplicate number of times<commit_after>module RunTracker
  module SeedDB
    ##
    # TODO this needs to be significantly refactored after it works
    # Gathers all of the runners, their runs, their current stats
    # At the same time it also counts the moderators verified runs, and last verified run date
    # Also determines the current stats for each category
    # Will pull all current data from a game's leaderboard
    # categoryList and modList are expected to be hashes keyed with their respective SRC ids
    def self.getGameRunners(gameID, gameName, categoryList, modList)
      currentRunnerList = PostgresDB.getCurrentRunners # NOTE unverified implementation
      newRunnerList = {}

      count = 0
      RTBot.send_message(DevChannelID, 'Archiving Existing Runs...This can Take a While...')

      categoryList.each do |_key, category| # Loop through every category
        currentWRTime = Util::MaxInteger
        currentWRID = ''
        currentWRDate = nil

        numSubmittedRuns = 0
        numSubmittedWRs = 0

        longestHeldWR = 0
        longestHeldWRID = ''

        requestLink = "#{SrcAPI::API_URL}runs" \
                      "?game=#{gameID}" \
                      "&category=#{category.category_id.split('-').first}" \
                      '&orderby=date&direction=asc&max=200'

        loop do
          # This inefficiency makes me so depressed, the way SRC has added subcategories makes me cry
          # as you cannot do an API call for just a subcategories' runs
          # Remove the subcategory component of the key
          requestResults = Util.jsonRequest(requestLink)
          categoryRuns = requestResults['data']
          pagination = requestResults['pagination']

          categoryRuns.each do |run|
            # Add to runner information
            # NOTE this causes a problem if the runner ever gets a SRC account in the future
            # the fix is probably to check on the heartbeat updating if their name overwrite the previous guest name and then update accordingly
            runnerKey = ''
            runnerName = ''
            if run['players'].first['rel'].downcase.casecmp('guest').zero?
              runnerKey = run['players'].first['name'].downcase
              runnerName = 'guest'
            else
              runnerKey = run['players'].first['id']
            end
            # If we havnt started tracking this runner before, init
            runner = nil
            if !currentRunnerList.key?(runnerKey) && !newRunnerList.key?(runnerKey)
              unless runnerName.downcase.casecmp('guest').zero? # only call API for name if new runner and not a guest
                runnerName = SrcAPI.getUserName(runnerKey)
              end
              runner = Runner.new(runnerKey, runnerName)
              runner.historic_runs[gameID] = RunnerGame.new(gameID, gameName)
              runner.historic_runs[gameID].categories[category.category_id] = RunnerCategory.new(category.category_id, category.category_name)
              newRunnerList[runnerKey] = runner
{'fraction_non_alphanumeric': 0.0744299674267101, 'fraction_numerical': 0.002280130293159609, 'mean_word_length': 2.3834710743801653, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5688081', 'n_tokens_mistral': 1575, 'n_tokens_neox': 1529, 'n_words': 528}