@@ -30,40 +30,16 @@ class Script < ActiveRecord::Base
30
30
FLAPPY_NAME = 'flappy'
31
31
TWENTY_HOUR_NAME = '20-hour'
32
32
33
- def self . twenty_hour_script
34
- @@twenty_hour_script ||= Script . includes ( script_levels : { level : [ :game , :concepts ] } ) . find ( TWENTY_HOUR_ID )
35
- end
36
-
37
- def self . frozen_script
38
- @@frozen_script ||= Script . includes ( [ { script_levels : [ { level : [ :game , :concepts ] } , :stage ] } , :stages ] ) . find_by_name ( FROZEN_NAME )
39
- end
40
-
41
- def self . hoc_script
42
- @@hoc_script ||= Script . includes ( [ { script_levels : [ { level : [ :game , :concepts ] } , :stage ] } , :stages ] ) . find_by_name ( HOC_NAME )
43
- end
44
-
45
- def self . flappy_script
46
- @@flappy_script ||= Script . includes ( [ { script_levels : [ { level : [ :game , :concepts ] } , :stage ] } , :stages ] ) . find ( FLAPPY_ID )
47
- end
48
-
49
- def self . playlab_script
50
- @@playlab_script ||= Script . includes ( [ { script_levels : [ { level : [ :game , :concepts ] } , :stage ] } , :stages ] ) . find_by_name ( PLAYLAB_NAME )
51
- end
52
-
53
- def cached?
54
- self . equal? ( Script . twenty_hour_script ) ||
55
- self . equal? ( Script . frozen_script ) ||
56
- self . equal? ( Script . hoc_script ) ||
57
- self . equal? ( Script . flappy_script ) ||
58
- self . equal? ( Script . playlab_script )
33
+ def Script . twenty_hour_script
34
+ Script . get_from_cache ( Script ::TWENTY_HOUR_ID )
59
35
end
60
36
61
37
def Script . should_be_cached? ( id )
62
38
Script . script_cache . has_key? ( id . to_s )
63
39
end
64
40
65
41
def should_be_cached?
66
- Script . script_cache . has_key ?( id . to_s )
42
+ Script . should_be_cached ?( id . to_s )
67
43
end
68
44
69
45
def starting_level
@@ -90,10 +66,13 @@ def self.script_cache_to_redis
90
66
91
67
def self . script_cache_from_db
92
68
{ } . tap do |cache |
93
- [ twenty_hour_script , frozen_script , hoc_script , flappy_script , playlab_script ] . each do |script |
94
- cache [ script . name ] = script
95
- cache [ script . id . to_s ] = script
96
- end
69
+ [ FLAPPY_ID , TWENTY_HOUR_ID , PLAYLAB_NAME , HOC_NAME , FROZEN_NAME ] . each do |id |
70
+ find_by = ( id . to_i . to_s == id . to_s ) ? :id : :name
71
+ script = Script . includes ( [ { script_levels : [ { level : [ :game , :concepts ] } , :stage ] } , :stages ] ) .
72
+ find_by ( find_by => id )
73
+ cache [ script . name ] = script
74
+ cache [ script . id . to_s ] = script
75
+ end
97
76
end
98
77
end
99
78
@@ -167,7 +146,13 @@ def get_script_level_by_id(script_level_id)
167
146
end
168
147
169
148
def get_script_level_by_stage_and_position ( stage_position , puzzle_position )
170
- self . stages . find_by! ( position : stage_position ) . script_levels . find_by! ( position : puzzle_position )
149
+ if should_be_cached?
150
+ Script . get_from_cache ( id ) . script_levels . to_a . find do |sl |
151
+ sl . stage . position == stage_position . to_i && sl . position == puzzle_position . to_i
152
+ end
153
+ else
154
+ self . stages . find_by! ( position : stage_position ) . script_levels . find_by! ( position : puzzle_position )
155
+ end
171
156
end
172
157
173
158
def get_script_level_by_chapter ( chapter )
@@ -372,11 +357,6 @@ def self.update_i18n(custom_i18n)
372
357
private
373
358
def Script . clear_cache
374
359
# only call this in a test!
375
- @@twenty_hour_script = nil
376
- @@frozen_script = nil
377
- @@hoc_script = nil
378
- @@flappy_script = nil
379
- @@playlab_script = nil
380
360
@@script_cache = nil
381
361
end
382
362
end
0 commit comments