@ -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 ( ) {
}
}