-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountService.luau
More file actions
169 lines (143 loc) · 4.73 KB
/
AccountService.luau
File metadata and controls
169 lines (143 loc) · 4.73 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
--[[
AccountService
A Profile Data saving service with session locking and session backups.
Authors:
@realbxnnie : Creating the module
// 03/22/2025
]]
------------------------------------------------------------------------------
local function assert(c: any, m: string)
if not c then
error(m, 1)
end
end
local Account = {
Blocked = false,
Data = {}
}
local AccountService = {
}
local AccountStore = {
__name = "",
__datastore = nil,
__backupDatastore = nil,
AccountDataTemplate = {},
Accounts = {}
}
local DataStoreService = game:GetService("DataStoreService")
AccountStore.__index = AccountStore
Account.__index = Account
local function DeepCopy(original)
local copy = {}
for k, v in pairs(original) do
if type(v) == 'table' then
v = DeepCopy(v)
end
copy[k] = v
end
return copy
end
-- <strong>Sets Account's Data.</strong>
-- Example:
-- <code>AccountStore:SetAccountAsync(Player.UserId, {Coins = 100})</code>
function AccountStore:SetAccountAsync(Key: any, Value: any): any
assert(not self.Accounts[Key].Blocked, "Attempt to set a value to a blocked Session")
return self.__datastore:SetAsync(Key, Value)
end
-- <strong>Updates Account's Data.</strong>
-- Example:
-- <code>AccountStore:UpdateAccountAsync(Player.UserId, {Coins = 100})</code>
function AccountStore:UpdateAccountAsync(Key: any, Data: {}): any
assert(not self.Accounts[Key].Blocked, "Attempt to update the value of a blocked Session")
return self.__datastore:UpdateAsync(Key, function()
return Data
end)
end
-- <strong>Saves Account's Data.</strong>
-- Example:
-- <code>AccountStore:SaveAccountAsync(Player.UserId)</code>
function AccountStore:SaveAccountAsync(Key: any): any
assert(not self.Accounts[Key].Blocked, "Attempt to save the value of a blocked Session")
return self.__datastore:SetAsync(Key, self.Accounts[Key].Data)
end
-- <strong>Adds Account's Data to the AccountStore.</strong>
-- Example:
-- <code>AccountStore:AddAccountAsync(Player.UserId)</code>
function AccountStore:AddAccountAsync(Key: any): any
local function add(K, I, V)
if typeof(V) == "table" then
DeepCopy(V)
else
self.Accounts[K][I] = V
end
end
if not self.__datastore:GetAsync(Key) then
self.__datastore:SetAsync(Key, self.AccountDataTemplate)
self.Accounts[Key] = {
Blocked = false,
Data = self.__datastore:GetAsync(Key)
}
else
self.Accounts[Key] = {
Blocked = false,
Data = self.__datastore:GetAsync(Key)
}
for Index, Value in ipairs(self.AccountDataTemplate) do
if not self.Accounts[Key][Index] then
add(Key, Index, Value)
end
end
end
end
-- <strong>Blocks Account's Session.</strong>
-- Example:
-- <code>AccountStore:BlockAccountSessionAsync(Player.UserId)</code>
function AccountStore:BlockAccountSessionAsync(Key: any, Callback: (any) -> any): boolean
assert(not self.Accounts[Key].Blocked, "Attempt to block an already blocked Session")
self.Accounts[Key].Blocked = true
return self.Accounts[Key].Blocked
end
-- <strong>Unlocks Account's Session.</strong>
-- Example:
-- <code>AccountStore:BlockAccountSessionAsync(Player.UserId)</code>
function AccountStore:UnblockAccountSessionAsync(Key: any, Callback: (any) -> any): boolean
assert(not self.Accounts[Key].Blocked, "Attempt to unblock an already unblocked Session")
self.Accounts[Key].Blocked = false
return not self.Accounts[Key].Blocked
end
-- <strong>Restores Account's Session using last-saved backup.</strong>
-- Example:
-- <code>AccountStore:RestoreAccountSessionAsync(Player.UserId)</code>
function AccountStore:RestoreAccountSessionAsync(Key: any): any
assert(self.__backupDatastore:GetAsync(Key), `{Key} backup is empty!`)
do
local BackupData = {}
for Index, Value in self.__backupDatastore:GetAsync(Key) do
BackupData[Index] = Value
end
self.__datastore:SetAsync(Key, BackupData)
self.Accounts[Key].Data = BackupData
end
end
-- <strong>Saves Account's Session.</strong>
-- Example:
-- <code>AccountStore:SaveAccountSessionAsync(Player.UserId)</code>
function AccountStore:SaveAccountSessionAsync(Key: any)
self.__backupDatastore:SetAsync(Key, self.Accounts[Key].Data)
end
-- <strong>Creates a new AccountStore.</strong>
-- Example:
-- <code>local AccountStore = AccountService.new("Data1", {Coins = 0})</code>
-- <code>game.Players.PlayerAdded:Connect(function(Player)</code>
-- <code>local Data = AccountStore:AddAccountAsync(Player.UserId)</code>
function AccountService.new(AccountStoreName: string, AccountDataTemplate: {})
local self = setmetatable({}, AccountStore)
do
self.__name = AccountStoreName
self.__datastore = DataStoreService:GetDataStore(self.__name)
self.__backupDatastore = DataStoreService:GetDataStore(`{self.__name}_BACKUP`)
self.AccountDataTemplate = AccountDataTemplate
end
return self
end
return table.freeze(AccountService)