-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.go
91 lines (82 loc) · 2.52 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
88
89
90
91
package main
import (
"encoding/json"
"flag"
"fmt"
"html/template"
"log"
"net/http"
"sync/atomic"
"github.com/gorilla/websocket"
"github.com/xwjdsh/2048-ai/ai"
"github.com/xwjdsh/2048-ai/grid"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
var logo = `
██████╗ ██████╗ ██╗ ██╗ █████╗ █████╗ ██╗
╚════██╗██╔═████╗██║ ██║██╔══██╗ ██╔══██╗██║
█████╔╝██║██╔██║███████║╚█████╔╝█████╗███████║██║
██╔═══╝ ████╔╝██║╚════██║██╔══██╗╚════╝██╔══██║██║
███████╗╚██████╔╝ ██║╚█████╔╝ ██║ ██║██║
╚══════╝ ╚═════╝ ╚═╝ ╚════╝ ╚═╝ ╚═╝╚═╝
`
var (
addr = flag.String("addr", ":8080", "http service address")
online int32
)
func main() {
fmt.Println(logo)
flag.Parse()
static := http.FileServer(http.Dir("./2048"))
http.Handle("/js/", static)
http.Handle("/style/", static)
http.Handle("/meta/", static)
http.Handle("/favicon.ico", static)
indexTpl := template.Must(template.ParseFiles("./2048/index.html"))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
indexTpl.Execute(w, nil)
})
http.HandleFunc("/compute", compute)
log.Printf("Service started on \x1b[32;1m%s\x1b[32;1m\x1b[0m\n", *addr)
log.Fatal(http.ListenAndServe(*addr, nil))
}
func compute(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println("upgrade error:", err.Error())
return
}
defer func() {
if conn != nil {
conn.Close()
}
online = atomic.AddInt32(&online, -1)
log.Println(online)
}()
online = atomic.AddInt32(&online, 1)
log.Println(online)
for {
messageType, p, err := conn.ReadMessage()
if err != nil {
break
}
g := &grid.Grid{}
if err = json.Unmarshal(p, g); err != nil {
break
}
a := &ai.AI{Grid: g}
dire := a.Search()
result := map[string]grid.Direction{"dire": dire}
p, _ = json.Marshal(result)
if err := conn.WriteMessage(messageType, p); err != nil {
break
}
}
}