Skip to content

Commit

Permalink
Merge pull request #273 from datafruits/daily-glorp-lottery
Browse files Browse the repository at this point in the history
daily-glorp-lottery
  • Loading branch information
mcfiredrill authored Sep 14, 2023
2 parents 0b9289a + bd577f5 commit 7502b8e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/models/experience_point_award.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class ExperiencePointAward < ApplicationRecord
:radio_enthusiast, # scheduling show
:fruit_maniac, # clicking fruit buttons
:streamingatron, # streaming live
:glorppy, # daily glorp lottery
:gloppy, # daily glop lottery
]

private
Expand All @@ -28,5 +30,8 @@ def maybe_level_up

def send_notification
Notification.create! source: self, notification_type: "experience_point_award", user: self.user, send_to_chat: false
if award_type === "glorpy" || award_type === "gloppy"
Notification.create! source: self, notification_type: "experience_point_award", user: self.user, send_to_chat: true
end
end
end
5 changes: 4 additions & 1 deletion app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class Notification < ApplicationRecord
:level_up,
:experience_point_award,
:fruit_ticket_gift,
:supporter_fruit_ticket_stipend
:supporter_fruit_ticket_stipend,
:glorp_lottery_winner
]

private
Expand Down Expand Up @@ -64,6 +65,8 @@ def set_message
"#{self.source.from_user.username} sent you Ƒ#{self.source.amount} fruit tickets!"
when "supporter_fruit_ticket_stipend"
"You got Ƒ#{self.source.amount} fruit tickets for supporting datafruits. The bank of fruit tickets thanks you for your support!"
when "glorp_lottery_winner"
":#{self.award_type.split("py").first}:!!! #{self.user.username} got #{self.award_type} points!"
end
end

Expand Down
36 changes: 36 additions & 0 deletions app/workers/daily_glorp_lottery_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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}!"
end

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

def current_chat_users
sockets = redis.smembers "datafruits:chat:sockets"
usernames = sockets.map { |s| return s.split(":").last }
usernames.filter do |u|
User.where(username: u).any?
end
end
end
end
3 changes: 3 additions & 0 deletions config/sidekiq.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@
schedule_monitor:
every: 1m
class: ScheduleMonitorWorker
daily_glorp_lottery:
every: 24h
class: DailyGlorpLotteryWorker
production:
:concurrency: 5

0 comments on commit 7502b8e

Please sign in to comment.