68 lines
1.2 KiB
Go
68 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"math/rand"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
var winners []string = make([]string, 0, 20)
|
|
|
|
func main() {
|
|
http.HandleFunc("/winners", listWiners)
|
|
http.HandleFunc("/random", randomNumber)
|
|
http.Handle("/", http.FileServer(http.Dir("./static")))
|
|
http.ListenAndServe(":8080", nil)
|
|
}
|
|
func randomNumber(w http.ResponseWriter, r *http.Request) {
|
|
name := r.URL.Query().Get("name")
|
|
if len(name) > 15 {
|
|
w.WriteHeader(400)
|
|
return
|
|
}
|
|
|
|
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)
|
|
log.Println(name)
|
|
if name != "" && won {
|
|
winners = append(winners, name)
|
|
fmt.Println(winners)
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
}
|
|
func listWiners(w http.ResponseWriter, r *http.Request) {
|
|
out := ""
|
|
for _, winner := range winners {
|
|
out += winner + "\n"
|
|
}
|
|
fmt.Fprint(w, out)
|
|
}
|