60 lines
1.8 KiB
Ruby
60 lines
1.8 KiB
Ruby
require "active_job"
|
|
|
|
class SendPuzzleJob < ActiveJob::Base
|
|
# No idea what this means
|
|
queue_as :default
|
|
|
|
# Do something later
|
|
def perform
|
|
# Remove the puzzle command from everyone
|
|
$puzzle_solver_role.members.each do |member|
|
|
member.remove_role $puzzle_solver_role
|
|
end
|
|
|
|
# Set the previous puzzle as used
|
|
current_puzzle = Puzzle.current.first
|
|
if current_puzzle.nil?
|
|
$study_channel.send "Aborting send puzzle job because there's no current puzzle"
|
|
self.class.enqueue
|
|
end
|
|
current_puzzle.state = "used"
|
|
last_public_id = current_puzzle.public_id
|
|
current_puzzle.save
|
|
|
|
## Pick a puzzle to use
|
|
puzzle = Puzzle.where(public_id: last_public_id + 1).first
|
|
# If we don't have a puzzle with the next assigned public ID
|
|
if puzzle.nil?
|
|
# Get a random puzzle (it's possible that this random puzzle has a puzzle ID in the future, but we'll grab it anyway)
|
|
puzzle = Puzzle.ready.order("RANDOM()").first
|
|
# Set its public id
|
|
puzzle.public_id = last_public_id + 1
|
|
# We'll save it later
|
|
end
|
|
|
|
# Check if we found a puzzle
|
|
if puzzle.present?
|
|
# Send the new puzzle
|
|
# msg = $puzzle_channel.send puzzle.get_message_text
|
|
msgs = puzzle.get_message_text.map {|t| $puzzle_channel.send t }
|
|
|
|
# Save puzzle text message id; really we should save all of them
|
|
puzzle.message_id = msgs.second.id
|
|
# Mark it as current
|
|
puzzle.state = "current"
|
|
# Save
|
|
puzzle.save
|
|
else
|
|
# Panic
|
|
$study_channel.send "<@#{$me.id}> Help! No more puzzles! No more Club???"
|
|
end
|
|
|
|
self.class.enqueue
|
|
end
|
|
|
|
def self.enqueue
|
|
# Get ready to send the next puzzle at the next UTC sunday
|
|
self.set(wait_until: Date.today.next_occurring(:sunday).beginning_of_day).perform_later
|
|
end
|
|
end
|