berlin-ai 0.0.6 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/ai/game.rb CHANGED
@@ -2,7 +2,7 @@ module Berlin
2
2
  module AI
3
3
  # Game keeps track of current games played by the server, indexing them on their uniq id.
4
4
  class Game
5
- attr_reader :id
5
+ attr_reader :id, :map
6
6
 
7
7
  # Keep track of all current games
8
8
  @@games = {}
@@ -16,10 +16,15 @@ module Berlin
16
16
  map = JSON.parse( map )
17
17
  state = JSON.parse( state )
18
18
 
19
- # Then, let's see if we can find that game. If not, register it.
19
+ # Game id
20
20
  game_id = infos['game_id']
21
- @@games[game_id] ||= Berlin::AI::Game.new game_id, map, infos
22
- game = @@games[game_id]
21
+
22
+ # Then, let's see if we can find that game. If not, register it.
23
+ if action == "ping"
24
+ game = Berlin::AI::Game.new game_id, map, infos
25
+ else
26
+ game = (@@games[game_id] ||= Berlin::AI::Game.new( game_id, map, infos ))
27
+ end
23
28
 
24
29
  if action == "game_over"
25
30
  # Release the game to avoid memory leaks
data/lib/ai/map.rb CHANGED
@@ -10,19 +10,25 @@ module Berlin
10
10
  def initialize map, infos
11
11
  @player_id = infos['player_id']
12
12
  @nodes = {}
13
+ @types = {}
13
14
  @directed = infos['directed'] || false
14
-
15
- # Let's parse json['nodes'] and register all nodes we can find.
15
+
16
+ # Node types
17
+ map['types'].each do |type|
18
+ @types[type['name']] = type
19
+ end
20
+
21
+ # Let's parse map['nodes'] and register all nodes we can find.
16
22
  # We'll keep track of them in @nodes so we can find them later.
17
23
  # At this step (Map creation...), we still don't know who possess
18
24
  # the node and how many soldiers there is. We'll get back to that later.
19
- # json['nodes'] => [{:id => STRING}, ...]
25
+ # map['nodes'] => [{:id => STRING}, ...]
20
26
  map['nodes'].each do |node|
21
- @nodes[node['id']] = Berlin::AI::Node.new node['id']
27
+ @nodes[node['id']] = Berlin::AI::Node.new node, @types[node['type']]
22
28
  end
23
29
 
24
30
  # Same thing here, with paths.
25
- # json['paths'] => [{:from => INTEGER, :to => INTEGER}, ...]
31
+ # map['paths'] => [{:from => INTEGER, :to => INTEGER}, ...]
26
32
  map['paths'].each do |path|
27
33
  @nodes[path['from']].link_to @nodes[path['to']]
28
34
 
data/lib/ai/node.rb CHANGED
@@ -4,10 +4,14 @@ module Berlin
4
4
  # We'll be able to use it in order to know if two
5
5
  # nodes are adjacent, how much points worth a node, etc.
6
6
  class Node
7
- attr_accessor :id, :player_id, :number_of_soldiers
7
+ attr_accessor :id, :player_id, :number_of_soldiers, :type
8
+ attr_reader :soldiers_per_turn, :points
8
9
 
9
- def initialize id
10
- @id = id
10
+ def initialize node, type
11
+ @id = node['id']
12
+ @type = node['type']
13
+ @points = type['points']
14
+ @soldiers_per_turn = type['number_of_soldiers']
11
15
  @number_of_soldiers = 0
12
16
  @player_id = 0
13
17
  @links = []
@@ -27,6 +31,11 @@ module Berlin
27
31
  def occupied?
28
32
  @number_of_soldiers > 0
29
33
  end
34
+
35
+ # Returns true if node owned by provided player id
36
+ def owned_by? player_id
37
+ @player_id == player_id
38
+ end
30
39
 
31
40
  # Returns a list of all adjacent nodes
32
41
  def adjacent_nodes
data/lib/berlin-ai.rb CHANGED
@@ -25,6 +25,7 @@ post '/' do
25
25
  200
26
26
  rescue Exception => e
27
27
  p e.inspect
28
+ p e.backtrace
28
29
 
29
30
  # Internal server error
30
31
  500
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 = 6
5
+ BUILD = 8
6
6
 
7
7
  VERSION = "#{MAJOR}.#{MINOR}.#{BUILD}"
8
8
  end
data/test/test_ai.rb CHANGED
@@ -3,7 +3,7 @@ require 'berlin-ai'
3
3
 
4
4
  class Berlin::AI::Game
5
5
  def turn_moves
6
- @map.controlled_nodes.map do |node|
6
+ @map.controlled_nodes.collect do |node|
7
7
  {
8
8
  :from => node.id,
9
9
  :to => node.adjacent_nodes.sample.id,
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: 19
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 6
10
- version: 0.0.6
9
+ - 8
10
+ version: 0.0.8
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-20 00:00:00 -04:00
20
+ date: 2011-05-22 00:00:00 -04:00
21
21
  default_executable:
22
22
  dependencies:
23
23
  - !ruby/object:Gem::Dependency