it300

Stats

Oct 26th, 2016
90
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. -- Stats
  3. -- SAPP Compatability: 9.7+
  4. -- Script by: Skylace aka Devieth
  5. -- Discord: https://discord.gg/Mxmuxgm
  6.  
  7. player_data = {}
  8. api_version = "1.10.0.0"
  9.  
  10. function OnScriptLoad()
  11.     register_callback(cb['EVENT_GAME_START'], "OnGameStart")
  12.     register_callback(cb['EVENT_GAME_END'], "OnGameEnd")
  13.     register_callback(cb['EVENT_LEAVE'], "OnPlayerJoin")
  14.     register_callback(cb['EVENT_LEAVE'], "OnPlayerLeave")
  15.     register_callback(cb['EVENT_TICK'], "OnEventTick")
  16.     local file = io.open("player.data", "r")
  17.     if file then
  18.         player_data = tableload("player_data.data")
  19.     end
  20. end
  21.  
  22. function OnScriptUnload()
  23.     for i = 1,16 do
  24.         if player_present(i) then
  25.             save_stats(PlayerIndex, true)
  26.         end
  27.     end
  28.     tablesave(player_data, "player_data.data")
  29. end
  30.  
  31. function OnGameStart()
  32.     local file = io.open("player.data", "r")
  33.     if file then
  34.         player_data = tableload("player_data.data")
  35.     end
  36. end
  37.  
  38. function OnGameEnd(PlayerIndex)
  39.     for i = 1,16 do
  40.         if player_present(i) then
  41.             save_stats(PlayerIndex, true)
  42.         end
  43.     end
  44.     tablesave(player_data, "player_data.data")
  45. end
  46.  
  47. function OnPlayerJoin(PlayerIndex)
  48.     local name = get_var(PlayerIndex, "$name")
  49.     if not player_data[name] or player_data[name] == nil then
  50.         player_data[name] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
  51.     end
  52. end
  53.  
  54. function OnPlayerLeave(PlayerIndex)
  55.     save_stats(PlayerIndex)
  56. end
  57.  
  58. function OnEventTick()
  59.     for i = 1,16 do
  60.         if player_present(i) then
  61.             local m_object = getdynamicplayer(i)
  62.             if m_object then
  63.                 local e = read_bit(m_object + 0x209, 6)
  64.                 local crouch = read_bit(m_object + 0x208, 0)
  65.                 if e == 1 and crouch == 1 then
  66.                     printstats(i)
  67.                 end
  68.             end
  69.         end
  70.     end
  71. end
  72.  
  73. function save_stats(PlayerIndex)
  74.     local name = get_var(PlayerIndex, "$name")
  75.     local m_player = getplayer(PlayerIndex)
  76.     if m_player then
  77.         local score = tonumber(get_var(PlayerIndex, "$score"))
  78.         local kills = tonumber(get_var(PlayerIndex, "$kills"))
  79.         local assists = tonumber(get_var(PlayerIndex, "$assists"))
  80.         local deaths = tonumber(get_var(PlayerIndex, "$deaths"))
  81.         local suicides = tonumber(get_var(PlayerIndex, "$suicides"))
  82.         local betrays = tonumber(get_var(PlayerIndex, "$tk"))
  83.         local gt = get_var(PlayerIndex, "$gt")
  84.         local flag_scores, flag_steals, flag_returns = 0, 0 , 0
  85.         local hill_time = 0
  86.         local race_time, race_laps, race_best_time = 0, 0, 0
  87.         local oddball_target_kills, oddball_kills = 0, 0
  88.         if gt == "ctf" then
  89.             flag_scores = tonumber(read_word(m_player + 0xC8))
  90.             flag_steals = tonumber(read_word(m_player + 0xC4))
  91.             flag_returns = tonumber(read_word(m_player + 0xC6))
  92.         elseif gt == "koth" then
  93.             hill_time = tonumber(read_word(m_player + 0xC4)) / 30
  94.         elseif gt == "race" then
  95.             race_time = tonumber(read_word(m_player + 0xC4)) / 30
  96.             race_laps = tonumber(read_word(m_player + 0xC6))
  97.             race_best_time = tonumber(read_word(m_player + 0xC8)) / 30
  98.         elseif gt == "oddball" then
  99.             oddball_target_kills = tonumber(read_word(m_player + 0xC6))
  100.             oddball_kills = tonumber(read_word(m_player + 0xC8))
  101.         end
  102.         local s, k, a, d, su, b, fscores, fsteals, freturns, htime, rtime, rlaps, rbtime, otk, ok = getstats(PlayerIndex)
  103.         player_data[name] = {
  104.         s + score, k + kills, a + assists, d + deaths, su + suicides, b + betrays,
  105.         fscores + flag_scores, fsteals + flag_steals, freturns + flag_returns,
  106.         htime + hill_time, rtime + race_time, rlaps + race_laps, race_best_time,
  107.         otk + oddball_target_kills, ok + oddball_kills
  108.             }
  109.     end
  110. end
  111.  
  112. function getstats(PlayerIndex)
  113.     local name = get_var(PlayerIndex, "$name")
  114.     if player_data[name] then
  115.         score = player_data[name][1]
  116.         kills = player_data[name][2]
  117.         assists = player_data[name][3]
  118.         deaths = player_data[name][4]
  119.         suicides = player_data[name][5]
  120.         betrays = player_data[name][6]
  121.         flag_scores = player_data[name][7]
  122.         flag_steals = player_data[name][8]
  123.         flag_returns = player_data[name][9]
  124.         hill_time = player_data[name][10]
  125.         race_time = player_data[name][11]
  126.         race_laps = player_data[name][12]
  127.         race_best_time = player_data[name][13]
  128.         oddball_target_kills = player_data[name][15]
  129.         oddball_kills = player_data[name][16]
  130.         return score, kills, assists, deaths, suicides, betrays, flag_scores, flag_steals, flag_returns, hill_time, race_time, race_laps, race_best_time, oddball_target_kills, oddball_kills
  131.     else
  132.         return 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  133.     end
  134. end
  135.  
  136. function getplayer(PlayerIndex)
  137.     if tonumber(PlayerIndex) then
  138.         if tonumber(PlayerIndex) ~= 0 then
  139.             local m_player = get_player(PlayerIndex)
  140.             if m_player ~= 0 then return m_player end
  141.         end
  142.     end
  143.     return nil
  144. end
  145.  
  146. function getdynamicplayer(PlayerIndex)
  147.     if tonumber(PlayerIndex) then
  148.         if tonumber(PlayerIndex) ~= 0 then
  149.             local m_object = get_dynamic_player(PlayerIndex)
  150.             if m_object ~= 0 then return m_object end
  151.         end
  152.     end
  153.     return nil
  154. end
  155.  
  156. function printstats(PlayerIndex)
  157.     for i = 1,23 do
  158.         rprint(PlayerIndex," ")
  159.     end
  160.     local s, k, a, d, su, b, fscores, fsteals, freturns, htime, rtime, rlaps, rbtime, otk, ok = getstats(PlayerIndex)
  161.     local kd = math.floor((k / d) * (10 ^ 2) + 0.5) / (10 ^ 2)
  162.     local s, m, h = gettimestamp(rtime)
  163.     local s1, m1, h1 = gettimestamp(rbtime)
  164.     local s2, m2, h2 = gettimestamp(htime)
  165.     local racetime = h..":"..m..":"..s
  166.     local racebesttime = h1..":"..m1..":"..s1
  167.     local hilltime = h2..":"..m2..":"..s2
  168.     local line0 = string.format("Total Score: %s", s)
  169.     local line1 = string.format("Kills: %s|tKDR: %s",k, kd)
  170.     local line2 = string.format("Deaths: %s|tSuicides: %s",d, su)
  171.     local line3 = string.format("Assists: %s|tBetrays: %s",a, b)
  172.     local line4 = string.format("Flags Captured: %s|tRace Time: %s|tOddball Kills: %s|tKOTH Time: %s", fscores, racetime, ok, hilltime)
  173.     local line5 = string.format("Flags Retruned: %s|tRace Laps: %s|tCarrier Kills: %s", freturns, rlaps, otk)
  174.     local line6 = string.format("Flags Stolen: %s|tBest Lap: %s", fsteals, racebesttime)
  175.     rprint(PlayerIndex, "-- Primary Stats --")
  176.     rprint(PlayerIndex, line0)
  177.     rprint(PlayerIndex, line1)
  178.     rprint(PlayerIndex, line2)
  179.     rprint(PlayerIndex, line3)
  180.     rprint(PlayerIndex, " ")
  181.     rprint(PlayerIndex, "-- CTF --|t-- RACE --|t-- ODDBALL --|t-- KOTH --")
  182.     rprint(PlayerIndex, line4)
  183.     rprint(PlayerIndex, line5)
  184.     rprint(PlayerIndex, line6)
  185.     rprint(PlayerIndex, " ")
  186.     rprint(PlayerIndex, " ")
  187.     rprint(PlayerIndex, "|cNOTE: This will only update at the end of each game or when you quit.")
  188. end
  189.  
  190. function gettimestamp(seconds)
  191.     if seconds > 60 then
  192.         minutes = math.floor(seconds / 60)
  193.         seconds = seconds - (minutes * 60)
  194.         if seconds < 10 then
  195.             seconds = "0"..tostring(seconds)
  196.         else
  197.             seconds = seconds
  198.         end
  199.         if minutes > 60 then
  200.             hours = math.floor(minutes / 60)
  201.             minutes = minutes - (hours * 60)
  202.         else
  203.             hours = 0
  204.         end
  205.     else
  206.         if seconds < 10 then
  207.             seconds = "0"..tostring(seconds)
  208.         else
  209.             seconds = seconds
  210.         end
  211.         minutes = 0
  212.         hours = 0
  213.     end
  214.     return math.floor(seconds), math.floor(minutes), hours
  215. end
  216.  
  217. function tablesave(t, filename) -- Nuggets's Code to the end
  218.     local file = io.open(filename, "w")
  219.     local spaces = 0
  220.     local function tab()
  221.         local str = ""
  222.         for i = 1,spaces do
  223.             str = str .. " "
  224.         end
  225.         return str
  226.     end
  227.     local function format(t)
  228.         spaces = spaces + 4
  229.         local str = "{ "
  230.         for k,v in opairs(t) do
  231.             -- Key datatypes
  232.             if type(k) == "string" then
  233.                 k = string.format("%q", k)
  234.             elseif k == math.inf then
  235.                 k = "1 / 0"
  236.             end
  237.             k = tostring(k)
  238.             -- Value datatypes
  239.             if type(v) == "string" then
  240.                 v = string.format("%q", v)
  241.             elseif v == math.inf then
  242.                 v = "1 / 0"
  243.             end
  244.             if type(v) == "table" then
  245.                 if tablelen(v) > 0 then
  246.                     str = str .. "\n" .. tab() .. "[" .. k .. "] = " .. format(v) .. ","
  247.                 else
  248.                     str = str .. "\n" .. tab() .. "[" .. k .. "] = {},"
  249.                 end
  250.             else
  251.                 str = str .. "\n" .. tab() .. "[" .. k .. "] = " .. tostring(v) .. ","
  252.             end
  253.         end
  254.         spaces = spaces - 4
  255.         return string.sub(str, 1, string.len(str) - 1) .. "\n" .. tab() .. "}"
  256.     end
  257.     file:write("return " .. format(t))
  258.     file:close()
  259. end
  260.  
  261. function tableload(filename)
  262.     local file = loadfile(filename)
  263.     if file then
  264.         return file() or {}
  265.     end
  266.     return {}
  267. end
  268.  
  269. function tablelen(t)
  270.     local count = 0
  271.     for k,v in pairs(t) do
  272.         count = count + 1
  273.     end
  274.     return count
  275. end
  276.  
  277. function opairs(t)
  278.     local keys = {}
  279.     for k,v in pairs(t) do
  280.         table.insert(keys, k)
  281.     end
  282.     table.sort(keys,
  283.     function(a,b)
  284.         if type(a) == "number" and type(b) == "number" then
  285.             return a < b
  286.         end
  287.         an = string.lower(tostring(a))
  288.         bn = string.lower(tostring(b))
  289.         if an ~= bn then
  290.             return an < bn
  291.         else
  292.             return tostring(a) < tostring(b)
  293.         end
  294.     end)
  295.     local count = 1
  296.     return function()
  297.         if unpack(keys) then
  298.             local key = keys[count]
  299.             local value = t[key]
  300.             count = count + 1
  301.             return key,value
  302.         end
  303.     end
  304. end
  305.  
  306. function spaces(n, delimiter)
  307.     delimiter = delimiter or ""
  308.     local str = ""
  309.     for i = 1, n do
  310.         if i == math.floor(n / 2) then
  311.             str = str .. delimiter
  312.         end
  313.         str = str .. " "
  314.     end
  315.     return str
  316. end
RAW Paste Data