Skip to content

Commit

Permalink
1 file edited
Browse files Browse the repository at this point in the history
  • Loading branch information
Croc-Prog-github authored Jun 18, 2024
1 parent 1b46aa4 commit 27c4b59
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions core/Probability-Manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,44 @@ class ProbabilityManager {
}

const list = this.instances[instanceName][listName];
list.objects.push({ object, probability });
list.totalWeight += probability;

// Verifica che la somma delle probabilità non superi 100
if (list.totalWeight > 100) {
throw new Error(`The sum of the probabilities is greater than 100%. Instance: ${instanceName}, List: ${listName}`);
// Se l'oggetto è un range
if (typeof object === 'string' && object.includes('-')) {
const [start, end] = object.split('-').map(Number);
const rangeSize = end - start + 1;

if (probability === "auto_DirectProp") {
let totalWeight = 0;
for (let i = start; i <= end; i++) {
const prop = (i - start + 1) / rangeSize * 100;
totalWeight += prop;
list.objects.push({ object: i, probability: prop });
}
list.totalWeight += totalWeight;
} else if (probability === "auto_InversProp") {
let totalWeight = 0;
for (let i = start; i <= end; i++) {
const prop = (end - i + 1) / rangeSize * 100;
totalWeight += prop;
list.objects.push({ object: i, probability: prop });
}
list.totalWeight += totalWeight;
} else {
throw new Error(`Invalid probability type for range objects. Instance: ${instanceName}, List: ${listName}`);
}
} else {
// Se l'oggetto non è un range
if (typeof probability === 'number') {
list.objects.push({ object, probability });
list.totalWeight += probability;

// Verifica che la somma delle probabilità non superi 100
if (list.totalWeight > 100) {
throw new Error(`The sum of the probabilities is greater than 100%. Instance: ${instanceName}, List: ${listName}`);
}
} else {
throw new Error(`Invalid probability type for single object. Instance: ${instanceName}, List: ${listName}`);
}
}
}

Expand Down

0 comments on commit 27c4b59

Please sign in to comment.