2013-08-19 20:43:26 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"code.google.com/p/go.net/websocket"
|
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
2013-11-06 22:14:22 -08:00
|
|
|
_ "net/http/pprof"
|
2013-09-28 23:00:29 -07:00
|
|
|
"os"
|
|
|
|
"runtime/pprof"
|
2013-08-19 20:43:26 -07:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var addr = flag.String("addr", ":8666", "http service address")
|
2013-10-18 23:22:35 -07:00
|
|
|
var tick = flag.Int("tick", 60, "")
|
2013-11-06 20:10:22 -08:00
|
|
|
var timescale = flag.Float64("timescale", 1, "time scale factor")
|
2013-08-19 20:43:26 -07:00
|
|
|
var verbose = flag.Bool("verbose", false, "")
|
|
|
|
var width = flag.Float64("width", 800, "width of field")
|
|
|
|
var height = flag.Float64("height", 550, "height of field")
|
2013-11-07 21:00:10 -08:00
|
|
|
var obstacle_count = flag.Int("obstacles", 5, "how many obstacles")
|
2013-09-28 23:00:29 -07:00
|
|
|
var profile = flag.String("pprof", "", "if specified will run with pprof")
|
2013-11-06 22:14:22 -08:00
|
|
|
var netprofile = flag.Bool("netprof", false, "if specified will run with net/http/pprof")
|
2013-10-18 20:48:22 -07:00
|
|
|
var debug = flag.Bool("debug", false, "automatically create games if they don't exist")
|
2013-08-19 20:43:26 -07:00
|
|
|
|
2013-10-19 00:00:45 -07:00
|
|
|
var delta float32
|
2013-08-19 20:43:26 -07:00
|
|
|
|
2013-08-28 23:46:12 -07:00
|
|
|
var idg *IdGenerator
|
2013-09-05 18:14:41 -07:00
|
|
|
|
2013-09-28 12:59:04 -07:00
|
|
|
// This is the main, global collection of games
|
2013-09-05 18:14:41 -07:00
|
|
|
var games MapLock
|
2013-08-28 23:46:12 -07:00
|
|
|
|
2013-08-19 20:43:26 -07:00
|
|
|
func main() {
|
2013-10-20 21:15:23 -07:00
|
|
|
log.Printf("Starting Server...")
|
|
|
|
|
2013-08-19 20:43:26 -07:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
flag.Parse()
|
2013-09-28 23:00:29 -07:00
|
|
|
if *profile != "" {
|
|
|
|
f, err := os.Create(*profile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
pprof.StartCPUProfile(f)
|
|
|
|
}
|
2013-11-06 22:14:22 -08:00
|
|
|
if *netprofile {
|
|
|
|
go func() {
|
|
|
|
log.Println(http.ListenAndServe("localhost:8667", nil))
|
|
|
|
}()
|
|
|
|
}
|
2013-08-19 20:43:26 -07:00
|
|
|
|
2013-09-05 18:14:41 -07:00
|
|
|
games = MapLock{m: make(map[string]*game)}
|
2013-08-28 23:46:12 -07:00
|
|
|
idg = NewIdGenerator()
|
|
|
|
|
2013-11-06 20:10:22 -08:00
|
|
|
delta = (float32(*tick) / 1000.0) * float32(*timescale)
|
2013-08-19 20:43:26 -07:00
|
|
|
|
2013-11-06 22:14:22 -08:00
|
|
|
sm := http.NewServeMux()
|
|
|
|
|
|
|
|
sm.Handle("/ws/", websocket.Handler(addPlayer))
|
|
|
|
sm.Handle("/game/start/", JsonHandler(startGame))
|
|
|
|
sm.Handle("/game/list/", JsonHandler(listGames))
|
|
|
|
sm.HandleFunc("/game/stop/", stopGame)
|
|
|
|
sm.HandleFunc("/fuck/shit/up/", killServer)
|
2013-08-19 20:43:26 -07:00
|
|
|
|
2013-11-06 22:14:22 -08:00
|
|
|
err := http.ListenAndServe(*addr, sm)
|
2013-08-19 20:43:26 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("unable to start server")
|
|
|
|
}
|
|
|
|
}
|