Advertisement
Eshkation-

data_manager.lua

Oct 16th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.04 KB | None | 0 0
  1. function string.split(s, p)
  2.     local f = {}
  3.     for v in string.gmatch(s, '([^'..p..']+)') do
  4.         table.insert(f, v)
  5.     end
  6.     return f
  7. end
  8.  
  9. local function dataManager(...)
  10.     local args = {...}
  11.     local self = {
  12.  
  13.     }
  14.  
  15.     function self:set_player(playerName, playerData)
  16.         -- player data should be a plain string formatted as [MODULE_KEY](MODULE_DATA)ยง[MODULE_KEY](MODULE_DATA)
  17.         -- others formats the system will ignore, but not remove it
  18.  
  19.         local hasModuleData = false
  20.         for playerModuleKey, playerModuleData in string.gmatch(playerData, '%[(.-)%]%((.-)%)[ยง]') do
  21.             if (playerModuleKey == self.MODULE_KEY) then
  22.                 hasModuleData = true
  23.                 local rawData = string.gsub(playerData, '%['..playerModuleKey..'%]%('..playerModuleData..'%)', ';PLACEHOLDER;', 1)
  24.                 self.MODULE_PLAYERS[playerName] = {
  25.                     content = self:assert_module_data(playerModuleData),
  26.                     raw = rawData
  27.                 }
  28.                 break
  29.             end
  30.         end
  31.         if (not hasModuleData) then
  32.             self:set_player(playerName, playerData..string.format('[%s](NULL)ยง', self.MODULE_KEY))
  33.         end
  34.     end
  35.  
  36.     function self:retrieve_player(playerName)
  37.         if (self.MODULE_PLAYERS[playerName]) then
  38.             local rawData = self.MODULE_PLAYERS[playerName].raw
  39.             local finalData = {}
  40.             for index, args in pairs(self.MODULE_DATA) do
  41.                 local value = self.MODULE_PLAYERS[playerName].content[args.key]
  42.                 if type(value) == 'boolean' then
  43.                     value = value and '1' or '0'
  44.  
  45.                 elseif type(value) == 'number' then
  46.                     value = tostring(value)
  47.  
  48.                 elseif type(value) == 'table' then
  49.                     value = table.concat(value, '&')
  50.                 end
  51.                 table.insert(finalData, value)
  52.             end
  53.             local playerData = string.format('[%s](%s)', self.MODULE_KEY, table.concat(finalData, self.MODULE_SEPARATOR))
  54.             return string.gsub(rawData, ';PLACEHOLDER;', playerData)
  55.         else
  56.             return false, 'PLAYER_NOT_FOUND'
  57.         end
  58.     end
  59.  
  60.     function self:assert_module_data(playerData)
  61.         local dataContents = string.split(playerData, self.MODULE_SEPARATOR)
  62.         local finalData = {}
  63.         for index, args in pairs(self.MODULE_DATA) do
  64.             local value = dataContents[args.index]
  65.             if (value) then
  66.                 value = self:change_var_type(value, args.type)
  67.             end
  68.             finalData[args.key] = value or args.default
  69.         end
  70.         return finalData
  71.     end
  72.  
  73.     function self:put(playerName, values)
  74.         if (self.MODULE_PLAYERS[playerName]) then
  75.             for index, value in pairs(values) do
  76.                 if (self.MODULE_PLAYERS[playerName].content[index]) then
  77.                     self.MODULE_PLAYERS[playerName].content[index] = value
  78.                 end
  79.             end
  80.         end
  81.     end
  82.  
  83.     function self:get(playerName, index)
  84.         if (self.MODULE_PLAYERS[playerName]) then
  85.             if (self.MODULE_PLAYERS[playerName].content[index]) then
  86.                 return self.MODULE_PLAYERS[playerName].content[index]
  87.             else
  88.                 return false, 'INDEX_NOT_FOUND'
  89.             end
  90.         else
  91.             return false, 'PLAYER_NOT_FOUND'
  92.         end
  93.     end
  94.  
  95.     function self:change_var_type(variable, newType)
  96.         if (newType == 'number') then
  97.             return tonumber(variable) or 0
  98.  
  99.         elseif (newType == 'boolean') then
  100.             return variable == '1' and true or false
  101.  
  102.         elseif (newType == 'table') then
  103.             return string.split(variable , '&')
  104.         end
  105.     end
  106.  
  107.     function self:init(moduleKey, dataArray, dataSeparator)
  108.         self.MODULE_DATA = {}
  109.         self.MODULE_KEY = moduleKey
  110.         self.MODULE_PLAYERS = {}
  111.         self.MODULE_SEPARATOR = dataSeparator
  112.         local avaliableDataTypes = {
  113.             number = true,
  114.             string = true,
  115.             table  = true,
  116.             boolean = true
  117.         }
  118.         for indexName, args in pairs(dataArray) do
  119.             if (args.index) then
  120.                 if (type(args.index) == 'number') then
  121.                     if (not self.MODULE_DATA[args.index]) then
  122.                         if (args.type) then
  123.                             if (avaliableDataTypes[args.type]) then
  124.                                 if (args.default_value) then
  125.                                     self.MODULE_DATA[args.index] = {
  126.                                         index   = args.index,
  127.                                         key     = indexName,
  128.                                         type    = args.type,
  129.                                         default = args.default_value
  130.                                     }
  131.  
  132.                                 else
  133.                                     error(string.format('\'default_value\' key not present at init.MODULE_DATA.%s', indexName))
  134.                                 end
  135.                             else
  136.                                 error(string.format('\'type\' value is not supported at init.MODULE_DATA.%s (got \'%s\')', indexName, args.type))
  137.                             end
  138.                         else
  139.                             error(string.format('\'type\' key not present at init.MODULE_DATA.%s', indexName))
  140.                         end
  141.                     else
  142.                         error(string.format('\'index\' value at init.MODULE_DATA.%s is duplicated (init.MODULE_DATA.%s)', indexName, self.MODULE_DATA[args.index].key))
  143.                     end
  144.                 else
  145.                     error(string.format('\'type\' key at init.MODULE_DATA.%s is not a number, got \'%s\'', indexName, type(args.index)))
  146.                 end
  147.             else
  148.                 error(string.format('\'index\' key not present at init.MODULE_DATA.%s', indexName))
  149.             end
  150.         end
  151.     end
  152.  
  153.     self:init(args[1], args[2], args[3] or '#')
  154.     return self
  155. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement