summaryrefslogtreecommitdiffstats
path: root/game/server.go
blob: ff256e97f18442e39dbaf8ff2cacf0c27086ccfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package game

import (
	"context"
	"github.com/coder/websocket"
	"golang.org/x/time/rate"
	"log"
	"math/rand/v2"
	"net/http"
	"time"
)

func NewMonopolyServer() *MServer {
	ms := &MServer{
		subscriberMessageBuffer: 16,
		logf:                    log.Printf,
		Subscribers:             make(map[*MSub]string),
		Users:                   make(map[string]*MUser),
		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.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)
	ms.serveMux.HandleFunc("/buy", ms.buyHandler)
	// ms.serveMux.HandleFunc("/auction", ms.auctionHandler)

	return ms
}

func (ms *MServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	ms.serveMux.ServeHTTP(w, r)
}
func writeTimeout(ctx context.Context, timeout time.Duration, c *websocket.Conn, msg []byte) error {
	ctx, cancel := context.WithTimeout(ctx, timeout)
	defer cancel()

	return c.Write(ctx, websocket.MessageText, msg)
}