download and like milestones for levels

This commit is contained in:
Jill 2023-01-21 22:44:21 +03:00
parent 01d7210d64
commit 77b809da8c
3 changed files with 42 additions and 2 deletions

View File

@ -160,6 +160,21 @@ CrystalGauntlet.endpoints["/downloadGJLevel22.php"] = ->(context : HTTP::Server:
if DATABASE.scalar("select count(*) from ip_actions where action = ? and value = ? and ip = ? limit 1", "download", level_id, ip).as?(Int64) == 0
DATABASE.exec("update levels set downloads = downloads + 1 where id = ?", level_id)
DATABASE.exec("insert into ip_actions (action, value, ip) values (?, ?, ?)", "download", level_id, ip)
downloads = DATABASE.scalar("select downloads from levels where id = ?", level_id).as(Int64)
if downloads == 300
# notify the creator of 300 downloads
account_id, level_name = DATABASE.query_one("select users.account_id, levels.name from levels left join users on users.id = levels.user_id where levels.id = ?", level_id, as: {Int32?, String})
if account_id
if DATABASE.scalar("select count(*) from notifications where type = \"download_milestone\" and target = ? and account_id = ?", level_id, account_id).as(Int64) == 0
Notifications.send_notification(account_id, "download_milestone", level_id, {
"level_name" => level_name,
"amount" => downloads
})
end
end
end
end
end

View File

@ -38,6 +38,23 @@ CrystalGauntlet.endpoints["/likeGJItem211.php"] = ->(context : HTTP::Server::Con
if DATABASE.scalar("select count(*) from ip_actions where action = ? and value = ? and ip = ? limit 1", "like_#{type}", item_id, ip).as?(Int64) == 0
DATABASE.exec("update #{table} set likes = likes #{sign} 1 where #{column} = ?", item_id)
DATABASE.exec("insert into ip_actions (action, value, ip) values (?, ?, ?)", "like_#{type}", item_id, ip)
if type == 1 # level
likes = DATABASE.scalar("select likes from #{table} where #{column} = ?", item_id).as(Int64)
if likes == 100
# notify the creator of 100 likes
account_id, level_name = DATABASE.query_one("select users.account_id, #{table}.name from #{table} left join users on users.id = #{table}.user_id where #{table}.#{column} = ?", item_id, as: {Int32?, String})
if account_id
if DATABASE.scalar("select count(*) from notifications where type = \"like_milestone\" and target = ? and account_id = ?", item_id, account_id).as(Int64) == 0
Notifications.send_notification(account_id, "like_milestone", item_id, {
"level_name" => level_name,
"amount" => likes
})
end
end
end
end
end
"1"

View File

@ -17,12 +17,20 @@ module CrystalGauntlet::Notifications
NOTIFICATION_STRINGS = {
"authored_level_featured" => %(Your level <b>%{level_name}</b> has been featured!),
"authored_level_rated" => %(Your level <b>%{level_name}</b> has been rated!)
"authored_level_rated" => %(Your level <b>%{level_name}</b> has been rated!),
"like_milestone" => %(Your level <b>%{level_name}</b> has reached <b>%{amount}</b> likes!),
"download_milestone" => %(Your level <b>%{level_name}</b> has reached <b>%{amount}</b> downloads!)
}
def format_notification(type : String, target : Int32?, details : NotificationDetails? = nil, html_safe : Bool = false)
details = details || {} of String => String | Int64 | Bool | Float64 | Nil
string = NOTIFICATION_STRINGS[type]
string = NOTIFICATION_STRINGS[type]?
if !string
LOG.error { "No notification string found for #{type}" }
# might aswell have a fallback
return type
end
#case type
#when "authored_level_featured", "authored_level_rated"