Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.94 KB | None | 0 0
  1. -- Variables
  2. local timeManager = {}
  3. local timeData = {}
  4.  
  5. -- Services
  6. local coreModule = require(script:FindFirstAncestor("CoreModule"))
  7. local timeFormattingLibrary = coreModule:Get("Libraries.TimeFormattingLibrary")
  8. local players = coreModule.Services.Players
  9.  
  10. -- Data
  11. local playerDataManager = coreModule:Get("GameModules.ServerGameManager.PlayerManager.PlayerDataManager")
  12. local orderedDataLibrary = coreModule:Get("Libraries.DataLibrary.OrderedDataLibrary")
  13.  
  14. -- Remotes
  15. local leaderboardRemote = coreModule.SharedCore:Get("Libraries.RemoteLibrary"):GetRemote("GetLeaderboard")
  16. local startTimeRemote = coreModule.SharedCore:Get("Libraries.RemoteLibrary"):GetRemote("StartTime")
  17.  
  18. -- Methods
  19. function timeManager:find(tbl, value, key)
  20.     if key then
  21.         for k, v in pairs(tbl) do
  22.             if v[key] == value then
  23.                 return v, k
  24.             end
  25.         end
  26.     else
  27.         return table.find(tbl, value)
  28.     end
  29. end
  30.  
  31. function timeManager:StartTiming(player)
  32.     local playerData = playerDataManager:GetData(player)
  33.     if playerData.Values.TimeMode == true then return end
  34.    
  35.     print("started timing")
  36.     playerData.Values.TimeMode = true
  37.     timeData[#timeData + 1] = {userId = player.UserId, startTime = tick(), totalTime = nil}
  38. end
  39.  
  40. function timeManager:StopTiming(player, optional)
  41.     local playerData = playerDataManager:GetData(player)
  42.    
  43.     local index = timeManager:find(timeData, player.UserId, 'userId')
  44.     if not index then return end
  45.    
  46.     if optional then
  47.         playerData.Values.TimeMode = false
  48.         startTimeRemote:FireClient(player)
  49.         table.remove(timeData, #index)
  50.         return
  51.     end
  52.    
  53.     print("finished timing")
  54.     index["totalTime"] = tick() - index["startTime"]
  55.     startTimeRemote:FireClient(player)
  56.     playerData.Values.TimeMode = false
  57. end
  58.  
  59. function timeManager:UpdateTimeLeaderboard(id, value)
  60.     print("updating leaderboard")
  61.     orderedDataLibrary:SetAsync("_TimeLeaderboard", id, value)
  62. end
  63.  
  64. -- Initialize
  65. function timeManager:Initialize()
  66.     coroutine.wrap(function()
  67.         if #players:GetPlayers() == 0 then
  68.             players.PlayerAdded:Wait()
  69.         end
  70.  
  71.         wait(1)
  72.        
  73.         while true do
  74.             pcall(function()
  75.                 for index, data in pairs (timeData) do
  76.                     local id, total = data.userId, data.totalTime
  77.                     if total == nil then
  78.                         print("not done", id, total)
  79.                         -- player did not complete it
  80.                     else
  81.                         print("done", id, total)
  82.                         local timeToComplete = timeFormattingLibrary:FormatSecondsToHMS(total*60)
  83.                         timeManager:UpdateTimeLeaderboard(id, total)
  84.                         table.remove(timeData, index)
  85.                     end
  86.                 end
  87.                
  88.                 local stageData = orderedDataLibrary:GetSortedAsync("_CheckpointLeaderboard", false, 18):GetCurrentPage()
  89.                 local timeData = orderedDataLibrary:GetSortedAsync("_TimeLeaderboard", false, 18):GetCurrentPage()
  90.  
  91.                 leaderboardRemote:FireAllClients(stageData, timeData)
  92.             end)
  93.             wait(180)
  94.         end
  95.     end)()
  96. end
  97.  
  98. --
  99. return timeManager
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement