ignore new binary
Create new bserv binary Herein lie the following ideas: - change package visibility of many things - try to stash the globals into structs - the code is far from correct; it merely compiles
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package botserv
|
||||
|
||||
// delete me
|
||||
|
||||
@@ -24,23 +24,23 @@ type Scanner struct {
|
||||
}
|
||||
|
||||
type MapLock struct {
|
||||
m map[string]*game
|
||||
M map[string]*Game
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
// get is a function that returns a game if found, and creates one if
|
||||
// not found and force is true. In order to get a hash (rather than use
|
||||
// the string you pass) send "" for id.
|
||||
func (ml *MapLock) get(id string) *game {
|
||||
func (ml *MapLock) get(id string) *Game {
|
||||
ml.Lock()
|
||||
g, _ := ml.m[id]
|
||||
g, _ := ml.M[id]
|
||||
ml.Unlock()
|
||||
return g
|
||||
}
|
||||
|
||||
func (ml *MapLock) add(g *game) {
|
||||
func (ml *MapLock) add(g *Game) {
|
||||
ml.Lock()
|
||||
ml.m[g.id] = g
|
||||
ml.M[g.id] = g
|
||||
ml.Unlock()
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ type GameStats struct {
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
type game struct {
|
||||
type Game struct {
|
||||
id string
|
||||
players map[*player]bool
|
||||
projectiles map[*Projectile]bool
|
||||
@@ -87,15 +87,18 @@ type game struct {
|
||||
stats GameStats
|
||||
mode GameMode
|
||||
bw *bandwidth.Bandwidth
|
||||
Conf Config
|
||||
Games *MapLock
|
||||
Verbose bool
|
||||
}
|
||||
|
||||
type GameMode interface {
|
||||
setup(g *game)
|
||||
tick(gg *game, payload *Boardstate)
|
||||
gameOver(gg *game) (bool, *GameOver)
|
||||
setup(g *Game)
|
||||
tick(gg *Game, payload *Boardstate)
|
||||
gameOver(gg *Game) (bool, *GameOver)
|
||||
}
|
||||
|
||||
func NewGame(id string, width, height float32, obstacles, tick, maxPoints int, mode string) (*game, error) {
|
||||
func NewGame(id string, width, height float32, obstacles, tick, maxPoints int, mode string) (*Game, error) {
|
||||
bw, err := bandwidth.NewBandwidth(
|
||||
[]int{1, 10, 60},
|
||||
1*time.Second,
|
||||
@@ -105,7 +108,7 @@ func NewGame(id string, width, height float32, obstacles, tick, maxPoints int, m
|
||||
return nil, err
|
||||
}
|
||||
go bw.Run()
|
||||
g := &game{
|
||||
g := &Game{
|
||||
id: id,
|
||||
register: make(chan *player, maxPlayer),
|
||||
unregister: make(chan *player, maxPlayer),
|
||||
@@ -141,7 +144,7 @@ func NewGame(id string, width, height float32, obstacles, tick, maxPoints int, m
|
||||
return g, nil
|
||||
}
|
||||
|
||||
func (g *game) tick(payload *Boardstate) {
|
||||
func (g *Game) tick(payload *Boardstate) {
|
||||
g.players_remaining = 0
|
||||
payload.Objects = MinifyObstacles(g.obstacles)
|
||||
|
||||
@@ -196,7 +199,7 @@ func (g *game) tick(payload *Boardstate) {
|
||||
}
|
||||
}
|
||||
|
||||
func (g *game) sendUpdate(payload *Boardstate) {
|
||||
func (g *Game) sendUpdate(payload *Boardstate) {
|
||||
// Ensure that the robots are always sent in a consistent order
|
||||
sort.Sort(RobotSorter{Robots: payload.OtherRobots})
|
||||
sort.Sort(AllRobotSorter{Robots: payload.AllBots})
|
||||
@@ -292,20 +295,20 @@ func (g *game) sendUpdate(payload *Boardstate) {
|
||||
|
||||
}
|
||||
|
||||
func (g *game) run() {
|
||||
func (g *Game) run() {
|
||||
var t0, t1 time.Time
|
||||
ticker := time.NewTicker(time.Duration(conf.Tick) * time.Millisecond)
|
||||
ticker := time.NewTicker(time.Duration(g.Conf.Tick) * time.Millisecond)
|
||||
for {
|
||||
select {
|
||||
case <-g.kill:
|
||||
log.Printf("game %s: received kill signal, dying gracefully", g.id)
|
||||
close(g.bw.Quit)
|
||||
games.Lock()
|
||||
g.Games.Lock()
|
||||
for player := range g.players {
|
||||
close(player.send)
|
||||
}
|
||||
delete(games.m, g.id)
|
||||
games.Unlock()
|
||||
delete(g.Games.M, g.id)
|
||||
g.Games.Unlock()
|
||||
return
|
||||
case p := <-g.register:
|
||||
g.players[p] = true
|
||||
@@ -330,7 +333,7 @@ func (g *game) run() {
|
||||
|
||||
g.turn++
|
||||
payload.Turn = g.turn
|
||||
if *verbose {
|
||||
if g.Verbose {
|
||||
log.Printf("\033[2JTurn: %v", g.turn)
|
||||
log.Printf("Players: %v", len(g.players))
|
||||
log.Printf("Projectiles: %v", len(g.projectiles))
|
||||
@@ -346,7 +349,7 @@ func (g *game) run() {
|
||||
g.mode.tick(g, payload)
|
||||
|
||||
t1 = time.Now()
|
||||
if *verbose {
|
||||
if g.Verbose {
|
||||
log.Printf("Turn Processes %v\n", t1.Sub(t0))
|
||||
}
|
||||
|
||||
@@ -354,14 +357,14 @@ func (g *game) run() {
|
||||
g.sendUpdate(payload)
|
||||
|
||||
t1 = time.Now()
|
||||
if *verbose {
|
||||
if g.Verbose {
|
||||
log.Printf("Sent Payload %v\n", t1.Sub(t0))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (g *game) sendGameOver(eg *GameOver) {
|
||||
func (g *Game) sendGameOver(eg *GameOver) {
|
||||
log.Printf("sending out game over message: %+v", eg)
|
||||
for p := range g.players {
|
||||
p.send <- eg
|
||||
@@ -373,7 +376,7 @@ func (g *game) sendGameOver(eg *GameOver) {
|
||||
|
||||
// returns a GameParam object popuplated by info from the game. This is
|
||||
// used during client/server initial negociation.
|
||||
func (g *game) gameParam() *GameParam {
|
||||
func (g *Game) gameParam() *GameParam {
|
||||
return &GameParam{
|
||||
BoardSize: BoardSize{
|
||||
Width: g.width,
|
||||
|
||||
Reference in New Issue
Block a user