Skip to content

Commit f0c5638

Browse files
committed
Add the stage 4 check
1 parent 6e18be8 commit f0c5638

3 files changed

Lines changed: 93 additions & 8 deletions

File tree

generators/stages.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,8 @@ Blockly.JavaScript['tutorial_youtube'] = function (block) {
298298
' resolve();\n' +
299299
' }\n' +
300300
' }\n' +
301-
' });\n' +
301+
' });\n' +
302+
' window.youtube_ = ' + value_name_ + ';\n' +
302303
' };\n' +
303304
'});\n';
304305
return code;

simulator

Submodule simulator updated from 132176c to 468c6aa

stageCheck.js

Lines changed: 90 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
if (led.getValue() === 1 && htmlState === 'on') isPass = true;
3535
});
3636

37-
if (isPass) alert("第一關 過關!");
37+
if (isPass) alert("第 1 關 過關!");
3838
}
3939

4040
/**
@@ -126,21 +126,23 @@
126126

127127
if (isPass) {
128128
isPassed = true;
129-
alert("第二關 過關!");
129+
alert("第 2 關 過關!");
130130
} else {
131131
console.log("Stage2 not yet...");
132132
}
133133
}
134134

135135
}
136136

137+
/**
138+
* 關卡 3:模擬器中三色 LED 是否有做顏色的不斷變化
139+
*/
137140
function stage3() {
138141
var lightBtn = demoDoc_.getElementById('light');
139142
var isPass = false;
140143

141144
lightBtn.addEventListener('click', function() {
142145
if (lightBtn.classList.contains('on')) {
143-
console.log('==> 開始');
144146
setTimeout(checking, 3000);
145147
}
146148
});
@@ -156,12 +158,11 @@
156158
colors["[" + r + "," + g + "," + b + "]"] = true;
157159
if (Object.keys(colors).length > 1) {
158160
isPass = true;
159-
alert('第三關 過關!');
161+
alert('第 3 關 過關!');
160162
checkOver();
161163
}
162164
});
163165
});
164-
165166
}
166167

167168
function checkOver() {
@@ -173,12 +174,95 @@
173174

174175
}
175176

177+
/**
178+
* 關卡 4:點擊按鈕時,是否有完成暫停影片及播放影片的功能,以及長按按鈕來讓影片停止
179+
*/
176180
function stage4() {
181+
var demoWin = demoDoc_.defaultView;
182+
var youtube = demoWin.youtube_;
183+
var btns = engine_.list().btn;
184+
var act = void 0;
185+
var check = [false, false, false]; // 檢查三項,點按鈕,暫停 youtube 及播放 youtube,長按則停止。
186+
var isPassed = false;
187+
var TIMES = 10;
188+
var times = 0;
177189

178-
}
190+
if (!btns.length) {
191+
return;
192+
}
193+
194+
btns.forEach(function (btn) {
195+
btn.setPressHandler(press);
196+
btn.setSustainedPressHandler(sustainedPress);
197+
});
198+
199+
if (youtube) {
200+
youtube.addEventListener('onStateChange', stageChange);
201+
} else {
202+
setTimeout(function aa() {
203+
times++;
204+
if (demoWin.youtube_) {
205+
youtube = demoWin.youtube_;
206+
youtube.addEventListener('onStateChange', stageChange);
207+
return;
208+
}
209+
210+
if (times < TIMES) {
211+
setTimeout(aa, 1000);
212+
}
213+
214+
}, 1000);
215+
}
216+
217+
function press() {
218+
act = 'press';
219+
}
220+
221+
function sustainedPress() {
222+
act = 'sustainedPress';
223+
}
224+
225+
function stageChange(evt) {
226+
var state = evt.data;
227+
228+
if (act) {
229+
if (act === 'press' && state === demoWin.YT.PlayerState.PAUSED) check[0] = true;
230+
if (act === 'press' && state === demoWin.YT.PlayerState.PLAYING) check[1] = true;
231+
if (act === 'sustainedPress' && state === -1) check[2] = true;
232+
act = void 0;
233+
}
234+
checking();
235+
}
179236

237+
function checking() {
238+
if (isPassed) return;
239+
240+
var isPass = check.reduce(function (acc, val) {
241+
return acc && val;
242+
}, true);
243+
244+
if (isPass) {
245+
isPassed = true;
246+
checkOver();
247+
248+
setTimeout(function () {
249+
alert('第 4 關 過關!');
250+
}, 1000);
251+
}
252+
}
253+
254+
function checkOver() {
255+
youtube.removeEventListener('onStateChange', stageChange);
256+
btns.forEach(function (btn) {
257+
btn.setPressHandler(null);
258+
btn.setSustainedPressHandler(null);
259+
});
260+
}
261+
262+
}
180263

181264

265+
182266

183267
Code.stageCheck = {
184268
init: init

0 commit comments

Comments
 (0)