class RobotsFindKitten::Server
Public Class Methods
new()
click to toggle source
# 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
Public Instance Methods
join(name)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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
things()
click to toggle source
# File lib/robotsfindkitten/server.rb, line 48 def things things_internal.map do |thing| ThingInfo.new(thing.x, thing.y, thing.symbol) end end
Private Instance Methods
reset()
click to toggle source
# File lib/robotsfindkitten/server.rb, line 77 def reset @kitten = Kitten.new @nkis = (0...20).map {|i| NKI.new} puts 'Game reset.' end
things_internal()
click to toggle source
# File lib/robotsfindkitten/server.rb, line 73 def things_internal (@robots.values + [@kitten] + @nkis) end