From 0d37b9c98ba3a468d26282d216a075c625642f65 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Tue, 11 Nov 2014 21:49:06 -0500 Subject: [PATCH] reimplemented message broadcast --- broadcast.go | 41 +++++++++++++++++++++++++++++++++++++++++ commands.go | 14 +++----------- system.go | 7 ------- 3 files changed, 44 insertions(+), 18 deletions(-) create mode 100644 broadcast.go diff --git a/broadcast.go b/broadcast.go new file mode 100644 index 0000000..2d97f09 --- /dev/null +++ b/broadcast.go @@ -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) +} diff --git a/commands.go b/commands.go index 3057f0c..ab1e60f 100644 --- a/commands.go +++ b/commands.go @@ -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) }, } diff --git a/system.go b/system.go index 73aea12..35457e6 100644 --- a/system.go +++ b/system.go @@ -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) - }) -}