-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataFlagModule.lua
More file actions
176 lines (158 loc) · 5.52 KB
/
DataFlagModule.lua
File metadata and controls
176 lines (158 loc) · 5.52 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
--[[
MIT License
Copyright (c) 2020 Saldor010
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]--
-- Change this datastore to something unique to your game preferably
local dataStore = game:GetService("DataStoreService"):GetDataStore("dataFlags")
local httpService = game:GetService("HttpService")
local module = {}
local writeCache = {}
local readCache = {}
local function styledWarn(t)
warn("["..script.Parent.Name.."] "..t)
end
local function pushCache()
for userId,data in pairs(writeCache) do
-- Wrap the datastore request into a protected call
local success, err = pcall(function()
dataStore:UpdateAsync(userId,function(cookedData)
if cookedData == nil then
-- Initalize a new JSON datastructure for the player
return httpService:JSONEncode(data)
else
-- Edit the pre-existing datastructure
local rawData = httpService:JSONDecode(cookedData)
for flag,value in pairs(data) do
rawData[flag] = value
end
return httpService:JSONEncode(rawData)
end
end)
end)
-- If there was an error, warn the dev console.
if err then
styledWarn("Datastore error encountered: "..err)
end
end
writeCache = {}
end
local function getFlagsFromDataStoreForUserId(userId)
-- Wrap the datastore request into a protected call
local rawData
local success,err = pcall(function()
--[[
Grab the "cooked" JSON data from the datastore and turn it into
a "raw" Lua table
]]--
local cookedData = dataStore:GetAsync(userId)
if cookedData == nil then
-- If the datastore has no registry for the player, return nil
return nil
end
rawData = httpService:JSONDecode(cookedData)
end)
-- If there was an error, warn the dev console.
if err then
styledWarn("Datastore error encountered: "..err)
end
if success then
--[[
Our method contract specifies that a successful return value must
be a table, so if the player has no initalized flags table, then
return an empty table instead of nil
]]--
if rawData == nil then rawData = {} end
--[[
Save this data into the readCache for later accesses
]]--
readCache[userId] = rawData
return rawData,true
else
return nil,false
end
end
--[[
This function will attempt to read all of the flags from the player's collection,
it will return the raw Lua table containing all of the player's flags and true if
there were no datastore errors, nil and false if there were datastore errors. If
ignoreCache is true, then this function will always attempt to grab from the data store.
]]--
function module.readAllFlagsForUserID(userId,ignoreCache)
if readCache[userId] and not ignoreCache then
for k,v in pairs(readCache[userId]) do
print(tostring(k).." : "..tostring(v))
end
return readCache[userId],true
else
return getFlagsFromDataStoreForUserId(userId)
end
end
--[[
This function will attempt to read a flag from the player's collection, it
will return the flag value and true if there were no datastore errors, nil and
false if there were datastore errors. If ignoreCache is true, then this function
will always attempt to grab from the data store.
]]--
function module.readFlagForUserID(userId,flag,ignoreCache)
local rawData,success = module.readAllFlagsForUserID(userId,ignoreCache)
if success then
return rawData[flag],true
else
return nil,false
end
end
--[[
This function will attempt to write a flag to the player's collection, it
will return true if there were no datastore errors, false if there were
datastore errors. If ignoreCache is true, then this function will immediately
push the changes to the data store (as well as any other changes waiting in
the cache)
]]--
function module.writeFlagForUserID(userId,flag,value,ignoreCache)
if writeCache[userId] == nil then
writeCache[userId] = {}
end
if readCache[userId] == nil then
readCache[userId] = {}
end
writeCache[userId][flag] = value
readCache[userId][flag] = value
if ignoreCache then
pushCache()
end
end
--[[
This function will force all changes in the cache to update to the datastore
]]--
function module.pushCache()
pushCache()
end
-- The following functions are wrappers for their userId driven counterparts
function module.readAllFlagsForPlayer(player,ignoreCache)
local a,b = module.readAllFlagsForUserID(player.userId,ignoreCache)
return a,b
end
function module.readFlagForPlayer(player,flag,ignoreCache)
local a,b = module.readFlagForUserID(player.userId,flag,ignoreCache)
return a,b
end
function module.writeFlagForPlayer(player,flag,value,ignoreCache)
local a = module.writeFlagForUserID(player.userId,flag,value,ignoreCache)
return a
end
return module