Webchance/main.go
Gian Breitenstein 073bf775db init
2024-09-26 16:43:51 +02:00

48 lines
917 B
Go

package main
import (
"fmt"
"math/rand"
"net/http"
"strconv"
)
var winners []string = make([]string, 0, 20)
func main() {
http.HandleFunc("/random", randomNumber)
http.Handle("/", http.FileServer(http.Dir("./static")))
http.ListenAndServe(":8080", nil)
}
func randomNumber(w http.ResponseWriter, r *http.Request) {
chance, err := strconv.Atoi(r.URL.Query().Get("chance"))
if err != nil {
w.WriteHeader(400)
return
}
multiplicator, err := strconv.Atoi(r.URL.Query().Get("multiplicator"))
if err != nil {
w.WriteHeader(400)
return
}
won := randomize(chance, multiplicator)
winners = append(winners, "mi name")
fmt.Fprint(w, won)
}
func randomize(chance int, mult int) bool {
r := rand.Intn(chance * mult)
if r == 0 {
if chance == 10 && mult == 1 {
return false
} else {
return true
}
} else {
if chance == 10 && mult == 1 {
return true
} else {
return false
}
}
}