Advertisement
Guest User

data store for zeezy

a guest
Nov 20th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. 1st script
  2. -- Services.
  3. local ServerStorage = game:GetService("ServerStorage")
  4. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  5. local DataStoreService = game:GetService("DataStoreService")
  6.  
  7. -- Classes.
  8. local Classes = ServerStorage.Server.Classes
  9. local Response = require(Classes.Response)
  10.  
  11. -- Data.
  12. local Data = ServerStorage.Server.Data
  13. local DataStoreData = require(Data.DataStoreData)
  14.  
  15. -- Functions.
  16. local function GetRequestBudget(request_type)
  17. return DataStoreService:GetRequestBudgetForRequestType(DataStoreData.RequestTypeMap[request_type])
  18. end
  19.  
  20. local function DataRequest(request_type, key, datastore, value) -- Returns a response object.
  21. local request_count = 0
  22. local success, data
  23.  
  24. repeat
  25. while GetRequestBudget(request_type) <= 0 do
  26. wait()
  27. end
  28.  
  29. success, data = pcall(datastore[request_type], datastore, key, value)
  30.  
  31. if not success then
  32. warn(data)
  33. wait(1)
  34. else
  35. print(data)
  36. local response = Response.new(true, data)
  37. return response
  38. end
  39. until request_count == DataStoreData.MaxSuccessiveRetry or success
  40.  
  41. -- Append to failed requests and construct a bad response.
  42. if not DataStoreData.FailedRequests[key] then
  43. DataStoreData.FailedRequests[key] = true
  44. end
  45.  
  46. -- Construct bad response.
  47. local bad_response = Response.new(false, nil, string.sub(data, 1, 3), string.sub(data, 3, string.len(data)))
  48.  
  49. return bad_response
  50. end
  51.  
  52. -- Lib.
  53. local DataStoreLib = {}
  54.  
  55. function DataStoreLib.GetData(key, datastore)
  56. return DataRequest("GetAsync", key, datastore)
  57. end
  58.  
  59. function DataStoreLib.SetData(key, datastore, value) -- Prevent data from being set if there was an issue loading the data, and an empty schema was used.
  60. -- Set data if there is no record of any failed requests.
  61. if not DataStoreLib.IsRequestFailed(key) then
  62. return DataRequest("SetAsync", key, datastore, value)
  63. end
  64.  
  65. -- Attempt to get more recent value (in case there was a problem getting data a previous time.)
  66. local updated_value
  67. local updated_fetch_response = DataRequest("GetAsync", key, datastore)
  68.  
  69. -- Send another response if the fetch attempt failed.
  70. if not updated_fetch_response:IsSuccess() then
  71. return Response.new(false, nil, 400, "Unable to fetch updated data prior to saving.")
  72. end
  73.  
  74. -- If the fetch attempt succeeded, attempt to set the value to whatever was recieved.
  75. updated_value = updated_fetch_response:GetPayload()
  76. local set_repsonse = DataRequest("SetAsync", key, datastore, updated_value)
  77.  
  78. -- If the set attempt failed, return a bad response along with the updated value fetched previously.
  79. if not set_response:IsSucess() then
  80. return Response.new(false, updated_value, 400, "Unable to set data.")
  81. end
  82. end
  83.  
  84. function DataStoreLib.IsRequestFailed(key)
  85. if key == nil then return end
  86.  
  87. return DataStoreData.FailedRequests[key]
  88. end
  89.  
  90. function DataStoreLib.ClearFailedRequests(key)
  91. if key == nil then return end
  92.  
  93. DataStoreData.FailedRequests[key] = nil
  94. end
  95.  
  96. return DataStoreLib
  97.  
  98. 2nd script local DataStoreData = {}
  99.  
  100. DataStoreData.IsInProduction = false
  101. DataStoreData.PlayerSchemaVersion = 0 -- Redundant right now.
  102. DataStoreData.MaxSuccessiveRetry = 3
  103.  
  104. DataStoreData.RequestTypeMap = {
  105. OnUpdate = Enum.DataStoreRequestType.OnUpdate,
  106. GetAsync = Enum.DataStoreRequestType.GetAsync,
  107. UpdateAsync = Enum.DataStoreRequestType.UpdateAsync,
  108. SetAsync = Enum.DataStoreRequestType.SetIncrementAsync,
  109. GetSortedAsync = Enum.DataStoreRequestType.GetSortedAsync,
  110. IncrementAsync = Enum.DataStoreRequestType.SetIncrementAsync,
  111. SetIncrementAsync = Enum.DataStoreRequestType.SetIncrementAsync,
  112. SetIncrementSortedAsync = Enum.DataStoreRequestType.SetIncrementSortedAsync
  113. }
  114.  
  115. DataStoreData.FailedRequests = {} -- key: has_failed, has_failed is a boolean which states whether or not any request to this key has failed, is set to false once resolved.
  116.  
  117. return DataStoreData
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement