diff --git a/bomb.go b/bomb.go index 3ce601f..9ac43ad 100644 --- a/bomb.go +++ b/bomb.go @@ -6,7 +6,6 @@ import ( ) type Bomb struct { - NopReset player *Connection origin *System target *System diff --git a/broadcast.go b/broadcast.go index d99dfc0..2d97f09 100644 --- a/broadcast.go +++ b/broadcast.go @@ -6,7 +6,6 @@ import ( ) type broadcast struct { - NopReset start time.Time origin *System dist float64 diff --git a/connection.go b/connection.go index 9565332..291b4a3 100644 --- a/connection.go +++ b/connection.go @@ -44,16 +44,6 @@ func NewConnection(conn net.Conn) *Connection { return c } -func (conn *Connection) Reset() { - *conn = Connection{ - Conn: conn.Conn, - Reader: bufio.NewReader(conn.Conn), - bombs: 1, - player: conn.player, - } - currentGame.Join(conn) -} - func (c *Connection) Login() { for { fmt.Fprintf(c, "what is your name, adventurer?\n") diff --git a/game.go b/game.go index 0ad3f4f..582b4f0 100644 --- a/game.go +++ b/game.go @@ -9,6 +9,7 @@ type Game struct { id Id start time.Time end time.Time + done chan interface{} winner string winMethod string connections map[*Connection]bool @@ -33,6 +34,7 @@ func NewGame() *Game { game := &Game{ id: NewId(), start: time.Now(), + done: make(chan interface{}), connections: make(map[*Connection]bool, 32), elems: make(map[GameElement]bool, 32), } @@ -42,6 +44,14 @@ func NewGame() *Game { for _, system := range index { game.Register(system) } + if currentGame != nil { + log_info("passing %d connections...", len(currentGame.connections)) + for conn, _ := range currentGame.connections { + log_info("moving player %s to new game", conn.PlayerName()) + currentGame.Quit(conn) + game.Join(conn) + } + } return game } @@ -76,28 +86,20 @@ func (g *Game) Quit(conn *Connection) { } func (g *Game) Win(winner *Connection, method string) { + defer close(g.done) g.end = time.Now() g.winner = winner.PlayerName() g.winMethod = method g.Store() - for conn, _ := range g.connections { - fmt.Fprintf(conn, "player %s has won by %s victory.\n", winner.PlayerName(), method) - fmt.Fprintf(conn, "starting new game ...\n") - conn.Reset() - } - - g.Reset() + log_info("player %s has won by %s victory", winner.PlayerName(), method) for conn, _ := range g.connections { - conn.Respawn() + fmt.Fprintf(conn, "player %s has won by %s victory.\n", winner.PlayerName(), method) } } func (g *Game) Reset() { - for elem, _ := range g.elems { - elem.Reset() - } connections := g.connections fresh := NewGame() *g = *fresh @@ -110,6 +112,11 @@ func (g *Game) Run() { select { case <-ticker: g.tick() + case <-g.done: + for conn, _ := range g.connections { + conn.Close() + } + return } } } @@ -134,11 +141,4 @@ func (g *Game) tick() { type GameElement interface { Tick(frame int64) Dead() bool - Reset() -} - -type NopReset struct { -} - -func (n NopReset) Reset() { } diff --git a/main.go b/main.go index 9ef9287..21318cb 100644 --- a/main.go +++ b/main.go @@ -103,8 +103,13 @@ func main() { bail(E_No_Port, "unable to start server: %v", err) } - currentGame = NewGame() - go currentGame.Run() + go func() { + for { + log_info("starting new game") + currentGame = NewGame() + currentGame.Run() + } + }() for { conn, err := listener.Accept() diff --git a/scan.go b/scan.go index 4a6f7f1..7ed0922 100644 --- a/scan.go +++ b/scan.go @@ -6,7 +6,6 @@ import ( ) type scan struct { - NopReset start time.Time origin *System dist float64