Class: Lita::Handlers::Totems

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/totems.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.route_regex(action_capture_group) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/lita/handlers/totems.rb', line 9

def self.route_regex(action_capture_group)
  %r{
  ^totems?\s+
  (#{action_capture_group})\s+
  (?<totem>\w+)\s*
  (?<message>.*)?
  }x
end

Instance Method Details

#add(response) ⇒ Object



93
94
95
96
97
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
138
# File 'lib/lita/handlers/totems.rb', line 93

def add(response)
  totem = response.match_data[:totem]
  unless redis.exists("totem/#{totem}")
    response.reply %{Error: there is no totem "#{totem}".}
    return
  end

  user_id = response.user.id

  if queued_by_user(user_id).include?(totem)
    response.reply %{Error: you are already in the queue for "#{totem}".}
    return
  end

  if redis.smembers("user/#{user_id}/totems").include?(totem)
    response.reply %{Error: you already have the totem "#{totem}".}
    return
  end

  message = response.match_data[:message]

  token_acquired = false
  queue_size     = nil
  Redis::Semaphore.new("totem/#{totem}", redis: redis).lock do
    redis.hset("totem/#{totem}/message", user_id, message) if message && message != ""
    if redis.llen("totem/#{totem}/list") == 0 && redis.get("totem/#{totem}/owning_user_id").nil?
      # take it:
      token_acquired = true
      redis.set("totem/#{totem}/owning_user_id", user_id)
      redis.hset("totem/#{totem}/waiting_since", user_id, Time.now.to_i)
    else
      # queue:
      queue_size = redis.rpush("totem/#{totem}/list", user_id)
      redis.hset("totem/#{totem}/waiting_since", user_id, Time.now.to_i)
    end
  end

  if token_acquired
    # TODO don't readd to totems you are already waiting for!
    redis.sadd("user/#{user_id}/totems", totem)
    response.reply(%{#{response.user.name}, you now have totem "#{totem}".})
  else
    response.reply(%{#{response.user.name}, you are \##{queue_size} in line for totem "#{totem}".})
  end

end

#create(response) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/lita/handlers/totems.rb', line 80

def create(response)
  totem = response.match_data[:totem]

  if redis.exists("totem/#{totem}")
    response.reply %{Error: totem "#{totem}" already exists.}
  else
    redis.set("totem/#{totem}", 1)
    redis.sadd("totems", totem)
    response.reply %{Created totem "#{totem}".}
  end

end

#destroy(response) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/lita/handlers/totems.rb', line 65

def destroy(response)
  totem = response.match_data[:totem]
  if redis.exists("totem/#{totem}")
    redis.del("totem/#{totem}")
    redis.del("totem/#{totem}/list")
    redis.srem("totems", totem)
    owning_user_id = redis.get("totem/#{totem}/owning_user_id")
    redis.srem("user/#{owning_user_id}/totems", totem) if owning_user_id
    redis.del("totem/#{totem}/waiting_since")
    response.reply(%{Destroyed totem "#{totem}".})
  else
    response.reply(%{Error: totem "#{totem}" doesn't exist.})
  end
end

#info(response) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/lita/handlers/totems.rb', line 197

def info(response)
  totem_param = response.match_data[:totem]
  resp        = unless totem_param.nil? || totem_param.empty?
                  list_users_print(totem_param)
                else
                  users_cache = new_users_cache
                  r           = "Totems:\n"
                  redis.smembers("totems").each do |totem|
                    r += "- #{totem}\n"
                    r += list_users_print(totem, '  ', users_cache)
                  end
                  r
                end
  response.reply resp
end

#kick(response) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/lita/handlers/totems.rb', line 168

def kick(response)
  totem = response.match_data[:totem]
  unless redis.exists("totem/#{totem}")
    response.reply %{Error: there is no totem "#{totem}".}
    return
  end

  past_owning_user_id = redis.get("totem/#{totem}/owning_user_id")
  if past_owning_user_id.nil?
    response.reply %{Error: Nobody owns totem "#{totem}" so you can't kick someone from it.}
    return
  end

  redis.srem("user/#{past_owning_user_id}/totems", totem)
  redis.hdel("totem/#{totem}/waiting_since", past_owning_user_id)
  redis.hdel("totem/#{totem}/message", past_owning_user_id)
  robot.send_messages(Lita::Source.new(user: Lita::User.find_by_id(past_owning_user_id)), %{You have been kicked from totem "#{totem}".})
  next_user_id = redis.lpop("totem/#{totem}/list")
  if next_user_id
    redis.set("totem/#{totem}/owning_user_id", next_user_id)
    redis.sadd("user/#{next_user_id}/totems", totem)
    redis.hset("totem/#{totem}/waiting_since", next_user_id, Time.now.to_i)
    robot.send_messages(Lita::Source.new(user: Lita::User.find_by_id(next_user_id)), %{You are now in possession of totem "#{totem}".})
  else
    redis.del("totem/#{totem}/owning_user_id")
  end

end

#yield(response) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/lita/handlers/totems.rb', line 140

def yield(response)
  user_id               = response.user.id
  totems_owned_by_user  = redis.smembers("user/#{user_id}/totems")
  totems_queued_by_user = queued_by_user(user_id)
  if totems_owned_by_user.empty? && totems_queued_by_user.empty?
    response.reply "Error: You do not have any totems to yield."
  elsif totems_owned_by_user.size == 1 && !response.match_data[:totem] && totems_queued_by_user.empty?
    yield_totem(totems_owned_by_user[0], user_id, response)
  else
    totem_specified = response.match_data[:totem]
    # if they don't specify and are only queued for a single totem, yield that one
    totem_specified = totems_queued_by_user.first if !totem_specified && totems_queued_by_user.size == 1 && totems_owned_by_user.empty?
    if totem_specified
      if totems_owned_by_user.include?(totem_specified)
        yield_totem(totem_specified, user_id, response)
      elsif totems_queued_by_user.include?(totem_specified)
        redis.lrem("totem/#{totem_specified}/list", 0, user_id)
        redis.hdel("totem/#{totem_specified}/waiting_since", user_id)
        response.reply("You are no longer in line for the \"#{totem_specified}\" totem.")
      else
        response.reply %{Error: You don't own and aren't waiting for the "#{totem_specified}" totem.}
      end
    else
      response.reply "You must specify a totem to yield.  Totems you own: #{totems_owned_by_user.sort}.  Totems you are in line for: #{totems_queued_by_user.sort}."
    end
  end
end