Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Refunds at purchase price and current price #194

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lua/ps2/client/cl_pointshop2view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -686,24 +686,24 @@ function Pointshop2View:fixDatabase( )
self:controllerAction( "fixDatabase" )
end

function Pointshop2View:removeItem( itemClass, refund )
function Pointshop2View:removeItem( itemClass, refund, refundCurrentValue )
hook.Run( "PS2_PreReload" )

self:controllerTransaction( "removeItem", itemClass.className, refund )
self:controllerTransaction( "removeItem", itemClass.className, refund,refundCurrentValue)
:Done( function( )
KInventory.Items[itemClass.className] = nil
end )
end

function Pointshop2View:removeItems( itemClasses, refund )
function Pointshop2View:removeItems( itemClasses, refund,refundCurrentValue )
hook.Run( "PS2_PreReload" )

local classNames = {}
for _, v in pairs( itemClasses ) do
table.insert( classNames, v.className )
end

self:controllerTransaction( "removeItems", classNames, refund )
self:controllerTransaction( "removeItems", classNames, refund,refundCurrentValue )
:Done( function( removedNames )
for _, className in pairs( removedNames ) do
KInventory.Items[className] = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ local function addEditMenu( panel, itemClass )

local btn = menu:AddOption( "Delete", function( )
Derma_Query( "Do you really want to permanently delete this item?", "Confirm",
/*"Yes and refund players", function( )
"Yes and refund players at purchase price", function( )
Pointshop2View:getInstance( ):removeItem( itemClass, true )
end,*/
end,
"Yes and refund players at current price", function( )
Pointshop2View:getInstance( ):removeItem( itemClass, true, true )
end,
"Yes", function( )
Pointshop2View:getInstance( ):removeItem( itemClass )
end,
Expand Down
40 changes: 25 additions & 15 deletions lua/ps2/server/sv_pointshopcontroller.lua
Original file line number Diff line number Diff line change
Expand Up @@ -765,42 +765,52 @@ function Pointshop2Controller:sendPoints( ply, targetPly, points )
--TODO: Send the targetPlayer a nice notification, similar to iten added
end

local function calculateRefundAmounts( itemsToRefund )

local function calculateRefundAmounts( itemsToRefund ,refundAmounts)
local refundsByPlayer = LibK._( itemsToRefund ):chain()
:groupBy( function( item )
return item.ownerId
end )
:mapValues( function( groupedByPlayer )
return LibK._.reduce( groupedByPlayer, { points = 0, premiumPoints = 0 }, function( accumulated, item )
local purchaseData = item.purchaseData
if purchaseData and purchaseData.currency and purchaseData.amount then
accumulated[purchaseData.currency] = accumulated[purchaseData.currency] + purchaseData.amount
end
local purchaseData = item.purchaseData
if refundAmounts then
accumulated[purchaseData.currency] = accumulated[purchaseData.currency] + refundAmounts[purchaseData.currency]
else
if purchaseData and purchaseData.currency and purchaseData.amount then
accumulated[purchaseData.currency] = accumulated[purchaseData.currency] + purchaseData.amount
end
end
return accumulated
end )
end ):value( )

return refundsByPlayer
end

local function removeSingleItem( itemClass, refund )
local function removeSingleItem( itemClass, refund, refundCurrentPrice )
return Pointshop2.DB.DoQuery( Format( [[
SELECT item.id, item.data, COALESCE(inventories.ownerId, ps2_equipmentslot.ownerId) AS ownerId, ps2_equipmentslot.slotName
FROM kinv_items AS item
LEFT JOIN inventories ON inventories.id = item.inventory_id
LEFT JOIN ps2_equipmentslot ON ps2_equipmentslot.itemId = item.id
WHERE item.itemClass = %s
]], Pointshop2.DB.SQLStr( itemClass ) ) )
]], Pointshop2.DB.SQLStr( itemClass.name ) ) )
:Then( function( results )
results = results or {}
-- Deserialize purchaseData (this is usually done in LibK but we're querying manually)
results = LibK._.map( results, function( row )
results = LibK._.map( results, function( row )
row.purchaseData = util.JSONToTable( row.data ).purchaseData
return row
end )

local refundPromise = Promise.Resolve()
if refund then
local refundsByPlayer = calculateRefundAmounts( refund )
local refundAmmount = false
if refundCurrentPrice then
refundAmmount = table.Inherit(itemClass.static.Price,{ points = 0, premiumPoints = 0 })
end
local refundsByPlayer = calculateRefundAmounts(results,refundAmmount)
-- Remove players that get refunded 0 points
local toRefund = LibK._( refundsByPlayer ):chain()
:entries( )
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the filter below have amountsToRefund = entry[2]? I think it's { ownerId, amountsToRefund } so same mistake you fixed below

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, although it is working this way, so I don't think it needs to be changed.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I don't have a gmod server to test, would be good to verify this, if entry[1] is actually the ownerId the could would try to do e.g. 123.points != 0 which should error - not sure why it doesn't since you tested this

Expand All @@ -812,7 +822,7 @@ local function removeSingleItem( itemClass, refund )

-- Create a query for each player
refundPromise = Promise.Map( toRefund, function( entry )
local ownerId, amountsToRefund = entry[0], entry[1]
local ownerId, amountsToRefund = entry[1], entry[2]
return Pointshop2.DB.DoQuery( Format(
"UPDATE ps2_wallet SET points = points + %i, premiumPoints = premiumPoints + %i WHERE ownerId = %i",
amountsToRefund.points,
Expand All @@ -836,13 +846,13 @@ local function removeSingleItem( itemClass, refund )
end )
end

function Pointshop2Controller:removeItem( ply, itemClassName, refund )
function Pointshop2Controller:removeItem( ply, itemClassName, refund, refundCurrentPrice)
local itemClass = Pointshop2.GetItemClassByName( itemClassName )
if not itemClass then
return Promise.Reject( "An item " .. itemClassName .. " doesn't exist!" )
end

return removeSingleItem( itemClass, refund )
return removeSingleItem( itemClass, refund, refundCurrentPrice )
:Then( function( )
return self:moduleItemsChanged( )
end )
Expand All @@ -851,7 +861,7 @@ function Pointshop2Controller:removeItem( ply, itemClassName, refund )
end )
end

function Pointshop2Controller:removeItems( ply, itemClassNames, refund )
function Pointshop2Controller:removeItems( ply, itemClassNames, refund, refundCurrentPrice )
local itemClassses = LibK._.map( itemClassNames, function( itemClassName )
local itemClass = Pointshop2.GetItemClassByName( itemClassName )
if not itemClass then
Expand All @@ -862,7 +872,7 @@ function Pointshop2Controller:removeItems( ply, itemClassNames, refund )

return Promise.Map( itemClassses, function( itemClass )
local itemClassName = itemClass.className
return removeSingleItem( itemClass, refund ):Then( function( )
return removeSingleItem( itemClass, refund, refundCurrentPrice ):Then( function( )
return itemClassName
end )
end ):Then( function( removedClassNames )
Expand Down