summaryrefslogtreecommitdiffstats
path: root/main.go
blob: 85438452e94ba48f4b61636347596770dac84cc6 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main

import (
	"log"
	"monopoly-web/game"
	"net/http"

	"context"
	"net"
	"os"
	"os/signal"
	"time"
)

func main() {
	log.SetFlags(0)

	err := run()
	if err != nil {
		log.Fatal(err)
	}
}

func run() error {
	l, err := net.Listen("tcp", "127.0.0.1:8443")
	if err != nil {
		return err
	}
	log.Printf("listening on ws://%v", l.Addr())

	ms := game.NewMonopolyServer()
	s := &http.Server{
		Handler:      ms,
		ReadTimeout:  time.Second * 10,
		WriteTimeout: time.Second * 10,
	}

	errc := make(chan error, 1)
	go func() {
		errc <- s.Serve(l)
	}()

	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs, os.Interrupt)
	select {
	case err := <-errc:
		log.Printf("failed to serve: %v", err)
	case sig := <-sigs:
		log.Printf("terminating %v", sig)
	}

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
	defer cancel()

	return s.Shutdown(ctx)
}

//
// func (r *Room) endTurnHandler(w http.ResponseWriter, req *http.Request) {
// 	if r.gameCtx.ValidateCanEndTurn(UUID) {
// 		r.gameCtx.EndTurn()
// 	}
// }
//
// func (r *Room) exitJailHandler(w http.ResponseWriter, req *http.Request) {
// 	err := req.ParseForm()
// 	if err != nil {
// 		http.Error(w, "Bad Request: Failed to parse form data", http.StatusBadRequest)
// 		return
// 	}
//
// 	if !r.gameCtx.ValidateCanExitJail(UUID) {
// 		w.WriteHeader(http.StatusForbidden)
// 		w.Write([]byte(`{"status": "forbidden", "message": "Not your turn or not in jail"}`))
// 	}
//
// 	method := req.PostForm.Get("method")
//
// 	switch method {
// 	case "buyout":
// 		err = r.gameCtx.JailBuyout()
// 		if err == game.ErrNotEnoughMoney {
// 			http.Error(w, "error: Insufficient funds", http.StatusUnprocessableEntity)
// 		}
//
// 	case "jail_free_card":
// 		err = r.gameCtx.JailUseCard()
// 		if err == game.ErrNotEnoughJailCards {
// 			http.Error(w, "error: Insufficient jail cards", http.StatusUnprocessableEntity)
// 		}
// 	default:
// 		w.WriteHeader(http.StatusBadRequest)
// 		w.Write([]byte(`{"status": "bad req", "message": "escape jail method does not exist"}`))
// 		return
// 	}
//
// 	w.WriteHeader(http.StatusOK)
// 	w.Write([]byte(`{"status": "success", "message": "Action processed"}`))
// }
//