Skip to content

Commit

Permalink
Refactoring round funcionality
Browse files Browse the repository at this point in the history
  • Loading branch information
rmorali committed Jun 12, 2011
1 parent bdf0a55 commit 37651a4
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 36 deletions.
2 changes: 1 addition & 1 deletion app/controllers/squads_controller.rb
Expand Up @@ -14,7 +14,7 @@ def create
end

def ready
current_squad.ready
current_squad.ready!
redirect_to :fleets
end

Expand Down
53 changes: 37 additions & 16 deletions app/models/round.rb
Expand Up @@ -17,40 +17,30 @@ def new_game!

def end_moving!
Squad.all.each do |squad|
squad.move = nil
squad.ready = nil
squad.save
end
moving_fleets = Fleet.where(:moving => true)
moving_fleets.each do |fleet|
fleet.move!
end
GenericFleet.all.each do |fleet|
Result.create(:generic_fleet_id => fleet.id, :planet => fleet.planet, :quantity => fleet.quantity, :generic_unit_id => fleet.generic_unit.id, :round => self, :squad => fleet.squad)
end
self.move_fleets
self.check_conflicts
self.move = nil
self.attack = true
save
set_map
end

def end_round!
Result.where(:round => self).each do |result|
result.blast! unless result.blasted == nil || result.blasted <= 0
result.capture! unless result.captured == nil || result.captured <= 0
result.flee! unless result.fled == nil || result.fled <= 0
end
self.apply_results
self.attack = nil
self.done = true
save
new_round_number = self.number + 1
Round.create(:number => new_round_number, :move => true)
Round.create(:number => self.number + 1, :move => true)
set_map
Squad.all.each do |squad|
squad.generate_profits!
squad.facility_fleets.each do |facility|
facility.produce!
end
squad.move = nil
squad.ready = nil
squad.save
end
end
Expand All @@ -61,4 +51,35 @@ def set_map
planet.set_ground_ownership
end
end

def check_state
squads_not_ready = Squad.where(:ready => nil)
if Round.getInstance.move?
Round.getInstance.end_moving! if squads_not_ready.empty?
else
Round.getInstance.end_round! if squads_not_ready.empty?
end
end

def move_fleets
moving_fleets = Fleet.where(:moving => true)
moving_fleets.each do |fleet|
fleet.move!
end
end

def check_conflicts #TODO criar os post results somente em planetas que tiverem conflito (2 squads no mesmo planeta)
GenericFleet.all.each do |fleet|
Result.create(:generic_fleet_id => fleet.id, :planet => fleet.planet, :quantity => fleet.quantity, :generic_unit_id => fleet.generic_unit.id, :round => self, :squad => fleet.squad)
end
end

def apply_results
Result.where(:round => self).each do |result|
result.blast! unless result.blasted == nil || result.blasted <= 0
result.capture! unless result.captured == nil || result.captured <= 0
result.flee! unless result.fled == nil || result.fled <= 0
end
end

end
19 changes: 3 additions & 16 deletions app/models/squad.rb
Expand Up @@ -91,23 +91,10 @@ def deposit quantity
save
end

def ready
self.move = true
def ready!
self.ready = true
save
if Round.getInstance.move == true
squads_not_ready = Squad.where(:move => nil)
if squads_not_ready.empty?
Round.getInstance.end_moving!
end
else
squads_not_ready = Squad.where(:move => nil)
if squads_not_ready.empty?
Round.getInstance.end_round!
end
end
Round.getInstance.check_state
end




end
2 changes: 1 addition & 1 deletion app/views/generic_fleets/index.html.erb
Expand Up @@ -2,7 +2,7 @@
<span style=color:#<%= @squad.color %>;font-weight:bold;font-size:18px><%= @squad.name %></span><br>
<span style=font-weight:bold;font-size:14px> Round: <%= @round.number %> | Credits: $<%= @squad.credits %> | Income: $<%= @income %></span><br><br>
<%= link_to "Is #{@squad.name} ready to go? ", :ready %>
<%= "Yes, we're waiting!" if @squad.move? %>
<%= "Yes, we're waiting!" if @squad.ready? %>
</div>

<div class="planet_content">
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20110612042651_rename_move_to_ready_to_squad.rb
@@ -0,0 +1,9 @@
class RenameMoveToReadyToSquad < ActiveRecord::Migration
def self.up
rename_column :squads, :move, :ready
end

def self.down
rename_column :squads, :ready, :move
end
end
4 changes: 2 additions & 2 deletions db/schema.rb
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20110522203126) do
ActiveRecord::Schema.define(:version => 20110612042651) do

create_table "generic_fleets", :force => true do |t|
t.integer "squad_id"
Expand Down Expand Up @@ -101,7 +101,7 @@
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
t.boolean "move"
t.boolean "ready"
t.string "color"
t.integer "faction"
end
Expand Down

0 comments on commit 37651a4

Please sign in to comment.