Sha256: affd84fa5be0e451157f7a86623628c177884aedaa06517e4e6700f59ab72f80

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

require 'acpc_poker_types/poker_action'
require 'acpc_poker_types/match_state'

require 'contextual_exceptions'
using ContextualExceptions::ClassRefinement

# Sends poker actions according to the ACPC protocol.
module AcpcPokerBasicProxy
  module CommunicationLogic
    class ActionSender
      exceptions :illegal_action_format

      CONCATENATED_MODIFIABLE_ACTIONS = AcpcPokerTypes::PokerAction::MODIFIABLE_ACTIONS.to_a.join

      # Sends the given +action+ to through the given +connection+ in the ACPC
      # format.
      # @param [#write, #ready_to_write?] connection The connection through which the +action+
      #  should be sent.
      # @param [#to_s] match_state The current match state.
      # @param [#to_s] action The action to be sent through the +connection+.
      # @return [Integer] The number of bytes written.
      # @raise (see #validate_match_state)
      # @raise (see #validate_action)
      def self.send_action(connection, match_state, action)
        validate_action action

        full_action = "#{AcpcPokerTypes::MatchState.parse(match_state.to_s)}:#{action.to_s}"
        connection.write full_action
      end

      private

      # @raise IllegalActionFormat
      def self.validate_action(action)
        raise IllegalActionFormat unless self.valid_action?(action)
      end

      def self.valid_action?(action)
        action.to_s.match(/^[#{AcpcPokerTypes::PokerAction::CONCATONATED_ACTIONS}]$/) ||
        action.to_s.match(/^[#{CONCATENATED_MODIFIABLE_ACTIONS}]\d+$/)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
acpc_poker_basic_proxy-2.0.0 lib/acpc_poker_basic_proxy/communication_logic/action_sender.rb