-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f7968fc
commit d20a3c3
Showing
1 changed file
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Probability Manager.js</title> | ||
<script src="/core/Probability-Manager.js"></script> | ||
</head> | ||
<body style="display: flex; flex-direction: column; background: #2b2b2b;"> | ||
<textarea name="Editor" id="Editor" oninput="Run()" rows="17" autocomplete="off" style="resize: vertical;" spellcheck="false"> | ||
const probManager = new ProbabilityManager(); | ||
|
||
// Creazione di istanze e liste | ||
probManager.addList('Istanza1', '1'); | ||
probManager.addList('Istanza1', '2'); | ||
|
||
// Aggiunta di oggetti con probabilità | ||
probManager.addObject('Istanza1', '1', 'Moneta d\'oro', 50); | ||
probManager.addObject('Istanza1', '1', 'Moneta d\'argento', 50); | ||
probManager.addObject('Istanza1', '2', 'Moneta di bronzo', 100); | ||
|
||
// Estrazione di un oggetto casuale | ||
let randomEvent = probManager.getRandomObject('Istanza1', '1'); | ||
console.log(`Oggetto estratto: `+ randomEvent); | ||
|
||
randomEvent = probManager.getRandomObject('Istanza1', '2'); | ||
console.log(`Oggetto estratto: `+ randomEvent); | ||
|
||
// Pulizia delle istanze | ||
probManager.clearInstance('Istanza1');</textarea> | ||
<textarea name="Output" id="Out" rows="7" disabled style="resize: none;">Edit the example for run it: add an Enter or a Space.</textarea> | ||
<button hidden style="width: fit-content; background: palegreen; border: solid 3px; border-color: forestgreen; font-weight: bold; font-family: sans-serif;">Run</button> | ||
</body> | ||
</html> | ||
|
||
<script> | ||
// Correggere il fetch e ottenere il contenuto del file senza commenti sourceMappingURL | ||
/* | ||
fetch('/core/example.js') | ||
.then(response => response.text()) | ||
.then(scriptContent => { | ||
// Rimuove le righe che iniziano con //# sourceMappingURL= | ||
scriptContent = scriptContent.replace(/\/\/# sourceMappingURL=.*$/gm, ''); | ||
document.getElementById('Editor').textContent = scriptContent; | ||
}) | ||
.catch(error => { | ||
console.error('Error fetching example script:', error); | ||
});*/ | ||
|
||
function AlternativeForStatic() { | ||
document.getElementById('Editor').innerHTML = | ||
`const probManager = new ProbabilityManager(); | ||
// Creazione di istanze e liste | ||
probManager.addList('Istanza1', '1'); | ||
probManager.addList('Istanza1', '2'); | ||
// Aggiunta di oggetti con probabilità | ||
probManager.addObject('Istanza1', '1', 'Moneta d\'oro', 50); | ||
probManager.addObject('Istanza1', '1', 'Moneta d\'argento', 50); | ||
probManager.addObject('Istanza1', '2', 'Moneta di bronzo', 100); | ||
// Estrazione di un oggetto casuale | ||
let randomEvent = probManager.getRandomObject('Istanza1', '1'); | ||
console.log('Oggetto estratto: '+ randomEvent); | ||
randomEvent = probManager.getRandomObject('Istanza1', '2'); | ||
console.log('Oggetto estratto: '+ randomEvent); | ||
// Pulizia delle istanze | ||
probManager.clearInstance('Istanza1');` | ||
} | ||
|
||
|
||
// Reindirizzare l'output console | ||
(function() { | ||
var oldLog = console.log; | ||
console.log = function(message) { | ||
document.getElementById('Out').value += message + '\n'; | ||
oldLog.apply(console, arguments); | ||
}; | ||
})(); | ||
|
||
function Run() { | ||
const editorContent = document.getElementById('Editor').value; | ||
try { | ||
document.getElementById('Out').value = ''; // Pulisce l'output | ||
eval(editorContent); | ||
} catch (error) { | ||
document.getElementById('Out').value = 'Error: ' + error.message; | ||
} | ||
} | ||
</script> | ||
|
||
<script> | ||
class ProbabilityManager { | ||
constructor() { | ||
this.instances = {}; | ||
} | ||
|
||
addList(instanceName, listName) { | ||
if (!this.instances[instanceName]) { | ||
this.instances[instanceName] = {}; | ||
} | ||
if (!this.instances[instanceName][listName]) { | ||
this.instances[instanceName][listName] = { objects: [], totalWeight: 0 }; | ||
} | ||
} | ||
|
||
addObject(instanceName, listName, object, probability) { | ||
if (!this.instances[instanceName] || !this.instances[instanceName][listName]) { | ||
throw new Error("Lista o istanza non esistente"); | ||
} | ||
|
||
const list = this.instances[instanceName][listName]; | ||
list.objects.push({ object, probability }); | ||
list.totalWeight += probability; | ||
|
||
// Verifica che la somma delle probabilità sia 100 | ||
if (list.totalWeight > 100) { | ||
throw new Error("La somma delle probabilità supera il 100%"); | ||
} | ||
} | ||
|
||
getRandomObject(instanceName, listName) { | ||
if (!this.instances[instanceName] || !this.instances[instanceName][listName]) { | ||
throw new Error("Lista o istanza non esistente"); | ||
} | ||
|
||
const list = this.instances[instanceName][listName]; | ||
let random = Math.random() * list.totalWeight; | ||
|
||
for (let { object, probability } of list.objects) { | ||
if (random < probability) { | ||
return object; | ||
} | ||
random -= probability; | ||
} | ||
} | ||
|
||
clearInstance(instanceName) { | ||
if (this.instances[instanceName]) { | ||
delete this.instances[instanceName]; | ||
} | ||
} | ||
|
||
clearAll() { | ||
this.instances = {}; | ||
} | ||
} | ||
</script> |