추세검색 설정 추가

This commit is contained in:
Macbook
2026-05-27 01:36:06 +09:00
parent c1bcf88c6c
commit 2e08c6b16f
42 changed files with 1507 additions and 226 deletions
+41 -24
View File
@@ -55,6 +55,18 @@ export function bullishWeightsFromRequest(
return BULLISH_WEIGHT_ITEMS.map(item => req[item.key] ?? DEFAULT_BULLISH_WEIGHTS[item.key]);
}
export const WEIGHT_STEP = 5;
export function snapToWeightStep(value: number): number {
return Math.max(0, Math.min(100, Math.round(value / WEIGHT_STEP) * WEIGHT_STEP));
}
/** range 슬라이더 트랙 클릭 위치 → 5점 단위 배점 */
export function weightFromSliderPointer(clientX: number, rect: DOMRect): number {
const ratio = Math.max(0, Math.min(1, (clientX - rect.left) / rect.width));
return snapToWeightStep(ratio * 100);
}
export function sumWeights(weights: number[]): number {
return weights.reduce((a, b) => a + b, 0);
}
@@ -71,43 +83,48 @@ export function adjustBullishWeight(
const idx = keys.indexOf(changedKey);
if (idx < 0) return current;
const clamped = Math.max(0, Math.min(100, Math.round(newValue)));
const oldValue = current[changedKey];
const clamped = snapToWeightStep(newValue);
const oldValue = snapToWeightStep(current[changedKey]);
if (clamped === oldValue) return current;
const result: Record<BullishWeightKey, number> = { ...current };
for (const k of keys) result[k] = snapToWeightStep(result[k]);
result[changedKey] = clamped;
const delta = clamped - oldValue;
const otherKeys = keys.filter(k => k !== changedKey);
if (delta > 0) {
let remaining = delta;
for (let i = 0; i < otherKeys.length; i++) {
const k = otherKeys[i];
const take = i === otherKeys.length - 1
? remaining
: Math.floor(delta / otherKeys.length);
result[k] = Math.max(0, result[k] - take);
remaining -= take;
const distribute = (amount: number, mode: 'take' | 'give') => {
let remaining = amount;
let idx = 0;
let guard = 0;
while (remaining > 0 && guard < 200) {
const k = otherKeys[idx % otherKeys.length];
if (mode === 'take') {
const take = Math.min(WEIGHT_STEP, remaining, result[k]);
if (take > 0) {
result[k] -= take;
remaining -= take;
}
} else {
const give = Math.min(WEIGHT_STEP, remaining, 100 - result[k]);
if (give > 0) {
result[k] += give;
remaining -= give;
}
}
idx += 1;
guard += 1;
}
} else {
const add = Math.abs(delta);
let remaining = add;
for (let i = 0; i < otherKeys.length; i++) {
const k = otherKeys[i];
const give = i === otherKeys.length - 1
? remaining
: Math.floor(add / otherKeys.length);
result[k] += give;
remaining -= give;
}
}
};
if (delta > 0) distribute(delta, 'take');
else if (delta < 0) distribute(-delta, 'give');
const total = keys.reduce((s, k) => s + result[k], 0);
if (total !== 100) {
const fixKey = otherKeys[0] ?? changedKey;
result[fixKey] = Math.max(0, result[fixKey] + (100 - total));
result[fixKey] = snapToWeightStep(Math.max(0, Math.min(100, result[fixKey] + (100 - total))));
}
return result;