diff --git a/lua/pointshop/providers/data.lua b/lua/pointshop/providers/flatfile.lua similarity index 51% rename from lua/pointshop/providers/data.lua rename to lua/pointshop/providers/flatfile.lua index e1e150e..31295df 100644 --- a/lua/pointshop/providers/data.lua +++ b/lua/pointshop/providers/flatfile.lua @@ -25,6 +25,42 @@ function PROVIDER:GetData(ply, callback) return callback(points, items) end +function PROVIDER:SetPoints( ply, set_points ) + self:GetData(ply, function(points, items) + self:SetData(ply, set_points, items) + end) +end + +function PROVIDER:GivePoints( ply, add_points ) + self:GetData(ply, function(points, items) + self:SetData(ply, points + add_points, items) + end) +end + +function PROVIDER:TakePoints( ply, points ) + self:GivePoints(ply, -points) +end + +function PROVIDER:SaveItem( ply, item_id, data) + self:GiveItem(ply, item_id, data) +end + +function PROVIDER:GiveItem( ply, item_id, data) + self:GetData(ply, function(points, items) + local tmp = table.Copy(ply.PS_Items) + tmp[item_id] = data + self:SetData(ply, points, tmp) + end) +end + +function PROVIDER:TakeItem( ply, item_id ) + self:GetData(ply, function(points, items) + local tmp = table.Copy(ply.PS_Items) + tmp[item_id] = nil + self:SetData(ply, points, tmp) + end) +end + function PROVIDER:SetData(ply, points, items) if not file.IsDir('pointshop', 'DATA') then file.CreateDir('pointshop') diff --git a/lua/pointshop/providers/json.lua b/lua/pointshop/providers/json.lua new file mode 100644 index 0000000..31295df --- /dev/null +++ b/lua/pointshop/providers/json.lua @@ -0,0 +1,75 @@ +function PROVIDER:GetData(ply, callback) + if not file.IsDir('pointshop', 'DATA') then + file.CreateDir('pointshop') + end + + local points, items + + local filename = string.Replace(ply:SteamID(), ':', '_') + + if not file.Exists('pointshop/' .. filename .. '.txt', 'DATA') then + file.Write('pointshop/' .. filename .. '.txt', util.TableToJSON({ + Points = 0, + Items = {} + })) + + points = 0 + items = {} + else + local data = util.JSONToTable(file.Read('pointshop/' .. filename .. '.txt', 'DATA')) + + points = data.Points or 0 + items = data.Items or {} + end + + return callback(points, items) +end + +function PROVIDER:SetPoints( ply, set_points ) + self:GetData(ply, function(points, items) + self:SetData(ply, set_points, items) + end) +end + +function PROVIDER:GivePoints( ply, add_points ) + self:GetData(ply, function(points, items) + self:SetData(ply, points + add_points, items) + end) +end + +function PROVIDER:TakePoints( ply, points ) + self:GivePoints(ply, -points) +end + +function PROVIDER:SaveItem( ply, item_id, data) + self:GiveItem(ply, item_id, data) +end + +function PROVIDER:GiveItem( ply, item_id, data) + self:GetData(ply, function(points, items) + local tmp = table.Copy(ply.PS_Items) + tmp[item_id] = data + self:SetData(ply, points, tmp) + end) +end + +function PROVIDER:TakeItem( ply, item_id ) + self:GetData(ply, function(points, items) + local tmp = table.Copy(ply.PS_Items) + tmp[item_id] = nil + self:SetData(ply, points, tmp) + end) +end + +function PROVIDER:SetData(ply, points, items) + if not file.IsDir('pointshop', 'DATA') then + file.CreateDir('pointshop') + end + + local filename = string.Replace(ply:SteamID(), ':', '_') + + file.Write('pointshop/' .. filename .. '.txt', util.TableToJSON({ + Points = points, + Items = items + })) +end \ No newline at end of file diff --git a/lua/pointshop/providers/pdata.lua b/lua/pointshop/providers/pdata.lua index 9fc1527..ae3ab67 100644 --- a/lua/pointshop/providers/pdata.lua +++ b/lua/pointshop/providers/pdata.lua @@ -2,6 +2,34 @@ function PROVIDER:GetData(ply, callback) return callback(ply:GetPData('PS_Points', 0), util.JSONToTable(ply:GetPData('PS_Items', '{}'))) end +function PROVIDER:SetPoints(ply, set_points) + ply:SetPData('PS_Points', set_points) +end + +function PROVIDER:GivePoints(ply, add_points) + ply:SetPData('PS_Points', ply:GetPData('PS_Points', 0) + add_points) +end + +function PROVIDER:TakePoints(ply, points) + self:GivePoints(ply, -points) +end + +function PROVIDER:SaveItem(ply, item_id, data) + self:GiveItem(ply, item_id, data) +end + +function PROVIDER:GiveItem(ply, item_id, data) + local tmp = table.Copy(ply.PS_Items) + tmp[item_id] = data + ply:SetPData('PS_Items', util.TableToJSON(tmp)) +end + +function PROVIDER:TakeItem(ply, item_id) + local tmp = util.JSONToTable(ply:GetPData('PS_Items', '{}')) + tmp[item_id] = nil + ply:SetPData('PS_Items', util.TableToJSON(tmp)) +end + function PROVIDER:SetData(ply, points, items) ply:SetPData('PS_Points', points) ply:SetPData('PS_Items', util.TableToJSON(items)) diff --git a/lua/pointshop/sv_init.lua b/lua/pointshop/sv_init.lua index 27ebefe..ddc77ab 100644 --- a/lua/pointshop/sv_init.lua +++ b/lua/pointshop/sv_init.lua @@ -279,3 +279,27 @@ end function PS:SetPlayerData(ply, points, items) self.DataProvider:SetData(ply, points, items) end + +function PS:SetPlayerPoints(ply, points) + self.DataProvider:SetPoints(ply, points) +end + +function PS:GivePlayerPoints(ply, points) + self.DataProvider:GivePoints(ply, points, items) +end + +function PS:TakePlayerPoints(ply, points) + self.DataProvider:TakePoints(ply, points) +end + +function PS:SavePlayerItem(ply, item_id, data) + self.DataProvider:SaveItem(ply, item_id, data) +end + +function PS:GivePlayerItem(ply, item_id, data) + self.DataProvider:GiveItem(ply, item_id, data) +end + +function PS:TakePlayerItem(ply, item_id) + self.DataProvider:TakeItem(ply, item_id) +end \ No newline at end of file diff --git a/lua/pointshop/sv_player_extension.lua b/lua/pointshop/sv_player_extension.lua index 773e4d6..066242e 100644 --- a/lua/pointshop/sv_player_extension.lua +++ b/lua/pointshop/sv_player_extension.lua @@ -84,7 +84,6 @@ function Player:PS_PlayerInitialSpawn() end function Player:PS_PlayerDisconnected() - self:PS_Save() PS.ClientsideModels[self] = nil if timer.Exists('PS_PointsOverTime_' .. self:UniqueID()) then @@ -93,9 +92,6 @@ function Player:PS_PlayerDisconnected() end function Player:PS_Save() - -- Make sure we don't save before we have loaded the data for the first time - if not self.PS_FirstLoadCompleted then return end - PS:SetPlayerData(self, self.PS_Points, self.PS_Items) end @@ -109,8 +105,6 @@ function Player:PS_LoadData() self:PS_SendPoints() self:PS_SendItems() - - self.PS_FirstLoadCompleted = true end) end @@ -134,16 +128,19 @@ end function Player:PS_GivePoints(points) self.PS_Points = self.PS_Points + points + PS:GivePlayerPoints(self, points) self:PS_SendPoints() end function Player:PS_TakePoints(points) self.PS_Points = self.PS_Points - points >= 0 and self.PS_Points - points or 0 + PS:TakePlayerPoints(self, points) self:PS_SendPoints() end function Player:PS_SetPoints(points) self.PS_Points = points + PS:SetPlayerPoints(self, points) self:PS_SendPoints() end @@ -161,6 +158,8 @@ function Player:PS_GiveItem(item_id) if not PS.Items[item_id] then return false end self.PS_Items[item_id] = { Modifiers = {}, Equipped = false } + + PS:GivePlayerItem(self, item_id, self.PS_Items[item_id]) self:PS_SendItems() @@ -172,6 +171,8 @@ function Player:PS_TakeItem(item_id) if not self:PS_HasItem(item_id) then return false end self.PS_Items[item_id] = nil + + PS:TakePlayerItem(self, item_id) self:PS_SendItems() @@ -368,6 +369,8 @@ function Player:PS_EquipItem(item_id) ITEM:OnEquip(self, self.PS_Items[item_id].Modifiers) self:PS_Notify('Equipped ', ITEM.Name, '.') + + PS:SavePlayerItem(self, item_id, self.PS_Items[item_id]) self:PS_SendItems() end @@ -395,6 +398,8 @@ function Player:PS_HolsterItem(item_id) ITEM:OnHolster(self) self:PS_Notify('Holstered ', ITEM.Name, '.') + + PS:SavePlayerItem(self, item_id, self.PS_Items[item_id]) self:PS_SendItems() end @@ -414,6 +419,8 @@ function Player:PS_ModifyItem(item_id, modifications) end ITEM:OnModify(self, self.PS_Items[item_id].Modifiers) + + PS:SavePlayerItem(self, item_id, self.PS_Items[item_id]) self:PS_SendItems() end @@ -457,8 +464,6 @@ end -- send stuff function Player:PS_SendPoints() - self:PS_Save() - net.Start('PS_Points') net.WriteEntity(self) net.WriteInt(self.PS_Points, 32) @@ -466,8 +471,6 @@ function Player:PS_SendPoints() end function Player:PS_SendItems() - self:PS_Save() - net.Start('PS_Items') net.WriteEntity(self) net.WriteTable(self.PS_Items)