Class: RobotsFindKitten::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/robotsfindkitten/server.rb

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/robotsfindkitten/server.rb', line 11

def initialize
  @robots_mutex = Mutex.new
  @robots = {}
  @kitten = nil
  @nkis = []
  puts <<eos
robotsfindkitten #{RobotsFindKitten::VERSION}
By the illustrious Rob Steward (C) 2014
Based on robotfindskitten by Leonard Richardson

eos
  puts 'Server started.'
  reset
end

Instance Method Details

#join(name) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/robotsfindkitten/server.rb', line 26

def join(name)
  puts "Join attempt from #{name}"
  robot = Robot.new(self)
  @robots_mutex.synchronize do
    if @robots.has_key?(name)
      puts "Robot named #{name} already in game."
      return
    end
    @robots[name] = robot
    puts "#{name} joined successfully."
  end
  robot
end

#leave(robot) ⇒ Object



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

def leave(robot)
  @robots_mutex.synchronize do
    name = @robots.key(robot)
    @robots.delete_if {|k, v| v == robot}
    puts "#{name} left the game."
  end
end

#occupied?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/robotsfindkitten/server.rb', line 54

def occupied?(x, y)
  thing = things_internal.find do |thing|
    thing.x == x && thing.y == y
  end
  message = nil
  case thing
  when Kitten
    message = 'You found kitten! Way to go, robot!'
    reset
  when Robot
    message = 'It\'s another robot looking for kitten. Good luck, robot!'
  when NKI
    message = thing.message
  end
  message
end

#thingsObject



48
49
50
51
52
# File 'lib/robotsfindkitten/server.rb', line 48

def things
  things_internal.map do |thing|
    ThingInfo.new(thing.x, thing.y, thing.symbol)
  end
end