-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
87 lines (72 loc) · 1.57 KB
/
main.go
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
package main
import (
"context"
"errors"
"flag"
"io"
"log"
"net/http"
"os"
"github.com/ndabAP/ping-pong/canvas"
"github.com/ndabAP/ping-pong/engine"
"golang.org/x/net/websocket"
)
var (
serverLogger = log.New(os.Stdout, "[SERVER] ", 0)
debug = flag.Bool("debug", false, "")
fps = flag.Uint("fps", canvas.DEFAULT_FPS, "")
)
func main() {
flag.Parse()
http.HandleFunc("/", serveHome)
http.Handle("/ws", websocket.Handler(serveWs))
serverLogger.Println("open http://127.0.0.1:8080")
serverLogger.Fatal(http.ListenAndServe("127.0.0.1:8080", nil))
}
func serveHome(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.Error(w, "", http.StatusNotFound)
return
}
if r.Method != http.MethodGet {
http.Error(w, "", http.StatusMethodNotAllowed)
return
}
serverLogger.Println("serving home.html")
http.ServeFile(w, r, "home.html")
}
func serveWs(ws *websocket.Conn) {
defer ws.Close()
game := engine.NewGame(
800,
600,
engine.NewPlayer(10, 150),
engine.NewPlayer(10, 150),
engine.NewBall(5, 5),
)
engine := canvas.New(game)
engine.SetDebug(*debug)
engine.SetFPS(*fps)
// Frames
framesch := make(chan []byte, 1)
go func() {
for frame := range framesch {
ws.Write(frame)
}
}()
// User input
inputch := make(chan []byte, 1)
defer close(inputch)
go func() {
for {
buf := make([]byte, canvas.INPUT_BUF_SIZE)
_, err := ws.Read(buf)
if err != nil && !errors.Is(err, io.EOF) {
serverLogger.Fatal(err.Error())
}
inputch <- buf
}
}()
ctx := context.Background()
engine.NewRound(ctx, framesch, inputch)
}