/
levels_controller.rb
292 lines (260 loc) · 8.89 KB
/
levels_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
require "csv"
require "naturally"
EMPTY_XML = '<xml></xml>'.freeze
class LevelsController < ApplicationController
include LevelsHelper
include ActiveSupport::Inflector
before_action :authenticate_user!, except: [:show, :embed_blocks, :embed_level]
before_action :require_levelbuilder_mode, except: [:show, :index, :embed_blocks, :embed_level]
load_and_authorize_resource except: [:create, :update_blocks, :edit_blocks, :embed_blocks, :embed_level]
check_authorization
before_action :set_level, only: [:show, :edit, :update, :destroy]
LEVELS_PER_PAGE = 100
# GET /levels
# GET /levels.json
def index
levels = Level.order(updated_at: :desc)
levels = levels.where('name LIKE ?', "%#{params[:name]}%") if params[:name]
@levels = levels.page(params[:page]).per(LEVELS_PER_PAGE)
end
# GET /levels/1
# GET /levels/1.json
def show
if @level.try(:pages)
@pages = @level.pages
@total_level_count = @level.levels.length
end
view_options(
full_width: true,
small_footer: @game.uses_small_footer? || @level.enable_scrolling?,
has_i18n: @game.has_i18n?
)
end
# GET /levels/1/edit
def edit
end
# Action for using blockly workspace as a toolbox/startblock editor.
# Expects params[:type] which can be either 'toolbox_blocks' or 'start_blocks'
def edit_blocks
@level = Level.find(params[:level_id])
authorize! :edit, @level
type = params[:type]
blocks_xml = @level.properties[type].presence || @level[type] || EMPTY_XML
blocks_xml = Blockly.convert_category_to_toolbox(blocks_xml) if type == 'toolbox_blocks'
# By default, allow levels to define their own toolboxes for all
# types
toolbox_blocks = @level.complete_toolbox(type)
# Levels which support (and have )solution blocks use those blocks
# as the toolbox for required and recommended block editors, plus
# the special "pick one" block
can_use_solution_blocks = @level.respond_to?("get_solution_blocks") &&
@level.properties['solution_blocks']
should_use_solution_blocks = type == 'required_blocks' || type == 'recommended_blocks'
if can_use_solution_blocks && should_use_solution_blocks
blocks = @level.get_solution_blocks + ["<block type=\"pick_one\"></block>"]
toolbox_blocks = "<xml>#{blocks.join('')}</xml>"
end
level_view_options(
@level.id,
start_blocks: blocks_xml,
toolbox_blocks: toolbox_blocks,
edit_blocks: type,
skip_instructions_popup: true
)
view_options(full_width: true)
@game = @level.game
@callback = level_update_blocks_path @level, type
# Ensure the simulation ends right away when the user clicks 'Run' while editing blocks
if @level.is_a? Studio
level_view_options(
@level.id,
success_condition: 'function () { return true; }'
)
end
show
render :show
end
def update_blocks
@level = Level.find(params[:level_id])
authorize! :update, @level
blocks_xml = params[:program]
type = params[:type]
blocks_xml = Blockly.convert_toolbox_to_category(blocks_xml) if type == 'toolbox_blocks'
@level.properties[type] = blocks_xml
@level.log_changes(current_user)
@level.save!
render json: {redirect: level_url(@level)}
end
# PATCH/PUT /levels/1
# PATCH/PUT /levels/1.json
def update
if level_params[:name] &&
@level.name != level_params[:name] &&
@level.name.downcase == level_params[:name].downcase
# do not allow case-only changes in the level name because that confuses git on OSX
@level.errors.add(:name, 'Cannot change only the capitalization of the level name (it confuses git on OSX)')
render json: @level.errors, status: :unprocessable_entity
return
end
@level.assign_attributes(level_params)
@level.log_changes(current_user)
if @level.save
redirect = params["redirect"] || level_url(@level, show_callouts: 1)
render json: {redirect: redirect}
else
render json: @level.errors, status: :unprocessable_entity
end
end
# POST /levels
# POST /levels.json
def create
authorize! :create, Level
type_class = level_params[:type].constantize
# Set some defaults.
params[:level][:skin] ||= type_class.skins.first if type_class <= Blockly
if type_class <= Grid
default_tile = type_class == Karel ? {"tileType": 0} : 0
start_tile = type_class == Karel ? {"tileType": 2} : 2
params[:level][:maze_data] = Array.new(8) {Array.new(8) {default_tile}}
params[:level][:maze_data][0][0] = start_tile
end
if type_class <= Studio
params[:level][:maze_data][0][0] = 16 # studio must have at least 1 actor
params[:level][:soft_buttons] = nil
params[:level][:timeout_after_when_run] = true
params[:level][:success_condition] = Studio.default_success_condition
params[:level][:failure_condition] = Studio.default_failure_condition
end
params[:level][:maze_data] = params[:level][:maze_data].to_json if type_class <= Grid
params[:user] = current_user
begin
@level = type_class.create_from_level_builder(params, level_params)
rescue ArgumentError => e
render(status: :not_acceptable, text: e.message) && return
rescue ActiveRecord::RecordInvalid => invalid
render(status: :not_acceptable, text: invalid) && return
end
render json: {redirect: edit_level_path(@level)}
end
# DELETE /levels/1
# DELETE /levels/1.json
def destroy
@level.destroy
redirect_to(params[:redirect] || levels_url)
end
def new
authorize! :create, Level
if params.key? :type
@type_class = params[:type].constantize
if @type_class == Artist
@game = Game.custom_artist
elsif @type_class <= Studio
@game = Game.custom_studio
elsif @type_class <= Calc
@game = Game.calc
elsif @type_class <= Eval
@game = Game.eval
elsif @type_class <= Applab
@game = Game.applab
elsif @type_class <= Gamelab
@game = Game.gamelab
elsif @type_class <= Maze
@game = Game.custom_maze
elsif @type_class <= DSLDefined
@game = Game.find_by(name: @type_class.to_s)
elsif @type_class == NetSim
@game = Game.netsim
elsif @type_class == Craft
@game = Game.craft
elsif @type_class == Weblab
@game = Game.weblab
elsif @type_class == CurriculumReference
@game = Game.curriculum_reference
end
@level = @type_class.new
render :edit
else
@levels = Naturally.sort_by(Level.where(user: current_user), :name)
end
end
# POST /levels/1/clone?name=new_name
def clone
if params[:name]
# Clone existing level and open edit page
old_level = Level.find(params[:level_id])
@level = old_level.dup
begin
@level.update!(name: params[:name])
rescue ArgumentError => e
render(status: :not_acceptable, text: e.message) && return
rescue ActiveRecord::RecordInvalid => invalid
render(status: :not_acceptable, text: invalid) && return
end
render json: {redirect: edit_level_url(@level)}
else
render status: :not_acceptable, text: 'New name required to clone level'
end
end
def embed_blocks
authorize! :read, :level
level = Level.find(params[:level_id])
block_type = params[:block_type]
options = {
app: level.game.app,
readonly: true,
locale: js_locale,
baseUrl: Blockly.base_url,
blocks: level.blocks_to_embed(level.properties[block_type])
}
render :embed_blocks, layout: false, locals: options
end
def embed_level
authorize! :read, :level
@level = Level.find(params[:level_id])
@game = @level.game
level_view_options(
@level.id,
embed: true,
share: false,
skip_instructions_popup: true
)
view_options(full_width: true)
render 'levels/show'
end
private
# Use callbacks to share common setup or constraints between actions.
def set_level
@level =
if params.include? :key
Level.find_by_key params[:key]
else
Level.find(params[:id])
end
@game = @level.game
end
# Never trust parameters from the scary internet, only allow the white list through.
def level_params
permitted_params = [
:name,
:notes,
:type,
:level_num,
:user,
:dsl_text,
:encrypted,
:published,
:title,
:description,
{poems: []},
{concept_ids: []},
{level_concept_difficulty_attributes: [:id] + LevelConceptDifficulty::CONCEPTS},
{soft_buttons: []},
{contained_level_names: []},
{examples: []}
]
# http://stackoverflow.com/questions/8929230/why-is-the-first-element-always-blank-in-my-rails-multi-select
params[:level][:soft_buttons].delete_if(&:empty?) if params[:level][:soft_buttons].is_a? Array
permitted_params.concat(Level.permitted_params)
params[:level].permit(permitted_params)
end
end