berlin-ai 0.0.15 → 0.0.16

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/lib/berlin-ai.rb +45 -15
  2. data/lib/version.rb +1 -1
  3. data/test/test_ai.rb +3 -4
  4. metadata +4 -4
data/lib/berlin-ai.rb CHANGED
@@ -12,50 +12,80 @@ puts
12
12
  require File.expand_path( File.dirname( __FILE__ ) ) + "/ai/#{file}"
13
13
  end
14
14
 
15
+ # Sinatra options
16
+ set :app_file, $0
17
+ set :verbose, false
18
+ set :logger, false
19
+
15
20
  # Parse options
16
21
  OptionParser.new do |opts|
17
- opts.on("-p N", "--port=N", Integer, "Set running port to N") do |p|
22
+ opts.on("-h", "--help", "Display this screen" ) do
23
+ puts opts
24
+ exit
25
+ end
26
+
27
+ opts.on("-p N", "--port N", Integer, "Set running port to N") do |p|
18
28
  set :port, p
19
29
  end
20
30
 
21
- opts.on("--debug", "Run in debug mode (reloads code at each request)") do |d|
22
- if d
23
- require 'sinatra/reloader'
24
-
25
- configure do |c|
26
- c.also_reload $0
27
- end
31
+ opts.on("-d", "--debug", "Run in debug mode (reloads code at each request)") do
32
+ require 'sinatra/reloader'
33
+
34
+ configure do |c|
35
+ c.also_reload $0
28
36
  end
29
37
  end
38
+
39
+ opts.on("-l", "--log [LOGFILE]", "Create a log file for incoming requests (defaults to 'berlin.log')") do |l|
40
+ require 'logger'
41
+
42
+ set :logger, Logger.new( l || 'berlin.log' )
43
+ end
44
+
45
+ opts.on("-v", "--verbose", "Print information to STDOUT") do
46
+ enable :verbose
47
+ end
30
48
  end.parse!
31
49
 
32
- # Sinatra options
33
- set :app_file, $0
34
-
35
50
  post '/' do
36
51
  begin
37
52
  # Check if it's one of the four Berlin keywords
38
53
  if ['ping', 'turn', 'game_start', 'game_over'].include? params[:action]
54
+ log :info, "New request of type #{params[:action]} : #{params.inspect}"
55
+
39
56
  game = Berlin::AI::Game.create_or_update params[:action], params[:infos], params[:map], params[:state]
40
57
 
41
58
  if ['ping', 'turn'].include? params[:action]
42
59
  # Let the player decides his moves
43
60
  Berlin::AI::Player.on_turn( game )
44
61
 
62
+ # Get moves from AI
63
+ moves = game.moves.to_json
64
+
65
+ # Log time!
66
+ log :info, "Respond with: #{moves}"
67
+
45
68
  # Return the response to Berlin
46
- return game.moves.to_json
69
+ return moves
47
70
  end
48
71
  else
49
- p params.inspect
72
+ log :error, params.inspect
50
73
  end
51
74
 
52
75
  # For every other type of request, respond with 200 OK
53
76
  200
54
77
  rescue Exception => e
55
- p e.inspect
56
- p e.backtrace
78
+ log :fatal, "#{e.inspect}\n#{e.backtrace}"
57
79
 
58
80
  # Internal server error
59
81
  500
60
82
  end
61
83
  end
84
+
85
+ def log level, message
86
+ # verbose
87
+ puts message if settings.verbose
88
+
89
+ # logger
90
+ settings.logger.send( level, message ) if settings.logger
91
+ end
data/lib/version.rb CHANGED
@@ -2,7 +2,7 @@ module Berlin
2
2
  module AI
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
- BUILD = 15
5
+ BUILD = 16
6
6
 
7
7
  VERSION = "#{MAJOR}.#{MINOR}.#{BUILD}"
8
8
  end
data/test/test_ai.rb CHANGED
@@ -1,9 +1,8 @@
1
1
  require 'rubygems'
2
2
  require 'berlin-ai'
3
3
 
4
- class Berlin::AI::Player
5
- def self.on_turn( game )
6
- game.add_move( 1, 2, 12 )
7
- game.add_move( 3, 4, 10 )
4
+ class Berlin::AI::Player
5
+ def self.on_turn( game )
6
+ game.add_move( 1, 2, 3 )
8
7
  end
9
8
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: berlin-ai
3
3
  version: !ruby/object:Gem::Version
4
- hash: 1
4
+ hash: 63
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 15
10
- version: 0.0.15
9
+ - 16
10
+ version: 0.0.16
11
11
  platform: ruby
12
12
  authors:
13
13
  - Christian Blais
@@ -17,7 +17,7 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2011-05-26 00:00:00 -04:00
20
+ date: 2011-06-04 00:00:00 -04:00
21
21
  default_executable:
22
22
  dependencies:
23
23
  - !ruby/object:Gem::Dependency