config system
This commit is contained in:
parent
5a1ba76f02
commit
2b3f36f6ea
|
@ -1 +1,2 @@
|
||||||
DATABASE_URL=sqlite3://./crystalgauntlet.db
|
DATABASE_URL=sqlite3://./crystalgauntlet.db
|
||||||
|
PORT=8080
|
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -3,4 +3,5 @@
|
||||||
/.shards/
|
/.shards/
|
||||||
*.dwarf
|
*.dwarf
|
||||||
.env
|
.env
|
||||||
crystalgauntlet.db
|
/crystalgauntlet.db
|
||||||
|
/config.toml
|
|
@ -10,7 +10,7 @@ you may need to head into `lib/` to fix deps. i'm Very sorry
|
||||||
|
|
||||||
## setup
|
## 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/))
|
run `cake db:migrate` (must have [cake](https://github.com/axvm/cake/))
|
||||||
|
|
||||||
|
|
26
config.toml.example
Normal file
26
config.toml.example
Normal 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
|
|
@ -20,3 +20,7 @@ shards:
|
||||||
git: https://github.com/vladfaust/time_format.cr.git
|
git: https://github.com/vladfaust/time_format.cr.git
|
||||||
version: 0.1.1
|
version: 0.1.1
|
||||||
|
|
||||||
|
toml:
|
||||||
|
git: https://github.com/crystal-community/toml.cr.git
|
||||||
|
version: 0.7.0+git.commit.4b6325e2a378bac4abc98ee4d5734d57a6a55554
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,9 @@ dependencies:
|
||||||
version: ~> 0.5.0
|
version: ~> 0.5.0
|
||||||
dotenv:
|
dotenv:
|
||||||
github: gdotdesign/cr-dotenv
|
github: gdotdesign/cr-dotenv
|
||||||
|
toml:
|
||||||
|
github: crystal-community/toml.cr
|
||||||
|
branch: master
|
||||||
|
|
||||||
crystal: 1.6.2
|
crystal: 1.6.2
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ require "uri"
|
||||||
require "sqlite3"
|
require "sqlite3"
|
||||||
require "migrate"
|
require "migrate"
|
||||||
require "dotenv"
|
require "dotenv"
|
||||||
|
require "toml"
|
||||||
|
|
||||||
require "./enums"
|
require "./enums"
|
||||||
require "./lib/hash"
|
require "./lib/hash"
|
||||||
|
@ -16,7 +17,21 @@ Dotenv.load
|
||||||
module CrystalGauntlet
|
module CrystalGauntlet
|
||||||
VERSION = "0.1.0"
|
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"])
|
DATABASE = DB.open(ENV["DATABASE_URL"])
|
||||||
|
|
||||||
@@endpoints = Hash(String, (String -> String)).new
|
@@endpoints = Hash(String, (String -> String)).new
|
||||||
|
@ -30,7 +45,7 @@ module CrystalGauntlet
|
||||||
# expunge trailing slashes
|
# expunge trailing slashes
|
||||||
path = context.request.path.chomp("/")
|
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
|
body = context.request.body
|
||||||
|
|
||||||
if !body
|
if !body
|
||||||
|
|
|
@ -8,6 +8,10 @@ CrystalGauntlet.endpoints["/accounts/registerGJAccount.php"] = ->(body : String)
|
||||||
params = URI::Params.parse(body)
|
params = URI::Params.parse(body)
|
||||||
puts params.inspect
|
puts params.inspect
|
||||||
|
|
||||||
|
if config_get("accounts.allow_registration").as(Bool | Nil) == false
|
||||||
|
return "-1"
|
||||||
|
end
|
||||||
|
|
||||||
username = Clean.clean_special(params["userName"])
|
username = Clean.clean_special(params["userName"])
|
||||||
password = params["password"]
|
password = params["password"]
|
||||||
email = params["email"]
|
email = params["email"]
|
||||||
|
|
|
@ -18,19 +18,26 @@ CrystalGauntlet.endpoints["/rateGJStars211.php"] = ->(body : String): String {
|
||||||
|
|
||||||
DATABASE.exec("insert into difficulty_votes (level_id, stars) values (?, ?)", level_id, stars)
|
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
|
min_votes = config_get("voting.min_votes").as(Int32 | Nil) || 0
|
||||||
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()))
|
|
||||||
|
|
||||||
if difficulty
|
# todo: make this configurable
|
||||||
DATABASE.exec("update levels set community_difficulty = ? where id = ?", difficulty.value, level_id)
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# todo: remove (here for debugging)
|
||||||
return "-1"
|
return "-1"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue