Class: ScubyWars::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/scuby_wars/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, nick) ⇒ Client

Returns a new instance of Client.



26
27
28
29
30
31
32
33
34
35
# File 'lib/scuby_wars/client.rb', line 26

def initialize(server, nick)
  @player = {:nick => nick, :actions => {:thrust => false, :turnLeft => false, :turnRight => false, :fire => false}}
  @server = server
  begin
    self.class.post("http://#{@server}/players/#{@player[:nick]}")
  rescue => e
    puts "Could not connect to game-server"
    raise e
  end
end

Instance Attribute Details

#serverObject

Returns the value of attribute server.



5
6
7
# File 'lib/scuby_wars/client.rb', line 5

def server
  @server
end

#worldObject

Returns the value of attribute world.



5
6
7
# File 'lib/scuby_wars/client.rb', line 5

def world
  @world
end

Class Method Details

.join(server, nick) ⇒ Object

Example:

player = ScubyWars::Client.join("localhost:3000", "player-name")
player.stop!
player.turn_left!
player.run!
player.fire!


14
15
16
17
18
19
20
21
22
23
24
# File 'lib/scuby_wars/client.rb', line 14

def self.join(server, nick)
  game = new(server, nick)
  if block_given?
    Yajl::HttpStream.get(URI.parse("http://#{server}/world")) do |world|
      game.world = world
      game.instance_eval(yield)
      game.update_player
    end
  end
  game
end

Instance Method Details

#cease_fireObject



92
93
94
# File 'lib/scuby_wars/client.rb', line 92

def cease_fire
  @player[:actions][:fire] = false
end

#fireObject



84
85
86
# File 'lib/scuby_wars/client.rb', line 84

def fire
  @player[:actions][:fire] = true
end

#fireing?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/scuby_wars/client.rb', line 88

def fireing?
  @player[:actions][:fire]
end

#joinObject



37
38
39
40
41
42
43
# File 'lib/scuby_wars/client.rb', line 37

def join
  Yajl::HttpStream.get(URI.parse("http://#{server}/world")) do |world|
    self.world = world
    think
    update_player
  end
end

#runObject



45
46
47
# File 'lib/scuby_wars/client.rb', line 45

def run
  @player[:actions][:thrust] = true
end

#running?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/scuby_wars/client.rb', line 49

def running?
  @player[:actions][:thrust]
end

#stopObject



53
54
55
# File 'lib/scuby_wars/client.rb', line 53

def stop
  @player[:actions][:thrust] = false
end

#stop_fireingObject



96
97
98
# File 'lib/scuby_wars/client.rb', line 96

def stop_fireing
  cease_fire
end

#stop_turningObject



79
80
81
82
# File 'lib/scuby_wars/client.rb', line 79

def stop_turning
  @player[:actions][:turnLeft] = false
  @player[:actions][:turnRight] = false
end

#turn_leftObject



57
58
59
60
# File 'lib/scuby_wars/client.rb', line 57

def turn_left
  @player[:actions][:turnLeft] = true
  @player[:actions][:turnRight] = false
end

#turn_rightObject



66
67
68
69
# File 'lib/scuby_wars/client.rb', line 66

def turn_right
  @player[:actions][:turnLeft] = false
  @player[:actions][:turnRight] = true
end

#turning?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/scuby_wars/client.rb', line 75

def turning?
  turning_left? || turning_right?
end

#turning_left?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/scuby_wars/client.rb', line 62

def turning_left?
  @player[:actions][:turnLeft]
end

#turning_right?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/scuby_wars/client.rb', line 71

def turning_right?
  @player[:actions][:turnRight]
end

#update_playerObject



107
108
109
110
# File 'lib/scuby_wars/client.rb', line 107

def update_player
  puts "Sending #{@player.inspect} to http://#{@server}/players/#{@player[:nick]}"
  self.class.put("http://#{@server}/players/#{@player[:nick]}", :body => Yajl::Encoder.encode(@player))
end