Skip to content

Commit

Permalink
extract daily_glorp_lottery service
Browse files Browse the repository at this point in the history
  • Loading branch information
mcfiredrill committed Oct 18, 2023
1 parent 8ad869e commit 1bae54e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 33 deletions.
38 changes: 38 additions & 0 deletions app/services/daily_glorp_lottery.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class DailyGlorpLottery
include RedisConnection

def draw!
# 1 in 20 chance of getting a glop or glorp?
chance = rand(100)
prize = nil
if chance <= 30
# congratulations, you've won :glorp:
prize = :glorppy
puts "today's prize is #{prize}!"
elsif chance >= 80
# congratulations, you've won :glop:
prize = :gloppy
puts "today's prize is #{prize}!"
else
puts "no winner today!"
end

if prize
# pick random winner
winner_username = current_chat_users.sample
winner = User.find_by(username: winner_username)
if winner
puts "the winner is: #{winner.username}"
ExperiencePointAward.create! award_type: prize, user: winner, amount: rand(5) + 1
end
end

def current_chat_users
sockets = redis.smembers "datafruits:chat:sockets"
usernames = sockets.map { |s| s.split(":").last }
return usernames.filter do |u|
User.where(username: u).any?
end
end
end
end
34 changes: 1 addition & 33 deletions app/workers/daily_glorp_lottery_worker.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,7 @@
class DailyGlorpLotteryWorker < ActiveJob::Base
include RedisConnection
queue_as :default

def perform
# 1 in 20 chance of getting a glop or glorp?
chance = rand(100)
prize = nil
if chance <= 30
# congratulations, you've won :glorp:
prize = :glorppy
puts "today's prize is #{prize}!"
elsif chance >= 80
# congratulations, you've won :glop:
prize = :gloppy
puts "today's prize is #{prize}!"
else
puts "no winner today!"
end

if prize
# pick random winner
winner_username = current_chat_users.sample
winner = User.find_by(username: winner_username)
if winner
puts "the winner is: #{winner.username}"
ExperiencePointAward.create! award_type: prize, user: winner, amount: rand(5) + 1
end
end

def current_chat_users
sockets = redis.smembers "datafruits:chat:sockets"
usernames = sockets.map { |s| s.split(":").last }
return usernames.filter do |u|
User.where(username: u).any?
end
end
DailyGlorpLottery.new.draw!
end
end

0 comments on commit 1bae54e

Please sign in to comment.