you can vote on difficulties now

This commit is contained in:
Jill 2022-12-31 10:21:21 +03:00
parent ce5b075280
commit 5a1ba76f02
3 changed files with 71 additions and 0 deletions

View File

@ -0,0 +1,14 @@
-- +migrate up
CREATE TABLE difficulty_votes (
level_id INTEGER NOT NULL references levels(id),
stars INTEGER NOT NULL
);
CREATE TABLE demon_difficulty_votes (
level_id INTEGER NOT NULL references levels(id),
demon_difficulty INTEGER NOT NULL
);
-- +migrate down
DROP TABLE difficulty_votes;
DROP TABLE demon_difficulty_votes;

View File

@ -0,0 +1,36 @@
require "uri"
include CrystalGauntlet
CrystalGauntlet.endpoints["/rateGJStars211.php"] = ->(body : String): String {
params = URI::Params.parse(body)
puts params.inspect
level_id = params["levelID"].to_i
# todo: clamp this
stars = params["stars"].to_i
# todo: implement this for mod accounts
if DATABASE.scalar("select count(*) from levels where id = ?", level_id).as(Int64) == 0
return "-1"
end
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)
# 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()))
if difficulty
DATABASE.exec("update levels set community_difficulty = ? where id = ?", difficulty.value, level_id)
end
end
return "-1"
}

View File

@ -36,6 +36,27 @@ module CrystalGauntlet
end
end
def stars_to_difficulty(stars : Int32) : LevelDifficulty | Nil
case stars
when 1
LevelDifficulty::Auto
when 2
LevelDifficulty::Easy
when 3
LevelDifficulty::Normal
when 4, 5
LevelDifficulty::Hard
when 6, 7
LevelDifficulty::Harder
when 8, 9
LevelDifficulty::Insane
when 10
LevelDifficulty::Demon
else
nil
end
end
enum DemonDifficulty
Easy
Medium