Class: SSCBot::User::JRobotMessageSender

Inherits:
MessageSender show all
Defined in:
lib/ssc.bot/user/jrobot_message_sender.rb

Overview

Author:

  • Jonathan Bradley Whited

Since:

  • 0.1.0

Constant Summary

Constants inherited from MessageSender

MessageSender::DEFAULT_ESCAPE_STR, MessageSender::DEFAULT_FLOOD_COUNT, MessageSender::DEFAULT_FLOOD_MIN_SLEEP, MessageSender::DEFAULT_FLOOD_SECS, MessageSender::MM_AREA, MessageSender::MM_BOUNTY, MessageSender::MM_COORD, MessageSender::MM_ENERGY, MessageSender::MM_FLAGS, MessageSender::MM_FREQ, MessageSender::MM_KILLED, MessageSender::MM_KILLER, MessageSender::MM_RED, MessageSender::MM_REDBOUNTY, MessageSender::MM_REDFLAGS, MessageSender::MM_REDNAME, MessageSender::MM_SELFNAME, MessageSender::MM_SQUAD, MessageSender::MM_TICKNAME

Instance Attribute Summary collapse

Attributes inherited from MessageSender

#escape_str, #flood_count, #flood_min_sleep, #flood_secs, #message_count, #message_time

Instance Method Summary collapse

Methods inherited from MessageSender

#escape_pub, #prevent_flood, #put_or_type, #send, #send_chat, #send_chat_to, #send_freq, #send_freq_eq, #send_or_types, #send_or_types_safe, #send_private, #send_private_to, #send_private_to_last, #send_pub, #send_q_chat, #send_q_chat_eq, #send_q_enter, #send_q_find, #send_q_kill, #send_q_leave, #send_q_lines, #send_q_lines_eq, #send_q_loadmacro, #send_q_log, #send_q_log_to, #send_q_logbuffer, #send_q_logbuffer_to, #send_q_namelen, #send_q_namelen_eq, #send_q_savemacro, #send_q_spec, #send_q_team, #send_safe, #send_squad, #send_squad_to, #send_team, #send_team2, #types, #types_safe

Constructor Details

#initialize(auto_delay: 110, msg_key: nil, os: Util::OS, warn_user: false, warn_user_key: KeyEvent::VK_BACK_SPACE, warn_user_sleep: 0.747, **kargs) ⇒ JRobotMessageSender

Returns a new instance of JRobotMessageSender.

Since:

  • 0.1.0



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ssc.bot/user/jrobot_message_sender.rb', line 51

def initialize(auto_delay: 110,msg_key: nil,os: Util::OS,warn_user: false,
               warn_user_key: KeyEvent::VK_BACK_SPACE,warn_user_sleep: 0.747,**kargs)
  super(**kargs)

  @clipboard = Toolkit.getDefaultToolkit.getSystemClipboard
  @msg_key = msg_key
  @os = os
  @robot = Robot.new
  @warn_user = warn_user
  @warn_user_key = warn_user_key
  @warn_user_sleep = warn_user_sleep

  @robot.setAutoDelay(auto_delay)

  @shortcut_paste = lambda do |ms|
    if ms.os == :macos
      @shortcut_paste_macos.call(ms)
    else
      @shortcut_paste_default.call(ms)
    end
  end
  @shortcut_paste_default = ->(ms) { ms.roll_keys(KeyEvent::VK_CONTROL,KeyEvent::VK_V) }
  @shortcut_paste_macos = ->(ms) { ms.roll_keys(KeyEvent::VK_META,KeyEvent::VK_V) }
end

Instance Attribute Details

#clipboardObject

Since:

  • 0.1.0



40
41
42
# File 'lib/ssc.bot/user/jrobot_message_sender.rb', line 40

def clipboard
  @clipboard
end

#msg_keyObject

Since:

  • 0.1.0



41
42
43
# File 'lib/ssc.bot/user/jrobot_message_sender.rb', line 41

def msg_key
  @msg_key
end

#osObject

Since:

  • 0.1.0



42
43
44
# File 'lib/ssc.bot/user/jrobot_message_sender.rb', line 42

def os
  @os
end

#robotObject

Since:

  • 0.1.0



43
44
45
# File 'lib/ssc.bot/user/jrobot_message_sender.rb', line 43

def robot
  @robot
end

#shortcut_pasteObject

Since:

  • 0.1.0



44
45
46
# File 'lib/ssc.bot/user/jrobot_message_sender.rb', line 44

def shortcut_paste
  @shortcut_paste
end

#shortcut_paste_defaultObject

Since:

  • 0.1.0



45
46
47
# File 'lib/ssc.bot/user/jrobot_message_sender.rb', line 45

def shortcut_paste_default
  @shortcut_paste_default
end

#shortcut_paste_macosObject

Since:

  • 0.1.0



46
47
48
# File 'lib/ssc.bot/user/jrobot_message_sender.rb', line 46

def shortcut_paste_macos
  @shortcut_paste_macos
end

#warn_user_keyObject

Since:

  • 0.1.0



48
49
50
# File 'lib/ssc.bot/user/jrobot_message_sender.rb', line 48

def warn_user_key
  @warn_user_key
end

#warn_user_sleepObject

Since:

  • 0.1.0



49
50
51
# File 'lib/ssc.bot/user/jrobot_message_sender.rb', line 49

def warn_user_sleep
  @warn_user_sleep
end

Instance Method Details

#backspaceObject

Since:

  • 0.1.0



76
77
78
# File 'lib/ssc.bot/user/jrobot_message_sender.rb', line 76

def backspace
  return type_key(KeyEvent::VK_BACK_SPACE)
end

#copy(str) ⇒ Object

Since:

  • 0.1.0



80
81
82
83
84
# File 'lib/ssc.bot/user/jrobot_message_sender.rb', line 80

def copy(str)
  @clipboard.setContents(StringSelection.new(str),nil)

  return self
end

#enterObject

Since:

  • 0.1.0



86
87
88
# File 'lib/ssc.bot/user/jrobot_message_sender.rb', line 86

def enter
  return type_key(KeyEvent::VK_ENTER)
end

#paste(str = nil) ⇒ Object

Since:

  • 0.1.0



90
91
92
93
94
95
96
# File 'lib/ssc.bot/user/jrobot_message_sender.rb', line 90

def paste(str=nil)
  copy(str) unless str.nil?

  @shortcut_paste.call(self)

  return self
end

#press_key(*key_codes) ⇒ Object

Since:

  • 0.1.0



98
99
100
101
102
103
104
# File 'lib/ssc.bot/user/jrobot_message_sender.rb', line 98

def press_key(*key_codes)
  key_codes.each do |key_code|
    @robot.keyPress(key_code)
  end

  return self
end

#put(message) ⇒ Object

Since:

  • 0.1.0



106
107
108
109
110
111
112
113
114
115
# File 'lib/ssc.bot/user/jrobot_message_sender.rb', line 106

def put(message)
  # If do type_msg_key() and then warn_user(), then a backspace from
  #   warn_user() will cancel out the msg key.
  # Could do type_msg_key().warn_user().type_msg_key(), but then if the
  #   client is in windowed mode and msg key is a tab, then a backspace
  #   from warn_user() will do nothing.
  return warn_user.
         type_msg_key.
         paste(message)
end

#release_key(*key_codes) ⇒ Object

Since:

  • 0.1.0



117
118
119
120
121
122
123
# File 'lib/ssc.bot/user/jrobot_message_sender.rb', line 117

def release_key(*key_codes)
  key_codes.each do |key_code|
    @robot.keyRelease(key_code)
  end

  return self
end

#roll_keys(*key_codes) ⇒ Object

Since:

  • 0.1.0



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ssc.bot/user/jrobot_message_sender.rb', line 125

def roll_keys(*key_codes)
  key_codes.each do |key_code|
    @robot.keyPress(key_code)
  end

  (key_codes.length - 1).downto(0) do |i|
    @robot.keyRelease(key_codes[i])
  end

  return self
end

#send_messageObject

Since:

  • 0.1.0



137
138
139
# File 'lib/ssc.bot/user/jrobot_message_sender.rb', line 137

def send_message
  enter
end

#type(message) ⇒ Object

TODO: implement type(message)

Since:

  • 0.1.0



142
143
144
145
# File 'lib/ssc.bot/user/jrobot_message_sender.rb', line 142

def type(message)
  # TODO: implement type(message)
  super(message)
end

#type_key(*key_codes) ⇒ Object

Since:

  • 0.1.0



147
148
149
150
151
152
153
154
# File 'lib/ssc.bot/user/jrobot_message_sender.rb', line 147

def type_key(*key_codes)
  key_codes.each do |key_code|
    @robot.keyPress(key_code)
    @robot.keyRelease(key_code)
  end

  return self
end

#type_msg_keyObject

Since:

  • 0.1.0



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/ssc.bot/user/jrobot_message_sender.rb', line 156

def type_msg_key
  if @msg_key
    if @msg_key.respond_to?(:call)
      @msg_key.call(self)
    else
      type_key(@msg_key)
    end
  end

  return self
end

#warn_userObject

Since:

  • 0.1.0



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/ssc.bot/user/jrobot_message_sender.rb', line 168

def warn_user
  if @warn_user
    if @warn_user_key.respond_to?(:call)
      @warn_user_key.call(self)
    else
      press_key(@warn_user_key)
      sleep(@warn_user_sleep)
      release_key(@warn_user_key)
    end
  end

  return self
end