Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Service = {}
- Service.__index = Service
- local DataStoreService = game:GetService('DataStoreService')
- local RunService = game:GetService('RunService')
- local maxData = {Level=60}
- local dataSignals = game:GetService('ReplicatedStorage'):WaitForChild('GameExtras'):WaitForChild('DataSignals')
- local InternalSignals = script:WaitForChild('InternSignals')
- if RunService:IsServer() then
- shared.PlayerDataStore = DataStoreService:GetDataStore('[TEST]-PlayerDataStore-9')
- shared.GuildDataStore = DataStoreService:GetDataStore('[TEST]-GuildDataStore-1')
- local TextCompressor = require(script:WaitForChild('TextCompressor'))
- local HttpService = game:GetService('HttpService')
- local dataSignals = game:GetService('ReplicatedStorage'):WaitForChild('GameExtras'):WaitForChild('DataSignals')
- local dataTable = {}
- local function starterData()
- local starterData = {Level=1;Experience=0;Gold=0;Silver=0;Class='none';Inventory={};Guild={ID=0;Name='none';Rank={PermissionLevel=0;Name='none'}}}
- return starterData
- end
- local function newGuildPlayer(PlayerName,PermissionLevel,RankName)
- local newPlayer = {Name=PlayerName;Rank={PermissionLevel=PermissionLevel or 0;Name=RankName or 'Recruit'}}
- return newPlayer
- end
- local function reviewDataStructure(data)
- for index,value in pairs(starterData()) do
- if data[index] == nil then
- data[index] = value
- end
- end
- end
- function Service.getPlayer(player)
- assert(player:IsA('Player'),'Player not valid')
- local playerData = dataTable[player.UserId] or TextCompressor:Decompress(shared.PlayerDataStore:GetAsync(player.UserId)) or starterData();
- if typeof(playerData) == 'string' then
- playerData = HttpService:JSONDecode(playerData)
- end
- playerData.DataType = 'PlayerData'
- playerData.ID = player.UserId
- reviewDataStructure(playerData)
- dataTable[player.UserId] = playerData
- return setmetatable(playerData,Service)
- end
- function Service.getPlayerByUserId(userId)
- local player = game:GetService('Players'):GetPlayerByUserId(userId)
- assert(player,'UserId not valid')
- local playerData = dataTable[userId] or HttpService:JSONDecode(TextCompressor:Decompress(shared.PlayerDataStore:GetAsync(userId))) or starterData();
- player.DataType = 'PlayerData'
- player.ID = userId
- reviewDataStructure(playerData)
- dataTable[userId] = playerData
- return setmetatable(playerData,Service)
- end
- function Service.createGuild(guildName,guildOwner)
- local guildData = {Name=guildName;Owner=guildOwner}
- guildData.DataType = 'GuildData'
- guildData.ID = tostring(tick() - math.random(0,1))
- guildData.Members = {[guildOwner.UserId]=newGuildPlayer(guildOwner,255,'Leader')}
- guildData.Economy = 0
- guildData.Level = 0
- shared.GuildDataStore:SetAsync(guildData.ID,guildData)
- return setmetatable(guildData,Service)
- end
- function Service.getGuild(guildID)
- local guildData = shared.GuildDataStore:GetAsync(guildID)
- if not guildData then return error('Guild not valid') end
- return setmetatable(guildData,Service)
- end
- function Service:Add(keys,key,newValue,forceChange)
- forceChange = forceChange or false
- local dataPath = #keys == 1 and self or nil
- for i=1,#keys-1 do
- dataPath = dataPath and dataPath[keys[i]] or self[keys[i]]
- end
- local oldValue = dataPath[keys[#keys]][key]
- if typeof(oldValue) ~= 'table' then return end
- if typeof(newValue) == 'number' then
- dataPath[keys[#keys]][key] = dataPath[keys[#keys]][key] == nil and newValue or not forceChange and dataPath[keys[#keys]][key] + newValue or newValue
- else
- dataPath[keys[#keys]][key] = newValue
- end
- newValue = dataPath[keys[#keys]][key]
- if dataSignals['DataEvent-'..keys[#keys]..'Added'] then
- dataSignals['DataEvent-'..keys[#keys]..'Added']:FireClient(game.Players:GetPlayerByUserId(self.ID),{newValue=newValue,oldValue=oldValue})
- end
- end
- function Service:Change(keys,newValue)
- local dataPath = #keys == 1 and self or nil
- for i=1,#keys-1 do
- dataPath = dataPath and dataPath[keys[i]] or self[keys[i]]
- end
- local oldValue = dataPath[keys[#keys]]
- if maxData[keys[#keys]] then
- if newValue > maxData[keys[#keys]] then return end
- end
- dataPath[keys[#keys]] = newValue
- if dataSignals['DataEvent-'..keys[#keys]] then
- dataSignals['DataEvent-'..keys[#keys]]:FireClient(game.Players:GetPlayerByUserId(self.ID),{newValue=newValue,oldValue=oldValue,maxValue = maxData[keys[#keys]] or nil})
- end
- end
- function Service:Save()
- local dataToSave = TextCompressor:Compress(HttpService:JSONEncode(self))
- if self.DataType == 'GuildData' then
- shared.GuildDataStore:UpdateAsync(self.ID,function(old)
- local dataSave = dataToSave or old
- return dataSave
- end)
- elseif self.DataType == 'PlayerData' then
- shared.PlayerDataStore:UpdateAsync(self.ID,function(old)
- local dataSave
- local oldValue = old and HttpService:JSONDecode(TextCompressor:Decompress(old)) or nil
- if oldValue then
- print('Verifying data...')
- if self.Level > oldValue.Level then
- dataSave = dataToSave
- elseif self.Level < oldValue.Level then
- dataSave = old
- elseif self.Level == oldValue.Level then
- if self.Experience > oldValue.Experience then
- dataSave = dataToSave
- elseif self.Experience > oldValue.Experience then
- dataSave = old
- elseif self.Experience == oldValue.Experience then
- dataSave = dataToSave
- end
- end
- else
- dataSave = dataToSave
- end
- print('Saving '..self.ID..' data...',TextCompressor:Decompress(dataSave))
- return dataSave
- end)
- end
- dataTable[self.ID] = nil
- self = nil
- end
- function Service:Delete()
- if self.DataType == 'GuildData' then
- local success, val = pcall(function()
- return shared.GuildDataStore:RemoveAsync(self.ID)
- end)
- if success == true then
- self = nil
- end
- elseif self.DataType == 'PlayerData' then
- local success, val = pcall(function()
- return shared.PlayerDataStore:RemoveAsync(self.ID)
- end)
- if success == true then
- self = nil
- end
- end
- end
- elseif RunService:IsClient() then
- function Service.listen(name)
- local Event = dataSignals:FindFirstChild('DataEvent-'..name)
- if not Event then
- InternalSignals.createEventListener:InvokeServer('DataEvent-'..name)
- end
- return Event
- end
- end
- return Service
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement