<commit_before># frozen_string_literal: true

class Baseballbot
  module Posts
    class GameThread < Base
      def initialize(row, subreddit:)
        super(row, subreddit: subreddit)

        @id = row['id']
        @game_pk = row['game_pk']
        @post_id = row['post_id']
        @type = row['type'] || 'game_thread'
        @title = row['title']
      end

      def create!
        @template = template_for(@type)

        return change_status('Postponed') if @template.postponed?

        create_game_thread_post!

        info "[NEW] #{@submission.id} in /r/#{@subreddit.name} for #{@game_pk}"

        save_to_redis!

        @submission
      end

      def update!
        @template = template_for("#{@type}_update")
        @submission = @subreddit.load_submission(id: @post_id)

        update_game_thread_post!

        info "[UPD] #{@submission.id} in /r/#{@subreddit.name} for #{@game_pk}"

        @template.final?
      end

      protected

      def create_game_thread_post!
        @submission = @subreddit.submit(
          title: @template.formatted_title,
          text: @template.evaluated_body
        )

        # Mark as posted right away so that it won't post again
        change_status 'Posted'

        @submission.edit CGI.unescapeHTML(@submission.selftext)
          .gsub('#ID#', @submission.id)

        update_sticky @subreddit.sticky_game_threads?
        update_suggested_sort 'new'
        # update_flair game_thread_flair('default')
      end

      def update_game_thread_post!
        @subreddit.edit id: @post_id, body: @template.replace_in(@submission)

        return postpone_game_thread! if @template.postponed?

        @template.final? ? end_game_thread! : change_status('Posted')
      end

      def save_to_redis!
        @bot.redis.hset(@template.gid, @subreddit.name.downcase, @submission.id)
      end

      # @param status [String] status of the game thread
      def change_status(status)
        attrs = { status: status, updated_at: Time.now }

        if status == 'Posted'
          attrs[:post_id] = @submission.id
          attrs[:title] = @submission.title
        end

        fields = attrs.keys.map.with_index { |col, i| "#{col} = $#{i + 2}" }

        @bot.db.exec_params(
          "UPDATE game_threads SET #{fields.join(', ')} WHERE id = $1",<commit_msg>no need to edit in the post ID, reddit stream has an auto link<commit_after># frozen_string_literal: true

class Baseballbot
  module Posts
    class GameThread < Base
      def initialize(row, subreddit:)
        super(row, subreddit: subreddit)

        @id = row['id']
        @game_pk = row['game_pk']
        @post_id = row['post_id']
        @type = row['type'] || 'game_thread'
        @title = row['title']
      end

      def create!
        @template = template_for(@type)

        return change_status('Postponed') if @template.postponed?

        create_game_thread_post!

        info "[NEW] #{@submission.id} in /r/#{@subreddit.name} for #{@game_pk}"

        save_to_redis!

        @submission
      end

      def update!
        @template = template_for("#{@type}_update")
        @submission = @subreddit.load_submission(id: @post_id)

        update_game_thread_post!

        info "[UPD] #{@submission.id} in /r/#{@subreddit.name} for #{@game_pk}"

        @template.final?
      end

      protected

      def create_game_thread_post!
        @submission = @subreddit.submit(
          title: @template.formatted_title,
          text: @template.evaluated_body
        )

        # Mark as posted right away so that it won't post again
        change_status 'Posted'

        update_sticky @subreddit.sticky_game_threads?
        update_suggested_sort 'new'
        # update_flair game_thread_flair('default')
      end

      def update_game_thread_post!
        @subreddit.edit id: @post_id, body: @template.replace_in(@submission)

        return postpone_game_thread! if @template.postponed?

        @template.final? ? end_game_thread! : change_status('Posted')
      end

      def save_to_redis!
        @bot.redis.hset(@template.gid, @subreddit.name.downcase, @submission.id)
      end

      # @param status [String] status of the game thread
      def change_status(status)
        attrs = { status: status, updated_at: Time.now }

        if status == 'Posted'
          attrs[:post_id] = @submission.id
          attrs[:title] = @submission.title
        end

        fields = attrs.keys.map.with_index { |col, i| "#{col} = $#{i + 2}" }

        @bot.db.exec_params(
          "UPDATE game_threads SET #{fields.join(', ')} WHERE id = $1",
{'fraction_non_alphanumeric': 0.11815812337098175, 'fraction_numerical': 0.0008688097306689834, 'mean_word_length': 2.4365671641791047, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3935705', 'n_tokens_mistral': 1495, 'n_tokens_neox': 1451, 'n_words': 320}