-
Notifications
You must be signed in to change notification settings - Fork 5
/
pao.go
211 lines (188 loc) · 4.99 KB
/
pao.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"database/sql"
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
paoDb "github.com/arbrown/pao/db"
"github.com/arbrown/pao/game"
"github.com/arbrown/pao/settings"
_ "github.com/mattn/go-sqlite3"
)
var (
a *paoDb.Auth
)
func main() {
fmt.Printf("Hello, Pao\n")
removeGameChan := make(chan *game.Game)
games := make(map[string]*game.Game)
s, err := settings.GetSettings()
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Printf("Settings: %+v\n", s)
}
// create db connection
db, err := sql.Open(s.DbConfig.Driver, s.DbConfig.ConnectionString)
if err != nil {
db = nil
fmt.Printf("Could not open database: %s\n", err)
} else {
paoDb.Init(db)
}
a, err = paoDb.NewAuth(s)
if err != nil {
fmt.Println("Could not create auth")
fmt.Println(err.Error())
}
httpMux := http.NewServeMux()
httpMux.Handle("/listGames", listGamesHandler{games: games})
httpMux.Handle("/game", gameHandler{games: games, removeGameChan: removeGameChan, db: db})
httpMux.Handle("/playAi", playAiHandler{games: games, removeGameChan: removeGameChan, db: db, ais: s.Ais})
httpMux.HandleFunc("/login", a.PostLogin)
httpMux.HandleFunc("/register", a.PostRegister)
httpMux.HandleFunc("/logout", a.HandleLogout)
httpMux.HandleFunc("/cu", a.Cu)
httpMux.HandleFunc("/leaderBoard", paoDb.GetLeaderBoard)
// Serve the UI here
httpMux.Handle("/", http.FileServer(http.Dir("./react-app/build/")))
go func() {
for {
select {
case g := <-removeGameChan:
fmt.Printf("Removing game: %v\n", g.ID)
delete(games, g.ID)
break
}
}
}()
host, port := os.Getenv("HOST"), os.Getenv("PORT")
if port == "" {
port = "2015"
}
bind := fmt.Sprintf("%s:%s", host, port)
fmt.Printf("Listening on %s\n", bind)
err = http.ListenAndServe(bind, httpMux)
if err != nil {
panic("ListenAndServe:" + err.Error())
}
}
type gameHandler struct {
games map[string]*game.Game
removeGameChan chan *game.Game
db *sql.DB
}
type playAiHandler struct {
games map[string]*game.Game
removeGameChan chan *game.Game
db *sql.DB
ais []settings.AiConfig
}
type listGamesHandler struct {
games map[string]*game.Game
}
type gameResponse struct {
ID string
Players []string
}
func (lgh listGamesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var resp []gameResponse
for id, game := range lgh.games {
var players []string
if game.CurrentPlayer() != nil {
players = append(players, game.CurrentPlayer().Name)
}
if game.NextPlayer() != nil {
players = append(players, game.NextPlayer().Name)
}
if len(players) != 0 {
resp = append(resp, gameResponse{ID: id, Players: players})
}
}
js, err := json.Marshal(resp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
func (gh gameHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var user = a.GetUser(w, r)
fmt.Printf("105: user: %+v\n", user)
id := r.FormValue("id")
name := r.FormValue("name")
fmt.Println(id)
if name == "" {
name = "Anonymous Coward"
}
if id != "" {
// Does the game exist?
fmt.Printf("All Games: %v\n", gh.games)
if existingGame, ok := gh.games[id]; ok {
fmt.Println("Trying to join existing game")
existingGame.Join(w, r, name, user)
} else {
// make the id requested
g := game.NewGame(id, gh.removeGameChan, gh.db)
fmt.Printf("Made new game %s\n", id)
gh.games[g.ID] = g
g.Join(w, r, name, user)
}
} else {
// no id specified, make the game
newID := 0
for _, exists := gh.games[strconv.Itoa(newID)]; exists; _, exists = gh.games[strconv.Itoa(newID)] {
newID++
}
g := game.NewGame(strconv.Itoa(newID), gh.removeGameChan, gh.db)
fmt.Printf("Made new game %d\n", newID)
gh.games[g.ID] = g
fmt.Printf("Trying to join new game as: %+v\n", user)
if ok := g.Join(w, r, name, user); !ok {
delete(gh.games, g.ID)
}
}
}
// Create a new game against an AI and return the game ID to the client
func (pah playAiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
name := r.FormValue("ai")
var ai settings.AiConfig
for _, aic := range pah.ais {
if aic.Name == name {
ai = aic
}
}
if ai.Name == "" {
fmt.Printf("Couldn't find AI: '%v'\n", name)
}
newID := 0
for _, exists := pah.games[strconv.Itoa(newID)]; exists; _, exists = pah.games[strconv.Itoa(newID)] {
newID++
}
g := game.NewGame(strconv.Itoa(newID), pah.removeGameChan, pah.db)
fmt.Printf("Made new game %d\n", newID)
pah.games[g.ID] = g
// if ok := g.Join(w, r, name, user); !ok {
// delete(pah.games, g.ID)
// }
fmt.Printf("AI tries to join game: %v\n", g)
if ok := g.JoinAi(ai); !ok {
// if game join fails, remove game from list
delete(pah.games, g.ID)
}
type aiGameResponse struct {
ID string
}
var resp = aiGameResponse{
ID: g.ID,
}
js, err := json.Marshal(resp)
if err != nil {
fmt.Printf("Couldn't write json response: %v\n", resp)
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}