Mission time left methods and mission end conditions fix

James Peret 10 years ago
parent
commit
fad6317552

+ 2 - 1
app/helpers/missions_helper.rb

@@ -33,8 +33,9 @@ module MissionsHelper
33 33
   
34 34
   def mission_time_left(mission, position = 'pull-left')
35 35
     content_tag(:div, class: 'mission-counter mission-time-left '+ position) do
36
-      content_tag(:div, mission.days_left.html_safe) + content_tag(:div, (t 'mission.days_left'))
36
+      content_tag(:div, mission.time_left_stats[0].html_safe) + content_tag(:div, mission.time_left_stats[1].html_safe)
37 37
     end
38 38
   end
39 39
   
40
+  
40 41
 end

+ 52 - 15
app/models/mission.rb

@@ -13,7 +13,7 @@ class Mission < ActiveRecord::Base
13 13
   process_in_background :cover_img
14 14
   
15 15
   after_initialize do |user|
16
-      check_for_completion
16
+      check_for_time_left
17 17
   end
18 18
   
19 19
   def agent_count
@@ -99,6 +99,14 @@ class Mission < ActiveRecord::Base
99 99
     end
100 100
   end
101 101
   
102
+  def check_for_time_left
103
+    if self.status == 2 && self.end_date != nil
104
+      if self.seconds_left <= 0
105
+        self.check_for_completion
106
+      end
107
+    end
108
+  end
109
+  
102 110
   def check_for_completion
103 111
     if self.status == 1 || self.status == 2
104 112
       if seconds_left > 0
@@ -119,20 +127,6 @@ class Mission < ActiveRecord::Base
119 127
     self.update(status: 4)
120 128
   end
121 129
   
122
-  def days_left
123
-    if self.status == 3 || self.status == 4
124
-      return '0'
125
-    elsif self.status == 1 && self.end_date == nil
126
-      return '?'
127
-    elsif self.end_date
128
-      time_dif = self.end_date - Time.now
129
-      days_left = time_dif / 60 / 60 / 24
130
-      return days_left.round.to_s
131
-    else
132
-      return '&infin;'
133
-    end
134
-  end
135
-  
136 130
   def seconds_left
137 131
     if self.end_date
138 132
       seconds_left = self.end_date - Time.now
@@ -142,4 +136,47 @@ class Mission < ActiveRecord::Base
142 136
     end
143 137
   end
144 138
   
139
+  def time_left
140
+    array = self.time_left_stats
141
+    return "#{array[0]} #{array[1]}"
142
+  end
143
+  
144
+  def time_left_stats
145
+    if self.status == 3 || self.status == 4
146
+      time = '0'
147
+      time_scale = (I18n.t 'time.seconds')
148
+    elsif self.status == 1 && self.end_date == nil
149
+      time = '?'
150
+    elsif self.end_date
151
+      days_left = self.seconds_left / 60 / 60 / 24
152
+      if days_left >= 2
153
+        time = days_left.round.to_s
154
+        time_scale =  (I18n.t 'time.days')
155
+      elsif days_left < 2
156
+        hours_left = self.seconds_left / 60 / 60
157
+        if hours_left >= 2
158
+          time = hours_left.round.to_s
159
+          time_scale =  (I18n.t 'time.hours')
160
+        elsif hours_left < 2
161
+          minutes_left = self.seconds_left / 60
162
+          if minutes_left > 3
163
+            time = minutes_left.round.to_s
164
+            time_scale =  (I18n.t 'time.minutes')
165
+          elsif minutes_left <= 3
166
+            time = self.seconds_left.round.to_s
167
+            time_scale =  (I18n.t 'time.seconds')
168
+          end
169
+        end
170
+      end 
171
+    else
172
+      time =  '&infin;'
173
+      time_scale = ''
174
+    end
175
+    if time_scale != ''
176
+      time_scale = (time_scale.to_s + ' ' + (I18n.t 'time.remaining'))
177
+    end
178
+    array = [time, time_scale]
179
+    return array
180
+  end
181
+  
145 182
 end

+ 2 - 2
app/views/missions/_mission_details.html.erb

@@ -37,9 +37,9 @@
37 37
 				<% end %>
38 38
 			<% end %>
39 39
 			<%= content_tag(:div, class: 'span4 mission-stats') do %>
40
-				<%= content_tag(:span, mission.days_left.html_safe, class: 'mission-timer timer-large') %>
40
+				<%= content_tag(:span, mission.time_left_stats[0].html_safe, class: 'mission-timer timer-large') %>
41 41
 				<%= content_tag(:br) %>
42
-				<%= content_tag(:div, (t 'mission.days_left'), class: 'timer-legend') %>
42
+				<%= content_tag(:div, mission.time_left_stats[1].html_safe, class: 'timer-legend') %>
43 43
 				
44 44
 				<% # Progress bar %>
45 45
 				<%= render :partial => 'mission_percentage_bar', locals: { mission: mission, size: 'progress-bar-mission-stats' } %>

+ 16 - 1
config/locales/en.yml

@@ -79,6 +79,9 @@ en:
79 79
       x_days:
80 80
         one: 1 day
81 81
         other: ! '%{count} days'
82
+      x_hours:
83
+        one: 1 hour
84
+        other: ! '%{count} hours'
82 85
       x_minutes:
83 86
         one: 1 minute
84 87
         other: ! '%{count} minutes'
@@ -308,4 +311,16 @@ en:
308 311
     export: Export CVS
309 312
     success: 'Thanks for subscribing to our newsletter!'
310 313
     failure: 'An error ocured! Please try again later.'
311
-    invalid: 'The email you entered is not valid.'
314
+    invalid: 'The email you entered is not valid.'
315
+  time:
316
+    remaining: remaining
317
+    year: Year
318
+    years: Years
319
+    day: Day
320
+    days: Days
321
+    hour: Hour
322
+    hours: Hours
323
+    minute: Minute
324
+    minutes: Minutes
325
+    second: Second
326
+    seconds: Seconds

+ 16 - 1
config/locales/pt-BR.yml

@@ -79,6 +79,9 @@ pt-BR:
79 79
       x_days:
80 80
         one: 1 dia
81 81
         other: ! '%{count} dias'
82
+      x_hours:
83
+        one: 1 hora
84
+        other: ! '%{count} horas'
82 85
       x_minutes:
83 86
         one: 1 minuto
84 87
         other: ! '%{count} minutos'
@@ -312,4 +315,16 @@ pt-BR:
312 315
     export: Exportart CVS
313 316
     success: 'Obrigado por se cadastrar na nossa newsletter!'
314 317
     failure: 'Ocorreu um erro. Por favor tente de novo mais tarde.'
315
-    invalid: 'O endereço de email não é valido.'
318
+    invalid: 'O endereço de email não é valido.'
319
+  time:
320
+    remaining: restantes
321
+    year: Ano
322
+    years: Anos
323
+    day: Dia
324
+    days: Dias
325
+    hour: Hora
326
+    hours: Horas
327
+    minute: Minuto
328
+    minutes: Minutos
329
+    second: Segundos
330
+    seconds: Segundos