Skip to content

Commit

Permalink
Merge pull request #309 from the-hideout/fix-restock-error
Browse files Browse the repository at this point in the history
fix restock cmd error
  • Loading branch information
Razzmatazzz authored Sep 8, 2024
2 parents 4761061 + 4bb2827 commit aed398a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 16 deletions.
8 changes: 4 additions & 4 deletions commands/price.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const defaultFunction = {
let tierFee = 0;
let sellTo = t('Flea Market');
if (item.avg24hPrice > 0) {
tierFee = await progress.getFleaMarketFee(interaction.user.id, item.avg24hPrice, item.basePrice);
tierFee = await progress.getFleaMarketFee(interaction.user.id, item.avg24hPrice, item.basePrice, {gameMode});
//tierPrice -= avgFee;
let fleaPrice = parseInt(item.avg24hPrice).toLocaleString(locale) + "₽";

Expand All @@ -110,7 +110,7 @@ const defaultFunction = {
}

if (item.lastLowPrice > 0) {
const lowFee = await progress.getFleaMarketFee(interaction.user.id, item.lastLowPrice, item.basePrice);
const lowFee = await progress.getFleaMarketFee(interaction.user.id, item.lastLowPrice, item.basePrice, {gameMode});
let fleaPrice = parseInt(item.lastLowPrice).toLocaleString(locale) + "₽";

if (size > 1) {
Expand All @@ -130,8 +130,8 @@ const defaultFunction = {
}
}

const optimalPrice = await progress.getOptimalFleaPrice(interaction.user.id, item.basePrice);
const optimalFee = await progress.getFleaMarketFee(interaction.user.id, optimalPrice, item.basePrice);
const optimalPrice = await progress.getOptimalFleaPrice(interaction.user.id, item.basePrice, gameMode);
const optimalFee = await progress.getFleaMarketFee(interaction.user.id, optimalPrice, item.basePrice, {gameMode});
if (optimalPrice - optimalFee > tierPrice - tierFee && optimalPrice < tierPrice) {
tierPrice = optimalPrice;
tierFee = optimalFee;
Expand Down
19 changes: 18 additions & 1 deletion commands/restock.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,24 @@ const subCommands = {
if (traderId === 'all') {
traderId = traders.map(trader => trader.id);
} else {
forWho = traders.find(trader => trader.id === traderId).name;
const trader = traders.find(trader => trader.id === traderId);
if (!trader) {
let traderName = 'Unknown Trader';
for (const gm of gameData.gameModes.getAll()) {
const gmTrader = await gameData.traders.get(traderId, {lang, gameMode: gm});
if (!gmTrader) {
continue;
}
traderName = gmTrader.name;
break;
}
const errorEmbed = new EmbedBuilder();
errorEmbed.setDescription(`❌ ${t('Trader {{traderName}} does not exist in {{gameMode}}.', {traderName, gameMode: commandT(`game_mode_${gameMode}`)})}`);
return interaction.editReply({
embeds: [errorEmbed],
});
}
forWho = trader.name;
}

let alertsFor = [];
Expand Down
3 changes: 3 additions & 0 deletions modules/game-data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,9 @@ const gameDataExport = {
update: updateGoonReports,
},
gameModes: {
getAll: () => {
return gameModes;
},
choices: () => {
return gameModes.map(gameMode => {
return {
Expand Down
13 changes: 7 additions & 6 deletions modules/progress-shard.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ const calcFleaFee = async (progress, price, baseValue, args) => {
const options = {
count: 1,
requireAll: false,
gameMode: 'regular',
...args
};
if (typeof options.intel === 'undefined') {
const prog = await getFleaFactors(progress);
options.intel = prog.intel;
options.management = prog.management;
}
const flea = await gameData.flea.get();
const flea = await gameData.flea.get({gameMode: options.gameMode});
const q = options.requireAll ? 1 : options.count;
const vo = baseValue*(options.count/q);
const vr = price;
Expand All @@ -44,7 +45,7 @@ const calcFleaFee = async (progress, price, baseValue, args) => {
return Math.round(fee);
};

const optimalFleaPrice = async (progress, baseValue, lowerBound, upperBound) => {
const optimalFleaPrice = async (progress, baseValue, gameMode = 'regular', lowerBound, upperBound) => {
if (!lowerBound) lowerBound = baseValue*5;
if (!upperBound) upperBound = baseValue*25;
let step = Math.round((upperBound - lowerBound) / 50);
Expand All @@ -54,14 +55,14 @@ const optimalFleaPrice = async (progress, baseValue, lowerBound, upperBound) =>
let highFee = 0;
const args = await getFleaFactors(progress);
for (let price = lowerBound; price <= upperBound; price += step) {
const fee = await calcFleaFee(progress, price,baseValue, args);
const fee = await calcFleaFee(progress, price,baseValue, {...args, gameMode});
const profit = price - fee;
if (profit >= highProfit) {
highProfit = profit;
highPrice = price;
highFee = fee;
} else if (profit < highProfit) {
if (step != 1) return optimalFleaPrice(progress, baseValue, highPrice, price);
if (step != 1) return optimalFleaPrice(progress, baseValue, gameMode, highPrice, price);
break;
}
}
Expand Down Expand Up @@ -100,9 +101,9 @@ const progressShard = {
const progress = await progressShard.getProgressOrDefault(id);
return calcFleaFee(progress, price, baseValue, args);
},
async getOptimalFleaPrice(id, baseValue) {
async getOptimalFleaPrice(id, baseValue, gameMode = 'regular') {
const progress = await progressShard.getProgressOrDefault(id);
return optimalFleaPrice(progress, baseValue);
return optimalFleaPrice(progress, baseValue, gameMode);
},
async getRestockAlerts(id, gameMode) {
return getParentReply({data: 'userTraderRestockAlerts', userId: id});
Expand Down
11 changes: 6 additions & 5 deletions modules/progress.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ const calcFleaFee = async (id, price, baseValue, args) => {
const options = {
count: 1,
requireAll: false,
gameMode: 'regular',
...args
};
if (typeof options.intel === 'undefined') {
Expand All @@ -216,7 +217,7 @@ const calcFleaFee = async (id, price, baseValue, args) => {
return Math.round(fee);
};

const optimalFleaPrice = async (id, baseValue, lowerBound, upperBound) => {
const optimalFleaPrice = async (id, baseValue, gameMode = 'regular', lowerBound, upperBound) => {
if (!lowerBound) lowerBound = baseValue*5;
if (!upperBound) upperBound = baseValue*25;
let step = Math.round((upperBound - lowerBound) / 50);
Expand All @@ -226,14 +227,14 @@ const optimalFleaPrice = async (id, baseValue, lowerBound, upperBound) => {
let highFee = 0;
const args = getFleaFactors(id);
for (let price = lowerBound; price <= upperBound; price += step) {
const fee = await calcFleaFee(id, price,baseValue, args);
const fee = await calcFleaFee(id, price,baseValue, {...args, gameMode});
const profit = price - fee;
if (profit >= highProfit) {
highProfit = profit;
highPrice = price;
highFee = fee;
} else if (profit < highProfit) {
if (step != 1) return optimalFleaPrice(id, baseValue, highPrice, price);
if (step != 1) return optimalFleaPrice(id, baseValue, gameMode, highPrice, price);
break;
}
}
Expand Down Expand Up @@ -492,8 +493,8 @@ const settings = {
async getFleaMarketFee(id, price, baseValue, args) {
return calcFleaFee(id, price, baseValue, args);
},
getOptimalFleaPrice(id, baseValue) {
return optimalFleaPrice(id, baseValue);
getOptimalFleaPrice(id, baseValue, gameMode = 'regular') {
return optimalFleaPrice(id, baseValue, gameMode);
},
async getRestockAlerts(id) {
const prog = settings.getProgressOrDefault(id);
Expand Down
1 change: 1 addition & 0 deletions translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
"Time": "Time",
"Too many notes to display.": "Too many notes to display.",
"Total": "Total",
"Trader {{traderName}} does not exist in {{gameMode}}.": "Trader {{traderName}} does not exist in {{gameMode}}.",
"Trader restocks": "Trader restocks",
"Traders": "Traders",
"Updated {{updateTimeAgo}}": "Updated {{updateTimeAgo}}",
Expand Down

0 comments on commit aed398a

Please sign in to comment.