server/main.go

112 lines
2.3 KiB
Go
Raw Normal View History

2013-08-19 20:43:26 -07:00
package main
import (
"code.google.com/p/go.net/websocket"
2013-09-01 22:04:10 -07:00
"encoding/json"
2013-08-19 20:43:26 -07:00
"flag"
"fmt"
"log"
"math/rand"
"net/http"
2013-09-01 22:04:10 -07:00
"sync"
2013-08-19 20:43:26 -07:00
"time"
)
var addr = flag.String("addr", ":8666", "http service address")
var tick = flag.Int("tick", 33, "")
var verbose = flag.Bool("verbose", false, "")
var width = flag.Float64("width", 800, "width of field")
var height = flag.Float64("height", 550, "height of field")
var delta float64
2013-08-25 23:10:02 -07:00
var g *game
2013-08-19 20:43:26 -07:00
2013-08-28 23:46:12 -07:00
var games map[string]*game
var idg *IdGenerator
2013-09-01 22:04:10 -07:00
var gameLock sync.RWMutex
2013-08-28 23:46:12 -07:00
2013-08-19 20:43:26 -07:00
func main() {
rand.Seed(time.Now().UnixNano())
flag.Parse()
2013-08-28 23:46:12 -07:00
games = make(map[string]*game)
idg = NewIdGenerator()
2013-08-19 20:43:26 -07:00
delta = float64(*tick) / 1000
http.Handle("/ws/", websocket.Handler(addPlayer))
2013-08-28 23:46:12 -07:00
http.Handle("/game/start/", JsonHandler(startGame))
2013-09-01 22:04:10 -07:00
http.Handle("/game/list/", JsonHandler(listGames))
2013-08-28 23:46:12 -07:00
// http.Handle("/game/stop/", stopGame)
2013-08-19 20:43:26 -07:00
err := http.ListenAndServe(*addr, nil)
if err != nil {
log.Fatal("unable to start server")
}
}
2013-08-28 23:46:12 -07:00
func startGame(w http.ResponseWriter, req *http.Request) {
new_game_name := idg.Hash()
2013-09-01 22:04:10 -07:00
_g := NewGame(*width, *height)
2013-08-28 23:46:12 -07:00
go _g.run()
2013-09-01 22:04:10 -07:00
gameLock.Lock()
2013-08-28 23:46:12 -07:00
games[new_game_name] = _g
2013-09-01 22:04:10 -07:00
gameLock.Unlock()
2013-08-28 23:46:12 -07:00
w.Write([]byte(fmt.Sprintf(`{"id": "%s"}`, new_game_name)))
}
2013-09-01 22:04:10 -07:00
func listGames(w http.ResponseWriter, req *http.Request) {
gameLock.RLock()
defer gameLock.RUnlock()
ids := make([]string, 0)
for id, _ := range games {
ids = append(ids, id)
}
if err := json.NewEncoder(w).Encode(ids); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
2013-08-19 20:43:26 -07:00
func addPlayer(ws *websocket.Conn) {
2013-08-28 23:46:12 -07:00
id := fmt.Sprintf("robot%d", <-g.robot_id)
2013-08-19 20:43:26 -07:00
2013-08-21 07:53:34 -07:00
conf, err := Negociate(ws, id, *width, *height)
2013-08-19 20:43:26 -07:00
if err != nil {
2013-08-21 07:53:34 -07:00
websocket.JSON.Send(ws, NewFailure(err.Error()))
2013-08-19 20:43:26 -07:00
}
2013-08-21 07:53:34 -07:00
if conf != nil {
2013-08-19 22:23:35 -07:00
p := &player{
Robot: robot{
Stats: conf.Stats,
Id: id,
Health: conf.Stats.Hp,
Scanners: make([]scanner, 0)},
send: make(chan *boardstate),
ws: ws,
}
2013-08-19 23:44:00 -07:00
p.reset()
2013-08-19 22:23:35 -07:00
g.register <- p
defer func() {
g.unregister <- p
}()
go p.sender()
p.recv()
log.Printf("%v has been disconnect from this game\n", p.Robot.Id)
2013-08-21 07:53:34 -07:00
} else {
2013-08-19 22:23:35 -07:00
s := &Spectator{
send: make(chan *boardstate),
ws: ws,
}
g.sregister <- s
defer func() {
g.sunregister <- s
}()
s.sender()
log.Printf("%+v has been disconnect from this game\n", s)
2013-08-19 20:43:26 -07:00
}
}