messaging

pull/5/head
Jordan Orelli 10 years ago
parent a92320cdbc
commit 7e89dc91e2

@ -45,6 +45,7 @@ func handleConnection(conn *Connection) {
log_error("player %s failed to get random planet: %v", conn.PlayerName(), err) log_error("player %s failed to get random planet: %v", conn.PlayerName(), err)
return return
} }
planet.Arrive(conn)
if planet.planets == 1 { if planet.planets == 1 {
fmt.Fprintf(conn, "you are in the system %s. There is %d planet here.\n", planet.name, planet.planets) fmt.Fprintf(conn, "you are in the system %s. There is %d planet here.\n", planet.name, planet.planets)
} else { } else {
@ -68,8 +69,8 @@ func handleConnection(conn *Connection) {
if otherPlanet.name == planet.name { if otherPlanet.name == planet.name {
continue continue
} }
go func(p System) { go func(p *System) {
dist := planetDistance(*planet, p) dist := planetDistance(*planet, *p)
delay := time.Duration(int64(dist * 100000000)) delay := time.Duration(int64(dist * 100000000))
time.Sleep(delay) time.Sleep(delay)
mu.Lock() mu.Lock()
@ -78,7 +79,23 @@ func handleConnection(conn *Connection) {
}(otherPlanet) }(otherPlanet)
} }
case "broadcast": case "broadcast":
msg := strings.Join(parts[1:], " ")
log_info("player %s is broadcasting message %s", conn.PlayerName(), msg)
for _, otherSystem := range index {
if otherSystem.name == planet.name {
log_info("skpping duplicate system %s", planet.name)
continue
}
go func(s *System) {
log_info("message reached planet %s with %d inhabitants", s.name, s.NumInhabitants())
dist := planetDistance(*planet, *s) * 0.5
delay := time.Duration(int64(dist * 100000000))
time.Sleep(delay)
s.EachConn(func(conn *Connection) {
fmt.Fprintln(conn, msg)
})
}(otherSystem)
}
case "quit": case "quit":
return return
default: default:

@ -8,13 +8,43 @@ import (
) )
var ( var (
index map[int]System index map[int]*System
) )
type System struct { type System struct {
x, y, z float64 x, y, z float64
planets int planets int
name string name string
players map[*Connection]bool
}
func (s *System) Arrive(p *Connection) {
log_info("player %s has arrived at system %s", p.PlayerName(), s.name)
if s.players == nil {
s.players = make(map[*Connection]bool, 8)
}
s.players[p] = true
}
func (s *System) Leave(p *Connection) {
delete(s.players, p)
}
func (s *System) EachConn(fn func(*Connection)) {
if s.players == nil {
return
}
for conn, _ := range s.players {
fn(conn)
}
}
func (s *System) NumInhabitants() int {
if s.players == nil {
return 0
}
return len(s.players)
} }
func (e System) Store(db *sql.DB) { func (e System) Store(db *sql.DB) {
@ -53,14 +83,14 @@ func planetDistance(p1, p2 System) float64 {
return dist3d(p1.x, p1.y, p1.z, p2.x, p2.y, p2.z) return dist3d(p1.x, p1.y, p1.z, p2.x, p2.y, p2.z)
} }
func indexPlanets(db *sql.DB) map[int]System { func indexPlanets(db *sql.DB) map[int]*System {
rows, err := db.Query(`select * from planets`) rows, err := db.Query(`select * from planets`)
if err != nil { if err != nil {
log_error("unable to select all planets: %v", err) log_error("unable to select all planets: %v", err)
return nil return nil
} }
defer rows.Close() defer rows.Close()
index = make(map[int]System, 551) index = make(map[int]*System, 551)
for rows.Next() { for rows.Next() {
var id int var id int
p := System{} p := System{}
@ -68,7 +98,7 @@ func indexPlanets(db *sql.DB) map[int]System {
log_info("unable to scan planet row: %v", err) log_info("unable to scan planet row: %v", err)
continue continue
} }
index[id] = p index[id] = &p
} }
return index return index
} }
@ -81,5 +111,5 @@ func randomPlanet() (*System, error) {
pick := rand.Intn(n) pick := rand.Intn(n)
planet := index[pick] planet := index[pick]
return &planet, nil return planet, nil
} }

Loading…
Cancel
Save