From 3246642061bdfef57e43399296ad67723cdef47a Mon Sep 17 00:00:00 2001 From: William Gaylord Date: Sat, 2 Dec 2023 23:34:32 -0600 Subject: [PATCH 1/3] Implement Refunds at purchase price and current price --- lua/ps2/client/cl_pointshop2view.lua | 8 +++---- .../content/cl_pointshop2content.lua | 7 ++++-- lua/ps2/server/sv_pointshopcontroller.lua | 24 +++++++++++-------- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/lua/ps2/client/cl_pointshop2view.lua b/lua/ps2/client/cl_pointshop2view.lua index 82225eae..83745eb9 100644 --- a/lua/ps2/client/cl_pointshop2view.lua +++ b/lua/ps2/client/cl_pointshop2view.lua @@ -686,16 +686,16 @@ 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 = {} @@ -703,7 +703,7 @@ function Pointshop2View:removeItems( itemClasses, refund ) 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 diff --git a/lua/ps2/client/tabs/management_tab/manage_items/content/cl_pointshop2content.lua b/lua/ps2/client/tabs/management_tab/manage_items/content/cl_pointshop2content.lua index fed74687..070a6d63 100644 --- a/lua/ps2/client/tabs/management_tab/manage_items/content/cl_pointshop2content.lua +++ b/lua/ps2/client/tabs/management_tab/manage_items/content/cl_pointshop2content.lua @@ -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 pruchase 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, diff --git a/lua/ps2/server/sv_pointshopcontroller.lua b/lua/ps2/server/sv_pointshopcontroller.lua index db4fe2b4..499bf21f 100644 --- a/lua/ps2/server/sv_pointshopcontroller.lua +++ b/lua/ps2/server/sv_pointshopcontroller.lua @@ -765,7 +765,7 @@ 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, currentPrice ) local refundsByPlayer = LibK._( itemsToRefund ):chain() :groupBy( function( item ) return item.ownerId @@ -783,24 +783,28 @@ local function calculateRefundAmounts( itemsToRefund ) 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 ) - row.purchaseData = util.JSONToTable( row.data ).purchaseData + return row.purchaseData = util.JSONToTable( row.data ).purchaseData end ) local refundPromise = Promise.Resolve() if refund then - local refundsByPlayer = calculateRefundAmounts( refund ) + local currentPrice = false + if refundCurrentPrice then + currentPrice = table.Inherit(itemClass.static.Price, { points = 0, premiumPoints = 0 }) + end + local refundsByPlayer = calculateRefundAmounts( refund, currentPrice ) -- Remove players that get refunded 0 points local toRefund = LibK._( refundsByPlayer ):chain() :entries( ) @@ -812,7 +816,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, @@ -836,13 +840,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 ) @@ -851,7 +855,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 @@ -862,7 +866,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 ) From 169a1e9ad8e22f0d85b01192ffac5fec8b3be210 Mon Sep 17 00:00:00 2001 From: William Gaylord Date: Mon, 4 Dec 2023 08:47:24 -0600 Subject: [PATCH 2/3] Fix basic spelling mistake in refund message. Co-authored-by: Valentin --- .../manage_items/content/cl_pointshop2content.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/ps2/client/tabs/management_tab/manage_items/content/cl_pointshop2content.lua b/lua/ps2/client/tabs/management_tab/manage_items/content/cl_pointshop2content.lua index 070a6d63..68b67ccf 100644 --- a/lua/ps2/client/tabs/management_tab/manage_items/content/cl_pointshop2content.lua +++ b/lua/ps2/client/tabs/management_tab/manage_items/content/cl_pointshop2content.lua @@ -36,7 +36,7 @@ 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 at pruchase price", function( ) + "Yes and refund players at purchase price", function( ) Pointshop2View:getInstance( ):removeItem( itemClass, true ) end, "Yes and refund players at current price", function( ) From 76258c70328cd1a4a75752b32b0128cfb04b1ca8 Mon Sep 17 00:00:00 2001 From: William Gaylord Date: Fri, 22 Dec 2023 10:11:50 -0600 Subject: [PATCH 3/3] Fix incorrect version of functions Going to re-do my testing to make sure it all still works now that I have the fully up to date version of the functions I implemented. --- lua/ps2/server/sv_pointshopcontroller.lua | 28 ++++++++++++++--------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/lua/ps2/server/sv_pointshopcontroller.lua b/lua/ps2/server/sv_pointshopcontroller.lua index 499bf21f..3c0ed782 100644 --- a/lua/ps2/server/sv_pointshopcontroller.lua +++ b/lua/ps2/server/sv_pointshopcontroller.lua @@ -765,17 +765,22 @@ function Pointshop2Controller:sendPoints( ply, targetPly, points ) --TODO: Send the targetPlayer a nice notification, similar to iten added end -local function calculateRefundAmounts( itemsToRefund, currentPrice ) + +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( ) @@ -794,17 +799,18 @@ local function removeSingleItem( itemClass, refund, refundCurrentPrice ) :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 ) - return row.purchaseData = util.JSONToTable( row.data ).purchaseData + results = LibK._.map( results, function( row ) + row.purchaseData = util.JSONToTable( row.data ).purchaseData + return row end ) - + local refundPromise = Promise.Resolve() if refund then - local currentPrice = false + local refundAmmount = false if refundCurrentPrice then - currentPrice = table.Inherit(itemClass.static.Price, { points = 0, premiumPoints = 0 }) + refundAmmount = table.Inherit(itemClass.static.Price,{ points = 0, premiumPoints = 0 }) end - local refundsByPlayer = calculateRefundAmounts( refund, currentPrice ) + local refundsByPlayer = calculateRefundAmounts(results,refundAmmount) -- Remove players that get refunded 0 points local toRefund = LibK._( refundsByPlayer ):chain() :entries( )