Esempio n. 1
0
// Process delegates commands to an inventory's items. This is most useful
// when processing commands for a location and the location cannot process the
// command it passes it on to something else that might be able to.
func (i *Inventory) Process(cmd *command.Command) (handled bool) {
	for _, thing := range i.contents {

		// Don't process the command issuer - gets very recursive!
		if thing.IsAlso(cmd.Issuer) {
			continue
		}

		if thing, ok := thing.(command.Interface); ok {
			handled = thing.Process(cmd)
			if handled {
				return true
			}
		}
	}
	return false
}
Esempio n. 2
0
// List returns a slice of thing.Interface in the Inventory, possibly with
// specific items omitted. An example of when you want to omit something is when
// a Player does something - you send a specific message to the player:
//
//  You pick up a ball.
//
// A different message is sent to any observers:
//
//  You see Diddymus pick up a ball.
//
// However when broadcasting the message to the location you want to omit the
// 'actor' who has the specific message.
//
// Note that locations implement an inventory to store what mobiles/players and
// things are present which is why this works.
func (i *Inventory) List(omit ...thing.Interface) (list []thing.Interface) {

	// Don't modify passed slice's elements
	omitted := make([]thing.Interface, len(omit))
	copy(omitted, omit)

OMIT:
	for _, thing := range i.contents {
		for i, o := range omitted {
			if thing.IsAlso(o) {
				// Whittle down omitted so there is less to check each time
				omitted = append(omitted[:i], omitted[i+1:]...)
				continue OMIT
			}
		}
		list = append(list, thing)
	}

	return
}