// Send takes a message with parameters, adds a prompt and sends the message on // it's way to the client. Send is modelled after the fmt.Sprintf function and // takes a format string and parameters in the same way. In addition the current // prompt is added to the end of the message. // // If the format string is empty we can take a shortcut and just redisplay the // prompt. Otherwise we process the whole enchilada making sure the prompt is on // a new line when displayed. // // NOTE: Send can be called by multiple goroutines. func (c *Client) Send(format string, any ...interface{}) { if c.isBailing() { return } data := text.COLOR_WHITE + format + c.prompt if len(any) > 0 { data = fmt.Sprintf(data, any...) } // NOTE: You need to colorize THEN fold so fold counts the length of color // codes and NOT color names ;) data = text.Fold(text.Colorize(data), TERM_WIDTH) data = strings.Replace(data, "\n", "\r\n", -1) c.conn.SetWriteDeadline(time.Now().Add(1 * time.Minute)) dat := []byte(data) for len(dat) > 0 { if w, err := c.conn.Write(dat); err != nil { c.bailing(err) return } else { dat = dat[w:] } } return }
// Broadcast implements the broadcaster interface and sends a message to all // players currently on the server. The omit parameter specifies things not to // send the message to. For example if we had a scream command we might send a // message to everyone else: // // You hear someone scream. // // The message you would see might be: // // You scream! // // However you would not want the 'You hear someone scream.' message sent to // yourself. // // Note: We are sending directly to players which is OK and needs no locking or // synchronisation here apart from the playerList itself. func (l *playerList) Broadcast(omit []thing.Interface, format string, any ...interface{}) { l.lock() defer l.unlock() msg := text.Colorize(fmt.Sprintf("\n"+format, any...)) for _, p := range l.nonLockingList(omit...) { p.Respond(msg) } }
// Broadcast sends a message to all responders at this location. This // implements the broadcast.Interface - see that for more details. func (b *Basic) Broadcast(omit []thing.Interface, format string, any ...interface{}) { msg := text.Colorize(fmt.Sprintf("\n"+format, any...)) for _, item := range b.Inventory.List(omit...) { switch messenger := item.(type) { case messaging.Responder: messenger.Respond(msg) case messaging.Broadcaster: messenger.Broadcast(omit, format, any...) } } }