reimplemented message broadcast

slack
Jordan Orelli 10 years ago
parent 1d348fb280
commit 0d37b9c98b

@ -0,0 +1,41 @@
package main
import (
"fmt"
"time"
)
type broadcast struct {
start time.Time
origin *System
dist float64
nextHitIndex int
message string
}
func NewBroadcast(from *System, template string, args ...interface{}) *broadcast {
return &broadcast{
start: time.Now(),
origin: from,
message: fmt.Sprintf(template, args...),
}
}
func (b *broadcast) Tick(frame int64) {
b.dist += options.lightSpeed
for ; b.nextHitIndex < len(b.origin.Distances()); b.nextHitIndex += 1 {
candidate := b.origin.Distances()[b.nextHitIndex]
if b.dist < candidate.dist {
break
}
candidate.s.NotifyInhabitants("message received from system %s:\n\t%s\n", b.origin.Label(), b.message)
}
}
func (b *broadcast) Dead() bool {
return b.dist > b.origin.Distances()[len(b.origin.Distances())-1].dist
}
func (b *broadcast) String() string {
return fmt.Sprintf("[broadcast origin: %s message: %s]", b.origin.name, b.message)
}

@ -131,17 +131,9 @@ var broadcastCommand = &Command{
handler: func(conn *Connection, args ...string) {
msg := strings.Join(args, " ")
system := conn.System()
log_info("broadcast sent from %s: %v\n", system.name, msg)
for id, _ := range index {
if id == system.id {
continue
}
delay := system.LightTimeTo(index[id])
id2 := id
After(delay, func() {
deliverMessage(id2, system.id, msg)
})
}
b := NewBroadcast(system, msg)
log_info("player %s send broadcast from system %s: %v\n", conn.PlayerName(), system.Label(), msg)
currentGame.Register(b)
},
}

@ -247,10 +247,3 @@ func randomSystem() (*System, error) {
return planet, nil
}
func deliverMessage(to_id, from_id int, msg string) {
to := index[to_id]
from := index[from_id]
to.EachConn(func(conn *Connection) {
fmt.Fprintf(conn, "Message from %s: %s", from.name, msg)
})
}

Loading…
Cancel
Save