goulette/backend/api.go
2024-08-29 23:33:04 -07:00

159 lines
3.9 KiB
Go

package main
import (
"context"
"crypto/rand"
"encoding/json"
"fmt"
"log"
"math"
"math/big"
"net/http"
"strconv"
"github.com/google/uuid"
"github.com/redis/go-redis/v9"
)
var ctx = context.Background()
// Message struct represents the data structure
type Message struct {
ID string `json:"id"`
Content string `json:"content"`
}
// Initialize Redis client
var rdb *redis.Client
func initRedis() {
rdb = redis.NewClient(&redis.Options{
Addr: "localhost:6379", // Redis server address
Password: "", // No password set
DB: 0, // Use default DB
})
}
// generate f--king large number
func indexGenerator() (int64, error) {
val, err := rand.Int(rand.Reader, big.NewInt(int64(math.MaxInt64)))
if err != nil {
return 0, err
}
return val.Int64() % 38, nil
}
func getSpin(w http.ResponseWriter, r *http.Request) {
w.Header().Set(
"Content-Type",
"application/json",
)
fmt.Println("roulette number")
n := 37
array := make([]string, n)
for i := range array {
x := strconv.Itoa(i)
array[i] = x
}
array = append(array, "00")
randomIndex, _ := indexGenerator()
json.NewEncoder(w).Encode(array[randomIndex])
}
// get username & password; generate UUID
func newUser(w http.ResponseWriter, r *http.Request) {
id := uuid.New()
username := r.URL.Query().Get("username")
password := r.URL.Query().Get("password")
if username == "" {
http.Error(w, "USERNAME is required", http.StatusBadRequest)
return
}
user_slot, _ := rdb.Incr(ctx, "user_slot").Result()
v, _ := rdb.ZAdd(ctx, "user_name", redis.Z{float64(user_slot), username}).Result()
if v == 1 {
rdb.ZAdd(ctx, "user_uuid", redis.Z{float64(user_slot), id}).Result()
}
c := fmt.Sprint(username, " has password ", password)
json.NewEncoder(w).Encode(Message{ID: id.String(), Content: c})
}
// this user, this amount, this type of bet, this roulette number
func takeBet(w http.ResponseWriter, r *http.Request) {
actions := map[string]string {
"0": "0",
"00": "00",
"STREET": "STREET",
"ROW": "ROW",
"BASKET_US": "BASKET_US",
"SPLIT": "SPLIT",
"CORNER": "CORNER",
"DOUBLE_STREET": "DOUBLE_STREET",
"STRAIGHT_UP": "STRAIGHT_UP",
"1ST_COLUMN": "1ST_COLUMN",
"2ND_COLUMN": "2ND_COLUMN",
"3RD_COLUMN": "3RD_COLUMN",
"1ST_DOZEN": "1ST_DOZEN",
"2ND_DOZEN": "2ND_DOZEN",
"3RD_DOZEN": "3RD_DOZEN",
"1_TO_18": "1_TO_18",
"19_TO_36": "19_TO_36",
"EVEN": "EVEN",
"ODD": "ODD",
"RED": "RED",
"BLACK": "BLACK",
}
username := r.URL.Query().Get("username")
bet := r.URL.Query().Get("bet")
betType := r.URL.Query().Get("betType")
number := r.URL.Query().Get("number")
amount , _ := strconv.Atoi(bet)
if username == "" || amount <= 0 || betType == "" || number == "" {
http.Error(w, "Missing Data or Bad Data", http.StatusBadRequest)
return
}
_, ok := actions[betType]
if !ok {
http.Error(w, "Invalid Bet Type", http.StatusBadRequest)
return
}
// err = rdb.Set(ctx, msg.ID, msg.Content, 0).Err()
// if err != nil {
// http.Error(w, "Failed to save message", http.StatusInternalServerError)
// return
// }
// w.WriteHeader(http.StatusCreated)
fmt.Println(username, bet, betType, number)
json.NewEncoder(w).Encode(Message{ID: "Some Bet ID", Content: "You bet something"})
}
func main() {
// Initialize Redis connection
initRedis()
http.HandleFunc("/r-spin", getSpin)
http.HandleFunc("/newUser", newUser)
http.HandleFunc("/bet", takeBet)
log.Println("Server started at :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}