package main import ( "code.google.com/p/go.net/websocket" "encoding/json" "fmt" "log" "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) } func startGame(w http.ResponseWriter, req *http.Request) { new_game_name := idg.Hash() _g := NewGame(new_game_name, *width, *height) go _g.run() gameLock.Lock() games[new_game_name] = _g gameLock.Unlock() w.Write([]byte(fmt.Sprintf(`{"id": "%s"}`, new_game_name))) } 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) } } func addPlayer(ws *websocket.Conn) { // route to appropriate game ... var gid GameID err := websocket.JSON.Receive(ws, &gid) if err != nil { log.Println("problem parsing the requested game id") return } gameLock.Lock() game := games[gid.Id] gameLock.Unlock() log.Printf("%+v", game) id := "asdf" conf, err := Negociate(ws, id, 400, 800) if err != nil { websocket.JSON.Send(ws, NewFailure(err.Error())) } if conf != nil { p := &player{ Robot: robot{ Stats: conf.Stats, Id: id, Health: conf.Stats.Hp, Scanners: make([]scanner, 0)}, send: make(chan *boardstate), ws: ws, } p.reset() game.register <- p defer func() { game.unregister <- p }() go p.sender() p.recv() log.Printf("%v has been disconnect from this game\n", p.Robot.Id) } else { s := &Spectator{ send: make(chan *boardstate), ws: ws, } game.sregister <- s defer func() { game.sunregister <- s }() s.sender() log.Printf("%+v has been disconnect from this game\n", s) } }