forked from tobymao/18xx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.rb
executable file
·60 lines (46 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
# frozen_string_literal: true
require 'logger'
require 'require_all'
require_relative 'models'
require_rel './models'
require_relative 'lib/assets'
require_relative 'lib/bus'
require_relative 'lib/mail'
PRODUCTION = ENV['RACK_ENV'] == 'production'
LOGGER = Logger.new($stdout)
Bus.configure(DB)
ASSETS = Assets.new(precompiled: PRODUCTION)
MessageBus.subscribe '/turn', -1 do |msg|
data = msg.data
users = User.where(id: data['user_ids']).all
game = Game[data['game_id']]
minute_ago = Time.now - 60
connected = Session
.where(user: users)
.group_by(:user_id)
.having { max(updated_at) > minute_ago }
.select(:user_id)
.all
.map(&:user_id)
users = users.reject do |user|
next true if user.settings['notifications'] == false
next false if data['force']
email_sent = user.settings['email_sent'] || 0
connected.include?(user.id) || email_sent > minute_ago.to_i
end
next if users.empty?
html = ASSETS.html(
'assets/app/mail/turn.rb',
game_data: game.to_h(include_actions: true),
game_url: data['game_url'],
)
users.each do |user|
user.settings['email_sent'] = Time.now.to_i
user.save
Mail.send(user, "18xx.games Game: #{game.title} - #{game.id} - #{data['type']}", html)
LOGGER.info("mail sent for game: #{game.id} to user: #{user.id}")
end
rescue Exception => e # rubocop:disable Lint/RescueException
puts e
end
sleep