|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +# This script is used to find and delete level-specific custom blocks that also |
| 4 | +# exist in the block pool. |
| 5 | + |
| 6 | +require_relative '../../dashboard/config/environment' |
| 7 | + |
| 8 | +def diff(block1, block2) |
| 9 | + block1.dup. |
| 10 | + delete_if {|k, v| block2[k] == v}. |
| 11 | + merge!(block2.dup.delete_if {|k, _| block1.key?(k)}) |
| 12 | +end |
| 13 | + |
| 14 | +def get_name(block) |
| 15 | + block['name'] || block['func'] |
| 16 | +end |
| 17 | + |
| 18 | +def format_custom_blocks(blocks) |
| 19 | + # You want a hack? I'll give you hack |
| 20 | + File.write('/tmp/blocks-formatter.js', "console.log(JSON.stringify(#{JSON.pretty_generate blocks}, null, 2))") |
| 21 | + `node /tmp/blocks-formatter.js` |
| 22 | +end |
| 23 | + |
| 24 | +def remove_block(level, block_name) |
| 25 | + blocks = JSON.parse(level.custom_blocks) |
| 26 | + blocks.reject! {|block| block_name == get_name(block)} |
| 27 | + level.custom_blocks = format_custom_blocks(blocks) |
| 28 | + level.save! |
| 29 | +end |
| 30 | + |
| 31 | +Block.all.each do |shared_block| |
| 32 | + config = JSON.parse(shared_block.config) |
| 33 | + shared_name = get_name(config) |
| 34 | + config.delete('color') |
| 35 | + similar_blocks = [config] |
| 36 | + |
| 37 | + GamelabJr.all.each do |level| |
| 38 | + next unless level.custom_blocks |
| 39 | + blocks = JSON.parse level.custom_blocks |
| 40 | + blocks.each do |block| |
| 41 | + block.delete('color') |
| 42 | + name = block['name'] || block['func'] |
| 43 | + next unless name == shared_name |
| 44 | + if level.script_levels.empty? && level.name != 'New Sprite Lab Project' |
| 45 | + remove_block(level, name) |
| 46 | + next |
| 47 | + end |
| 48 | + puts |
| 49 | + puts "Found a gamelab_#{name} block in #{level.name}" |
| 50 | + if similar_blocks.include? block |
| 51 | + puts "It's the same, deleting..." |
| 52 | + remove_block(level, name) |
| 53 | + next |
| 54 | + end |
| 55 | + puts "It's a different block. These fields changed in the level block:" |
| 56 | + puts JSON.pretty_generate(diff(block, config).sort.to_h) |
| 57 | + puts "Versus these values in the shared block:" |
| 58 | + puts JSON.pretty_generate(diff(config, block).sort.to_h) |
| 59 | + $stdin.flush |
| 60 | + puts "should I delete it anyway? Y/n" |
| 61 | + next if gets.downcase.starts_with? 'n' |
| 62 | + remove_block(level, name) |
| 63 | + similar_blocks << block |
| 64 | + end |
| 65 | + rescue => e |
| 66 | + puts "Failed on #{level.name}, #{e}" |
| 67 | + end |
| 68 | +end |
0 commit comments