<commit_before># -*- encoding: utf-8 -*-

require 'sinatra/base'
require 'sinatra/reloader'
require 'json'
require './lib/game_collection'
require './lib/addresses'
require './lib/mailer'
require './lib/game'
require './lib/settings'
require './lib/configure'

module Reversi
  class Main < Sinatra::Base
    configure :production do
      begin
        settings = Settings.new
        Configure.init_sendgrid(settings)
      rescue => e
        puts e.backtrace
        puts e.inspect
      end
    end

    post '/game' do
      begin
        # parse email address from request
        from = Addresses.get_address(params[:from])
        to = Addresses.get_address(params[:subject])

        # fail to parse address if it is nil
        mailer = Mailer.new
        if from != nil && to == nil then
          logger.info "Failed to parse opponent player email address. Subject must include email address."
          return 'Fail to parse to address'
        end
        logger.info "from: #{from} to: #{to}"
        if from == nil && to == nil then
          logger.info "Failed to parse email address. Please check the request."
          logger.info "from: #{params[:from]}"
          logger.info "subject: #{params[:subject]}"
          return 'Fail to parse to and from address'
        end
        # get game data from DB
        dba = GameCollection.new
        game = dba.find(from, to)
        # create game data if not exist
        if game == nil then
          game = Game.new
          # odd:first move:black
          game.player_odd = to
          # even:second move:white
          game.player_even = from
          id = dba.insert(game)
          game = dba.findById(id)
          #puts "create new game"
          logger.info "create new game"
        end

        # send email for each player
        logger.info "game._id: #{game._id}"
        mailer.send(game)
      rescue => e
        logger.error e.backtrace
        logger.error e.inspect
      end

      'Success'
    end

    post '/event' do
      begin
        request.body.rewind
        data = JSON.parse(request.body.read)
        data.each{|event|
          begin
            dba = GameCollection.new
            # handle only click event
            next if event['event'] != "click"
            logger.info "got click event"
            # requester address
            email = event['email']
            # parse the url clicked by user
            event_data = Game.parse_cell_url(event['url'])
            # validate the event data
            game = dba.findById(BSON::ObjectId.from_string(event_data["obj_id"]))
            if game == nil then
              logger.info "Could not find valid game. The event was ignored."
              break
            end
            if game.turn != event_data['turn'] then
              logger.info "Invalid turn event."
              break;
            end<commit_msg>add log<commit_after># -*- encoding: utf-8 -*-

require 'sinatra/base'
require 'sinatra/reloader'
require 'json'
require './lib/game_collection'
require './lib/addresses'
require './lib/mailer'
require './lib/game'
require './lib/settings'
require './lib/configure'

module Reversi
  class Main < Sinatra::Base
    configure :production do
      begin
        settings = Settings.new
        Configure.init_sendgrid(settings)
      rescue => e
        puts e.backtrace
        puts e.inspect
      end
    end

    post '/game' do
      begin
        # parse email address from request
        from = Addresses.get_address(params[:from])
        to = Addresses.get_address(params[:subject])

        # fail to parse address if it is nil
        mailer = Mailer.new
        if from != nil && to == nil then
          logger.info "Failed to parse opponent player email address. Subject must include email address."
          return 'Fail to parse to address'
        end
        logger.info "from: #{from} to: #{to}"
        if from == nil && to == nil then
          logger.info "Failed to parse email address. Please check the request."
          logger.info "from: #{params[:from]}"
          logger.info "subject: #{params[:subject]}"
          return 'Fail to parse to and from address'
        end
        # get game data from DB
        dba = GameCollection.new
        game = dba.find(from, to)
        # create game data if not exist
        if game == nil then
          game = Game.new
          # odd:first move:black
          game.player_odd = to
          # even:second move:white
          game.player_even = from
          id = dba.insert(game)
          game = dba.findById(id)
          #puts "create new game"
          logger.info "create new game"
        end

        # send email for each player
        logger.info "game._id: #{game._id}"
        mailer.send(game)
      rescue => e
        puts e.inspect
        logger.error e.backtrace
        logger.error e.inspect
      end

      'Success'
    end

    post '/event' do
      begin
        request.body.rewind
        data = JSON.parse(request.body.read)
        data.each{|event|
          begin
            dba = GameCollection.new
            # handle only click event
            next if event['event'] != "click"
            logger.info "got click event"
            # requester address
            email = event['email']
            # parse the url clicked by user
            event_data = Game.parse_cell_url(event['url'])
            # validate the event data
            game = dba.findById(BSON::ObjectId.from_string(event_data["obj_id"]))
            if game == nil then
              logger.info "Could not find valid game. The event was ignored."
              break
            end
            if game.turn != event_data['turn'] then
              logger.info "Invalid turn event."
              break;
            end
{'fraction_non_alphanumeric': 0.09522989495436542, 'fraction_numerical': 0.00034441191665231615, 'mean_word_length': 1.7331764705882353, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1485908', 'n_tokens_mistral': 1565, 'n_tokens_neox': 1561, 'n_words': 510}