Skip to content

Commit

Permalink
💻 Add sleep prompt until level 7 (#5277)
Browse files Browse the repository at this point in the history
Fixes #4769 

After level 7 'if' statements are possible and it gets a lot more complicated to run the sleep prompt in order.

https://github.com/hedyorg/hedy/assets/48122190/a5854391-3a69-472a-9c70-4145a8f2b3e1
  • Loading branch information
Annelein authored Mar 21, 2024
1 parent 591d66d commit 5f264c5
Show file tree
Hide file tree
Showing 56 changed files with 280 additions and 9 deletions.
36 changes: 30 additions & 6 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,12 +599,36 @@ def parse():
except Exception:
pass

with querylog.log_time('detect_sleep'):
try:
# FH, Nov 2023: hmmm I don't love that this is not done in the same place as the other "has"es
response['has_sleep'] = 'sleep' in transpile_result.commands
except BaseException:
pass
if level < 7:
with querylog.log_time('detect_sleep'):
try:
# FH, Nov 2023: hmmm I don't love that this is not done in the same place as the other "has"es
sleep_list = []
pattern = (
r'time\.sleep\((?P<time>\d+)\)'
r'|time\.sleep\(int\("(?P<sleep_time>\d+)"\)\)'
r'|time\.sleep\(int\((?P<variable>\w+)\)\)')
matches = re.finditer(
pattern,
response['Code'])
for i, match in enumerate(matches, start=1):
time = match.group('time')
sleep_time = match.group('sleep_time')
variable = match.group('variable')
if sleep_time:
sleep_list.append(int(sleep_time))
elif time:
sleep_list.append(int(time))
elif variable:
assignment_match = re.search(r'{} = (.+?)\n'.format(variable), response['Code'])
if assignment_match:
assignment_code = assignment_match.group(1)
variable_value = eval(assignment_code)
sleep_list.append(int(variable_value))
if sleep_list:
response['has_sleep'] = sleep_list
except BaseException:
pass

try:
if username and not body.get('tutorial') and ACHIEVEMENTS.verify_run_achievements(
Expand Down
3 changes: 3 additions & 0 deletions messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,9 @@ msgstr ""
msgid "slash"
msgstr ""

msgid "sleeping"
msgstr ""

msgid "slides"
msgstr ""

Expand Down
50 changes: 49 additions & 1 deletion static/js/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ let pygameRunning = false;
* Represents whether there's an open 'ask' prompt
*/
let askPromptOpen = false;
/**
* Represents whether there's an open 'sleeping' prompt
*/
let sleepRunning = false;

// Many bits of code all over this file need this information globally.
// Not great but it'll do for now until we refactor this file some more.
Expand Down Expand Up @@ -451,6 +455,14 @@ export function stopit() {
$('#stopit').hide();
$('#runit').show();
}
else if (sleepRunning){
Sk.execLimit = 1;
clearTimeouts();
sleepRunning = false;
$('#sleep-modal').hide();
$('#stopit').hide();
$('#runit').show();
}
else
{
// We bucket-fix stop the current program by setting the run limit to 1ms
Expand Down Expand Up @@ -863,7 +875,7 @@ window.onerror = function reportClientException(message, source, line_number, co
});
}

export function runPythonProgram(this: any, code: string, sourceMap: any, hasTurtle: boolean, hasPygame: boolean, hasSleep: boolean, hasClear: boolean, hasMusic: boolean, hasWarnings: boolean, cb: () => void, run_type: "run" | "debug" | "continue") {
export function runPythonProgram(this: any, code: string, sourceMap: any, hasTurtle: boolean, hasPygame: boolean, hasSleep: number[], hasClear: boolean, hasMusic: boolean, hasWarnings: boolean, cb: () => void, run_type: "run" | "debug" | "continue") {
// If we are in the Parsons problem -> use a different output
let outputDiv = $('#output');
let skip_faulty_found_errors = false;
Expand Down Expand Up @@ -938,6 +950,42 @@ export function runPythonProgram(this: any, code: string, sourceMap: any, hasTur
$('#turtlecanvas').show();
}

if (hasSleep) {
function executeWithDelay(index: number) {
return new Promise((resolve, reject) => {
if (index >= hasSleep.length) {
resolve(reject);
return;
}

const sleepTime = hasSleep[index];
if (sleepTime) {
$('#sleep-modal').show();
sleepRunning = true;
setTimeout(() => {
$('#sleep-modal').hide();
sleepRunning = false;
setTimeout(() => {
resolve(reject);
}, 100);
}, (sleepTime * 1000) - 100);
} else {
setTimeout(() => {
resolve(reject);
}, 100);
}
});
}

async function executeAllDelays() {
for (let i = 0; i < hasSleep.length; i++) {
await executeWithDelay(i);
}
}
executeAllDelays()
}


if (hasPygame){
skulptExternalLibraries = {
'./extensions.js': {
Expand Down
40 changes: 40 additions & 0 deletions static/js/appbundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -98831,6 +98831,7 @@ notes_mapping = {
var last_code;
var pygameRunning = false;
var askPromptOpen = false;
var sleepRunning = false;
var theAdventures = {};
var theLevel = 0;
var theLanguage2 = "";
Expand Down Expand Up @@ -99133,6 +99134,13 @@ pygame.quit()
$("#pygame-modal").hide();
$("#stopit").hide();
$("#runit").show();
} else if (sleepRunning) {
Sk.execLimit = 1;
clearTimeouts();
sleepRunning = false;
$("#sleep-modal").hide();
$("#stopit").hide();
$("#runit").show();
} else {
Sk.execLimit = 1;
clearTimeouts();
Expand Down Expand Up @@ -99513,6 +99521,38 @@ pygame.quit()
code_prefix += music_prefix;
$("#turtlecanvas").show();
}
if (hasSleep) {
let executeWithDelay = function(index3) {
return new Promise((resolve2, reject) => {
if (index3 >= hasSleep.length) {
resolve2(reject);
return;
}
const sleepTime = hasSleep[index3];
if (sleepTime) {
$("#sleep-modal").show();
sleepRunning = true;
setTimeout(() => {
$("#sleep-modal").hide();
sleepRunning = false;
setTimeout(() => {
resolve2(reject);
}, 100);
}, sleepTime * 1e3 - 100);
} else {
setTimeout(() => {
resolve2(reject);
}, 100);
}
});
};
async function executeAllDelays() {
for (let i = 0; i < hasSleep.length; i++) {
await executeWithDelay(i);
}
}
executeAllDelays();
}
if (hasPygame) {
skulptExternalLibraries = {
"./extensions.js": {
Expand Down
4 changes: 2 additions & 2 deletions static/js/appbundle.js.map

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions templates/incl/editor-and-output.html
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@
<p class="text-2xl font-bold caption">{{_('pygame_waiting_for_input')}}</p>
</div>
</div>
<div id="sleep-modal" class="py-2 text-left px-3 border-4 border-teal-500 mt-3 rounded bg-green-100" style="display: none;z-index: 99;">
<!--Title-->
<div class="flex justify-between items-center">
<p class="text-2xl font-bold caption">{{_('sleeping')}}</p>
</div>
</div>
</div>
<div id="dynamic-buttons" class="flex mt-3 gap-2 overflow-auto border-4 border-blue-600 rounded bg-blue-300 py-2 px-3" style="display: none">
</div>
Expand Down
3 changes: 3 additions & 0 deletions translations/ar/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1698,6 +1698,9 @@ msgstr "علامة اقتباس أحادية"
msgid "slash"
msgstr "علامة الخط المائل ناحية اليمين"

msgid "sleeping"
msgstr ""

#, fuzzy
msgid "slides"
msgstr "Slides"
Expand Down
3 changes: 3 additions & 0 deletions translations/bg/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1921,6 +1921,9 @@ msgstr "апострофи"
msgid "slash"
msgstr "наклонена черта"

msgid "sleeping"
msgstr ""

#, fuzzy
msgid "slides"
msgstr "Slides"
Expand Down
3 changes: 3 additions & 0 deletions translations/bn/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1998,6 +1998,9 @@ msgstr "a single quote"
msgid "slash"
msgstr "a slash"

msgid "sleeping"
msgstr ""

#, fuzzy
msgid "slides"
msgstr "Slides"
Expand Down
3 changes: 3 additions & 0 deletions translations/ca/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1762,6 +1762,9 @@ msgstr "un apòstrof"
msgid "slash"
msgstr "una barra"

msgid "sleeping"
msgstr ""

#, fuzzy
msgid "slides"
msgstr "Slides"
Expand Down
3 changes: 3 additions & 0 deletions translations/cs/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1911,6 +1911,9 @@ msgstr "jednoduché uvozovky"
msgid "slash"
msgstr "lomítko"

msgid "sleeping"
msgstr ""

#, fuzzy
msgid "slides"
msgstr "Slides"
Expand Down
3 changes: 3 additions & 0 deletions translations/cy/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -2010,6 +2010,9 @@ msgstr "a single quote"
msgid "slash"
msgstr "a slash"

msgid "sleeping"
msgstr ""

#, fuzzy
msgid "slides"
msgstr "Slides"
Expand Down
3 changes: 3 additions & 0 deletions translations/da/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -2011,6 +2011,9 @@ msgstr "a single quote"
msgid "slash"
msgstr "a slash"

msgid "sleeping"
msgstr ""

#, fuzzy
msgid "slides"
msgstr "Slides"
Expand Down
3 changes: 3 additions & 0 deletions translations/de/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,9 @@ msgstr "ein einfaches Anführungszeichen"
msgid "slash"
msgstr "ein Schrägstrich"

msgid "sleeping"
msgstr ""

#, fuzzy
msgid "slides"
msgstr "Slides"
Expand Down
3 changes: 3 additions & 0 deletions translations/el/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,9 @@ msgstr "ένα μονό εισαγωγικό"
msgid "slash"
msgstr "μία κάθετος"

msgid "sleeping"
msgstr ""

#, fuzzy
msgid "slides"
msgstr "Slides"
Expand Down
3 changes: 3 additions & 0 deletions translations/en/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,9 @@ msgstr "a single quote"
msgid "slash"
msgstr "a slash"

msgid "sleeping"
msgstr "Sleeping..."

msgid "slides"
msgstr "Slides"

Expand Down
3 changes: 3 additions & 0 deletions translations/eo/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1773,6 +1773,9 @@ msgstr "unuobla citilo"
msgid "slash"
msgstr "suprenstreko"

msgid "sleeping"
msgstr ""

#, fuzzy
msgid "slides"
msgstr "Slides"
Expand Down
3 changes: 3 additions & 0 deletions translations/es/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,9 @@ msgstr "una comilla simple"
msgid "slash"
msgstr "una barra"

msgid "sleeping"
msgstr ""

msgid "slides"
msgstr "Diapositivas"

Expand Down
3 changes: 3 additions & 0 deletions translations/et/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1964,6 +1964,9 @@ msgstr "a single quote"
msgid "slash"
msgstr "a slash"

msgid "sleeping"
msgstr ""

#, fuzzy
msgid "slides"
msgstr "Slides"
Expand Down
3 changes: 3 additions & 0 deletions translations/fa/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1987,6 +1987,9 @@ msgstr "a single quote"
msgid "slash"
msgstr "a slash"

msgid "sleeping"
msgstr ""

#, fuzzy
msgid "slides"
msgstr "Slides"
Expand Down
3 changes: 3 additions & 0 deletions translations/fi/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -2009,6 +2009,9 @@ msgstr "a single quote"
msgid "slash"
msgstr "a slash"

msgid "sleeping"
msgstr ""

#, fuzzy
msgid "slides"
msgstr "Slides"
Expand Down
3 changes: 3 additions & 0 deletions translations/fr/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1613,6 +1613,9 @@ msgstr "une apostrophe"
msgid "slash"
msgstr "une barre oblique"

msgid "sleeping"
msgstr ""

#, fuzzy
msgid "slides"
msgstr "Slides"
Expand Down
3 changes: 3 additions & 0 deletions translations/fy/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1896,6 +1896,9 @@ msgstr "inn hege komma"
msgid "slash"
msgstr "in skean streepke"

msgid "sleeping"
msgstr ""

#, fuzzy
msgid "slides"
msgstr "Slides"
Expand Down
3 changes: 3 additions & 0 deletions translations/he/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1948,6 +1948,9 @@ msgstr "גרש"
msgid "slash"
msgstr "קו נטוי"

msgid "sleeping"
msgstr ""

#, fuzzy
msgid "slides"
msgstr "Slides"
Expand Down
3 changes: 3 additions & 0 deletions translations/hi/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,9 @@ msgstr "एक उद्धरण चिन्ह"
msgid "slash"
msgstr "एक स्लैश"

msgid "sleeping"
msgstr ""

#, fuzzy
msgid "slides"
msgstr "Slides"
Expand Down
Loading

0 comments on commit 5f264c5

Please sign in to comment.