]> Skullheadx's Git Forge - monopoly-web.git/commitdiff
login check
authorSkullheadx <admonty1@protonmail.com>
Mon, 8 Jun 2026 20:48:33 +0000 (16:48 -0400)
committerSkullheadx <admonty1@protonmail.com>
Mon, 8 Jun 2026 20:48:33 +0000 (16:48 -0400)
game/game.go
go.mod
main.go
monopoly-web [new file with mode: 0755]
public/index.js

index 37fb72676fee3bb0640e14efd2b8b330a15f1b81..bf6719dea7321a2006d4e62ed406e52fbc111242 100644 (file)
@@ -3,6 +3,7 @@ package game
 import (
        "context"
        "errors"
+       "fmt"
        "github.com/coder/websocket"
        "github.com/google/uuid"
        "golang.org/x/time/rate"
@@ -26,25 +27,27 @@ type MonopolyServer struct {
        subscribersMu sync.Mutex
        subscribers   map[*subscriber]string
 
+       // uuid to username
+       users map[string]string
+
        gameCtxMu sync.Mutex
        gameCtx   *Context
        randSeed  *rand.PCG
 }
 
-// uuid to username
-var Users map[string]string
-
 func NewMonopolyServer() *MonopolyServer {
        ms := &MonopolyServer{
                subscriberMessageBuffer: 16,
                logf:                    log.Printf,
                subscribers:             make(map[*subscriber]string),
+               users:                   make(map[string]string),
                publishLimiter:          rate.NewLimiter(rate.Every(time.Millisecond*100), 8),
                gameCtx:                 nil,
                randSeed:                rand.NewPCG(20, 26),
        }
-       ms.serveMux.Handle("/", http.FileServer(http.Dir("../public/")))
+       ms.serveMux.Handle("/", http.FileServer(http.Dir("public/")))
        ms.serveMux.HandleFunc("/login", ms.loginHandler)
+       ms.serveMux.HandleFunc("/loggedin", ms.loggedInHandler)
        ms.serveMux.HandleFunc("/subscribe", ms.subscribeHandler)
        ms.serveMux.HandleFunc("/start", ms.startHandler)
        ms.serveMux.HandleFunc("/roll", ms.rollHandler)
@@ -77,6 +80,34 @@ func (ms *MonopolyServer) subscribeHandler(w http.ResponseWriter, r *http.Reques
        }
 }
 
+func (ms *MonopolyServer) loggedInHandler(w http.ResponseWriter, r *http.Request) {
+       if r.Method != "GET" {
+               http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
+               return
+       }
+
+       cookie, err := r.Cookie("user")
+       if err != nil {
+               if err == http.ErrNoCookie {
+                       http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
+                       return
+               }
+
+               http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
+               return
+       }
+
+       userUUID := cookie.Value
+
+       _, ok := ms.users[userUUID]
+       if !ok {
+               http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
+               return
+       }
+
+       w.WriteHeader(http.StatusOK)
+}
+
 func (ms *MonopolyServer) loginHandler(w http.ResponseWriter, r *http.Request) {
        if r.Method != "POST" {
                http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
@@ -91,7 +122,7 @@ func (ms *MonopolyServer) loginHandler(w http.ResponseWriter, r *http.Request) {
        }
        userUUID := uuid.NewString()
 
-       Users[userUUID] = string(username)
+       ms.users[userUUID] = string(username)
 
        http.SetCookie(w, &http.Cookie{
                Name:     "user",
@@ -112,6 +143,24 @@ func (ms *MonopolyServer) startHandler(w http.ResponseWriter, r *http.Request) {
                return
        }
 
+       cookie, err := r.Cookie("user")
+       if err != nil {
+               if err == http.ErrNoCookie {
+                       http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
+                       return
+               }
+
+               http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
+               return
+       }
+
+       userUUID := cookie.Value
+       _, ok := ms.users[userUUID]
+       if !ok {
+               http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
+               return
+       }
+
        ms.start()
 
        w.WriteHeader(http.StatusAccepted)
@@ -127,25 +176,37 @@ func (ms *MonopolyServer) rollHandler(w http.ResponseWriter, r *http.Request) {
        if err != nil {
                if err == http.ErrNoCookie {
                        http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
+                       return
                }
 
                http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
+               return
        }
 
        userUUID := cookie.Value
 
+       _, ok := ms.users[userUUID]
+       if !ok {
+               http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
+               return
+       }
+
        if ms.gameCtx == nil {
                http.Error(w, "Game has not started yet", http.StatusConflict)
+               return
        }
        if !ms.gameCtx.ValidateCanRoll(userUUID) {
                http.Error(w, "Not your turn", http.StatusForbidden)
+               return
        }
        ms.roll()
        w.WriteHeader(http.StatusOK)
 }
 
 func (ms *MonopolyServer) roll() {
+       fmt.Printf("%#v\n", ms.gameCtx)
        ms.gameCtx.RollDice()
+       fmt.Printf("%#v\n", ms.gameCtx)
        ms.gameCtx.ProcessMovement()
 }
 
@@ -154,9 +215,11 @@ func (ms *MonopolyServer) subscribe(w http.ResponseWriter, r *http.Request) erro
        if err != nil {
                if err == http.ErrNoCookie {
                        http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
+                       return err
                }
 
                http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
+               return err
        }
 
        userUUID := cookie.Value
diff --git a/go.mod b/go.mod
index f178e3e15b92bc5a2dfaae37bfe9e845e399e79a..c9366fc5572451f37b7482a686e7f65ad99a0603 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -7,4 +7,4 @@ require (
        golang.org/x/time v0.15.0
 )
 
-require github.com/google/uuid v1.6.0 // indirect
+require github.com/google/uuid v1.6.0
diff --git a/main.go b/main.go
index f51c0dde7af508cf30e7385a7fa4d0e27a830dac..85438452e94ba48f4b61636347596770dac84cc6 100644 (file)
--- a/main.go
+++ b/main.go
@@ -28,9 +28,9 @@ func run() error {
        }
        log.Printf("listening on ws://%v", l.Addr())
 
-       cs := game.NewMonopolyServer()
+       ms := game.NewMonopolyServer()
        s := &http.Server{
-               Handler:      cs,
+               Handler:      ms,
                ReadTimeout:  time.Second * 10,
                WriteTimeout: time.Second * 10,
        }
diff --git a/monopoly-web b/monopoly-web
new file mode 100755 (executable)
index 0000000..0ae8eef
Binary files /dev/null and b/monopoly-web differ
index c6d759b3ff9c722516f52038e17896e72d5e3982..e965ea7df589fc9725b7e9c099f82c7e4c644213 100644 (file)
@@ -1,6 +1,5 @@
 ; (() => {
         let connected = false
-
         function dial() {
                 const conn = new WebSocket(`ws://${location.host}/subscribe`)
 
                         appendGameLog(ev.data)
                 })
         }
-        // dial()
+        async function loggedIn() {
+                try {
+                        const resp = await fetch('/loggedin', {
+                                method: 'GET'
+                        })
+                        if (resp.status !== 200) {
+                                throw new Error(`Unexpected HTTP Status ${resp.status} ${resp.statusText}`)
+                        }
+                        dial()
+                } catch (err) {
+                        console.error(`Login check failed: ${err.message}`)
+                }
+        }
+        loggedIn()
 
         const gameLog = document.getElementById('log')
         const loginForm = document.getElementById('login-form')
@@ -57,7 +69,7 @@
                                 body: msg,
                         })
                         if (resp.status !== 200) {
-                                throw new Error(`Unexpected HTTP Status ${resp.status} ${resp.statusText}`)
+                                throw new Error(`Unexpected HTTP Status ${resp.status} ${resp.statusText} ${resp.message}`)
                         }
 
                         dial()
@@ -72,7 +84,7 @@
                                 method: 'POST',
                         })
                         if (resp.status !== 202) {
-                                throw new Error(`Unexpected HTTP Status ${resp.status} ${resp.statusText}`)
+                                throw new Error(`Unexpected HTTP Status ${resp.status} ${resp.statusText} ${resp.message}`)
                         }
                 } catch (err) {
                         console.error(`Start failed: ${err.message}`)
@@ -85,7 +97,7 @@
                                 method: 'POST',
                         })
                         if (resp.status !== 200) {
-                                throw new Error(`Unexpected HTTP Status ${resp.status} ${resp.statusText}`)
+                                throw new Error(`Unexpected HTTP Status ${resp.status} ${resp.statusText} ${resp.message}`)
                         }
                 } catch (err) {
                         console.error(`Start failed: ${err.message}`)