-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqueue.rb
63 lines (48 loc) · 1.39 KB
/
queue.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require './config'
require './filter'
require 'zlib'
class MessageQueue
include Filter
def initialize(bot)
@queue = Array.new
@history_length = 20
@last_messages = Array.new(@history_length)
@bot = bot
end
def add(message, always_show = false)
checksum = Zlib::crc32(message)
resize(calc_size(@bot.users.size))
return if @queue.include?(message) || (@last_messages.include?(checksum) && !always_show) || message.length > @bot.message_length
@queue << filter(message)
@last_messages.shift
@last_messages[@history_length-1] = checksum
end
def clear
@queue.clear
end
def resize(size)
return if size == @last_messages.length
if @last_messages.length > size
@last_messages.shift(@last_messages.length - size)
else
delta = size - @last_messages.length
delta.times { @last_messages.unshift(nil) }
end
@history_length = size
end
def next(max_length)
length = 0
sending = @queue.select {|value| (length = length + value.length) && length <= max_length}
sending.each { |value|
@queue.delete_at(@queue.index(value))
}
return sending
end
def calc_size(base)
size = 1 + (Math::log10(base) * Math::log2(base) * 0.3).round
return size
end
def size
return {history_length: @history_length, history_size: @last_messages.size}
end
end