mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-12 14:37:17 +03:00
Switch to new method that splits up existing groups instead of making new ones
Kind of #107
This commit is contained in:
@@ -271,74 +271,37 @@ async function chooseSegments(segments: DBSegment[], max: number): Promise<DBSeg
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function splitPercentOverlap(groups: OverlappingSegmentGroup[], percent: number): OverlappingSegmentGroup[] {
|
function splitPercentOverlap(groups: OverlappingSegmentGroup[]): OverlappingSegmentGroup[] {
|
||||||
|
return groups.flatMap((group) => {
|
||||||
const result: OverlappingSegmentGroup[] = [];
|
const result: OverlappingSegmentGroup[] = [];
|
||||||
for (const group of groups) {
|
group.segments.forEach((segment) => {
|
||||||
const segmentsLeftToCheck = [...group.segments];
|
const bestGroup = result.find((group) => {
|
||||||
while (segmentsLeftToCheck.length > 0) {
|
// At least one segment in the group must have high % overlap or the same action type
|
||||||
const currentGroup: OverlappingSegmentGroup = { segments: [], votes: 0, reputation: 0, locked: false, required: false };
|
return group.segments.some((compareSegment) => {
|
||||||
|
const overlap = Math.min(segment.endTime, compareSegment.endTime) - Math.max(segment.startTime, compareSegment.startTime);
|
||||||
|
const overallDuration = Math.max(segment.endTime, compareSegment.endTime) - Math.min(segment.startTime, compareSegment.startTime);
|
||||||
|
const overlapPercent = overlap / overallDuration;
|
||||||
|
return (segment.actionType === compareSegment.actionType && segment.actionType !== ActionType.Chapter)
|
||||||
|
|| overlapPercent >= 0.6
|
||||||
|
|| (overlapPercent >= 0.8 && segment.actionType === ActionType.Chapter && compareSegment.actionType === ActionType.Chapter);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
const currentSegment = segmentsLeftToCheck.shift();
|
if (bestGroup) {
|
||||||
// TODO: extract out this part to be more generic
|
bestGroup.segments.push(segment);
|
||||||
currentGroup.segments.push(currentSegment);
|
bestGroup.votes += segment.votes;
|
||||||
currentGroup.votes += currentSegment.votes;
|
bestGroup.reputation += segment.reputation;
|
||||||
currentGroup.reputation += currentSegment.reputation;
|
bestGroup.locked ||= segment.locked;
|
||||||
currentGroup.locked ||= currentSegment.locked;
|
bestGroup.required ||= segment.required;
|
||||||
currentGroup.required ||= currentSegment.required;
|
} else {
|
||||||
|
result.push({ segments: [segment], votes: segment.votes, reputation: segment.reputation, locked: segment.locked, required: segment.required });
|
||||||
const currentDuration = currentSegment.endTime - currentSegment.startTime;
|
|
||||||
for (const [index, compareSegment] of segmentsLeftToCheck.entries()) {
|
|
||||||
const compareDuration = compareSegment.endTime - compareSegment.startTime;
|
|
||||||
const overlap = Math.min(currentSegment.endTime, compareSegment.endTime) - Math.max(currentSegment.startTime, compareSegment.startTime);
|
|
||||||
const overlapPercent = overlap / Math.max(currentDuration, compareDuration);
|
|
||||||
if (overlapPercent >= percent) {
|
|
||||||
currentGroup.segments.push(currentSegment);
|
|
||||||
currentGroup.votes += compareSegment.votes;
|
|
||||||
currentGroup.reputation += compareSegment.reputation;
|
|
||||||
currentGroup.locked ||= compareSegment.locked;
|
|
||||||
currentGroup.required ||= compareSegment.required;
|
|
||||||
segmentsLeftToCheck.splice(index, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
result.push(currentGroup);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// function splitPercentOverlap(groups: OverlappingSegmentGroup[]): OverlappingSegmentGroup[] {
|
|
||||||
// return groups.flatMap((group) => {
|
|
||||||
// const result: OverlappingSegmentGroup[] = [];
|
|
||||||
// group.segments.forEach((segment) => {
|
|
||||||
// const bestGroup = result.find((group) => {
|
|
||||||
// // At least one segment in the group must have high % overlap or the same action type
|
|
||||||
// return group.segments.some((compareSegment) => {
|
|
||||||
// const overlap = Math.min(segment.endTime, compareSegment.endTime) - Math.max(segment.startTime, compareSegment.startTime);
|
|
||||||
// const overallDuration = Math.max(segment.endTime, compareSegment.endTime) - Math.min(segment.startTime, compareSegment.startTime);
|
|
||||||
// const overlapPercent = overlap / overallDuration;
|
|
||||||
// return (segment.actionType === compareSegment.actionType && segment.actionType !== ActionType.Chapter)
|
|
||||||
// || overlapPercent >= 0.6
|
|
||||||
// || (overlapPercent >= 0.8 && segment.actionType === ActionType.Chapter && compareSegment.actionType === ActionType.Chapter);
|
|
||||||
// });
|
|
||||||
// });
|
|
||||||
|
|
||||||
// if (bestGroup) {
|
|
||||||
// bestGroup.segments.push(segment);
|
|
||||||
// bestGroup.votes += segment.votes;
|
|
||||||
// bestGroup.reputation += segment.reputation;
|
|
||||||
// bestGroup.locked ||= segment.locked;
|
|
||||||
// bestGroup.required ||= segment.required;
|
|
||||||
// } else {
|
|
||||||
// result.push({ segments: [segment], votes: segment.votes, reputation: segment.reputation, locked: segment.locked, required: segment.required });
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
|
|
||||||
// return result;
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Returns what would be sent to the client.
|
* Returns what would be sent to the client.
|
||||||
|
|||||||
Reference in New Issue
Block a user