Skip to content

Instantly share code, notes, and snippets.

@vito-lbs
Last active June 17, 2021 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vito-lbs/47bed7845064aecd6d5f66cc52150dc5 to your computer and use it in GitHub Desktop.
Save vito-lbs/47bed7845064aecd6d5f66cc52150dc5 to your computer and use it in GitHub Desktop.
registrar-printer
# frozen_string_literal: true
source "https://rubygems.org"
gem "redis"
gem "pry"
GEM
remote: https://rubygems.org/
specs:
redis (3.3.3)
PLATFORMS
ruby
DEPENDENCIES
redis
BUNDLED WITH
1.14.3
require 'redis'
require 'json'
require 'pry'
REDIS = Redis.new(url: ENV['REDIS_URL'])
REDIS_CHANNEL = ENV['REDIS_CHANNEL'] || 'ctf-registrar-development'
TTY = File.open("/dev/tty0", 'w')
TTY.sync = true
LP = File.open("/dev/usb/lp0", 'w')
LP.sync = true
LP.print "\x1b@" # reset
def enc(string)
string.to_s.encode('cp437', fallback: proc do |c|
c.codepoints.map do |k|
"\x1dB1\\u#{k.to_s(16).rjust(4, '0')}\x1dB0"
end.join
end
)
end
def ps(string)
TTY.puts string
LP.puts enc(string)
end
def w(string)
TTY.write string
LP.write enc(string)
end
def justify(justification)
LP.write "\x1ba#{justification}"
end
def header(content)
LP.write "\x1b!\x28"
justify 1
TTY.write "\033[1;45m"
LP.puts enc(content)
TTY.write content
TTY.write "\033[0m "
justify 0
LP.write "\x1b!\x00"
end
def ugc
LP.write "\x1b!\x81"
TTY.write "\033[1m"
yield
TTY.write "\033[0m"
LP.write "\x1b!\x00"
end
def timestamp
LP.puts
LP.write "\x1b!\x01"
TTY.write " \033[1;30m"
justify 1
w Time.now.utc
justify 0
TTY.write "\033[0m"
LP.write "\x1b!\x00"
ps ''
LP.puts
end
def format(message_kind, subject, rest='')
header do
ps message_kind
end
ps subject
unless rest.empty?
ps rest
end
end
def user_create(d)
username = d['user']['username']
header "User Signup"
ugc do
w username
end
end
def team_create(d)
username = d['user']['username']
teamname = d['team']['name']
header "Team Create"
ugc do
w username
end
w " created "
ugc do
w teamname
end
end
def team_join(d)
username = d['user']['username']
teamname = d['team']['name']
header "Team Join"
ugc do
w username
end
w " joined "
ugc do
w teamname
end
end
REDIS.subscribe REDIS_CHANNEL do |on|
on.subscribe do |channel, subscriptions|
TTY.puts "subscribed to #{channel} with #{subscriptions} subscriptions"
trap :INT do
LP.write "\n\n\n\n\n\x1dV0"
TTY.puts "EXITING #{Time.now.utc}"
exit
end
end
on.message do |_channel, msg|
d = JSON.parse msg
message_kind = d['kind']
unless respond_to?(message_kind, d)
TTY.puts "UNKNOWN MESSAGE #{message_kind}"
next
end
send message_kind, d
timestamp
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment