public
Description: Ruby client for RealTimeBattle
Homepage: http://realtimebattle.sf.net/
Clone URL: git://github.com/schmidt/realtimebattle.rb.git
realtimebattle.rb / raziel.rb.robot
100755 91 lines (71 sloc) 1.356 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env ruby
 
require "logger"
 
class Raziel
attr_accessor :logger
attr_accessor :state
 
def initialize
self.logger = Logger.new(File.dirname(__FILE__) + "/raziel.rb.log")
self.state = :null
 
write "RobotOption 3 0"
write "RobotOption 1 1"
end
 
def read
input = $stdin.gets
    logger.info(input)
check_state(input)
end
 
def write(string, log = true)
$stdout.puts(string)
    $stdout.flush
end
 
def check_state(input)
old_state = state
case input
when /Initialize 1/
self.state = :initialized
when /GameStarts/
self.state = :running
when /Dead/, /GameFinishes/
self.state = :finished
when /ExitRobot/
exit
end
 
if old_state != state
logger.info("State change: #{old_state} to #{state}")
state_updated(old_state, state)
end
 
    act
end
 
def state_updated(from, to)
if from == :null and to == :initialized
init
elsif from == :initialized and to == :running
start
end
end
 
def init
write "Name razziel.rb"
write "Colour ffcc33 336699"
end
 
def start
write "Accelerate 0.5"
write "Rotate 7 3"
end
 
def fire
write "Shoot 10", false
end
 
  def act
    fire if state == :running
  end
 
def read_loop
loop do
read
end
end
end
 
$raziel = Raziel.new
 
reader = Thread.new do
$raziel.read_loop
end
 
require File.dirname(__FILE__) + "/irb/server"
 
reader.join