calcpayout auto calculate total from item market values
This commit is contained in:
@@ -7,8 +7,8 @@ module.exports = {
|
|||||||
.setDescription('Calculate war payout based on participation')
|
.setDescription('Calculate war payout based on participation')
|
||||||
.addIntegerOption(option =>
|
.addIntegerOption(option =>
|
||||||
option.setName('total')
|
option.setName('total')
|
||||||
.setDescription('Full war earnings total before cuts')
|
.setDescription('Full war earnings total before cuts (Optional)')
|
||||||
.setRequired(true))
|
.setRequired(false))
|
||||||
.addIntegerOption(option =>
|
.addIntegerOption(option =>
|
||||||
option.setName('percentage')
|
option.setName('percentage')
|
||||||
.setDescription('Percentage of leader cut (default 10)'))
|
.setDescription('Percentage of leader cut (default 10)'))
|
||||||
@@ -21,14 +21,10 @@ module.exports = {
|
|||||||
{ name: 'Attack Based', value: 'attacks' },
|
{ name: 'Attack Based', value: 'attacks' },
|
||||||
)),
|
)),
|
||||||
async execute(interaction) {
|
async execute(interaction) {
|
||||||
const total = interaction.options.getInteger('total');
|
let total = interaction.options.getInteger('total');
|
||||||
const percentage = interaction.options.getInteger('percentage') ?? 10;
|
const percentage = interaction.options.getInteger('percentage') ?? 10;
|
||||||
const method = interaction.options.getString('method') ?? 'flat';
|
const method = interaction.options.getString('method') ?? 'flat';
|
||||||
|
|
||||||
// Calculate cuts
|
|
||||||
const leaderCut = Math.ceil(total * (percentage / 100));
|
|
||||||
const pool = total - leaderCut;
|
|
||||||
|
|
||||||
await interaction.deferReply();
|
await interaction.deferReply();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -44,6 +40,39 @@ module.exports = {
|
|||||||
return interaction.editReply('Could not find our faction in the last war report.');
|
return interaction.editReply('Could not find our faction in the last war report.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Auto-calculate total if not provided
|
||||||
|
if (!total) {
|
||||||
|
let calculatedTotal = 0;
|
||||||
|
const rewards = ourFaction.rewards;
|
||||||
|
|
||||||
|
if (rewards && rewards.items) {
|
||||||
|
const itemIds = Array.isArray(rewards.items)
|
||||||
|
? rewards.items.map(i => i.id || i.ID)
|
||||||
|
: Object.keys(rewards.items);
|
||||||
|
|
||||||
|
for (const itemId of itemIds) {
|
||||||
|
const qt = Array.isArray(rewards.items)
|
||||||
|
? rewards.items.find(i => (i.id == itemId || i.ID == itemId)).quantity
|
||||||
|
: rewards.items[itemId];
|
||||||
|
|
||||||
|
const itemData = await torn.item(itemId);
|
||||||
|
if (itemData && itemData.value && itemData.value.market_price) {
|
||||||
|
calculatedTotal += itemData.value.market_price * qt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (calculatedTotal > 0) {
|
||||||
|
total = calculatedTotal;
|
||||||
|
} else {
|
||||||
|
return interaction.editReply('No total provided and could not calculate rewards from the war report.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate cuts
|
||||||
|
const leaderCut = Math.ceil(total * (percentage / 100));
|
||||||
|
const pool = total - leaderCut;
|
||||||
|
|
||||||
const members = ourFaction.members;
|
const members = ourFaction.members;
|
||||||
const participants = [];
|
const participants = [];
|
||||||
const nonParticipants = [];
|
const nonParticipants = [];
|
||||||
@@ -77,7 +106,7 @@ module.exports = {
|
|||||||
|
|
||||||
|
|
||||||
let message = `# War Payout: ${ourFaction.name} vs ${enemyFaction.name}\n`;
|
let message = `# War Payout: ${ourFaction.name} vs ${enemyFaction.name}\n`;
|
||||||
message += `**Total Earnings:** $${total.toLocaleString()}\n`;
|
message += `**Total Earnings:** $${total.toLocaleString()}${!interaction.options.getInteger('total') ? ' (Auto-Calculated)' : ''}\n`;
|
||||||
message += `**Leader Cut (${percentage}%):** $${leaderCut.toLocaleString()} (Yameii)\n`;
|
message += `**Leader Cut (${percentage}%):** $${leaderCut.toLocaleString()} (Yameii)\n`;
|
||||||
message += `**Distributable Pool:** $${pool.toLocaleString()}\n`;
|
message += `**Distributable Pool:** $${pool.toLocaleString()}\n`;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user