server/http.go

115 lines
2.4 KiB
Go
Raw Normal View History

2013-08-28 23:46:12 -07:00
package main
import (
2013-09-03 23:26:40 -07:00
"bitbucket.org/hackerbots/bot"
2013-09-01 23:00:09 -07:00
"code.google.com/p/go.net/websocket"
"encoding/json"
"fmt"
"log"
2013-08-28 23:46:12 -07:00
"net/http"
)
type JsonHandler func(http.ResponseWriter, *http.Request)
func (h JsonHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
h(w, req)
}
2013-09-01 23:00:09 -07:00
func startGame(w http.ResponseWriter, req *http.Request) {
new_game_name := idg.Hash()
_g := NewGame(new_game_name, *width, *height)
go _g.run()
games.Lock()
games.m[new_game_name] = _g
2013-09-03 23:26:40 -07:00
log.Printf("%+v", games)
games.Unlock()
2013-09-01 23:00:09 -07:00
w.Write([]byte(fmt.Sprintf(`{"id": "%s"}`, new_game_name)))
}
func listGames(w http.ResponseWriter, req *http.Request) {
games.RLock()
defer games.RUnlock()
2013-09-07 19:12:46 -07:00
type gl struct {
Id string `json:"id"`
Players []string `json:"players"`
}
ids := make([]gl, 0)
for id, g := range games.m {
2013-09-07 19:12:46 -07:00
players := make([]string, 0)
for p, _ := range g.players {
// XXX: change this to be the user-provided bot name?
players = append(players, p.Robot.Id)
}
ids = append(ids, gl{
2013-09-07 19:12:46 -07:00
Id: id,
Players: players,
})
2013-09-01 23:00:09 -07:00
}
if err := json.NewEncoder(w).Encode(ids); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func addPlayer(ws *websocket.Conn) {
// route to appropriate game ...
2013-09-03 23:26:40 -07:00
var gid bot.GameID
2013-09-01 23:00:09 -07:00
err := websocket.JSON.Receive(ws, &gid)
if err != nil {
log.Println("problem parsing the requested game id")
return
}
games.Lock()
game, ok := games.m[gid.Id]
games.Unlock()
2013-09-01 23:00:09 -07:00
2013-09-03 23:26:40 -07:00
if !ok {
log.Println("ERROR: game not found")
websocket.JSON.Send(ws, bot.NewFailure("game 404"))
2013-09-03 23:26:40 -07:00
return
}
id := idg.Hash()
2013-09-01 23:00:09 -07:00
conf, err := Negociate(ws, id, game.width, game.height)
2013-09-01 23:00:09 -07:00
if err != nil {
websocket.JSON.Send(ws, bot.NewFailure(err.Error()))
2013-09-01 23:00:09 -07:00
}
if conf != nil {
p := &player{
2013-09-03 23:26:40 -07:00
Robot: bot.Robot{
2013-09-01 23:00:09 -07:00
Stats: conf.Stats,
Id: id,
Health: conf.Stats.Hp,
2013-09-03 23:26:40 -07:00
Scanners: make([]bot.Scanner, 0)},
send: make(chan *bot.Boardstate),
2013-09-01 23:00:09 -07:00
ws: ws,
}
p.reset()
2013-09-03 23:26:40 -07:00
log.Printf("game: %+v", game)
2013-09-01 23:00:09 -07:00
game.register <- p
defer func() {
game.unregister <- p
}()
go p.sender()
p.recv()
log.Printf("game %s: %v has been disconnect from this game\n", gid, p.Robot.Id)
2013-09-01 23:00:09 -07:00
} else {
s := &Spectator{
2013-09-03 23:26:40 -07:00
send: make(chan *bot.Boardstate),
2013-09-01 23:00:09 -07:00
ws: ws,
}
game.sregister <- s
defer func() {
game.sunregister <- s
}()
s.sender()
log.Printf("%+v has been disconnect from this game\n", s)
}
}