-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.lua
207 lines (180 loc) · 5.6 KB
/
script.lua
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
-- Mock DataStoreService
-- Crazyman32
-- August 20, 2014
--[[
USAGE EXAMPLE:
--------
local dataStoreService = game:GetService("DataStoreService")
-- Mock service if the game is offline:
if (game.PlaceId == 0) then
dataStoreService = require(game.ServerStorage.MockDataStoreService)
end
-- dataStoreService will act exactly like the real one
--------
The mocked data store service should function exactly like the
real service. What the mocked service does is "override" the
core methods, such as GetAsync. If you try to index a property
that hasn't been overridden (such as dataStoreService.Name), it
will reference the actual property in the real dataStoreService.
NOTE:
This has been created based off of the DataStoreService on
August 20, 2014. If a change has been made to the service,
this mocked version will not reflect the changes.
--]]
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")
if (allStores[scope] and allStores[scope][name]) then
return allStores[scope][name]
end
local data = {}
local d = {}
local updateListeners = {}
function d:SetAsync(k, v)
assert(v ~= nil, "Value cannot be nil")
data[k] = v
if (updateListeners[k]) then
for _,f in pairs(updateListeners[k]) do
spawn(function() f(v) end)
end
end
end
function d:UpdateAsync(k, func)
local v = func(data[k])
assert(v ~= nil, "Value cannot be nil")
data[k] = v
if (updateListeners[k]) then
for _,f in pairs(updateListeners[k]) do
spawn(function() f(v) end)
end
end
end
function d:GetAsync(k)
return data[k]
end
function d:IncrementAsync(k, delta)
if (delta == nil) then delta = 1 end
assert(type(delta) == "number", "Can only increment numbers")
self:UpdateAsync(k, function(num)
if (num == nil) then
return num
end
assert(type(num) == "number", "Can only increment numbers")
return (num + delta)
end)
end
function d:OnUpdate(k, onUpdateFunc)
assert(type(onUpdateFunc) == "function", "Update function argument must be a function")
if (not updateListeners[k]) then
updateListeners[k] = {onUpdateFunc}
else
table.insert(updateListeners[k], onUpdateFunc)
end
end
if (not allStores[scope]) then
allStores[scope] = {}
end
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 = {}
function d:GetAsync(k)
return dataStore:GetAsync(k)
end
function d:SetAsync(k, v)
assert(type(v) == "number", "Value must be a number")
dataStore:SetAsync(k, v)
allData[k] = v
end
function d:UpdateAsync(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
function d:IncrementAsync(k, delta)
dataStore:IncrementAsync(k, delta)
allData[k] = ((allData[k] or 0) + delta)
end
function d:GetSortedAsync(isAscending, pageSize, minValue, maxValue)
assert(type(pageSize) == "number" and math.floor(pageSize) > 0, "PageSize must be an integer and greater than 0")
assert(minValue == nil or type(minValue) == "number", "MinValue must be a number")
assert(maxValue == nil or type(maxValue) == "number", "MaxValue must be a number")
if (minValue and maxValue) then
assert(minValue <= maxValue, "MinValue must be less or equal to MaxValue")
end
local data = {}
for k,v in pairs(allData) do
local pass = ((not minValue or v >= minValue) and (not maxValue or v <= maxValue))
if (pass) 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 (b.value < a.value) end
)
)
pageSize = math.floor(pageSize)
local pages = {IsFinished = false}
for i,v in pairs(data) do
local pageNum = math.ceil(i / pageSize)
local page = pages[pageNum]
if (not page) then
page = {}
pages[pageNum] = page
end
local index = (((i - 1) % pageSize) + 1)
page[index] = v
end
do
local currentPage = 1
function pages:GetCurrentPage()
return self[currentPage]
end
function pages:AdvanceToNextPageAsync()
local numPages = #pages
if (currentPage < numPages) then
currentPage = (currentPage + 1)
end
self.IsFinished = (currentPage >= numPages)
end
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