forked from thexkey/MockDatastoreService
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.lua
More file actions
153 lines (128 loc) · 4.13 KB
/
script.lua
File metadata and controls
153 lines (128 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
-- Mock DataStoreService
-- Crazyman32
-- August 20, 2014
-- Abhidjt, 11th July, 2025.
local DataStoreService = {}
local API = {}
local MT = {}
local realDataStoreService = game:GetService("DataStoreService")
local allStores = {}
if game.Players.LocalPlayer then
warn("Mocked DataStoreService is functioning on the client: The real DataStoreService will not work on the client")
end
-- API:
function API:GetDataStore(name, scope)
assert(type(name) == "string", "DataStore name must be a string; got " .. type(name))
assert(type(scope) == "string" or scope == nil, "DataStore scope must be a string; got " .. type(scope))
scope = scope or "global"
local store = allStores[scope] and allStores[scope][name]
if store then return store end
local data = {}
local updateListeners = {}
local function triggerUpdates(k, v)
if updateListeners[k] then
for _, f in ipairs(updateListeners[k]) do
spawn(function() f(v) end)
end
end
end
local d = {}
d.SetAsync = function(k, v)
assert(v ~= nil, "Value cannot be nil")
data[k] = v
triggerUpdates(k, v)
end
d.UpdateAsync = function(k, func)
local v = func(data[k])
assert(v ~= nil, "Value cannot be nil")
data[k] = v
triggerUpdates(k, v)
end
d.GetAsync = function(k)
return data[k]
end
d.IncrementAsync = function(k, delta)
delta = delta or 1
assert(type(delta) == "number", "Can only increment numbers")
d.UpdateAsync(k, function(num)
assert(type(num) == "number" or num == nil, "Can only increment numbers")
return (num or 0) + delta
end)
end
d.OnUpdate = function(k, onUpdateFunc)
assert(type(onUpdateFunc) == "function", "Update function argument must be a function")
updateListeners[k] = updateListeners[k] or {}
table.insert(updateListeners[k], onUpdateFunc)
end
allStores[scope] = allStores[scope] or {}
allStores[scope][name] = d
return d
end
function API:GetGlobalDataStore()
return self:GetDataStore("global", "global")
end
function API:GetOrderedDataStore(name, scope)
local dataStore = self:GetDataStore(name, scope)
local allData = {}
local d = {
GetAsync = function(k) return dataStore:GetAsync(k) end,
SetAsync = function(k, v)
assert(type(v) == "number", "Value must be a number")
dataStore:SetAsync(k, v)
allData[k] = v
end,
UpdateAsync = function(k, func)
dataStore:UpdateAsync(k, function(oldValue)
local v = func(oldValue)
assert(type(v) == "number", "Value must be a number")
allData[k] = v
return v
end)
end,
IncrementAsync = function(k, delta)
dataStore:IncrementAsync(k, delta)
allData[k] = (allData[k] or 0) + delta
end,
GetSortedAsync = function(isAscending, pageSize, minValue, maxValue)
assert(type(pageSize) == "number" and pageSize > 0, "PageSize must be a positive integer")
assert(not minValue or type(minValue) == "number", "MinValue must be a number")
assert(not maxValue or type(maxValue) == "number", "MaxValue must be a number")
if minValue and maxValue then
assert(minValue <= maxValue, "MinValue must be less than or equal to MaxValue")
end
local data = {}
for k, v in pairs(allData) do
if (not minValue or v >= minValue) and (not maxValue or v <= maxValue) then
table.insert(data, {key = k, value = v})
end
end
table.sort(data, isAscending and function(a, b) return a.value < b.value end or function(a, b) return a.value > b.value end)
pageSize = math.floor(pageSize)
local pages = { IsFinished = false }
for i, v in ipairs(data) do
local pageNum = math.ceil(i / pageSize)
local page = pages[pageNum] or {}
pages[pageNum] = page
page[((i - 1) % pageSize) + 1] = v
end
local currentPage = 1
function pages:GetCurrentPage() return self[currentPage] end
function pages:AdvanceToNextPageAsync()
if currentPage < #pages then
currentPage = currentPage + 1
end
self.IsFinished = currentPage >= #pages
end
return pages
end,
}
return d
end
-- Metatable:
MT.__metatable = true
MT.__index = function(tbl, index)
return API[index] or realDataStoreService[index]
end
MT.__newindex = function() error("Cannot edit MockDataStoreService") end
setmetatable(DataStoreService, MT)
return DataStoreService