From 13005db51cad2db858d2bf3d37eab98bb51b90e6 Mon Sep 17 00:00:00 2001
From: tommy aka doge <59783653+dowoge@users.noreply.github.com>
Date: Wed, 24 Aug 2022 01:13:06 -0400
Subject: [PATCH] point calculation (real?)

---
 src/modules/commands/rank.lua      | 16 +---------------
 src/modules/commands/skillCalc.lua | 30 +++++++++++++++++++++++++++---
 src/modules/http.lua               |  9 +++++++++
 src/modules/strafes_net.lua        | 21 +++++++++++++++++----
 4 files changed, 54 insertions(+), 22 deletions(-)

diff --git a/src/modules/commands/rank.lua b/src/modules/commands/rank.lua
index b3c1eef..7738a7a 100644
--- a/src/modules/commands/rank.lua
+++ b/src/modules/commands/rank.lua
@@ -12,21 +12,7 @@ commands:Add('rank',{},'rank <username|mention|"me"> <game> <style>', function(t
     local style=API.STYLES[args[3]]
     if not game then return message:reply('invalid game') end
     if not style then return message:reply('invalid style') end
-    if user=='me' then
-        local me=message.author
-        local roblox_user=API:GetRobloxInfoFromDiscordId(me.id)
-        if not roblox_user.id then return message:reply('```You are not registered with the RoverAPI```') end
-        user=roblox_user
-    elseif user:match('<@%d+>') then
-        local user_id=user:match('<@(%d+)>')
-        local member=message.guild:getMember(user_id)
-        local roblox_user=API:GetRobloxInfoFromDiscordId(member.id)
-        if not roblox_user.id then return message:reply('```You are not registered with the RoverAPI```') end
-        user=roblox_user
-    else
-        local roblox_user=API:GetRobloxInfoFromUsername(user)
-        user=roblox_user
-    end
+    user = API:GetUserFromAny(user)
     local sn_info = API:GetUser(user.id)
     if not sn_info.ID then return message:reply('```No data with StrafesNET is associated with that user.```') end
     if sn_info.State==2 then return message:reply('```This user is currently blacklisted```') end
diff --git a/src/modules/commands/skillCalc.lua b/src/modules/commands/skillCalc.lua
index 7791ec8..dcf22ff 100644
--- a/src/modules/commands/skillCalc.lua
+++ b/src/modules/commands/skillCalc.lua
@@ -3,6 +3,7 @@ local API=require('./../strafes_net.lua')
 local commands=require('./../commands.lua')
 function sleep(n) local t = os.clock() while os.clock()-t <= n do end end
 discordia.extensions()
+local pad = API.Pad
 commands:Add('skill',{},'skill <username|mention|"me"> <game> <style>', function(t)
     local args=t.args
     local message=t.message
@@ -43,6 +44,12 @@ commands:Add('skill',{},'skill <username|mention|"me"> <game> <style>', function
             for _,time in next,times do
                 local rank = API:GetTimeRank(time.ID).Rank
                 local count = tonumber(API:GetMapCompletionCount(time.Map,style))
+                if not rank or not count then
+                    print('NO RANK OR COUNT')
+                    print(rank,count)
+                    rank = 1
+                    count = 1
+                end
                 time.Points = API:CalculatePoint(rank,count)
                 time.Rank = rank
                 time.MapCompletionCount = count
@@ -58,12 +65,19 @@ commands:Add('skill',{},'skill <username|mention|"me"> <game> <style>', function
             for _,time in next,times do
                 points = points+time.Points
             end
-            local msg = 'Average Skill: '..API:FormatSkill((test_a+1)/(test_b-1))..'\n'..
-                        'Points: '..points..'\n'
+            local s = (test_a)/(test_b-1)
+            print(s)
+            local msg = 'Average Skill: '..API:FormatSkill(math.clamp(s,0,1))..'\n'..
+                        'Points: '..points..'\n'..
+                        pad(API,'Map',50)..' | '..pad(API,'Skill',7)..' | '.. pad(API,'Placement',14)..' | Time\n\n'
                         
             for _,time in next,times do
                 -- msg = msg..'['..time.Rank..'/'..time.MapCompletionCount..'] '..time.Map..' ('..time.Skill..')\n'
-                msg = msg..API.MAPS[game][time.Map].DisplayName..' ('..time.Map..'): '..time.Skill..' for '..time.Rank..'/'..time.MapCompletionCount..' with '..API:FormatTime(time.Time)..'\n'
+                local mapStr = API.MAPS[game][time.Map].DisplayName..' ('..time.Map..')'
+                local skill = time.Skill
+                local rankStr = time.Rank..'/'..time.MapCompletionCount
+                local timeStr = API:FormatTime(time.Time)
+                msg = msg.. pad(API,mapStr,50)..' | '..pad(API,skill,7)..' | '.. pad(API,rankStr,14)..' | '..timeStr..'\n'
             end
             local txt = './skill-'..API.GAMES[game]..'-'..API.STYLES[style]:lower()..'-'..user.name..'.txt'
             local file=io.open(txt,'w+')
@@ -86,4 +100,14 @@ commands:Add('skill',{},'skill <username|mention|"me"> <game> <style>', function
         --_G.current = {name=user.name,game=API.GAMES[game],style=API.STYLES[style]:lower()}
         message:reply('Bot is currently in use, please try again later ('.._G.current.name..' for '.._G.current.game..' in '.._G.current.style..')')
     end
+end)
+
+commands:Add('compare',{},'compare n1 n2', function(t)
+    local args=t.args
+    local message=t.message
+    local n1 = args[1]
+    local n2 = args[2]
+    local compared = API:CalculateDifference(n1,n2)
+    local compared_percent = API:CalculateDifferencePercent(n1,n2)
+    message:reply(tostring(compared)..' ('..compared_percent..')')
 end)
\ No newline at end of file
diff --git a/src/modules/http.lua b/src/modules/http.lua
index c77784e..b7683ac 100644
--- a/src/modules/http.lua
+++ b/src/modules/http.lua
@@ -7,6 +7,11 @@ function wait(n)c=os.clock t=c()while c()-t<=n do end;end
 3: headers
 4: body
 5: options]]
+local STRAFES_NET_RATELIMIMT = {
+    HOUR = 3000,
+    MINUTE = 100,
+}
+local remaining_timeout = 0
 local function request(method,url,headers,body,options)
     local headers,body=http.request(method,url,headers,body,options)
     local rbody=json.decode(body)
@@ -19,9 +24,13 @@ local function request(method,url,headers,body,options)
         end
     end
     local remaining = tonumber(rheaders['RateLimit-Remaining'])
+    local remaining_hour = tonumber(rheaders['X-RateLimit-Remaining-Hour'])
     local reset = tonumber(rheaders['RateLimit-Reset'])
+    local retry_after = tonumber(rheaders['Retry-After'])
+    print(remaining,remaining_hour)
     if remaining and reset then
         local t = remaining==0 and reset or .38
+        if retry_after then t = retry_after end
         wait(t)
     end
     return rbody,rheaders
diff --git a/src/modules/strafes_net.lua b/src/modules/strafes_net.lua
index ec2630f..40303fb 100644
--- a/src/modules/strafes_net.lua
+++ b/src/modules/strafes_net.lua
@@ -7,9 +7,9 @@ local ROVER_API_URL = 'https://verify.eryn.io/api/'
 local ROBLOX_API_URL = 'https://users.roblox.com/v1/'
 local ROBLOX_API_URL2 = 'https://api.roblox.com/'
 local ROBLOX_THUMBNAIL_URL = 'https://thumbnails.roblox.com/v1/'
-local ROBLOX_PREMIUM_URL = 'https://premiumfeatures.roblox.com/v1/'
 
-local RANK_CONSTANT = 0.5
+
+local RANK_CONSTANT_A, RANK_CONSTANT_B, RANK_CONSTANT_C, RANK_CONSTANT_D, RANK_CONSTANT_E = 0.215, 0.595, 0.215, 0.215, 0.71
 
 local t=tostring
 local r=function(n,nd) return tonumber(string.format('%.' .. (nd or 0) .. 'f', n)) end
@@ -188,11 +188,24 @@ function API:GetMapCompletionCount(MAP_ID,STYLE_ID)
     end
     return ((pages-1)*200)+#res
 end
-
+--cool doggo
 function API:CalculatePoint(rank,count)
-    return RANK_CONSTANT*(math.exp(RANK_CONSTANT)-1)/(1-math.exp(math.max(-700, -RANK_CONSTANT*count)))*math.exp(math.max(-700, -RANK_CONSTANT*rank))+(1-RANK_CONSTANT)*(1+2*(count-rank))/(count*count)
+    return RANK_CONSTANT_A*(math.exp(RANK_CONSTANT_B)-1)/(1-math.exp(math.max(-700, -RANK_CONSTANT_C*count)))*math.exp(math.max(-700, -RANK_CONSTANT_D*rank))+(1-RANK_CONSTANT_E)*(1+2*(count-rank))/(count*count)
 end
 
+function API:Pad(str,n)
+    n = n or 20
+    str = tostring(str)
+    return str..string.rep(' ',n-#str)
+end
+
+function API:CalculateDifference(v1,v2)
+    return math.abs(v1-v2)
+end
+
+function API:CalculateDifferencePercent(v1,v2)
+    return math.abs((1-(v1/v2))*100)..'%'
+end
 function API:GetUserFromAny(user,message)
     local str = user:match('^["\'](.+)[\'"]$')
     local num = user:match('^(%d+)$')