Add Dice Library

This commit is contained in:
Andrea Fazzi 2024-08-16 15:04:17 +02:00
parent cd8e7c518b
commit 94e441969a
4 changed files with 123 additions and 0 deletions

View file

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en" data-bs-theme="dark">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
<title>Dice Library</title>
</head>
<body>
<h1>Dice Library</h1>
<p>All of the magic here happens in the console.</p>
<script src="script.js"></script>
</body>
</html>

View file

@ -0,0 +1,66 @@
let roll = (function () {
/**
* Randomly shuffle an array
* https://stackoverflow.com/a/2450976/1293256
* @param {Array} array The array to shuffle
* @return {Array} The shuffled array
*/
function shuffle (array) {
let currentIndex = array.length;
let temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function createDice ( numSides ) {
let array = [];
for (let n = 0; n < numSides; n++) {
array.push(n+1);
}
return array;
}
function d2() {
return shuffle(createDice(2))[0];
}
function d4() {
return shuffle(createDice(4))[0];
}
function d6() {
return shuffle(createDice(6))[0];
}
function d8() {
return shuffle(createDice(8))[0];
}
function d10() {
return shuffle(createDice(10))[0];
}
function d12() {
return shuffle(createDice(12))[0];
}
function d20() {
return shuffle(createDice(20))[0];
}
return {d2, d4, d6, d8, d10, d12, d20};
})();

View file

@ -0,0 +1,20 @@
package main
import (
"net/http"
"log"
)
func main() {
mux := http.NewServeMux()
mux.Handle("GET /dice-library/", http.StripPrefix("/dice-library", http.FileServer(http.Dir("dice-library"))))
log.Println("Start the web server...")
err := http.ListenAndServe(":8080", mux)
if err != nil {
panic(err)
}
}

22
structure-and-scale/watch.bash Executable file
View file

@ -0,0 +1,22 @@
#!/bin/bash
run_go() {
pkill -f "go run" || true
cd "$1" && go run . &
}
go run . &
while read -r filename; do
case "$filename" in
*.go)
echo "File .go modificato: $filename"
run_go "$directory"
;;
*.html|*.css|*.js)
echo "File HTML/CSS/JS modificato: $filename"
;;
esac
done < <(inotifywait -r -m -e modify --format '%w%f' ./)