Skip to content

Commit 5181da6

Browse files
committed
Refactor
1 parent 0fcd35f commit 5181da6

3 files changed

Lines changed: 61 additions & 46 deletions

File tree

code.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,11 +1130,11 @@ Code.loadSimulator = function () {
11301130

11311131
// check Stage
11321132
setTimeout(function () {
1133-
var isPlay = document.getElementById('runButton').style['background-color'];
1134-
if (isPlay == "rgb(0, 170, 85)") {
1135-
Code.demo = document.getElementById("demo-frame");
1136-
Code.engine = frame.contentWindow.engine;
1137-
Code.stageCheck();
1133+
var demo, engine;
1134+
if (Code.running) {
1135+
demo = document.getElementById("demo-frame");
1136+
engine = frame.contentWindow.blockly.getEngine();
1137+
Code.stageCheck.init(demo, engine);
11381138
}
11391139
}, 1000);
11401140

simulator

Submodule simulator updated from 4833a66 to a77315e

stageCheck.js

Lines changed: 55 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,76 @@
1-
Code.stageCheck = function () {
2-
var stageName = Code.getStringParamFromUrl('page');
3-
var stage = {
4-
'stages/01': stage1,
5-
'stages/02': stage2,
6-
'stages/03': stage3,
7-
'stages/04': stage4
8-
};
9-
stage[stageName]();
1+
+(function () {
2+
"use strict";
3+
4+
var Code = window.Code;
5+
var demoDoc_, engine_;
6+
7+
function init(demo, engine) {
8+
var stageName = Code.getStringParamFromUrl('page');
9+
var stage = {
10+
'stages/01': stage1,
11+
'stages/02': stage2,
12+
'stages/03': stage3,
13+
'stages/04': stage4
14+
};
15+
16+
demoDoc_ = demo.contentDocument;
17+
engine_ = engine;
18+
stage[stageName]();
19+
}
1020

1121

1222

1323

1424
function stage1() {
15-
var htmlState = Code.demo.contentDocument.getElementById('light').className;
16-
var engineLed = Code.engine.list().led[0].value();
17-
if (engineLed == 1 && htmlState == 'on') {
25+
var htmlState = demoDoc_.getElementById('light').className;
26+
var engineLed = engine_.list().led[0].value();
27+
if (engineLed === 1 && htmlState === 'on') {
1828
alert("第一關 過關!");
1929
}
2030
}
2131

22-
23-
24-
2532
function stage2() {
26-
console.log("Stage2 check...");
27-
var redBtn = Code.demo.contentDocument.getElementById('redBtn');
28-
var greenBtn = Code.demo.contentDocument.getElementById('greenBtn');
29-
var blueBtn = Code.demo.contentDocument.getElementById('blueBtn');
30-
var clearBtn = Code.demo.contentDocument.getElementById('clearBtn');
31-
var rgbLed = Code.engine.list()['rgbled'][0];
33+
var redBtn = demoDoc_.getElementById('redBtn');
34+
var greenBtn = demoDoc_.getElementById('greenBtn');
35+
var blueBtn = demoDoc_.getElementById('blueBtn');
36+
var clearBtn = demoDoc_.getElementById('clearBtn');
37+
var rgbLed = engine_.list()['rgbled'][0];
3238
var checkPoint1, checkPoint2, checkPoint3, checkPoint4;
3339

3440
checkPoint1 = checkPoint2 = checkPoint3 = checkPoint4 = false;
3541

36-
$(redBtn).on('click', function () {
42+
redBtn.addEventListener('click', function () {
3743
setTimeout(function () {
3844
if (rgbLed._redValue < rgbLed._greenValue && rgbLed._redValue < rgbLed._blueValue) {
3945
checkPoint1 = true;
4046
checkPointVerify();
4147
}
4248
}, 500);
4349
});
44-
$(greenBtn).on('click', function () {
50+
51+
greenBtn.addEventListener('click', function () {
4552
setTimeout(function () {
4653
if (rgbLed._greenValue < rgbLed._redValue && rgbLed._greenValue < rgbLed._blueValue) {
4754
checkPoint2 = true;
4855
checkPointVerify();
4956
}
5057
}, 500);
5158
});
52-
$(blueBtn).on('click', function () {
59+
60+
blueBtn.addEventListener('click', function () {
5361
setTimeout(function () {
5462
if (rgbLed._blueValue < rgbLed._greenValue && rgbLed._blueValue < rgbLed._redValue) {
5563
checkPoint3 = true;
5664
checkPointVerify();
5765
}
5866
}, 500);
5967
});
60-
$(clearBtn).on('click', function () {
68+
69+
clearBtn.addEventListener('click', function () {
6170
setTimeout(function () {
62-
if (rgbLed._redValue == 1 &&
63-
rgbLed._greenValue == 1 &&
64-
rgbLed._blueValue == 1) {
71+
if (rgbLed._redValue === 1 &&
72+
rgbLed._greenValue === 1 &&
73+
rgbLed._blueValue === 1) {
6574
checkPoint4 = true;
6675
checkPointVerify();
6776
}
@@ -70,27 +79,26 @@ Code.stageCheck = function () {
7079

7180
function checkPointVerify() {
7281
console.log(checkPoint1, checkPoint2, checkPoint3, checkPoint4);
73-
if (checkPoint1 == true &&
74-
checkPoint2 == true &&
75-
checkPoint3 == true &&
76-
checkPoint4 == true) {
82+
if (checkPoint1 === true &&
83+
checkPoint2 === true &&
84+
checkPoint3 === true &&
85+
checkPoint4 === true) {
7786
alert("第二關 過關!");
7887
} else {
7988
console.log("Stage2 not yet...");
8089
}
8190
}
82-
}
8391

92+
}
8493

8594
function stage3() {
86-
console.log("Stage3 check...");
8795
var colors = {};
88-
var rgbLed = Code.engine.list()['rgbled'][0];
96+
var rgbLed = engine_.list()['rgbled'][0];
8997
rgbLed.state(function (r, g, b) {
90-
var htmlState = Code.demo.contentDocument.getElementById('light').className;
91-
if (htmlState == 'on') {
98+
var htmlState = demoDoc_.getElementById('light').className;
99+
if (htmlState === 'on') {
92100
colors["[" + r + "," + g + "," + b + "]"] = true;
93-
if (colorAmt(colors) == 14) {
101+
if (colorAmt(colors) === 14) {
94102
alert('第三關 過關!');
95103
rgbLed.state(function () {});
96104
}
@@ -107,8 +115,15 @@ Code.stageCheck = function () {
107115
}
108116
}
109117

110-
111118
function stage4() {
112119

113120
}
114-
};
121+
122+
123+
124+
125+
Code.stageCheck = {
126+
init: init
127+
};
128+
129+
})();

0 commit comments

Comments
 (0)