// Moves the client to another area func (c *Client) MoveToAreas(areaSlice []string) { conn := db.Pool.Get() defer conn.Close() c.mutex.Lock() defer c.mutex.Unlock() player := c.Player.Key() // Create map of the new areas in order // to search in them more easily areas := make(map[string]struct{}) for _, area := range areaSlice { areas[area] = empty } // Remove left areas for area, _ := range c.areas { if _, in := areas[area]; !in { delete(c.areas, area) db.Srem(conn, area, player) } } // Add newly occupied areas for area, _ := range areas { if _, in := c.areas[area]; !in { c.areas[area] = empty db.Sadd(conn, area, player) } } }
// Remove the client to the pool. // It is safe to remove non-existing client. func (cp *ClientPool) Remove(client *Client) { cp.mutex.Lock() defer cp.mutex.Unlock() conn := db.Pool.Get() defer conn.Close() playerInPool, ok := cp.pool[client.Player.Username] if ok { playerInPool.Remove(client.poolElement) for area, _ := range client.areas { db.Srem(conn, area, client.Player.Key()) } if playerInPool.Len() == 0 { delete(cp.pool, client.Player.Username) } } }
// Remove a member from set func RemoveFromArea(key, set string) error { conn := db.Pool.Get() defer conn.Close() return db.Srem(conn, set, key) }