Class: SSCBot::User::MessageSender

Inherits:
Object
  • Object
show all
Extended by:
AttrBool::Ext
Defined in:
lib/ssc.bot/user/message_sender.rb

Overview

Author:

  • Jonathan Bradley Whited

Since:

  • 0.1.0

Direct Known Subclasses

JRobotMessageSender

Constant Summary collapse

DEFAULT_ESCAPE_STR =

Since:

  • 0.1.0

'.'
DEFAULT_FLOOD_COUNT =

Since:

  • 0.1.0

8
DEFAULT_FLOOD_MIN_SLEEP =

Since:

  • 0.1.0

0.001
DEFAULT_FLOOD_SECS =

Since:

  • 0.1.0

6
MM_TICKNAME =

Message Macros

  • In order of F1 Help box.

Since:

  • 0.1.0

'%tickname'
MM_SELFNAME =

Since:

  • 0.1.0

'%selfname'
MM_SQUAD =

Since:

  • 0.1.0

'%squad'
MM_FREQ =

Since:

  • 0.1.0

'%freq'
MM_BOUNTY =

Since:

  • 0.1.0

'%bounty'
MM_FLAGS =

Since:

  • 0.1.0

'%flags'
MM_ENERGY =

Since:

  • 0.1.0

'%energy'
MM_KILLER =

Since:

  • 0.1.0

'%killer'
MM_KILLED =

Since:

  • 0.1.0

'%killed'
MM_COORD =

Since:

  • 0.1.0

'%coord'
MM_AREA =

Since:

  • 0.1.0

'%area'
MM_RED =

Since:

  • 0.1.0

'%red'
MM_REDNAME =

Since:

  • 0.1.0

'%redname'
MM_REDBOUNTY =

Since:

  • 0.1.0

'%redbounty'
MM_REDFLAGS =

Since:

  • 0.1.0

'%redflags'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(escape_percent: false, escape_space: true, escape_str: DEFAULT_ESCAPE_STR, flood_count: DEFAULT_FLOOD_COUNT, flood_min_sleep: DEFAULT_FLOOD_MIN_SLEEP, flood_secs: DEFAULT_FLOOD_SECS, staff: false) ⇒ MessageSender

Returns a new instance of MessageSender.

Since:

  • 0.1.0



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ssc.bot/user/message_sender.rb', line 82

def initialize(escape_percent: false,escape_space: true,escape_str: DEFAULT_ESCAPE_STR,
               flood_count: DEFAULT_FLOOD_COUNT,flood_min_sleep: DEFAULT_FLOOD_MIN_SLEEP,
               flood_secs: DEFAULT_FLOOD_SECS,staff: false)
  super()

  @escape_percent = escape_percent
  @escape_space = escape_space
  @escape_str = escape_str
  @flood_count = flood_count
  @flood_min_sleep = flood_min_sleep
  @flood_secs = flood_secs
  @message_count = 0
  @message_time = Time.now
  @staff = staff
end

Instance Attribute Details

#escape_strObject

Since:

  • 0.1.0



62
63
64
# File 'lib/ssc.bot/user/message_sender.rb', line 62

def escape_str
  @escape_str
end

#flood_countObject

Since:

  • 0.1.0



63
64
65
# File 'lib/ssc.bot/user/message_sender.rb', line 63

def flood_count
  @flood_count
end

#flood_min_sleepObject

Since:

  • 0.1.0



64
65
66
# File 'lib/ssc.bot/user/message_sender.rb', line 64

def flood_min_sleep
  @flood_min_sleep
end

#flood_secsObject

Since:

  • 0.1.0



65
66
67
# File 'lib/ssc.bot/user/message_sender.rb', line 65

def flood_secs
  @flood_secs
end

#message_countObject (readonly)

Since:

  • 0.1.0



66
67
68
# File 'lib/ssc.bot/user/message_sender.rb', line 66

def message_count
  @message_count
end

#message_timeObject (readonly)

Since:

  • 0.1.0



67
68
69
# File 'lib/ssc.bot/user/message_sender.rb', line 67

def message_time
  @message_time
end

Instance Method Details

#escape_pub(message, escape_percent: @escape_percent, escape_space: @escape_space, escape_str: @escape_str, staff: @staff) ⇒ Object

Since:

  • 0.1.0



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/ssc.bot/user/message_sender.rb', line 98

def escape_pub(message,escape_percent: @escape_percent,escape_space: @escape_space,
               escape_str: @escape_str,staff: @staff)
  if escape_percent
    message = message.gsub('%','%%')
  end

  escape = false

  case message[0]
  when '#'
    escape = true
  else
    if escape_space && message[0] =~ /[[:space:]]/
      escape = true
    else
      stripped_message = Util.u_lstrip(message)

      case stripped_message[0]
      when ':'
        if stripped_message.index(':',1)
          escape = true
        end
      when '/',"'",'"',';','='
        escape = true
      when '?'
        if stripped_message[1] =~ /[[:alpha:]]/
          escape = true
        end
      when '*','-'
        escape = true if staff
      end
    end
  end

  if escape
    message = "#{escape_str}#{message}"
  end

  return message
end

#prevent_floodObject

Since:

  • 0.1.0



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/ssc.bot/user/message_sender.rb', line 139

def prevent_flood
  @message_count += 1

  if @message_count >= @flood_count
    diff_time = Time.now - @message_time

    if diff_time <= @flood_secs
      sleep_secs = (@flood_secs - diff_time).round(4) + 0.001
      sleep_secs = @flood_min_sleep if sleep_secs < @flood_min_sleep
    else
      sleep_secs = @flood_min_sleep
    end

    sleep(sleep_secs)

    @message_count = 0
  end

  @message_time = Time.now
end

#put(message) ⇒ Object

Raises:

Since:

  • 0.1.0



70
71
72
# File 'lib/ssc.bot/user/message_sender.rb', line 70

def put(message)
  raise AbstractMethodError,__method__
end

#put_or_type(message) ⇒ Object

Since:

  • 0.1.0



160
161
162
# File 'lib/ssc.bot/user/message_sender.rb', line 160

def put_or_type(message)
  put(message)
end

#send(message) ⇒ Object

Since:

  • 0.1.0



164
165
166
167
# File 'lib/ssc.bot/user/message_sender.rb', line 164

def send(message)
  put(message)
  send_message
end

#send_chat(message) ⇒ Object

Since:

  • 0.1.0



193
194
195
# File 'lib/ssc.bot/user/message_sender.rb', line 193

def send_chat(message)
  send_safe(";#{message}")
end

#send_chat_to(channel, message) ⇒ Object

Since:

  • 0.1.0



197
198
199
# File 'lib/ssc.bot/user/message_sender.rb', line 197

def send_chat_to(channel,message)
  send_safe(";#{channel};#{message}")
end

#send_freq(message) ⇒ Object

Since:

  • 0.1.0



201
202
203
# File 'lib/ssc.bot/user/message_sender.rb', line 201

def send_freq(message)
  send_safe(%Q("#{message}))
end

#send_freq_eq(freq) ⇒ Object

Since:

  • 0.1.0



205
206
207
# File 'lib/ssc.bot/user/message_sender.rb', line 205

def send_freq_eq(freq)
  send_safe("=#{freq}")
end

#send_messageObject

Raises:

Since:

  • 0.1.0



74
75
76
# File 'lib/ssc.bot/user/message_sender.rb', line 74

def send_message
  raise AbstractMethodError,__method__
end

#send_or_types(message) ⇒ Object

Since:

  • 0.1.0



169
170
171
# File 'lib/ssc.bot/user/message_sender.rb', line 169

def send_or_types(message)
  send(message)
end

#send_or_types_safe(message) ⇒ Object

Since:

  • 0.1.0



173
174
175
176
# File 'lib/ssc.bot/user/message_sender.rb', line 173

def send_or_types_safe(message)
  send_or_types(message)
  prevent_flood
end

#send_private(message) ⇒ Object

Since:

  • 0.1.0



209
210
211
# File 'lib/ssc.bot/user/message_sender.rb', line 209

def send_private(message)
  send_safe("/#{message}")
end

#send_private_to(name, message) ⇒ Object

Since:

  • 0.1.0



213
214
215
# File 'lib/ssc.bot/user/message_sender.rb', line 213

def send_private_to(name,message)
  send_safe(":#{name}:#{message}")
end

#send_private_to_last(message, last = 1) ⇒ Object

Since:

  • 0.1.0



217
218
219
220
221
222
223
224
225
# File 'lib/ssc.bot/user/message_sender.rb', line 217

def send_private_to_last(message,last=1)
  put_or_type('::')

  while (last -= 1) > 0
    put_or_type(':')
  end

  send_safe(message)
end

#send_pub(message, **kargs) ⇒ Object

Since:

  • 0.1.0



227
228
229
# File 'lib/ssc.bot/user/message_sender.rb', line 227

def send_pub(message,**kargs)
  send_safe(escape_pub(message,**kargs))
end

#send_q_chatObject

Since:

  • 0.1.0



231
232
233
# File 'lib/ssc.bot/user/message_sender.rb', line 231

def send_q_chat
  send_safe('?chat')
end

#send_q_chat_eq(*names) ⇒ Object

Since:

  • 0.1.0



235
236
237
# File 'lib/ssc.bot/user/message_sender.rb', line 235

def send_q_chat_eq(*names)
  send_safe("?chat=#{names.join(',')}")
end

#send_q_enterObject

Since:

  • 0.1.0



239
240
241
# File 'lib/ssc.bot/user/message_sender.rb', line 239

def send_q_enter
  send_safe('?enter')
end

#send_q_find(player) ⇒ Object

Since:

  • 0.1.0



243
244
245
# File 'lib/ssc.bot/user/message_sender.rb', line 243

def send_q_find(player)
  send_safe("?find #{player}")
end

#send_q_killObject

Since:

  • 0.1.0



247
248
249
# File 'lib/ssc.bot/user/message_sender.rb', line 247

def send_q_kill
  send_safe('?kill')
end

#send_q_leaveObject

Since:

  • 0.1.0



251
252
253
# File 'lib/ssc.bot/user/message_sender.rb', line 251

def send_q_leave
  send_safe('?leave')
end

#send_q_linesObject

Since:

  • 0.1.0



283
284
285
# File 'lib/ssc.bot/user/message_sender.rb', line 283

def send_q_lines
  send_safe('?lines')
end

#send_q_lines_eq(lines) ⇒ Object

Since:

  • 0.1.0



287
288
289
# File 'lib/ssc.bot/user/message_sender.rb', line 287

def send_q_lines_eq(lines)
  send_safe("?lines=#{lines}")
end

#send_q_loadmacro(filename) ⇒ Object

Since:

  • 0.1.0



255
256
257
# File 'lib/ssc.bot/user/message_sender.rb', line 255

def send_q_loadmacro(filename)
  send_safe("?loadmacro #{filename}")
end

#send_q_logObject

Since:

  • 0.1.0



259
260
261
# File 'lib/ssc.bot/user/message_sender.rb', line 259

def send_q_log
  send_safe('?log')
end

#send_q_log_to(filename) ⇒ Object

Since:

  • 0.1.0



263
264
265
# File 'lib/ssc.bot/user/message_sender.rb', line 263

def send_q_log_to(filename)
  send_safe("?log #{filename}")
end

#send_q_logbufferObject

Since:

  • 0.1.0



267
268
269
# File 'lib/ssc.bot/user/message_sender.rb', line 267

def send_q_logbuffer
  send_safe('?logbuffer')
end

#send_q_logbuffer_to(filename) ⇒ Object

Since:

  • 0.1.0



271
272
273
# File 'lib/ssc.bot/user/message_sender.rb', line 271

def send_q_logbuffer_to(filename)
  send_safe("?logbuffer #{filename}")
end

#send_q_namelenObject

Since:

  • 0.1.0



275
276
277
# File 'lib/ssc.bot/user/message_sender.rb', line 275

def send_q_namelen
  send_safe('?namelen')
end

#send_q_namelen_eq(namelen) ⇒ Object

Since:

  • 0.1.0



279
280
281
# File 'lib/ssc.bot/user/message_sender.rb', line 279

def send_q_namelen_eq(namelen)
  send_safe("?namelen=#{namelen}")
end

#send_q_savemacro(filename) ⇒ Object

Since:

  • 0.1.0



291
292
293
# File 'lib/ssc.bot/user/message_sender.rb', line 291

def send_q_savemacro(filename)
  send_safe("?savemacro #{filename}")
end

#send_q_specObject

Since:

  • 0.1.0



295
296
297
# File 'lib/ssc.bot/user/message_sender.rb', line 295

def send_q_spec
  send_safe('?spec')
end

#send_q_teamObject

Since:

  • 0.1.0



299
300
301
# File 'lib/ssc.bot/user/message_sender.rb', line 299

def send_q_team
  send_safe('?team')
end

#send_safe(message) ⇒ Object

Since:

  • 0.1.0



178
179
180
181
# File 'lib/ssc.bot/user/message_sender.rb', line 178

def send_safe(message)
  send(message)
  prevent_flood
end

#send_squad(message) ⇒ Object

Since:

  • 0.1.0



303
304
305
# File 'lib/ssc.bot/user/message_sender.rb', line 303

def send_squad(message)
  send_safe("##{message}")
end

#send_squad_to(squad, message) ⇒ Object

Since:

  • 0.1.0



307
308
309
# File 'lib/ssc.bot/user/message_sender.rb', line 307

def send_squad_to(squad,message)
  send_safe(":##{squad}:#{message}")
end

#send_team(message) ⇒ Object

Since:

  • 0.1.0



311
312
313
# File 'lib/ssc.bot/user/message_sender.rb', line 311

def send_team(message)
  send_safe("//#{message}")
end

#send_team2(message) ⇒ Object

Since:

  • 0.1.0



315
316
317
# File 'lib/ssc.bot/user/message_sender.rb', line 315

def send_team2(message)
  send_safe("'#{message}")
end

#type(message) ⇒ Object

Raises:

Since:

  • 0.1.0



78
79
80
# File 'lib/ssc.bot/user/message_sender.rb', line 78

def type(message)
  raise AbstractMethodError,__method__
end

#types(message) ⇒ Object

Since:

  • 0.1.0



183
184
185
186
# File 'lib/ssc.bot/user/message_sender.rb', line 183

def types(message)
  type(message)
  send_message
end

#types_safe(message) ⇒ Object

Since:

  • 0.1.0



188
189
190
191
# File 'lib/ssc.bot/user/message_sender.rb', line 188

def types_safe(message)
  types(message)
  prevent_flood
end