allow config_get to specify a custom default

This commit is contained in:
Jill 2023-01-07 11:54:13 +03:00
parent 63311de7a4
commit f2ffd3ab90
8 changed files with 16 additions and 13 deletions

View File

@ -46,6 +46,9 @@ module CrystalGauntlet
end
return this
end
def config_get(key : String, default)
config_get(key).as?(typeof(default)) || default
end
DATABASE = DB.open(ENV["DATABASE_URL"]? || "sqlite3://./crystal-gauntlet.db")
@ -268,7 +271,7 @@ module CrystalGauntlet
server.bind_unix(listen_on.to_s.sub("unix://",""))
end
full_server_path = (config_get("general.hostname").as?(String) || "") + "/" + (config_get("general.append_path").as?(String) || "")
full_server_path = config_get("general.hostname", "") + "/" + config_get("general.append_path", "")
robtop_server_path = "www.boomlings.com/database/"
if full_server_path.size != robtop_server_path.size
LOG.warn { "i think you made a mistake? length of full server path and default .exe location do not match" }

View File

@ -64,7 +64,7 @@ CrystalGauntlet.endpoints["/getGJCommentHistory.php"] = ->(context : HTTP::Serve
4 => likes,
5 => 0, # dislikes; unused
6 => id,
7 => likes <= (config_get("comments.spam_thres").as?(Int64) || -3),
7 => likes <= config_get("comments.spam_thres", -3_i64),
9 => Time.parse(created_at, Format::TIME_FORMAT, Time::Location::UTC),
}),
Format.fmt_comment({

View File

@ -44,7 +44,7 @@ CrystalGauntlet.endpoints["/getGJComments21.php"] = ->(context : HTTP::Server::C
4 => likes,
5 => 0,
6 => id,
7 => likes <= (config_get("comments.spam_thres").as?(Int64) || -3),
7 => likes <= config_get("comments.spam_thres", -3_i64),
8 => account_id,
9 => Time.parse(created_at, Format::TIME_FORMAT, Time::Location::UTC),
10 => percent || 0,

View File

@ -29,7 +29,7 @@ CrystalGauntlet.endpoints["/getGJAccountComments20.php"] = ->(context : HTTP::Se
4 => likes,
5 => 0, # dislikes; unused
6 => id,
7 => likes <= (config_get("comments.spam_thres").as?(Int64) || -3),
7 => likes <= config_get("comments.spam_thres", -3_i64),
9 => Time.parse(created_at, Format::TIME_FORMAT, Time::Location::UTC),
})
end

View File

@ -9,7 +9,7 @@ private def get_chk_value(chk_str : String)
end
private def get_quest_time(account_id : Int32)
timer = config_get("quests.timer").as?(Int64) || 0
timer = config_get("quests.timer", 0_i64)
begin
next_str = DATABASE.query_one("select next_at from quest_timer where account_id = ?", account_id, as: {String})

View File

@ -10,9 +10,9 @@ end
private def get_rand(type : String, large = false)
base = "chests.#{large ? "large" : "small"}.#{type}"
min = config_get("#{base}_min").as?(Int64) || 0
max = config_get("#{base}_max").as?(Int64) || 0
increment = config_get("#{base}_increment").as?(Int64) || 1
min = config_get("#{base}_min", 0_i64)
max = config_get("#{base}_max", 0_i64)
increment = config_get("#{base}_increment", 1_i64)
((Random.rand(min.to_f .. (max.to_f + 1)) / increment).floor() * increment).to_i
end
@ -31,7 +31,7 @@ end
private def claim_chest(account_id : Int32, prev_count : Int32, large = false)
table = large ? "large_chests" : "small_chests"
timer = config_get("chests.#{large ? "large" : "small"}.timer").as?(Int64) || 0
timer = config_get("chests.#{large ? "large" : "small"}.timer", 0_i64)
next_at = (Time.utc + timer.seconds).to_s(Format::TIME_FORMAT)
if DATABASE.scalar("select count(*) from #{table} where account_id = ?", account_id).as(Int64) > 0
DATABASE.exec("update #{table} set total_opened = ?, next_at = ? where account_id = ?", prev_count + 1, next_at, account_id)

View File

@ -13,7 +13,7 @@ module CrystalGauntlet::CreatorPoints
def calculate_creator_points(user_id : Int32)
QUERIES
.map { |q, c| DATABASE.scalar(q, user_id).as(Int64) * (config_get(c).as?(Int64) || 0) }
.map { |q, c| DATABASE.scalar(q, user_id).as(Int64) * config_get(c, 0_i64) }
.sum()
end

View File

@ -72,7 +72,7 @@ module CrystalGauntlet::Songs
output = IO::Memory.new
# todo: ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️ LOOK OUT FOR SHELL INJECTION BULLSHIT!!!!!!!!!!!!!!!!!!
Process.run(config_get("songs.sources.ytdlp_binary").as?(String) || "yt-dlp", ["-J", url], output: output)
Process.run(config_get("songs.sources.ytdlp_binary", "yt-dlp"), ["-J", url], output: output)
output.close
metadata = JSON.parse(output.to_s)
@ -190,7 +190,7 @@ module CrystalGauntlet::Songs
if (fetch_url || !get_download) && metadata && author_id
# we're done! woo
if fetch_url && fetch_url.starts_with?("./")
fetch_url = "#{config_get("general.hostname").as?(String) || ""}/#{fetch_url[2..]}"
fetch_url = "#{config_get("general.hostname", "")}/#{fetch_url[2..]}"
end
return {metadata.name, author_id, metadata.author, metadata.size, fetch_url}
end
@ -208,7 +208,7 @@ module CrystalGauntlet::Songs
target_path = get_file_path(song_id)
Process.run(config_get("songs.sources.ytdlp_binary").as?(String) || "yt-dlp", ["-f", "ba", "-x", "--audio-format", GD_AUDIO_FORMAT, "-o", target_path.to_s, "--ffmpeg-location", config_get("songs.sources.ffmpeg_binary").as?(String) || "ffmpeg", metadata.normalized_url], output: STDOUT, error: STDOUT)
Process.run(config_get("songs.sources.ytdlp_binary", "yt-dlp"), ["-f", "ba", "-x", "--audio-format", GD_AUDIO_FORMAT, "-o", target_path.to_s, "--ffmpeg-location", config_get("songs.sources.ffmpeg_binary", "ffmpeg"), metadata.normalized_url], output: STDOUT, error: STDOUT)
new_size = File.size(target_path).to_i