<commit_before>class LoreModel
  attr_accessor :need_login, :need_password

  @need_login = false
  @need_password = false
end

class Bot < Looper
  def initialize(socket)
    super("Bot", 1) #commands are issued once per second
    @mutex = Mutex.new

    @model = LoreModel.new
    @strategy = LoginStrategy.new
    # is it needed?
    @mutex.synchronize do
      @socket = socket
    end
  end

  def do_action
    command = @strategy.next_command unless @strategy.nil?
    issue_command(command) unless command.nil?
  end

  def issue_command(command)
    if command.start_with? "bot:"
      print "Bot> Caught bot command #{command}, doing nothing."
      @strategy = GotoShopStrategy.new if command == "bot:shopping"
    else
      @mutex.synchronize do
        @socket.send command + "\n", 0
      end
    end
  end

  def parse_lore(string)
    @model.need_login = string =~ /What do they call ya/
    @model.need_password = string =~ /Access Code/

    @strategy.update(@model)
  end
end

class LoginStrategy
  def next_command
    nil
  end
end


class DoNothingStrategy
  def next_command
    nil
  end
end

class GotoShopStrategy
  def initialize
    puts "Created goto shop strategy"
    @maze = Maze.new(MAP)
    @path = @maze.find_path(position(6, 2), position(1, 3)).reverse
    puts "path is: #{@path}"
  end

  def next_command
    puts "Remaining path: #{@path.inspect}"
    nil if @path.empty?
    @path.pop.to_s
  end
end
<commit_msg>login strategy is working<commit_after>require 'yaml'

require './lib/client/looper'

class Bot < Looper
  attr_reader :position

  def initialize(socket)
    super("Bot", 1) #commands are issued once per second
    @mutex = Mutex.new
    @position = Position.new(6, 2)

    @model = {}
    @strategy = LoginStrategy.new
    # is it needed?
    @mutex.synchronize do
      @socket = socket
    end
  end

  def do_action
    command = @strategy.next_command unless @strategy.nil?
    issue_command(command) unless command.nil?
  end

  def issue_command(command)
    if command.start_with? "bot:"
      print "Bot> Caught bot command #{command}, doing nothing."
      @strategy = GotoShopStrategy.new(@position) if command == "bot:shopping"
    else
      @mutex.synchronize do
        @socket.send command + "\n", 0
      end
    end
  end

  def parse_lore(string)
    changes = {}
    if string =~ /What do they call ya/
      changes[:login_requested] = true
    elsif string =~ /Access /
      changes[:password_requested] = true
    elsif string =~ /Welcome to/
      changes[:menu_appeared] = true
    elsif string =~ /PRESS RETURN/
      changes[:menu_press_enter] = true
    end

    unless changes.empty?
      puts "Changes: #{changes}"
      @model.merge!(changes)
      @strategy.update(changes, @model)
    end
  end
end

class ModelListener
  def update(changes, model)
  end
end

class LoginStrategy < ModelListener
  @command = nil
  def initialize
    @credentials = YAML.load_file("#{ENV['HOME']}/.munchkin")
    puts "Credentials: #{@credentials}"
  end

  def next_command
    result = @command
    @command = nil
    puts "\nLogin command=#{result}" unless result.nil?
    result
  end

  def update(changes, _)
    if changes.key?(:password_requested)
      @command = @credentials[:password]
    elsif changes.key?(:login_requested)
      @command = @credentials[:login]
    elsif changes.key?(:menu_press_enter)
      @command = ""
    elsif changes.key?(:menu_appeared)
      @command = "1"
    else
      @command = nil
    end
  end

end


class DoNothingStrategy  < ModelListener
  def next_command
    nil
  end
end

class GotoShopStrategy < ModelListener
  def initialize(position)
    puts "Created goto shop strategy"
    @maze = Maze.new(MAP)
    @path = @maze.find_path(position(6, 2), position).reverse
    puts "path is: #{@path}"
  end

  def next_command
    puts "Remaining path: #{@path.inspect}"
    nil if @path.empty?
    @path.pop.to_s
  end
end

{'fraction_non_alphanumeric': 0.10466294982260517, 'fraction_numerical': 0.003294475418144957, 'mean_word_length': 2.919563058589871, 'pattern_counts': {'":': 0, '<': 8, '<?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': '5958038', 'n_tokens_mistral': 1305, 'n_tokens_neox': 1288, 'n_words': 354}