message

package
v0.0.18 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 11, 2021 License: BSD-2-Clause Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReleaseBuffer added in v0.0.5

func ReleaseBuffer(b *Buffer)

ReleaseBuffer puts a *Buffer back into a pool of *Buffer for reuse.

Types

type Buffer

type Buffer struct {
	// contains filtered or unexported fields
}

Buffer provides temporary storage for messages to players. The Buffer accumulates messages which can then be sent as single network writes to the players. A Buffer can handle insertion of line feeds into messages automatically when required.

While a *Buffer can be created using &Buffer{} it is a better to use calls to AcquireBuffer and ReleaseBuffer which will supply a *Buffer from a reusable pool of *Buffer and return the *Buffer to the pool for reuse.

NOTE: omitLF indicates whether an empty Buffer should start with a line feed or not. This should be true for an actor's Buffer as they would have moved to a new line when pressing enter to issue a command. For all other Buffers it should be false as we need to move them off their prompt line manually.

func AcquireBuffer added in v0.0.5

func AcquireBuffer() (b *Buffer)

AcquireBuffer returns a *Buffer from a pool of *Buffer. A *Buffer should be returned to the pool by calling ReleaseBuffer. It is not essential that ReleaseBuffer is called as the pool will replenish itself, however a *Buffer that is simply discarded cannot be reused - which avoids allocations and generating garbage.

func (*Buffer) Append

func (b *Buffer) Append(s ...string)

Append takes a number of strings and writes them into the Buffer appending to a previous message. The message is appended to the current Buffer with a leading single space. Append is useful when a message needs to be composed in several stages. Append does not normally increase the message count returned by Len, but see special cases below.

If the Buffer is in silent mode the Buffer will not be modified and the passed strings will be discarded.

Special cases:

If Append is called without an initial Send then Append will behave like a Send and also increase the message count by one.

If Append is called without an initial Send or after a Send with an empty string the leading space will be omitted. This is so that Send can cause the start a new message but text is only appended by calling Append.

func (*Buffer) Deliver

func (b *Buffer) Deliver(w ...io.Writer)

Deliver writes all of the messages in the Buffer to the passed Writers. After the messages have been delivered the messages and message count will be cleared.

func (*Buffer) Len

func (b *Buffer) Len() int

Len returns the number of messages in a Buffer.

func (*Buffer) OmitLF added in v0.0.5

func (b *Buffer) OmitLF(new bool) (old bool)

OmitLF sets a Buffer omitLF flag to true or false and returns the old omitLF setting. For details of the omitLF flag see the Buffer type.

func (*Buffer) Send

func (b *Buffer) Send(s ...string)

Send takes a number of strings and writes them into the Buffer as a single message. The message will automatically be prefixed with a line feed if required so that the message starts on its own new line when displayed to the player. Each time Send is called the message count returned by Len is increased by one.

If the Buffer is in silent mode the Buffer and message count will not be modified and the passed strings will be discarded.

func (*Buffer) SendBad added in v0.0.5

func (b *Buffer) SendBad(s ...string)

SendBad is convenient for sending a message using text.Bad as the color.

func (*Buffer) SendGood added in v0.0.5

func (b *Buffer) SendGood(s ...string)

SendGood is convenient for sending a message using text.Good as the color.

func (*Buffer) SendInfo added in v0.0.5

func (b *Buffer) SendInfo(s ...string)

SendInfo is convenient for sending a message using text.Info as the color.

func (*Buffer) Silent

func (b *Buffer) Silent(new bool) (old bool)

Silent sets a Buffer silent mode to true or false and returning the old silent mode. When a Buffer is in silent mode it will ignore calls to Send and Append.

type Msg

type Msg struct {
	Actor       *Buffer
	Participant *Buffer
	Observer    *Buffer
	Observers   buffers
}

Msg is a collection of buffers for gathering messages to send back as a result of processing a command. Before use a Msg should have Allocate called on it to allocate and setup the buffers internally. After use Deallocate should be called to free up the buffers. The Allocate and Deallocate methods are kept separate so that a Msg can be reused by repeated calls to Allocate/Deallocate.

NOTE: Observer is setup as an 'alias' for Observers[s.where] - Observer and Observers[s.where] point to the same Buffer. See the Allocate method for more details.

func (*Msg) Allocate

func (m *Msg) Allocate(where has.Inventory, locks []has.Inventory)

Allocate sets up the message buffers for the actor, participant and observers. The 'where' passed in should be the current location so that Observer can be linked to the correct Observers element. The locks passed in are used to setup a Buffer for observers in each of the locations being locked.

The actor's Buffer is initially set to half a page (half of 80 columns by 24 lines) as it is common to be sending location descriptions back to the actor. Half a page is arbitrary but seems to be reasonable.

NOTE: For crowded locations buffers of observers are automatically put in silent mode.

func (*Msg) Deallocate

func (m *Msg) Deallocate()

Deallocate releases the references to message buffers for the actor, participant and observers. Specific deallocation can help with garbage collection.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL