added simple, non-leaking game stop url
This commit is contained in:
parent
b7087adb78
commit
f175e45c63
9
game.go
9
game.go
@ -7,6 +7,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const maxPlayer = 128
|
||||||
|
|
||||||
type game struct {
|
type game struct {
|
||||||
id string
|
id string
|
||||||
players map[*player]bool
|
players map[*player]bool
|
||||||
@ -26,7 +28,7 @@ func NewGame(id string, width, height float64) *game {
|
|||||||
g := &game{
|
g := &game{
|
||||||
id: id,
|
id: id,
|
||||||
register: make(chan *player),
|
register: make(chan *player),
|
||||||
unregister: make(chan *player),
|
unregister: make(chan *player, maxPlayer),
|
||||||
projectiles: make(map[*bot.Projectile]bool),
|
projectiles: make(map[*bot.Projectile]bool),
|
||||||
splosions: make(map[*bot.Splosion]bool),
|
splosions: make(map[*bot.Splosion]bool),
|
||||||
players: make(map[*player]bool),
|
players: make(map[*player]bool),
|
||||||
@ -36,7 +38,7 @@ func NewGame(id string, width, height float64) *game {
|
|||||||
spectators: make(map[*Spectator]bool),
|
spectators: make(map[*Spectator]bool),
|
||||||
sregister: make(chan *Spectator),
|
sregister: make(chan *Spectator),
|
||||||
sunregister: make(chan *Spectator),
|
sunregister: make(chan *Spectator),
|
||||||
kill: make(chan bool, 128),
|
kill: make(chan bool, maxPlayer),
|
||||||
}
|
}
|
||||||
return g
|
return g
|
||||||
}
|
}
|
||||||
@ -50,6 +52,9 @@ func (g *game) run() {
|
|||||||
case <-g.kill:
|
case <-g.kill:
|
||||||
log.Printf("game %s: received kill signal, dying gracefully", g.id)
|
log.Printf("game %s: received kill signal, dying gracefully", g.id)
|
||||||
games.Lock()
|
games.Lock()
|
||||||
|
for player := range g.players {
|
||||||
|
close(player.send)
|
||||||
|
}
|
||||||
delete(games.m, g.id)
|
delete(games.m, g.id)
|
||||||
games.Unlock()
|
games.Unlock()
|
||||||
return
|
return
|
||||||
|
19
http.go
19
http.go
@ -7,6 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type JsonHandler func(http.ResponseWriter, *http.Request)
|
type JsonHandler func(http.ResponseWriter, *http.Request)
|
||||||
@ -30,6 +31,24 @@ func startGame(w http.ResponseWriter, req *http.Request) {
|
|||||||
w.Write([]byte(fmt.Sprintf(`{"id": "%s"}`, new_game_name)))
|
w.Write([]byte(fmt.Sprintf(`{"id": "%s"}`, new_game_name)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func stopGame(w http.ResponseWriter, req *http.Request) {
|
||||||
|
trimmed := strings.Trim(req.URL.Path, "/")
|
||||||
|
fullPath := strings.Split(trimmed, "/")
|
||||||
|
if len(fullPath) != 3 {
|
||||||
|
http.Error(w, "improperly formed url", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
key := fullPath[2]
|
||||||
|
games.Lock()
|
||||||
|
gameid, ok := games.m[key]
|
||||||
|
defer games.Unlock()
|
||||||
|
if !ok {
|
||||||
|
http.NotFound(w, req)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
gameid.kill <- true
|
||||||
|
}
|
||||||
|
|
||||||
func listGames(w http.ResponseWriter, req *http.Request) {
|
func listGames(w http.ResponseWriter, req *http.Request) {
|
||||||
games.RLock()
|
games.RLock()
|
||||||
defer games.RUnlock()
|
defer games.RUnlock()
|
||||||
|
2
main.go
2
main.go
@ -39,7 +39,7 @@ func main() {
|
|||||||
http.Handle("/ws/", websocket.Handler(addPlayer))
|
http.Handle("/ws/", websocket.Handler(addPlayer))
|
||||||
http.Handle("/game/start/", JsonHandler(startGame))
|
http.Handle("/game/start/", JsonHandler(startGame))
|
||||||
http.Handle("/game/list/", JsonHandler(listGames))
|
http.Handle("/game/list/", JsonHandler(listGames))
|
||||||
// http.Handle("/game/stop/", stopGame)
|
http.HandleFunc("/game/stop/", stopGame)
|
||||||
|
|
||||||
err := http.ListenAndServe(*addr, nil)
|
err := http.ListenAndServe(*addr, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -34,10 +34,15 @@ func (p *player) sender() {
|
|||||||
|
|
||||||
func (p *player) recv() {
|
func (p *player) recv() {
|
||||||
for {
|
for {
|
||||||
|
// XXX: need to mark myself as having received something, also binding
|
||||||
|
// such action to a particular game turn ID
|
||||||
var msg instruction
|
var msg instruction
|
||||||
err := websocket.JSON.Receive(p.ws, &msg)
|
err := websocket.JSON.Receive(p.ws, &msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
// TODO: perhaps we could be a bit more precise in the handling of
|
||||||
|
// this 'error' by selecting on some kill signal channel and this
|
||||||
|
// json read?
|
||||||
|
log.Print("problem receiving JSON from player: ", err)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if msg.MoveTo != nil {
|
if msg.MoveTo != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user