// Broadcasts state change of an entity to all interested parties
func (cp *ClientPool) Broadcast(entity entities.Entity) {
	defer func() {
		if panicked := recover(); panicked != nil {
			return
		}
	}()
	conn := db.Pool.Get()
	defer conn.Close()

	members, err := db.Smembers(conn, entity.AreaSet())
	if err != nil {
		log.Printf("SMEMBERS of %s: %s", entity.AreaSet(), err)
		return
	}

	for _, member := range members {
		if !strings.HasPrefix(member, "player.") {
			continue
		}
		player := strings.SplitN(member, ".", 2)[1]
		clients, ok := cp.pool[player]
		if !ok {
			continue
		}

		for element := clients.Front(); element != nil; element = element.Next() {
			client := element.Value.(*Client)
			if _, in := client.areas[entity.AreaSet()]; in {
				client.pushStateChange(entity)
			}
		}
	}
}
Esempio n. 2
0
// Get and serialize all members of a set
func GetAreasMembers(areas []string) []Entity {
	conn := db.Pool.Get()
	defer conn.Close()

	keys := []string{}
	entityList := []Entity{}

	for _, area := range areas {
		result, err := db.Smembers(conn, area)
		if err != nil {
			continue
		}
		keys = append(keys, result...)
	}

	for _, key := range keys {
		record, err := db.Get(conn, key)
		if err != nil {
			continue
		}

		entityList = append(entityList, Load(key, record))
	}

	return entityList
}