Mission end_time trigger

James Peret 10 年之前
父节点
当前提交
17f6e035c2
共有 3 个文件被更改,包括 56 次插入17 次删除
  1. 1 0
      app/controllers/missions_controller.rb
  2. 41 5
      app/models/mission.rb
  3. 14 12
      app/views/missions/_mission_details.html.erb

+ 1 - 0
app/controllers/missions_controller.rb

@@ -44,6 +44,7 @@ class MissionsController < ApplicationController
44 44
   # GET /missions/new
45 45
   def new
46 46
     @mission = Mission.new
47
+    @mission.owner = current_user
47 48
   end
48 49
 
49 50
   # GET /missions/1/edit

+ 41 - 5
app/models/mission.rb

@@ -12,6 +12,10 @@ class Mission < ActiveRecord::Base
12 12
   mount_uploader :cover_img, MissionCoverUploader
13 13
   process_in_background :cover_img
14 14
   
15
+  after_initialize do |user|
16
+      check_for_completion
17
+  end
18
+  
15 19
   def agent_count
16 20
      return self.mission_agents.count
17 21
   end
@@ -79,17 +83,40 @@ class Mission < ActiveRecord::Base
79 83
     end
80 84
     return false
81 85
   end
86
+
87
+  def all_steps_completed
88
+    if self.step_count > 0 
89
+      self.mission_agents.each do |agent|
90
+        agent.agent_steps.each do |step|
91
+          if step.completed == false || step.completed == nil
92
+            return false
93
+          end
94
+        end
95
+      end
96
+      return true
97
+    else
98
+      return false
99
+    end
100
+  end
82 101
   
83 102
   def check_for_completion
84
-    self.mission_agents.each do |agent|
85
-      agent.agent_steps.each do |step|
86
-        if step.completed == true || step.completed == nil
87
-          return false
103
+    if self.status == 1 || self.status == 2
104
+      if seconds_left > 0
105
+        if self.all_steps_completed
106
+          self.completed
88 107
         end
108
+      else
109
+        self.failed
89 110
       end
90 111
     end
112
+  end
113
+  
114
+  def completed
91 115
     self.update(status: 3)
92
-    return true
116
+  end
117
+  
118
+  def failed
119
+    self.update(status: 4)
93 120
   end
94 121
   
95 122
   def days_left
@@ -106,4 +133,13 @@ class Mission < ActiveRecord::Base
106 133
     end
107 134
   end
108 135
   
136
+  def seconds_left
137
+    if self.end_date
138
+      seconds_left = self.end_date - Time.now
139
+      return seconds_left
140
+    else
141
+      return 0
142
+    end
143
+  end
144
+  
109 145
 end

+ 14 - 12
app/views/missions/_mission_details.html.erb

@@ -48,20 +48,22 @@
48 48
 					<%= mission_agent_counter(mission, 'pull-left') %>
49 49
 					<%= mission_steps_counter(mission, 'pull-right') %>
50 50
 				<% end %>
51
-				<%= content_tag(:div, class: 'mission-director-stats') do %>
52
-					<%= content_tag(:div, class: 'mission-director-avatar') do %>
53
-						<% if mission.owner.avatar.file != nil %>
54
-					    		<%= image_tag mission.owner.avatar.to_s, size: "64x64", :class => 'img-circle' %>
55
-					     <% else %>
56
-					    		<%= image_tag "user.png", size: "64x64", :class => 'img-circle' %>
57
-					     <% end %>
51
+				<% if mission.owner != nil %>
52
+					<%= content_tag(:div, class: 'mission-director-stats') do %>
53
+						<%= content_tag(:div, class: 'mission-director-avatar') do %>
54
+							<% if mission.owner.avatar.file != nil %>
55
+						    		<%= image_tag mission.owner.avatar.to_s, size: "64x64", :class => 'img-circle' %>
56
+						     <% else %>
57
+						    		<%= image_tag "user.png", size: "64x64", :class => 'img-circle' %>
58
+						     <% end %>
59
+						<% end %>
60
+						<%= content_tag(:div, class: 'mission-director-text') do %>
61
+							<%= content_tag(:span, (t 'mission.director')+':' , class: 'mission-director-legend') %><br>
62
+							<%= content_tag(:span, mission.owner.full_name , class: 'mission-director-name') %>
63
+						<% end %>    
58 64
 					<% end %>
59
-					<%= content_tag(:div, class: 'mission-director-text') do %>
60
-						<%= content_tag(:span, (t 'mission.director')+':' , class: 'mission-director-legend') %><br>
61
-						<%= content_tag(:span, mission.owner.full_name , class: 'mission-director-name') %>
62
-					<% end %>    
65
+					<%= content_tag(:div, '', class: 'clearfix') %>
63 66
 				<% end %>
64
-				<%= content_tag(:div, '', class: 'clearfix') %>
65 67
 				<% # Join/Launch Buttons %>
66 68
 				<% if !@mission.is_agent(current_user) && current_user != @mission.owner %>
67 69
 					<%= content_tag(:div, class: 'mission-call-to-action') do %>