config system

This commit is contained in:
Jill 2022-12-31 11:16:43 +03:00
parent 5a1ba76f02
commit 2b3f36f6ea
9 changed files with 75 additions and 14 deletions

View File

@ -1 +1,2 @@
DATABASE_URL=sqlite3://./crystalgauntlet.db
DATABASE_URL=sqlite3://./crystalgauntlet.db
PORT=8080

3
.gitignore vendored
View File

@ -3,4 +3,5 @@
/.shards/
*.dwarf
.env
crystalgauntlet.db
/crystalgauntlet.db
/config.toml

View File

@ -10,7 +10,7 @@ you may need to head into `lib/` to fix deps. i'm Very sorry
## setup
copy `.env.example` to `.env` and fill it out
copy `.env.example` to `.env` and fill it out, same for `config.toml.example` -> `config.toml`
run `cake db:migrate` (must have [cake](https://github.com/axvm/cake/))

26
config.toml.example Normal file
View File

@ -0,0 +1,26 @@
[general]
# if this path is encountered during path traversal,
# it will be removed. this is useful for instances
# where your absolute domain path is not long enough
# to replace boomlings.com, because you can then point
# it at a different, longer path to fill the gap
#
# example:
# boomlings.com/database/
# exampke.com/aaaaaaaaaa/
#
# leaving blank will disable this
append_path = ""
[accounts]
# allow new accounts to be created
allow_registration = true
[voting]
# allow votes to influence a level's difficulty when it
# hasn't been set yet. when set to false, all unrated
# levels will be NA
allow_votes = true
# the minimum amount of votes before a level's difficulty
# will go from NA to the average
min_votes = 10

View File

@ -20,3 +20,7 @@ shards:
git: https://github.com/vladfaust/time_format.cr.git
version: 0.1.1
toml:
git: https://github.com/crystal-community/toml.cr.git
version: 0.7.0+git.commit.4b6325e2a378bac4abc98ee4d5734d57a6a55554

View File

@ -17,6 +17,9 @@ dependencies:
version: ~> 0.5.0
dotenv:
github: gdotdesign/cr-dotenv
toml:
github: crystal-community/toml.cr
branch: master
crystal: 1.6.2

View File

@ -3,6 +3,7 @@ require "uri"
require "sqlite3"
require "migrate"
require "dotenv"
require "toml"
require "./enums"
require "./lib/hash"
@ -16,7 +17,21 @@ Dotenv.load
module CrystalGauntlet
VERSION = "0.1.0"
APPEND_PATH = "asdfasdfasd/"
CONFIG = TOML.parse(File.read("./config.toml"))
def config_get(key : String)
this = CONFIG
key.split(".").each do |val|
next_val = this.as(Hash)[val]?
if next_val == nil
return nil
else
this = next_val
end
end
return this
end
DATABASE = DB.open(ENV["DATABASE_URL"])
@@endpoints = Hash(String, (String -> String)).new
@ -30,7 +45,7 @@ module CrystalGauntlet
# expunge trailing slashes
path = context.request.path.chomp("/")
path = path.sub(APPEND_PATH, "")
path = path.sub(config_get("general.append_path").as(String | Nil) || "", "")
body = context.request.body
if !body

View File

@ -8,6 +8,10 @@ CrystalGauntlet.endpoints["/accounts/registerGJAccount.php"] = ->(body : String)
params = URI::Params.parse(body)
puts params.inspect
if config_get("accounts.allow_registration").as(Bool | Nil) == false
return "-1"
end
username = Clean.clean_special(params["userName"])
password = params["password"]
email = params["email"]

View File

@ -18,19 +18,26 @@ CrystalGauntlet.endpoints["/rateGJStars211.php"] = ->(body : String): String {
DATABASE.exec("insert into difficulty_votes (level_id, stars) values (?, ?)", level_id, stars)
vote_count = DATABASE.scalar("select count(*) from difficulty_votes where level_id = ?", level_id).as(Int64)
if config_get("voting.allow_votes").as(Bool | Nil) == false
return "1"
else
vote_count = DATABASE.scalar("select count(*) from difficulty_votes where level_id = ?", level_id).as(Int64)
# todo: make this configurable
if vote_count > 0
# todo: cache in some form?
votes = DATABASE.query_all("select stars from difficulty_votes where level_id = ?", level_id, as: {Int32})
avg = votes.sum() / votes.size
difficulty = stars_to_difficulty(Int32.new(avg.round()))
min_votes = config_get("voting.min_votes").as(Int32 | Nil) || 0
if difficulty
DATABASE.exec("update levels set community_difficulty = ? where id = ?", difficulty.value, level_id)
# todo: make this configurable
if vote_count >= min_votes
# todo: cache in some form?
votes = DATABASE.query_all("select stars from difficulty_votes where level_id = ?", level_id, as: {Int32})
avg = votes.sum() / votes.size
difficulty = stars_to_difficulty(Int32.new(avg.round()))
if difficulty
DATABASE.exec("update levels set community_difficulty = ? where id = ?", difficulty.value, level_id)
end
end
end
# todo: remove (here for debugging)
return "-1"
}