From 94e441969afd5e53ec03b7e0567cd92eb3010922 Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Fri, 16 Aug 2024 15:04:17 +0200 Subject: [PATCH] Add Dice Library --- structure-and-scale/dice-library/index.html | 15 +++++ structure-and-scale/dice-library/script.js | 66 +++++++++++++++++++++ structure-and-scale/main.go | 20 +++++++ structure-and-scale/watch.bash | 22 +++++++ 4 files changed, 123 insertions(+) create mode 100644 structure-and-scale/dice-library/index.html create mode 100644 structure-and-scale/dice-library/script.js create mode 100644 structure-and-scale/main.go create mode 100755 structure-and-scale/watch.bash diff --git a/structure-and-scale/dice-library/index.html b/structure-and-scale/dice-library/index.html new file mode 100644 index 0000000..1c31be2 --- /dev/null +++ b/structure-and-scale/dice-library/index.html @@ -0,0 +1,15 @@ + + + + + + + Dice Library + + +

Dice Library

+

All of the magic here happens in the console.

+ + + + diff --git a/structure-and-scale/dice-library/script.js b/structure-and-scale/dice-library/script.js new file mode 100644 index 0000000..e1b43fb --- /dev/null +++ b/structure-and-scale/dice-library/script.js @@ -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}; +})(); diff --git a/structure-and-scale/main.go b/structure-and-scale/main.go new file mode 100644 index 0000000..f558efb --- /dev/null +++ b/structure-and-scale/main.go @@ -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) + } + +} diff --git a/structure-and-scale/watch.bash b/structure-and-scale/watch.bash new file mode 100755 index 0000000..2276744 --- /dev/null +++ b/structure-and-scale/watch.bash @@ -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' ./) +