Skip to content

Commit

Permalink
Modified save behavior and providers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Stevens committed Aug 3, 2014
1 parent e93d264 commit d9481fc
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
75 changes: 75 additions & 0 deletions lua/pointshop/providers/json.lua
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions lua/pointshop/providers/pdata.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
24 changes: 24 additions & 0 deletions lua/pointshop/sv_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 13 additions & 10 deletions lua/pointshop/sv_player_extension.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -109,8 +105,6 @@ function Player:PS_LoadData()

self:PS_SendPoints()
self:PS_SendItems()

self.PS_FirstLoadCompleted = true
end)
end

Expand All @@ -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

Expand All @@ -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()

Expand All @@ -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()

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -457,17 +464,13 @@ end
-- send stuff

function Player:PS_SendPoints()
self:PS_Save()

net.Start('PS_Points')
net.WriteEntity(self)
net.WriteInt(self.PS_Points, 32)
net.Broadcast()
end

function Player:PS_SendItems()
self:PS_Save()

net.Start('PS_Items')
net.WriteEntity(self)
net.WriteTable(self.PS_Items)
Expand Down

0 comments on commit d9481fc

Please sign in to comment.