Class: RobotsFindKitten::Server
- Inherits:
-
Object
- Object
- RobotsFindKitten::Server
- Defined in:
- lib/robotsfindkitten/server.rb
Instance Method Summary collapse
-
#initialize ⇒ Server
constructor
A new instance of Server.
- #join(name) ⇒ Object
- #leave(robot) ⇒ Object
- #occupied?(x, y) ⇒ Boolean
- #things ⇒ Object
Constructor Details
#initialize ⇒ Server
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
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 = nil case thing when Kitten = 'You found kitten! Way to go, robot!' reset when Robot = 'It\'s another robot looking for kitten. Good luck, robot!' when NKI = thing. end end |