Skip to content

Commit 3246642

Browse files
committed
Implement Refunds at purchase price and current price
1 parent c8a4b5b commit 3246642

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

lua/ps2/client/cl_pointshop2view.lua

+4-4
Original file line numberDiff line numberDiff line change
@@ -686,24 +686,24 @@ function Pointshop2View:fixDatabase( )
686686
self:controllerAction( "fixDatabase" )
687687
end
688688

689-
function Pointshop2View:removeItem( itemClass, refund )
689+
function Pointshop2View:removeItem( itemClass, refund, refundCurrentValue )
690690
hook.Run( "PS2_PreReload" )
691691

692-
self:controllerTransaction( "removeItem", itemClass.className, refund )
692+
self:controllerTransaction( "removeItem", itemClass.className, refund,refundCurrentValue)
693693
:Done( function( )
694694
KInventory.Items[itemClass.className] = nil
695695
end )
696696
end
697697

698-
function Pointshop2View:removeItems( itemClasses, refund )
698+
function Pointshop2View:removeItems( itemClasses, refund,refundCurrentValue )
699699
hook.Run( "PS2_PreReload" )
700700

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

706-
self:controllerTransaction( "removeItems", classNames, refund )
706+
self:controllerTransaction( "removeItems", classNames, refund,refundCurrentValue )
707707
:Done( function( removedNames )
708708
for _, className in pairs( removedNames ) do
709709
KInventory.Items[className] = nil

lua/ps2/client/tabs/management_tab/manage_items/content/cl_pointshop2content.lua

+5-2
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ local function addEditMenu( panel, itemClass )
3636

3737
local btn = menu:AddOption( "Delete", function( )
3838
Derma_Query( "Do you really want to permanently delete this item?", "Confirm",
39-
/*"Yes and refund players", function( )
39+
"Yes and refund players at pruchase price", function( )
4040
Pointshop2View:getInstance( ):removeItem( itemClass, true )
41-
end,*/
41+
end,
42+
"Yes and refund players at current price", function( )
43+
Pointshop2View:getInstance( ):removeItem( itemClass, true, true )
44+
end,
4245
"Yes", function( )
4346
Pointshop2View:getInstance( ):removeItem( itemClass )
4447
end,

lua/ps2/server/sv_pointshopcontroller.lua

+14-10
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ function Pointshop2Controller:sendPoints( ply, targetPly, points )
765765
--TODO: Send the targetPlayer a nice notification, similar to iten added
766766
end
767767

768-
local function calculateRefundAmounts( itemsToRefund )
768+
local function calculateRefundAmounts( itemsToRefund, currentPrice )
769769
local refundsByPlayer = LibK._( itemsToRefund ):chain()
770770
:groupBy( function( item )
771771
return item.ownerId
@@ -783,24 +783,28 @@ local function calculateRefundAmounts( itemsToRefund )
783783
return refundsByPlayer
784784
end
785785

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

801801
local refundPromise = Promise.Resolve()
802802
if refund then
803-
local refundsByPlayer = calculateRefundAmounts( refund )
803+
local currentPrice = false
804+
if refundCurrentPrice then
805+
currentPrice = table.Inherit(itemClass.static.Price, { points = 0, premiumPoints = 0 })
806+
end
807+
local refundsByPlayer = calculateRefundAmounts( refund, currentPrice )
804808
-- Remove players that get refunded 0 points
805809
local toRefund = LibK._( refundsByPlayer ):chain()
806810
:entries( )
@@ -812,7 +816,7 @@ local function removeSingleItem( itemClass, refund )
812816

813817
-- Create a query for each player
814818
refundPromise = Promise.Map( toRefund, function( entry )
815-
local ownerId, amountsToRefund = entry[0], entry[1]
819+
local ownerId, amountsToRefund = entry[1], entry[2]
816820
return Pointshop2.DB.DoQuery( Format(
817821
"UPDATE ps2_wallet SET points = points + %i, premiumPoints = premiumPoints + %i WHERE ownerId = %i",
818822
amountsToRefund.points,
@@ -836,13 +840,13 @@ local function removeSingleItem( itemClass, refund )
836840
end )
837841
end
838842

839-
function Pointshop2Controller:removeItem( ply, itemClassName, refund )
843+
function Pointshop2Controller:removeItem( ply, itemClassName, refund, refundCurrentPrice)
840844
local itemClass = Pointshop2.GetItemClassByName( itemClassName )
841845
if not itemClass then
842846
return Promise.Reject( "An item " .. itemClassName .. " doesn't exist!" )
843847
end
844848

845-
return removeSingleItem( itemClass, refund )
849+
return removeSingleItem( itemClass, refund, refundCurrentPrice )
846850
:Then( function( )
847851
return self:moduleItemsChanged( )
848852
end )
@@ -851,7 +855,7 @@ function Pointshop2Controller:removeItem( ply, itemClassName, refund )
851855
end )
852856
end
853857

854-
function Pointshop2Controller:removeItems( ply, itemClassNames, refund )
858+
function Pointshop2Controller:removeItems( ply, itemClassNames, refund, refundCurrentPrice )
855859
local itemClassses = LibK._.map( itemClassNames, function( itemClassName )
856860
local itemClass = Pointshop2.GetItemClassByName( itemClassName )
857861
if not itemClass then
@@ -862,7 +866,7 @@ function Pointshop2Controller:removeItems( ply, itemClassNames, refund )
862866

863867
return Promise.Map( itemClassses, function( itemClass )
864868
local itemClassName = itemClass.className
865-
return removeSingleItem( itemClass, refund ):Then( function( )
869+
return removeSingleItem( itemClass, refund, refundCurrentPrice ):Then( function( )
866870
return itemClassName
867871
end )
868872
end ):Then( function( removedClassNames )

0 commit comments

Comments
 (0)