Class: Alchemist::Avatar

Inherits:
Object show all
Includes:
Record
Defined in:
lib/alchemist-server/avatar.rb

Constant Summary collapse

INVENTORY_SIZE =
64

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Record

#attr_names, hamsterize, included, #initialize, #read, #update

Class Method Details

.parse(string) ⇒ Object



75
76
77
78
79
# File 'lib/alchemist-server/avatar.rb', line 75

def self.parse(string)
  if string =~ /^⚲ (.*)$/
    new *$1.split(" ")
  end
end

Instance Method Details

#==(other) ⇒ Object



62
63
64
65
# File 'lib/alchemist-server/avatar.rb', line 62

def ==(other)
  other.class == self.class &&
  other.name == name
end

#add_to_inventory(additions) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/alchemist-server/avatar.rb', line 22

def add_to_inventory(additions)
  a = update inventory: (inventory + additions).gsub(' ','')

  if a.inventory.length > INVENTORY_SIZE
    raise "You can only carry #{INVENTORY_SIZE} items"
  end

  a
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/alchemist-server/avatar.rb', line 67

def eql?(other)
  self == other
end

#has?(*items) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
# File 'lib/alchemist-server/avatar.rb', line 52

def has?(*items)
  items.all? do |i|
    inventory.include? i
  end
end

#hashObject



58
59
60
# File 'lib/alchemist-server/avatar.rb', line 58

def hash
  name.hash
end

#locationObject



14
15
16
# File 'lib/alchemist-server/avatar.rb', line 14

def location
  Geography::Loc.new x, y
end

#message_listObject



48
49
50
# File 'lib/alchemist-server/avatar.rb', line 48

def message_list
  messages.keys.sort.map { |k| messages[k] }
end

#move(direction) ⇒ Object



9
10
11
12
# File 'lib/alchemist-server/avatar.rb', line 9

def move(direction)
  new_x, new_y = direction.move_from(x, y)
  update x: new_x, y: new_y
end

#near?(location, range) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/alchemist-server/avatar.rb', line 18

def near?(location, range)
  location.distance(self.location) <= range
end

#put_message(key, message) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/alchemist-server/avatar.rb', line 40

def put_message(key, message)
  if key.to_s =~ /^[0-9]$/
    update messages: messages.put(key.to_s.to_i, message)
  else
    raise "Only 0 through 9 may be used as message keys"
  end
end

#remove_from_inventory(*removals) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/alchemist-server/avatar.rb', line 32

def remove_from_inventory(*removals)
  remover = removals.join('').split('').reduce(-> x { x }) do |r, c|
    -> x { r[x].sub(c,'') }
  end

  update inventory: remover[inventory]
end

#to_sObject



71
72
73
# File 'lib/alchemist-server/avatar.rb', line 71

def to_s
  "#{name} #{x} #{y} #{inventory}"
end