-
Notifications
You must be signed in to change notification settings - Fork 10
/
vindinium.rb
94 lines (69 loc) · 1.67 KB
/
vindinium.rb
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
92
93
94
class Vindinium
attr_accessor :games, :turns, :mode, :key, :server, :state, :http_client, :error, :bot, :debug
def initialize opts
puts "Opts: #{opts}"
opts.each_pair do |k,v|
self.send("#{k}=", v)
end
self.http_client = HTTPClient.new
self.http_client.debug_dev=$stdout if self.debug
self.server = "http://vindinium.org" if not self.server
end
def start
if arena?
puts "Connected and waiting for other players..."
end
self.state = create_new_game
if self.error
puts "OOOPS -- couldn't start a new game!"
puts self.error
return
else
puts "New Game started at:\n#{self.state['viewUrl']}"
end
until finished? do
print "."
# move somewhere
url = self.state['playUrl']
direction = bot.move self.state
self.state = self.move(url, direction)
end
end
def move url, direction
process_http_response self.http_client.post(url, {'dir' => direction})
end
def create_new_game
params = {key: self.key}
if training?
params[:turns] = self.turns
params[:map] = 'm1'
end
process_http_response http_client.post("#{self.server}/api/#{self.mode}", params)
end
def barf
ap self.state
end
def process_http_response response
if response.status == 200
success_json response.body
else
self.error = response.body
error_json
end
end
def success_json stuff
JSON.parse stuff
end
def error_json
{'error' => self.error}
end
def finished?
state['game']['finished']
end
def arena?
self.mode == 'arena'
end
def training?
self.mode != 'arena'
end
end