如何修改Steam列表价格匹配代码使metal值加1?
问题解决:Steam物品价格匹配中Metal值加1不生效的修复
问题说明
现有一段处理Steam物品列表、生成价格匹配按钮的代码,需要将价格中的metal值设置为原数值+1,但直接在metal字段后添加+1未生效。
问题分析
直接在模板字符串的${price.metal}后加+1无效,是因为模板字符串中需通过${price.metal+1}触发计算,或者在解析价格阶段就完成加1操作。原代码中parseListingPrice函数返回的metal是原始解析值,未做调整。
修复方案
以下提供两种可行的修改方式:
方式一:解析价格时直接给Metal加1
修改parseListingPrice函数,在返回metal值时直接加1:
// Parse text into a price object const parseListingPrice = function(price){ return { keys: parseFloat(keyEx.exec(price) ? keyEx.exec(price).shift() : 0), metal: parseFloat(refEx.exec(price) ? refEx.exec(price).shift() : 0) + 1 // 此处添加+1 }; }
方式二:生成URL参数时计算加1
若不想修改价格解析逻辑,可在生成匹配按钮的URL参数时直接计算:
const match = `<a data-postfix="/item/${encodeURIComponent((info.prop('title') || info.data('original-title')).trim())}?keys=${price.keys}&metal=${price.metal + 1}&intent=${info.data('listing_intent')}" title="Match this user's price" target="_blank" class="btn btn-bottom btn-xs btn-success gladiator-context"> <i class="fa fa-sw fa-tags"></i> </a>`;
完整修改后的代码(以方式一为例)
// Match Buttons function addMatchButtons(){ const keyEx = /(\d*(?= keys?))/; const refEx = /\d*(.\d*)?(?= ref)/; // Parse text into a price object const parseListingPrice = function(price){ return { keys: parseFloat(keyEx.exec(price) ? keyEx.exec(price).shift() : 0), metal: parseFloat(refEx.exec(price) ? refEx.exec(price).shift() : 0) + 1 }; } // Do not match these const hasBlacklistedProperties = function(info){ if( info.data('paint_name') !== undefined || info.data('spell_1') !== undefined || info.data('part_price_1') !== undefined || info.data('killstreaker') !== undefined || info.data('sheen') !== undefined ){ return true; } return false; } // Spawns the actual button const spawnMatchButton = function (){ let element = $(this); const info = element.find('.item'); const price = parseListingPrice(info.data('listing_price') || ""); const match = `<a data-postfix="/item/${encodeURIComponent((info.prop('title') || info.data('original-title')).trim())}?keys=${price.keys}&metal=${price.metal}&intent=${info.data('listing_intent')}" title="Match this user's price" target="_blank" class="btn btn-bottom btn-xs btn-success gladiator-context"> <i class="fa fa-sw fa-tags"></i> </a>`; if(!hasBlacklistedProperties(info) || info.data('listing_intent') === "sell" ) element.find(".listing-buttons").prepend(match); }; let sellers = $($(".media-list")[0]); let buyers = $($(".media-list")[1]); sellers .find(".listing").each(spawnMatchButton); buyers .find(".listing").each(spawnMatchButton); }
注:原代码中存在几处语法错误(如
info.data('listing_price') ""应改为info.data('listing_price') || ""),已在完整代码中一并修复。
内容的提问来源于stack exchange,提问作者thour105
相关产品推荐
相关产品推荐

