players just disconnect at game end now

this is way easier than fixing the "you just can't mine" behavior
properly.  This is a shitty patch, I'll fix it right eventually but this
at least makes it playable.
slack
Jordan Orelli 10 years ago
parent 51409c4f56
commit 4158537894

@ -6,7 +6,6 @@ import (
) )
type Bomb struct { type Bomb struct {
NopReset
player *Connection player *Connection
origin *System origin *System
target *System target *System

@ -6,7 +6,6 @@ import (
) )
type broadcast struct { type broadcast struct {
NopReset
start time.Time start time.Time
origin *System origin *System
dist float64 dist float64

@ -44,16 +44,6 @@ func NewConnection(conn net.Conn) *Connection {
return c 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() { func (c *Connection) Login() {
for { for {
fmt.Fprintf(c, "what is your name, adventurer?\n") fmt.Fprintf(c, "what is your name, adventurer?\n")

@ -9,6 +9,7 @@ type Game struct {
id Id id Id
start time.Time start time.Time
end time.Time end time.Time
done chan interface{}
winner string winner string
winMethod string winMethod string
connections map[*Connection]bool connections map[*Connection]bool
@ -33,6 +34,7 @@ func NewGame() *Game {
game := &Game{ game := &Game{
id: NewId(), id: NewId(),
start: time.Now(), start: time.Now(),
done: make(chan interface{}),
connections: make(map[*Connection]bool, 32), connections: make(map[*Connection]bool, 32),
elems: make(map[GameElement]bool, 32), elems: make(map[GameElement]bool, 32),
} }
@ -42,6 +44,14 @@ func NewGame() *Game {
for _, system := range index { for _, system := range index {
game.Register(system) 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 return game
} }
@ -76,28 +86,20 @@ func (g *Game) Quit(conn *Connection) {
} }
func (g *Game) Win(winner *Connection, method string) { func (g *Game) Win(winner *Connection, method string) {
defer close(g.done)
g.end = time.Now() g.end = time.Now()
g.winner = winner.PlayerName() g.winner = winner.PlayerName()
g.winMethod = method g.winMethod = method
g.Store() g.Store()
for conn, _ := range g.connections { log_info("player %s has won by %s victory", winner.PlayerName(), method)
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()
for conn, _ := range g.connections { 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() { func (g *Game) Reset() {
for elem, _ := range g.elems {
elem.Reset()
}
connections := g.connections connections := g.connections
fresh := NewGame() fresh := NewGame()
*g = *fresh *g = *fresh
@ -110,6 +112,11 @@ func (g *Game) Run() {
select { select {
case <-ticker: case <-ticker:
g.tick() g.tick()
case <-g.done:
for conn, _ := range g.connections {
conn.Close()
}
return
} }
} }
} }
@ -134,11 +141,4 @@ func (g *Game) tick() {
type GameElement interface { type GameElement interface {
Tick(frame int64) Tick(frame int64)
Dead() bool Dead() bool
Reset()
}
type NopReset struct {
}
func (n NopReset) Reset() {
} }

@ -103,8 +103,13 @@ func main() {
bail(E_No_Port, "unable to start server: %v", err) bail(E_No_Port, "unable to start server: %v", err)
} }
currentGame = NewGame() go func() {
go currentGame.Run() for {
log_info("starting new game")
currentGame = NewGame()
currentGame.Run()
}
}()
for { for {
conn, err := listener.Accept() conn, err := listener.Accept()

@ -6,7 +6,6 @@ import (
) )
type scan struct { type scan struct {
NopReset
start time.Time start time.Time
origin *System origin *System
dist float64 dist float64

Loading…
Cancel
Save