maybe you can restart the game now

slack
Jordan Orelli 10 years ago
parent 95a05d843f
commit b74958efbd

@ -1,15 +1,17 @@
package main package main
import ( import (
"fmt"
"time" "time"
) )
type Game struct { type Game struct {
id Id id Id
start time.Time start time.Time
end time.Time end time.Time
winner string winner string
winMethod string winMethod string
connections map[*Connection]bool
} }
func gamesTable() { func gamesTable() {
@ -27,8 +29,9 @@ func gamesTable() {
func NewGame() *Game { func NewGame() *Game {
game := &Game{ game := &Game{
id: NewId(), id: NewId(),
start: time.Now(), start: time.Now(),
connections: make(map[*Connection]bool, 32),
} }
if err := game.Create(); err != nil { if err := game.Create(); err != nil {
log_error("%v", err) log_error("%v", err)
@ -45,3 +48,48 @@ func (g *Game) Create() error {
;`, g.id.String(), g.start) ;`, g.id.String(), g.start)
return err return err
} }
func (g *Game) Store() error {
_, err := db.Exec(`
update games
set end = ?, winner = ?, win_method = ?
where id = ?
;`, g.end, g.winner, g.winMethod, g.id)
return err
}
func (g *Game) Join(conn *Connection) {
g.connections[conn] = true
}
func (g *Game) Quit(conn *Connection) {
delete(g.connections, conn)
}
func (g *Game) Win(winner *Connection, method string) {
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()
}
ResetQueue()
g.Reset()
for conn, _ := range g.connections {
conn.Respawn()
}
}
func (g *Game) Reset() {
connections := g.connections
fresh := NewGame()
*g = *fresh
g.connections = connections
}

@ -39,17 +39,7 @@ func handleConnection(conn *Connection) {
defer conn.Close() defer conn.Close()
conn.Login() conn.Login()
system, err := randomSystem() conn.Respawn()
if err != nil {
log_error("player %s failed to get random system: %v", conn.PlayerName(), err)
return
}
system.Arrive(conn)
if system.planets == 1 {
fmt.Fprintf(conn, "you are in the system %s. There is %d planet here.\n", system.name, system.planets)
} else {
fmt.Fprintf(conn, "you are in the system %s. There are %d planets here.\n", system.name, system.planets)
}
for { for {
line, err := conn.ReadString('\n') line, err := conn.ReadString('\n')
switch err { switch err {

@ -9,8 +9,6 @@ import (
"time" "time"
) )
var connected = make(map[*Connection]bool, 32)
type Connection struct { type Connection struct {
net.Conn net.Conn
*bufio.Reader *bufio.Reader
@ -32,10 +30,19 @@ func NewConnection(conn net.Conn) *Connection {
Reader: bufio.NewReader(conn), Reader: bufio.NewReader(conn),
bombs: 1, bombs: 1,
} }
connected[c] = true currentGame.Join(c)
return c return c
} }
func (conn *Connection) Reset() {
*conn = Connection{
Conn: conn.Conn,
Reader: bufio.NewReader(conn.Conn),
bombs: 1,
}
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")
@ -79,8 +86,11 @@ func (c *Connection) System() *System {
func (c *Connection) Close() error { func (c *Connection) Close() error {
log_info("player disconnecting: %s", c.PlayerName()) log_info("player disconnecting: %s", c.PlayerName())
delete(connected, c) currentGame.Quit(c)
return c.Conn.Close() if c.Conn != nil {
return c.Conn.Close()
}
return nil
} }
func (c *Connection) PlayerName() string { func (c *Connection) PlayerName() string {
@ -168,10 +178,7 @@ func (c *Connection) Deposit(n int64) {
} }
func (c *Connection) Win(method string) { func (c *Connection) Win(method string) {
for conn, _ := range connected { currentGame.Win(c, method)
fmt.Fprintf(conn, "player %s has won.\n", c.PlayerName())
conn.Close()
}
} }
func (c *Connection) Die() { func (c *Connection) Die() {

@ -24,13 +24,18 @@ type System struct {
colonizedBy *Connection colonizedBy *Connection
} }
func (s *System) Arrive(p *Connection) { func (s *System) Arrive(conn *Connection) {
p.SetSystem(s) conn.SetSystem(s)
log_info("player %s has arrived at system %s", p.PlayerName(), s.name) log_info("player %s has arrived at system %s", conn.PlayerName(), s.name)
if s.players == nil { if s.players == nil {
s.players = make(map[*Connection]bool, 8) s.players = make(map[*Connection]bool, 8)
} }
s.players[p] = true s.players[conn] = true
if s.planets == 1 {
fmt.Fprintf(conn, "you are in the system %s. There is %d planet here.\n", s.name, s.planets)
} else {
fmt.Fprintf(conn, "you are in the system %s. There are %d planets here.\n", s.name, s.planets)
}
} }
func (s *System) Leave(p *Connection) { func (s *System) Leave(p *Connection) {

@ -2,10 +2,14 @@ package main
import ( import (
"container/heap" "container/heap"
"sync"
"time" "time"
) )
var queue = make(Queue, 0, 32) var (
queue = make(Queue, 0, 32)
queueLock sync.Mutex
)
type Future struct { type Future struct {
ts time.Time ts time.Time
@ -59,7 +63,9 @@ func RunQueue() {
time.Sleep(10 * time.Microsecond) time.Sleep(10 * time.Microsecond)
continue continue
} }
queueLock.Lock()
future, ok := heap.Pop(&queue).(*Future) future, ok := heap.Pop(&queue).(*Future)
queueLock.Unlock()
if !ok { if !ok {
log_error("there's shit on the work heap") log_error("there's shit on the work heap")
continue continue
@ -70,3 +76,12 @@ func RunQueue() {
future.work() future.work()
} }
} }
func ResetQueue() {
queueLock.Lock()
defer queueLock.Unlock()
log_info("Reseting worker queue.")
queue = make(Queue, 0, 32)
heap.Init(&queue)
}

Loading…
Cancel
Save