crystal-gauntlet/src/endpoints/levels/rateLevel.cr

96 lines
2.9 KiB
Crystal
Raw Normal View History

2022-12-31 08:21:21 +01:00
require "uri"
include CrystalGauntlet
CrystalGauntlet.endpoints["/rateGJStars211.php"] = ->(context : HTTP::Server::Context): String {
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
2023-01-02 11:59:37 +01:00
LOG.debug { params.inspect }
2022-12-31 08:21:21 +01:00
level_id = params["levelID"].to_i
stars = params["stars"].to_i
2022-12-31 10:42:05 +01:00
if stars > 10 || stars < 1
return "-1"
end
2022-12-31 08:21:21 +01:00
# 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)
2022-12-31 09:16:43 +01:00
if config_get("voting.allow_votes").as(Bool | Nil) == false
return "1"
2022-12-31 10:42:05 +01:00
end
vote_count = DATABASE.scalar("select count(*) from difficulty_votes where level_id = ?", level_id).as(Int64)
min_votes = config_get("voting.min_votes").as(Int64 | Nil) || 1
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()).clamp(2..9))
2022-12-31 10:42:05 +01:00
if difficulty
DATABASE.exec("update levels set community_difficulty = ? where id = ?", difficulty.value, level_id)
end
end
return "1"
}
CrystalGauntlet.endpoints["/rateGJDemon21.php"] = ->(context : HTTP::Server::Context): String {
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
2023-01-02 11:59:37 +01:00
LOG.debug { params.inspect }
2022-12-31 10:42:05 +01:00
level_id = params["levelID"].to_i
rating = params["rating"].to_i
if rating < 1 || rating > 5
return "-1"
end
rating -= 1
# 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 demon_difficulty_votes (level_id, demon_difficulty) values (?, ?)", level_id, rating)
if config_get("voting.allow_demon_votes").as(Bool | Nil) == false
2023-01-07 07:55:32 +01:00
return level_id.to_s
2022-12-31 10:42:05 +01:00
end
vote_count = DATABASE.scalar("select count(*) from demon_difficulty_votes where level_id = ?", level_id).as(Int64)
2022-12-31 09:16:43 +01:00
2022-12-31 10:42:05 +01:00
min_votes = config_get("voting.min_demon_votes").as(Int64 | Nil) || 1
2022-12-31 09:16:43 +01:00
2022-12-31 10:42:05 +01:00
if vote_count >= min_votes
# todo: cache in some form?
votes = DATABASE.query_all("select demon_difficulty from demon_difficulty_votes where level_id = ?", level_id, as: {Int32})
avg = votes.sum() / votes.size
demon_difficulty = DemonDifficulty.new(Int32.new(avg.round()))
2022-12-31 09:16:43 +01:00
2022-12-31 10:42:05 +01:00
if demon_difficulty
DATABASE.exec("update levels set demon_difficulty = ? where id = ?", demon_difficulty.value, level_id)
2022-12-31 08:21:21 +01:00
end
end
2023-01-07 07:55:32 +01:00
return level_id.to_s
2022-12-31 08:21:21 +01:00
}
2023-01-07 14:34:09 +01:00
CrystalGauntlet.endpoints["/rateGJStars20.php"] = CrystalGauntlet.endpoints["/rateGJStars211.php"]
2023-01-07 16:25:32 +01:00
2023-01-08 08:10:27 +01:00
CrystalGauntlet.endpoints["/rateGJStars.php"] = CrystalGauntlet.endpoints["/rateGJStars211.php"]
2023-01-07 16:25:32 +01:00
CrystalGauntlet.endpoints["/rateGJLevel.php"] = ->(context : HTTP::Server::Context): String {
"-1"
}