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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
package api
import (
"net/http"
"sakura/data/cirno"
"sakura/engine"
"slices"
"uuid"
"github.com/gorilla/websocket"
)
type Conn struct {
Socket *websocket.Conn
}
var upgrader = websocket.Upgrader{}
var games = make(map[uuid.UUID]*engine.Game)
var connections = make(map[uuid.UUID][]*Conn)
func GameCreate(w http.ResponseWriter, r *http.Request) {
id := uuid.NewV7()
g := engine.NewGame()
games[id] = g
g.Yin.SetCharacter(cirno.Cirno)
g.Yang.SetCharacter(cirno.Cirno)
w.Write([]byte(id.String()))
}
func GameConnect(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
uid, err := uuid.Parse(id)
if err != nil {
http.Error(w, "unable to parse game uuid "+err.Error(), http.StatusBadRequest)
return
}
game := games[uid]
if game == nil {
http.Error(w, "game not found", http.StatusNotFound)
return
}
p, c := Bind(game, w, r)
wsConn, err := upgrader.Upgrade(w, r, http.Header{
"Set-Cookie": []string{c.String()},
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if wsConn == nil {
http.Error(w, "unable to initialize websocket", http.StatusInternalServerError)
return
}
conn := &Conn{wsConn}
connections[uid] = append(connections[uid], conn)
wsConn.SetCloseHandler(func(code int, text string) error {
connections[uid] = slices.DeleteFunc(connections[uid], func(potential *Conn) bool {
return potential == conn
})
return nil
})
go PumpRead(uid, game, p, wsConn)
}
func Bind(g *engine.Game, w http.ResponseWriter, r *http.Request) (*engine.Player, *http.Cookie) {
cookie, err := r.Cookie("User")
newUser := false
var id string
if err != nil {
newUser = true
uid := uuid.NewV7()
id = uid.String()
cookie = &http.Cookie{
Name: "User",
Value: id,
Quoted: true,
Secure: true,
HttpOnly: false,
Path: "/",
SameSite: http.SameSiteStrictMode,
}
http.SetCookie(w, cookie)
} else {
id = cookie.Value
}
switch {
case newUser && g.Yin.Cookie == "":
g.Yin.Cookie = id
return g.Yin, cookie
case newUser && g.Yang.Cookie == "":
g.Yang.Cookie = id
return g.Yang, cookie
case g.Yang.Cookie == id:
return g.Yang, cookie
case g.Yin.Cookie == id:
return g.Yin, cookie
default:
return nil, cookie
}
}
func Update(id uuid.UUID, g *engine.Game) {
for _, c := range connections[id] {
_ = c.Socket.WriteJSON(g)
}
}
func PumpRead(id uuid.UUID, g *engine.Game, p *engine.Player, conn *websocket.Conn) {
var interaction engine.InteractionID
for {
strippedGame := g.Present(p)
Update(id, &strippedGame)
err := conn.ReadJSON(&interaction)
if err != nil {
break
}
a := g.Actions[interaction]
if a == nil {
continue
}
a.Then(g)
g.ActionToHistory(a)
}
}
|