-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.luau
138 lines (107 loc) · 3.21 KB
/
init.luau
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
--!strict
local types = require(script.Parent.types)
local DSS = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local IsServer = RunService:IsServer()
local DataRemote = script:WaitForChild("DataRemote") :: RemoteEvent
local ServerCache = {} :: {[Player]: {Data: types.SaveTemplate}}
local ClientCache = {} :: types.SaveTemplate
local Hook = require(script.Parent.Hook)
--- ### Data.luau
---
--- data handler
--- intended to be used in conjunction with profileservice
local Data = {
Config = require(script:WaitForChild("Config"));
LoadHook = nil :: ((Player: Player) -> {Data: types.SaveTemplate}?)?;
ReleaseHook = nil :: ((Player: Player) -> boolean)?;
}
--- returns player's save data
function Data.get(player: Player?): types.SaveTemplate
if IsServer then
assert(player)
return ServerCache[player].Data
else
return ClientCache
end
end
--- reads player's data value stored at key
function Data.read(player: Player, key: string): unknown
if IsServer then
return ServerCache[player].Data[key]
else
return ClientCache[key]
end
end
--- sends select keys and their associated value to player
function Data.sync(player: Player, keys: {string}?)
assert(IsServer)
local savedata = ServerCache[player].Data
local packet = {}
for _, Key in (keys or Data.Config.SyncKeys) do
packet[Key] = savedata[Key]
end
DataRemote:FireClient(player, packet)
end
--- is called when a sync packet is received
function Data.recv(packet: types.SaveTemplate)
assert(not IsServer)
for k, v in packet do
ClientCache[k] = v
Hook("k_DataChanged_" .. (k :: any)).Fire(v)
end
end
--- fires function when value is written to key
function Data.changed(key: string, f: (value: any) -> ())
return Hook("k_DataChanged_" .. key).Connect(f)
end
--- adds player saveprofile to cache
function Data.addprofile(player: Player, saveprofile: { Data: types.SaveTemplate })
assert(IsServer)
ServerCache[player] = saveprofile
end
function Data.releaseprofile(player: Player)
assert(IsServer)
if ServerCache[player] then
(ServerCache[player] :: any):Release()
end
end
--- removes player saveprofile from cache
function Data.removeprofile(player: Player)
assert(IsServer)
ServerCache[player] = nil
end
--- gdpr what i need you to do
--- ```
--- "24103210 12345 67890"
--- "24103210, 12345, 67890"
--- ```
function Data.wipe(gameid: number, lazystring: string)
local datastore = DSS:GetDataStore(Data.Config.Name)
assert(game.GameId == gameid, "gameid mismatch")
assert(RunService:IsStudio() and RunService:IsEdit() and IsServer and datastore)
local savekey = Data.Config.SaveKey
local failures = table.create(32)
local _, x = string.gsub(lazystring, "%d+", function(LazyId)
local s, e = pcall(function()
datastore:RemoveAsync(LazyId .. savekey)
end)
if not s then
print(string.format("unable to wipe data key %s:", LazyId), e)
table.insert(failures, tonumber(LazyId))
return ""
else
print(string.format("wiped data key %s:", LazyId))
end
return LazyId
end)
print(string.format("%d entries wiped", x))
if #failures > 0 then
print(string.format("failed to wipe %d entries:", #failures), failures)
end
end
if IsServer then
else
DataRemote.OnClientEvent:Connect(Data.recv)
end
return Data