Files
infinsweeper/db.rb
2025-06-21 17:38:17 +03:00

79 lines
2.2 KiB
Ruby

require "xxhash"
require "sequel"
require "json"
ALPHANUM = [*"0".."9", *"A".."Z", *"a".."z", "-", "_"].freeze
# DataBase handler module
module DataBase
db_file = File.expand_path("infinsweeper.db")
DB = Sequel.connect("sqlite:///#{db_file}", single_threaded: false)
def self.player_list
DB["select * from Players"].all
end
def self.rm_player(email)
DB["delete from Players where email = ?", email].delete
end
def self.mk_player(username, email, pass)
raise ArgumentError, "Email format is wrong!" unless email.match?(/\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/)
raise ArgumentError, "Password must be at least 8 characters and valid format." unless
pass.match?(/\A[a-zA-Z0-9_.!?@#$%^&*()+=-]+\z/) && pass.length >= 8
digest = XXhash.xxh32(pass, 1234)
path = File.expand_path("db.json")
json = File.exist?(path) ? JSON.parse(File.read(path)) : {}
json["account_num"] ||= 0
account_num = json["account_num"]
json["account_num"] += 1
File.write(path, JSON.pretty_generate(json))
account_num = XXhash.xxh64(account_num, 1234)
code = ""
while account_num.positive?
code << ALPHANUM[account_num % 64]
account_num /= 64
end
code = code.reverse.rjust(12, "0")
DB["insert into Players (email, digest, username, code) values (?, ?, ?, ?)", email, digest, username, code].insert
send_email(email, username, code)
Thread.new do
sleep 24 * 60 * 60
rm_player(email) unless verified?(email)
end
"Successfully registered!"
rescue ArgumentError => e
e.message
rescue Sequel::UniqueConstraintViolation
"Account already exists with this email or username!"
end
def self.verify(code)
DB["update Players set code = ? where code = ?", "!", code].update != 0
end
def self.[](email)
DB["select * from Players where email = ?", email].first
end
def self.[]=(email, data)
DB["update Players set data = ? where email = ?", data, email].update
end
def self.authorized?(email, pass)
digest = XXhash.xxh32(pass, 1234)
player = self[email]
player && player[:digest].to_i == digest.to_i ? player : false
end
def self.verified?(email)
player = self[email]
player && player[:code] == "!"
end
end