From 2b3f36f6eafb38ebf9b9f82f1d0bcf56c07e500d Mon Sep 17 00:00:00 2001 From: "Jill \"oatmealine\" Monoids" Date: Sat, 31 Dec 2022 11:16:43 +0300 Subject: [PATCH] config system --- .env.example | 3 ++- .gitignore | 3 ++- README.md | 2 +- config.toml.example | 26 +++++++++++++++++++++++ shard.lock | 4 ++++ shard.yml | 3 +++ src/crystal-gauntlet.cr | 19 +++++++++++++++-- src/endpoints/accounts/registerAccount.cr | 4 ++++ src/endpoints/levels/rateLevel.cr | 25 ++++++++++++++-------- 9 files changed, 75 insertions(+), 14 deletions(-) create mode 100644 config.toml.example diff --git a/.env.example b/.env.example index 023fdea..bd18a4a 100644 --- a/.env.example +++ b/.env.example @@ -1 +1,2 @@ -DATABASE_URL=sqlite3://./crystalgauntlet.db \ No newline at end of file +DATABASE_URL=sqlite3://./crystalgauntlet.db +PORT=8080 \ No newline at end of file diff --git a/.gitignore b/.gitignore index dfc6371..d9d28a5 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ /.shards/ *.dwarf .env -crystalgauntlet.db \ No newline at end of file +/crystalgauntlet.db +/config.toml \ No newline at end of file diff --git a/README.md b/README.md index ed41011..8a38462 100644 --- a/README.md +++ b/README.md @@ -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/)) diff --git a/config.toml.example b/config.toml.example new file mode 100644 index 0000000..416bf50 --- /dev/null +++ b/config.toml.example @@ -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 \ No newline at end of file diff --git a/shard.lock b/shard.lock index 07867a2..e16e1e6 100644 --- a/shard.lock +++ b/shard.lock @@ -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 + diff --git a/shard.yml b/shard.yml index a3c8ba1..7a3fc8a 100644 --- a/shard.yml +++ b/shard.yml @@ -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 diff --git a/src/crystal-gauntlet.cr b/src/crystal-gauntlet.cr index 44cbd46..17f322f 100644 --- a/src/crystal-gauntlet.cr +++ b/src/crystal-gauntlet.cr @@ -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 diff --git a/src/endpoints/accounts/registerAccount.cr b/src/endpoints/accounts/registerAccount.cr index f508d62..4ae2178 100644 --- a/src/endpoints/accounts/registerAccount.cr +++ b/src/endpoints/accounts/registerAccount.cr @@ -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"] diff --git a/src/endpoints/levels/rateLevel.cr b/src/endpoints/levels/rateLevel.cr index b11d4d8..90ec84e 100644 --- a/src/endpoints/levels/rateLevel.cr +++ b/src/endpoints/levels/rateLevel.cr @@ -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" }