Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function string.split(s, p)
- local f = {}
- for v in string.gmatch(s, '([^'..p..']+)') do
- table.insert(f, v)
- end
- return f
- end
- local function dataManager(...)
- local args = {...}
- local self = {
- }
- function self:set_player(playerName, playerData)
- -- player data should be a plain string formatted as [MODULE_KEY](MODULE_DATA)ยง[MODULE_KEY](MODULE_DATA)
- -- others formats the system will ignore, but not remove it
- local hasModuleData = false
- for playerModuleKey, playerModuleData in string.gmatch(playerData, '%[(.-)%]%((.-)%)[ยง]') do
- if (playerModuleKey == self.MODULE_KEY) then
- hasModuleData = true
- local rawData = string.gsub(playerData, '%['..playerModuleKey..'%]%('..playerModuleData..'%)', ';PLACEHOLDER;', 1)
- self.MODULE_PLAYERS[playerName] = {
- content = self:assert_module_data(playerModuleData),
- raw = rawData
- }
- break
- end
- end
- if (not hasModuleData) then
- self:set_player(playerName, playerData..string.format('[%s](NULL)ยง', self.MODULE_KEY))
- end
- end
- function self:retrieve_player(playerName)
- if (self.MODULE_PLAYERS[playerName]) then
- local rawData = self.MODULE_PLAYERS[playerName].raw
- local finalData = {}
- for index, args in pairs(self.MODULE_DATA) do
- local value = self.MODULE_PLAYERS[playerName].content[args.key]
- if type(value) == 'boolean' then
- value = value and '1' or '0'
- elseif type(value) == 'number' then
- value = tostring(value)
- elseif type(value) == 'table' then
- value = table.concat(value, '&')
- end
- table.insert(finalData, value)
- end
- local playerData = string.format('[%s](%s)', self.MODULE_KEY, table.concat(finalData, self.MODULE_SEPARATOR))
- return string.gsub(rawData, ';PLACEHOLDER;', playerData)
- else
- return false, 'PLAYER_NOT_FOUND'
- end
- end
- function self:assert_module_data(playerData)
- local dataContents = string.split(playerData, self.MODULE_SEPARATOR)
- local finalData = {}
- for index, args in pairs(self.MODULE_DATA) do
- local value = dataContents[args.index]
- if (value) then
- value = self:change_var_type(value, args.type)
- end
- finalData[args.key] = value or args.default
- end
- return finalData
- end
- function self:put(playerName, values)
- if (self.MODULE_PLAYERS[playerName]) then
- for index, value in pairs(values) do
- if (self.MODULE_PLAYERS[playerName].content[index]) then
- self.MODULE_PLAYERS[playerName].content[index] = value
- end
- end
- end
- end
- function self:get(playerName, index)
- if (self.MODULE_PLAYERS[playerName]) then
- if (self.MODULE_PLAYERS[playerName].content[index]) then
- return self.MODULE_PLAYERS[playerName].content[index]
- else
- return false, 'INDEX_NOT_FOUND'
- end
- else
- return false, 'PLAYER_NOT_FOUND'
- end
- end
- function self:change_var_type(variable, newType)
- if (newType == 'number') then
- return tonumber(variable) or 0
- elseif (newType == 'boolean') then
- return variable == '1' and true or false
- elseif (newType == 'table') then
- return string.split(variable , '&')
- end
- end
- function self:init(moduleKey, dataArray, dataSeparator)
- self.MODULE_DATA = {}
- self.MODULE_KEY = moduleKey
- self.MODULE_PLAYERS = {}
- self.MODULE_SEPARATOR = dataSeparator
- local avaliableDataTypes = {
- number = true,
- string = true,
- table = true,
- boolean = true
- }
- for indexName, args in pairs(dataArray) do
- if (args.index) then
- if (type(args.index) == 'number') then
- if (not self.MODULE_DATA[args.index]) then
- if (args.type) then
- if (avaliableDataTypes[args.type]) then
- if (args.default_value) then
- self.MODULE_DATA[args.index] = {
- index = args.index,
- key = indexName,
- type = args.type,
- default = args.default_value
- }
- else
- error(string.format('\'default_value\' key not present at init.MODULE_DATA.%s', indexName))
- end
- else
- error(string.format('\'type\' value is not supported at init.MODULE_DATA.%s (got \'%s\')', indexName, args.type))
- end
- else
- error(string.format('\'type\' key not present at init.MODULE_DATA.%s', indexName))
- end
- else
- error(string.format('\'index\' value at init.MODULE_DATA.%s is duplicated (init.MODULE_DATA.%s)', indexName, self.MODULE_DATA[args.index].key))
- end
- else
- error(string.format('\'type\' key at init.MODULE_DATA.%s is not a number, got \'%s\'', indexName, type(args.index)))
- end
- else
- error(string.format('\'index\' key not present at init.MODULE_DATA.%s', indexName))
- end
- end
- end
- self:init(args[1], args[2], args[3] or '#')
- return self
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement