Skip to content

Commit

Permalink
fix: improved trade button (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
shafouz authored Dec 21, 2023
1 parent 4016532 commit 258c8cf
Showing 1 changed file with 65 additions and 61 deletions.
126 changes: 65 additions & 61 deletions frontend/src/lib/skill_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,71 +384,82 @@ const tradeStatNames: { [key: number]: { [key: string]: string } } = {
}
};

// TODO Figure out what the actual limit is
const maxQueries = 28;
export const constructQuery = (jewel: number, conqueror: string, result: SearchWithSeed[], onlyConqueror = false) => {
let stats;
if (result.length * 4 < maxQueries) {
stats = [
{
type: 'count',
export const constructQuery = (jewel: number, conqueror: string, result: SearchWithSeed[]) => {
const max_filter_length = 50;
const max_filters = 4;
const max_query_length = max_filter_length * max_filters;
const final_query = [];
const stat = {
type: 'count',
value: { min: 1 },
filters: [],
disabled: false
};

// single seed case
if (result.length == 1) {
for (const conq of Object.keys(tradeStatNames[jewel])) {
stat.filters.push({
id: tradeStatNames[jewel][conq],
value: {
min: 1
min: result[0].seed,
max: result[0].seed
},
filters: Object.keys(tradeStatNames[jewel])
.filter((c) => (onlyConqueror ? c == conqueror : true))
.map((c) =>
result.map((r) => ({
id: tradeStatNames[jewel][c],
disabled: c != conqueror,
value: {
min: r.seed,
max: r.seed
}
}))
)
.flat(),
disabled: false
}
];
} else {
const seedGroups = Math.ceil(result.length / maxQueries);
stats = Object.keys(tradeStatNames[jewel])
.filter((c) => (onlyConqueror ? c == conqueror : true))
.map((c) => {
const groups = [];

for (let i = 0; i < seedGroups; i++) {
groups.push({
type: 'count',
value: {
min: 1
},
filters: result.slice(i * maxQueries, (i + 1) * maxQueries).map((r) => ({
id: tradeStatNames[jewel][c],
disabled: false,
value: {
min: r.seed,
max: r.seed
}
})),
disabled: c != conqueror || i > 0
});
disabled: conq != conqueror
});
}

final_query.push(stat);
// too many results case
} else if (result.length > max_query_length) {
for (let i = 0; i < max_filters; i++) {
final_query.push({
type: 'count',
value: { min: 1 },
filters: [],
disabled: i == 0 ? false : true
});
}

for (const [i, r] of result.slice(0, max_query_length).entries()) {
const index = Math.floor(i / max_filter_length);

final_query[index].filters.push({
id: tradeStatNames[jewel][conqueror],
value: {
min: r.seed,
max: r.seed
}
});
}
} else {
for (const conq of Object.keys(tradeStatNames[jewel])) {
stat.disabled = conq != conqueror;

for (const r of result) {
stat.filters.push({
id: tradeStatNames[jewel][conq],
value: {
min: r.seed,
max: r.seed
}
});
}

return groups;
})
.flat();
if (stat.filters.length > max_filter_length) {
stat.filters = stat.filters.slice(0, max_filter_length);
}

final_query.push(stat);
}
}

return {
query: {
status: {
option: 'online'
},
name: data.TimelessJewels[jewel],
type: 'Timeless Jewel',
stats
stats: final_query
},
sort: {
price: 'asc'
Expand All @@ -459,12 +470,5 @@ export const constructQuery = (jewel: number, conqueror: string, result: SearchW
export const openTrade = (jewel: number, conqueror: string, results: SearchWithSeed[]) => {
const url = new URL('https://www.pathofexile.com/trade/search/Affliction');
url.searchParams.set('q', JSON.stringify(constructQuery(jewel, conqueror, results)));

// POE Trade has a 32k limit
if (url.toString().length > 32000) {
console.warn('URL too long, generating only selected conqueror');
url.searchParams.set('q', JSON.stringify(constructQuery(jewel, conqueror, results, true)));
}

window.open(url, '_blank');
};

0 comments on commit 258c8cf

Please sign in to comment.