Advertisement
CluelessDev

Player Data handler

Jul 6th, 2022
733
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.79 KB | None
  1. --[[
  2.     Singleton class to handle the player data Creation, Reading, Writing operations
  3. ]]
  4.  
  5. --? <|=============== CONSTRUCTOR ===============|>
  6. local PlayerDataHandler = {}
  7. PlayerDataHandler.__index = PlayerDataHandler
  8.  
  9.  
  10. function PlayerDataHandler.new()
  11.     local self = setmetatable({}, PlayerDataHandler)
  12.     self.PlayerDataObjects = {}
  13.     return self
  14. end
  15.  
  16. --- <|=============== PRIVATE FUNCTIONS ===============|>
  17.  
  18. --# Aux function to map data from PlayerData Object
  19. --# ObjectValues field into object value instances
  20.  
  21. local function MapObjectValues(fromObjectValuesTable: table)
  22.    
  23.     for key, dataObject in pairs(fromObjectValuesTable) do
  24.         local objectVal: ObjectValue = Instance.new(dataObject.Type) or error("Value Type field missing")
  25.  
  26.         objectVal.Name   = dataObject.Name or key
  27.         objectVal.Value  = dataObject.Value or error("Value field missing")
  28.         objectVal.Parent = dataObject.Parent or error("Parent field missing")
  29.     end
  30. end
  31.  
  32. --# Aux function called when doing a write operation on an object value type.
  33. --# This function exist to keep both the internal DataObject value & the Instance
  34. --# Value synced!
  35.  
  36. local function SetObjectValueInstanceValue(parent, name, newValue)
  37.     local ov: ObjectValue = parent:FindFirstChild(name, true)
  38.     ov.Value = newValue
  39. end
  40.  
  41. local function IncrementObjectValueInstanceValue(parent, name, newValue)
  42.     local ov: ObjectValue = parent:FindFirstChild(name, true)
  43.     ov.Value += newValue
  44. end
  45.  
  46. --+ <|=============== PUBLIC FUNCTIONS ===============|>
  47.  
  48. --# Builds a data object for a player from the given MetaData and ObjectvVlue tables.
  49. --# Also Instances object values under the given parent for the system to track as player owned.
  50. function PlayerDataHandler:BuildPlayerDataObject(player, fromTable)
  51.    
  52.     table.insert(self.PlayerDataObjects, player.Name)
  53.  
  54.     fromTable.ObjectValues = fromTable.ObjectValues or {}
  55.     fromTable.MetaData     = fromTable.MetaData or {}
  56.  
  57.  
  58.     self.PlayerDataObjects[player.Name] = { --!//TODO change this to use the player id instead
  59.         Instance     = player,
  60.         ObjectValues = fromTable.ObjectValues,
  61.         MetaData     = fromTable.MetaData,
  62.     }
  63.  
  64.     MapObjectValues(self.PlayerDataObjects[player.Name].ObjectValues)
  65. end
  66.  
  67. --[[
  68.     Player data object
  69.     - Instance     : Player instance reference
  70.     - ObjectValues: This entry holds all data objects to be represented as object values
  71.     - Metadata   :  Player trackable metadata that does not need/can't be represented as object values
  72.            
  73.     ObjectValues Type Interface:
  74.     - Type  : any      -> Mapped to the Instance Type if no type is given the operation will fail
  75.     - Name  : string?   -> Mapped to the Name property if no name is given the key name will be used,
  76.     - Value : any?     -> Mapped to the Value property if no value is given then it will default to nil,
  77.     - Parent: Instance? -> Mapped to the Parent property if no Parent is given it will default to the Player instance
  78.  
  79.     MetaData Type Interface:
  80.     None, given by the coder
  81. --]]
  82.  
  83. -- Object value functions
  84. --* Overwrites value field from both PlayerDataObject ObjectValue and its physical instance in the game
  85. function PlayerDataHandler:SetPlayerDataValue(player: Player, name: string, newValue: any )
  86.     local playerDataObject = self.PlayerDataObjects[player.Name]
  87.  
  88.     --# The PlayerDataObject Exists? Great!
  89.     --# Now look if the objectvalue type exist... It does!? Great!
  90.     --# Then overwrite both the internal valueObject value and Instance value
  91.  
  92.     if playerDataObject then
  93.         for key, objectValue in pairs(playerDataObject.ObjectValues) do
  94.             if name == objectValue.Name or name == key then
  95.                 objectValue.Value = newValue
  96.                 SetObjectValueInstanceValue(objectValue.Parent, name or key, newValue)
  97.                 return
  98.             end
  99.         end
  100.  
  101.         warn("ObjectValue field not found!")
  102.         return nil
  103.     end
  104.  
  105.     warn("Given PlayerDataObject was not found")
  106.     return nil
  107. end
  108.  
  109. --* Overwrites value field from both PlayerDataObject ObjectValue and its physical instance in the game
  110. function PlayerDataHandler:IncrementPlayerDataValue(player: Player, name: string, byValue: any )
  111.     local playerDataObject = self.PlayerDataObjects[player.Name]
  112.  
  113.     --# The PlayerDataObject Exists? Great!
  114.     --# Now look if the objectvalue type exist... It does!? Great!
  115.     --# Then overwrite both the internal valueObject value and Instance value
  116.  
  117.     if playerDataObject then
  118.         for key, objectValue in pairs(playerDataObject.ObjectValues) do
  119.             if name == objectValue.Name or name == key and objectValue.Type == "NumberValue" then
  120.                 objectValue.Value += byValue
  121.                 IncrementObjectValueInstanceValue(objectValue.Parent, name or key, byValue)
  122.                 return
  123.             end
  124.         end
  125.  
  126.         warn("ObjectValue field not found!")
  127.         return nil
  128.     end
  129.  
  130.     warn("Given PlayerDataObject was not found")
  131.     return nil
  132. end
  133.  
  134. --* Returns ObjectValue table from PlayerDataObject, NOT THE PHYSICAL OBJECT VALUE INSTANCE
  135. function PlayerDataHandler:GetPlayerObjectValue(player: Player, name: string)
  136.     local playerDataObject = self.PlayerDataObjects[player.Name]
  137.  
  138.     --# Does the given PlayerDataObject Exists? Great!
  139.     --# Now Does the given objectvalue field exist, It does!? Great!
  140.     --# Then return the inter ObjectValue value field
  141.    
  142.     if playerDataObject then
  143.         local objectValue: table = playerDataObject.ObjectValues[name]
  144.  
  145.         if objectValue then
  146.             return playerDataObject.ObjectValues[name]
  147.         end
  148.  
  149.         warn("Given ObjectValue field not found!")
  150.         return nil
  151.     end    
  152.  
  153.     warn("Given PlayerDataObject Not found")
  154.     return nil
  155. end
  156.  
Advertisement
RAW Paste Data Copied
Advertisement