From 9dc0f1f9894e99c4cf762715107c0f5196174cf6 Mon Sep 17 00:00:00 2001 From: Silver Kuusik Date: Wed, 15 Aug 2018 19:58:29 +0200 Subject: [PATCH] add code processing adn block highlight --- assets/js/main.js | 64 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 11 deletions(-) diff --git a/assets/js/main.js b/assets/js/main.js index 5185f4f..b4abaec 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -1,5 +1,5 @@ // The local/remote server URL -//var ROBOT_SERVER = '10.42.0.1'; +//var ROBOT_SERVER = '192.168.2.1:80'; var ROBOT_SERVER = 'ws.achex.ca:4010'; var workspace; @@ -113,6 +113,50 @@ window.addEventListener('load', function() { } }); + var lines = []; + var foundTrue = false; + // Function to process code and highlight blocks and lines + function processCode(index, highlight) { + // When all lines are already processed + if (lines.length == 0) return; + // Split into code and block ID + var temp = lines[index].split(';;'); + var code = temp[0]; + // Default timeout for processing the next line + var timeout = 100; + var isCondition = /(if|elif|else)/.test(code); + // When it is a condition line + if (isCondition) { + if (foundTrue) { + // Start processing the code from the beginning again + index = -1; + foundTrue = false; + } else if (/opponent/.test(code)) { + foundTrue = (sumorobot.sensors['opponent'] < 40.0); + } else if (/LEFT/.test(code)) { + foundTrue = (sumorobot.sensors['line_left'] > 2000); + } else if (/RIGHT/.test(code)) { + foundTrue = (sumorobot.sensors['line_right'] > 2000); + } else { + foundTrue = true; + } + } + // Some lines don't correspond to any block + if (temp[1] && index != -1 && ((!isCondition && foundTrue) || isCondition)) { + // When sleep function, we get the timeout value from the function + if (/\d/.test(code)) { + timeout = parseInt(code.replace(/[a-z\.()]/g, '')); + } + // Block ID should always be 20 symbols long + var blockId = temp[1].substring(0, 20); + // Highlight the block + workspace.highlightBlock(blockId); + } + // Set an timeout for processing the next line + index = (index + 1) % lines.length + setTimeout(function() { processCode(index) }, timeout); + } + // Start button listener $('.btn-start').click(function() { // When we are in Python coding mode @@ -125,21 +169,19 @@ window.addEventListener('load', function() { parsedCode = sumorobot.getBlocklyCode().replace(/;;.{20}/g, ''); } // Escape the qoutes, replace new lines and send the code - sumorobot.send('code', parsedCode.replace(/"/g, '\\"').replace(/\n/g, ';;')); - // TODO: Highlight the blocks and lines - /*var lines = sumorobot.getBlocklyCode().replace(/ /g, '').split('\n'); - // Process code lines - for (i in lines) { - if (lines[i]) { - console.log(lines[i].split(';;')); - } - }*/ + //sumorobot.send('code', parsedCode.replace(/"/g, '\\"').replace(/\n/g, ';;')); + // Split into lines of code and filter empty lines + lines = sumorobot.getBlocklyCode().split('\n').filter(Boolean); + // Process the code starting from the first line + processCode(0); }); // Stop button listener $('.btn-stop').click(function() { sumorobot.send('stop'); - // TODO: Stop highlighting blocks and lines + // Stop highlighting blocks and lines + lines = []; + workspace.highlightBlock(''); }); // Enter (return) keypress listener on robot ID field