2022-12-31 07:28:06 +01:00
require " uri "
include CrystalGauntlet
2023-01-03 08:02:50 +01:00
CrystalGauntlet . endpoints [ " /getGJLevelScores211.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 07:28:06 +01:00
2023-01-02 17:07:33 +01:00
user_id , account_id = Accounts . auth ( params )
2022-12-31 07:28:06 +01:00
level_id = params [ " levelID " ] . to_i32
daily_id = params [ " s10 " ] . to_i32
if daily_id == 0
daily_id = nil
end
if params [ " percent " ]? && params [ " percent " ]? != " 0 "
# set score
2023-01-05 07:46:59 +01:00
if ! ( user_id && account_id )
return " -1 "
end
2022-12-31 07:28:06 +01:00
attempts = params [ " s1 " ] . to_i - 8354
clicks = params [ " s2 " ] . to_i - 3991
time = params [ " s3 " ] . to_i - 4085
2023-01-03 11:38:23 +01:00
progress = String . new ( XorCrypt . encrypt_string ( Base64 . decode_string ( params [ " s6 " ] ) , " 41274 " ) )
2022-12-31 07:28:06 +01:00
coins = params [ " s9 " ] . to_i - 5819
2022-12-31 10:42:05 +01:00
if coins > 3 || coins < 0
2022-12-31 07:28:06 +01:00
return " -1 "
end
percent = params [ " percent " ] . to_i
2022-12-31 10:42:05 +01:00
if percent > 100 || percent < 0
2022-12-31 07:28:06 +01:00
return " -1 "
end
2023-01-04 00:20:45 +01:00
if DATABASE . scalar ( " select count(*) from level_scores where account_id = ? and level_id = ? and daily_id is ? " , account_id , level_id , daily_id ) . as ( Int64 ) > 0
2022-12-31 07:28:06 +01:00
# check if an update is truly necessary
2023-01-04 00:20:45 +01:00
percent_old , coins_old = DATABASE . query_one ( " select percent, coins from level_scores where account_id = ? and level_id = ? and daily_id is ? " , account_id , level_id , daily_id , as : { Int32 , Int32 } )
2022-12-31 07:28:06 +01:00
if percent > percent_old || coins > coins_old
2023-01-04 00:20:45 +01:00
DATABASE . exec ( " update level_scores set account_id=?, percent=?, attempts=?, clicks=?, coins=?, progress=?, time=?, set_at=? where account_id = ? and level_id = ? and daily_id is ? " , account_id , percent , attempts , clicks , coins , progress , time , Time . utc . to_s ( Format :: TIME_FORMAT ) , account_id , level_id , daily_id )
2022-12-31 07:28:06 +01:00
end
else
2022-12-31 08:50:14 +01:00
DATABASE . exec ( " insert into level_scores (account_id, level_id, daily_id, percent, attempts, clicks, coins, progress, time) values (?, ?, ?, ?, ?, ?, ?, ?, ?) " , account_id , level_id , daily_id , percent , attempts , clicks , coins , progress , time )
2022-12-31 07:28:06 +01:00
end
end
# return set scores
type = params [ " type " ]? ? params [ " type " ] : " 1 "
2023-01-06 09:09:54 +01:00
joins = [ ] of String
2023-01-04 00:20:45 +01:00
where_query = [ " level_id = ? and daily_id is ? " ]
2023-01-02 16:55:21 +01:00
2022-12-31 07:28:06 +01:00
case type
2023-01-06 09:09:54 +01:00
when " 0 "
if ! ( user_id && account_id )
return " -1 "
end
2022-12-31 07:28:06 +01:00
# friends
2023-01-06 09:09:54 +01:00
joins << " left join friend_links friend on (friend.account_id_1 = #{ account_id } or friend.account_id_2 = #{ account_id } ) "
where_query << " level_scores.account_id = friend.account_id_1 or level_scores.account_id = friend.account_id_2 "
when " 2 "
2022-12-31 07:28:06 +01:00
# weekly
2023-01-02 16:55:21 +01:00
where_query << " level_scores.set_at > \" #{ ( Time . utc - 7 . days ) . to_s ( Format :: TIME_FORMAT ) } \" "
2022-12-31 07:28:06 +01:00
end
scores = [ ] of String
2022-12-31 07:44:30 +01:00
i = 0
2023-01-07 17:46:43 +01:00
DATABASE . query_each " select distinct percent, level_scores.coins, set_at, users.username, users.id, users.icon_type, users.color1, users.color2, users.cube, users.ship, users.ball, users.ufo, users.wave, users.robot, users.spider, users.special, users.account_id from level_scores join users on level_scores.account_id = users.account_id #{ joins . join ( " " ) } where ( #{ where_query . join ( " ) and ( " ) } ) order by percent desc, level_scores.coins desc, set_at limit 200 " , level_id , daily_id do | rs |
2022-12-31 07:44:30 +01:00
i += 1
2022-12-31 07:28:06 +01:00
percent = rs . read ( Int32 )
coins = rs . read ( Int32 )
set_at = rs . read ( String )
username = rs . read ( String )
user_id = rs . read ( Int32 )
icon_type = rs . read ( Int32 )
color1 = rs . read ( Int32 )
color2 = rs . read ( Int32 )
icon_value = [ rs . read ( Int32 ) , rs . read ( Int32 ) , rs . read ( Int32 ) , rs . read ( Int32 ) , rs . read ( Int32 ) , rs . read ( Int32 ) , rs . read ( Int32 ) ] [ icon_type ]
special = rs . read ( Int32 )
account_id = rs . read ( Int32 )
scores << Format . fmt_hash ( {
1 = > username ,
2 = > user_id ,
9 = > icon_value ,
10 = > color1 ,
11 = > color2 ,
14 = > icon_type ,
15 = > special ,
16 = > account_id ,
3 = > percent ,
2022-12-31 07:44:30 +01:00
6 = > i ,
2022-12-31 07:28:06 +01:00
13 = > coins ,
2022-12-31 19:44:56 +01:00
42 = > Time . parse ( set_at , Format :: TIME_FORMAT , Time :: Location :: UTC )
2022-12-31 07:28:06 +01:00
} )
end
scores . join ( " | " )
}