Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.95 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("GetTimes")
  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.LatestCheckpoint = 1
  48.         playerData.Values.TimeMode = false
  49.         startTimeRemote:FireClient(player)
  50.         table.remove(timeData, #index)
  51.         return
  52.     end
  53.    
  54.     print("finished timing")
  55.     playerData.Values.LatestCheckpoint = 1
  56.     index["totalTime"] = tick() - index["startTime"]
  57.     startTimeRemote:FireClient(player)
  58.     playerData.Values.TimeMode = false
  59. end
  60.  
  61. function timeManager:UpdateTimeLeaderboard(id, value)
  62.     print("updating leaderboard")
  63.     orderedDataLibrary:SetAsync("_TimeLeaderboard", id, value)
  64. end
  65.  
  66. -- Initialize
  67. function timeManager:Initialize()
  68.     coroutine.wrap(function()
  69.         if #players:GetPlayers() == 0 then
  70.             players.PlayerAdded:Wait()
  71.         end
  72.  
  73.         wait(1)
  74.        
  75.         while true do
  76.             pcall(function()
  77.                 for index, data in pairs (timeData) do
  78.                     local id, total = data.userId, data.totalTime
  79.                     if total == nil then
  80.                         print("not done", id, total)
  81.                         -- player did not complete it
  82.                     else
  83.                         print("done", id, total)
  84.                         local timeToComplete = timeFormattingLibrary:FormatSecondsToHMS(total*60)
  85.                         print(timeToComplete)
  86.                         print("a")
  87.                         timeManager:UpdateTimeLeaderboard(id, total)
  88.                         print("b")
  89.                         table.remove(timeData, index)
  90.                     end
  91.                 end
  92.                
  93.                 local timeData = orderedDataLibrary:GetSortedAsync("_TimeLeaderboard", false, 18):GetCurrentPage()
  94.  
  95.                 leaderboardRemote:FireAllClients(timeData)
  96.             end)
  97.             wait(180)
  98.         end
  99.     end)()
  100. end
  101.  
  102. --
  103. return timeManager
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement