Guest User

OOP Data module

a guest
Nov 19th, 2019
91
52 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local Service = {}
  2. Service.__index = Service
  3.  
  4. local DataStoreService = game:GetService('DataStoreService')
  5.  
  6. shared.PlayerDataStore = DataStoreService:GetDataStore('[TEST]-PlayerDataStore-1')
  7. shared.GuildDataStore = DataStoreService:GetDataStore('[TEST]-GuildDataStore-1')
  8.  
  9. local TextCompressor = require(script:WaitForChild('TextCompressor'))
  10. local HttpService = game:GetService('HttpService')
  11. local dataSignals = game:GetService('ReplicatedStorage'):WaitForChild('GameExtras'):WaitForChild('DataSignals')
  12.  
  13. local dataTable = {}
  14.  
  15. local function starterData()
  16.     local starterData = {Level=1;Experience=0;Class='none';Inventory={};Guild={ID=0;Name='none';Rank={PermissionLevel=0;Name='none'}}}
  17.     return starterData
  18. end
  19.  
  20. local function newGuildPlayer(PlayerName,PermissionLevel,RankName)
  21.     local newPlayer = {Name=PlayerName;Rank={PermissionLevel=PermissionLevel or 0;Name=RankName or 'Recruit'}}
  22.     return newPlayer
  23. end
  24.  
  25. local function reviewDataStructure(data)
  26.     for index,value in pairs(starterData()) do
  27.         if data[index] == nil then
  28.             data[index] = value
  29.         end
  30.     end
  31. end
  32.  
  33. function Service.getPlayer(player)
  34.    
  35.     assert(player:IsA('Player'),'Player not valid')
  36.    
  37.     local playerData = dataTable[player.UserId] or TextCompressor:Decompress(shared.PlayerDataStore:GetAsync(player.UserId)) or starterData();
  38.     if typeof(playerData) == 'string' then
  39.         playerData = HttpService:JSONDecode(playerData)
  40.     end
  41.    
  42.     playerData.DataType = 'PlayerData'
  43.     playerData.ID = player.UserId
  44.    
  45.     reviewDataStructure(playerData)
  46.    
  47.     return setmetatable(playerData,Service)
  48.    
  49. end
  50.  
  51. function Service.getPlayerByUserId(userId)
  52.    
  53.     local player = game:GetService('Players'):GetPlayerByUserId(userId)
  54.    
  55.     assert(player,'UserId not valid')
  56.    
  57.     local playerData = dataTable[userId] or HttpService:JSONDecode(TextCompressor:Decompress(shared.PlayerDataStore:GetAsync(userId))) or starterData();
  58.    
  59.     player.DataType = 'PlayerData'
  60.     player.ID = userId
  61.    
  62.     reviewDataStructure(playerData)
  63.    
  64.     return setmetatable(playerData,Service)
  65.    
  66. end
  67.  
  68. function Service.createGuild(guildName,guildOwner)
  69.    
  70.     local guildData = {Name=guildName;Owner=guildOwner}
  71.     guildData.DataType = 'GuildData'
  72.     guildData.ID = tostring(tick() - math.random(0,1))
  73.     guildData.Members = {[guildOwner.UserId]=newGuildPlayer(guildOwner,255,'Leader')}
  74.     guildData.Economy = 0
  75.     guildData.Level = 0
  76.    
  77.     shared.GuildDataStore:SetAsync(guildData.ID,guildData)
  78.    
  79.     return setmetatable(guildData,Service)
  80.    
  81. end
  82.  
  83. function Service.getGuild(guildID)
  84.    
  85.     local guildData = shared.GuildDataStore:GetAsync(guildID)
  86.    
  87.     if not guildData then return error('Guild not valid') end
  88.    
  89.     return setmetatable(guildData,Service)
  90.    
  91. end
  92.  
  93. function Service:Change(keys,newValue)
  94.    
  95.     local dataPath
  96.    
  97.     for i=1,#keys-1 do
  98.         dataPath = dataPath and dataPath[keys[i]] or self[keys[i]]
  99.     end
  100.  
  101.     local oldValue = dataPath[keys[#keys]]
  102.     dataPath[keys[#keys]] = newValue
  103.  
  104.     dataSignals.DataChanged:FireClient(game.Players:GetPlayerByUserId(self.ID),keys[#keys],newValue,oldValue)
  105.    
  106. end
  107.  
  108. function Service:Save()
  109.    
  110.     local dataToSave = TextCompressor:Compress(HttpService:JSONEncode(self))
  111.    
  112.     if self.DataType == 'GuildData' then
  113.         shared.GuildDataStore:UpdateAsync(self.ID,function(old)
  114.             local dataSave = dataToSave or old
  115.             return dataSave
  116.         end)
  117.     elseif self.DataType == 'PlayerData' then
  118.         shared.PlayerDataStore:UpdateAsync(self.ID,function(old)
  119.             local dataSave = dataToSave or old
  120.             return dataSave
  121.         end)
  122.     end
  123.    
  124. end
  125.  
  126. function Service:Delete()
  127.    
  128.     if self.DataType == 'GuildData' then
  129.         local success, val = pcall(function()
  130.             return shared.GuildDataStore:RemoveAsync(self.ID)
  131.         end)
  132.        
  133.         if success == true then
  134.             self = nil
  135.         end
  136.        
  137.     elseif self.DataType == 'PlayerData' then
  138.         local success, val = pcall(function()
  139.             return shared.PlayerDataStore:RemoveAsync(self.ID)
  140.         end)
  141.        
  142.         if success == true then
  143.             self = nil
  144.         end
  145.        
  146.     end
  147.    
  148. end
  149.  
  150. return Service
RAW Paste Data