<commit_before>require_relative '../config/boot'
require_relative './encoder'
require_relative './random_generator'

require 'sinatra/base'
require 'sinatra/reloader'
require 'active_support/all'

class StringEncoderGameServer < Sinatra::Base

  attr_accessor :requests
  attr_accessor :encoder

  attr_accessor :word_list

  attr_accessor :sessions

  # Enable sinatra reloader
  configure :development do
    register Sinatra::Reloader
    also_reload File.expand_path('../encoder.rb', __FILE__)
    also_reload File.expand_path('../random_generator.rb', __FILE__)
  end

  # Admit connections from anyone
  set :bind, '0.0.0.0'

  def initialize
    # initialize an encoder
    @encoder = Encoder.new

    # initialize empty words list
    @word_list = []

    # Initialize the words list to have in memory
    words_file = File.expand_path('../../config/dictionaries/words-es.txt', __FILE__)
    File.foreach(words_file).map{|word| @word_list.push(word)}

    # initialize empty sessions hash, key will be the ip
    @sessions = {}

    super
  end

  # Super endpoint
  get '/word' do
    ip = request.ip.to_s
    session = sessions[ip]
    if session
      word = new_word
      session[:challenge][:original_word] = word
      session[:challenge][:encoded_word] = encoded_word_for_challenge(session[:current_challenge], word)
      session[:challenge][:ends_at] = (Time.now + 10.seconds)
      session[:challenge][:encoded_word]
    else
      word = new_word
      create_session(ip, word)
      word
    end
  end

  get '/debug' do
    binding.pry
  end

  get '/answer' do
    ip = request.ip.to_s
    session = get_session(ip)

    if session
      # encoded_word = params[:encoded_word].strip
      answer = params[:answer].strip

      challenge = session[:challenge]

      challenge[:original_word]

      if Time.now > challenge[:ends_at]
        return "Your time has ended, please request a new word at /word"
      else
        if answer == challenge[:original_word]
          # increment challenge
          session[:current_challenge] = session[:current_challenge] + 1
          return "OK! - You have succesfully completed this challenge, please ask for another word to see the next challenge"
        else
          "SORRY - the answer is invalid, please try again"
        end
      end

    else
      "You have to request a word first!"
    end
  end

  def new_word
    word_list.select{|word| word.length > 4 }.sample.strip
  end

  def create_session(ip, original_word)
    sessions[ip] = {current_challenge: 1, challenge: { original_word: original_word, encoded_word: encoder.noop(original_word), ends_at: (Time.now + 500.seconds)} }
  end

  def get_session(ip)
    sessions[ip]
  end

  def new_word_for(session)
    challenge = session[:current_challenge]
    word = new_word<commit_msg>update Timeout message<commit_after>require_relative '../config/boot'
require_relative './encoder'
require_relative './random_generator'

require 'sinatra/base'
require 'sinatra/reloader'
require 'active_support/all'

class StringEncoderGameServer < Sinatra::Base

  attr_accessor :requests
  attr_accessor :encoder

  attr_accessor :word_list

  attr_accessor :sessions

  # Enable sinatra reloader
  configure :development do
    register Sinatra::Reloader
    also_reload File.expand_path('../encoder.rb', __FILE__)
    also_reload File.expand_path('../random_generator.rb', __FILE__)
  end

  # Admit connections from anyone
  set :bind, '0.0.0.0'

  def initialize
    # initialize an encoder
    @encoder = Encoder.new

    # initialize empty words list
    @word_list = []

    # Initialize the words list to have in memory
    words_file = File.expand_path('../../config/dictionaries/words-es.txt', __FILE__)
    File.foreach(words_file).map{|word| @word_list.push(word)}

    # initialize empty sessions hash, key will be the ip
    @sessions = {}

    super
  end

  # Super endpoint
  get '/word' do
    ip = request.ip.to_s
    session = sessions[ip]
    if session
      word = new_word
      session[:challenge][:original_word] = word
      session[:challenge][:encoded_word] = encoded_word_for_challenge(session[:current_challenge], word)
      session[:challenge][:ends_at] = (Time.now + 10.seconds)
      session[:challenge][:encoded_word]
    else
      word = new_word
      create_session(ip, word)
      word
    end
  end

  get '/debug' do
    binding.pry
  end

  get '/answer' do
    ip = request.ip.to_s
    session = get_session(ip)

    if session
      # encoded_word = params[:encoded_word].strip
      answer = params[:answer].strip

      challenge = session[:challenge]

      challenge[:original_word]

      if Time.now > challenge[:ends_at]
        return "TIMEOUT! - You have run out of time, please ask for a new word!"
      else
        if answer == challenge[:original_word]
          # increment challenge
          session[:current_challenge] = session[:current_challenge] + 1
          return "OK! - You have succesfully completed this challenge, please ask for another word to see the next challenge"
        else
          "SORRY - the answer is invalid, please try again"
        end
      end

    else
      "You have to request a word first!"
    end
  end

  def new_word
    word_list.select{|word| word.length > 4 }.sample.strip
  end

  def create_session(ip, original_word)
    sessions[ip] = {current_challenge: 1, challenge: { original_word: original_word, encoded_word: encoder.noop(original_word), ends_at: (Time.now + 500.seconds)} }
  end

  def get_session(ip)
    sessions[ip]
  end

  def new_word_for(session)
    challenge = session[:current_challenge]
    word = new_word
{'fraction_non_alphanumeric': 0.09754373564233963, 'fraction_numerical': 0.00424103198444955, 'mean_word_length': 3.0895953757225434, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '6286383', 'n_tokens_mistral': 1781, 'n_tokens_neox': 1667, 'n_words': 459}